infino 0.2.1

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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! In-memory manifest types: `ManifestSnapshot`, `SuperfileEntry`,
//! `VectorSummary`. Per-column skip stats live on `SuperfileEntry` as
//! `HashMap<String, ScalarStatsAgg>` (scalar) and
//! `HashMap<String, FtsSummaryAgg>` (FTS).
//!
//! `ManifestSnapshot` is the single immutable point-in-time view of which
//! superfiles exist. `Supertable` holds the current manifest behind
//! an `ArcSwap<ManifestSnapshot>`; commits build a new `ManifestSnapshot` (superfiles:
//! old + new) and atomically swap it in. Readers
//! `ArcSwap::load_full` once at construction to pin a snapshot for
//! the lifetime of their queries.
//!
//! ## Construction is copy-on-write
//!
//! `ManifestSnapshot::with_appended` clones the outer `Vec` and shares each
//! existing `Arc<SuperfileEntry>` between the old and new manifests,
//! so the only per-commit allocation is the new entries plus the
//! `Vec` header. `ManifestSnapshot` itself is immutable — never mutated in
//! place — which is what makes lock-free reader-writer isolation
//! possible.

pub mod aggregates;
pub mod bloom;
pub mod commit;
pub mod disk_cache;
pub mod encoding;
pub mod hll;
pub mod list;
pub mod list_prune;
pub mod options_hash;
pub mod part;
pub mod partition;
pub mod term_range;

use std::{
    cmp::Ordering,
    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
    f32::consts::PI,
    fmt,
    ops::Deref,
    sync::{Arc, OnceLock},
};

use arrow::compute::kernels::aggregate as agg;
use arrow_array::*;
use arrow_schema::{DataType, TimeUnit};
use bytes::Bytes;
use dashmap::DashMap;
use futures::future;
/// Re-export the per-column skip aggregates so callers can refer to them as
/// `manifest::ScalarStatsAgg` / `manifest::FtsSummaryAgg` (the value types of
/// `SuperfileEntry.scalar_stats` / `SuperfileEntry.fts_summary`).
pub use list::{FtsSummaryAgg, GlobalVectorIndex, RoutingRef, ScalarStatsAgg};
use rayon::{ThreadPool, prelude::*};
use tokio::{sync::OnceCell, task::spawn_blocking};
use uuid::Uuid;
use xxhash_rust::xxh3::xxh3_64;

use super::options::SupertableOptions;
use crate::{
    storage::{StorageError, StorageProvider},
    superfile::{
        builder::VectorConfig,
        vector::{
            distance::{
                COSINE_DISTANCE_BASE, L2_CROSS_TERM_COEFF, Metric, all_centroid_scores_transposed,
                distance, dot, insert_ranked, nearest_k_centroids_transposed,
                transpose_centroids_cluster_major,
            },
            layout::VectorLayout,
            quant::BitQuantizer,
            rotation::RandomRotation,
        },
    },
    supertable::{
        CommitError,
        error::ManifestError,
        manifest::{
            commit::{
                EncodedPart, PointerFile, frame_content_size, part_uri, read_pointer,
                translate_contention, write_manifest, write_part_bytes, write_pointer,
            },
            disk_cache::ManifestDiskCache,
            encoding::SummaryWireMode,
            list::{
                FORMAT_VERSION as LIST_FORMAT_VERSION, Manifest, ManifestPartEntry,
                PartitionStrategy,
            },
            part::{ContentHash, ManifestPart, PartId},
            partition::{assign_partition, encode_partition_key},
        },
        query::{hierarchical_iter, prune::PruneLeaf},
        slow_vector_state,
    },
};

/// Object-store / LocalFS directory prefix under which committed superfile
/// bytes live (`<data>/seg-<id>.sf.parquet`). Shared by [`SuperfileUri::storage_path`]
/// and the GC live-set sweep so both agree on the superfile namespace.
pub(crate) const SUPERFILE_DATA_DIR: &str = "data";

/// Legacy storage-subtree prefix for the hidden vector-index sibling
/// supertable — the commit-time default stamped when a vector table's
/// manifest carries no explicit prefix. New tables generate a unique
/// prefix at create; this constant only keeps pre-prefix tables readable.
pub(crate) const DEFAULT_VECTOR_INDEX_PREFIX: &str = "_vector_index";

/// One immutable point-in-time view of the supertable.
///
/// **Construction is copy-on-write.** Adding a superfile via
/// [`ManifestSnapshot::with_appended`] returns a new `ManifestSnapshot` whose
/// `superfiles` is `Vec::clone()` + new entries appended; the original
/// `ManifestSnapshot`'s `superfiles` is unchanged. `Arc<SuperfileEntry>` shares
/// the underlying entries between the old and new manifests so the
/// only per-commit allocation is the outer `Vec` and the new
/// entries themselves.
///
/// **Reader isolation.** Readers `ArcSwap::load_full` an
/// `Arc<ManifestSnapshot>` at construction and hold it for their lifetime.
/// New commits don't affect them. Old manifests are dropped
/// automatically once no reader holds an Arc to them.
///
/// `ManifestSnapshot` is the outer hierarchical wrapper (it adds the
/// `list` / `parts` / `loader` persistence-side fields);
/// `SuperfileList` is the flat in-process view that `ManifestSnapshot`
/// derefs to, so callers can access `.manifest_id`,
/// `.superfiles[i]`, `.n_docs_total()` etc. directly through a
/// `ManifestSnapshot`.
#[derive(Debug, Clone)]
pub struct SuperfileList {
    /// Monotonic point-in-time identifier. Starts at 0 (empty
    /// initial manifest from `Supertable::create`); each commit
    /// derives `manifest_id = old.manifest_id + 1`. With a single
    /// writer at a time, no separate counter or atomic is needed —
    /// the read-then-store sequence is exclusive by construction.
    pub manifest_id: u64,
    /// Pointer back to the immutable per-supertable configuration.
    /// Same Arc across all manifests of one supertable.
    pub options: Arc<SupertableOptions>,
    /// Append-only list of superfile entries. Each entry's `Arc`-share
    /// is what makes the copy-on-write per-commit construction
    /// cheap.
    pub superfiles: Vec<Arc<SuperfileEntry>>,
    /// Hidden vector-index sibling prefix. Set at create before the
    /// first manifest list is persisted; cleared once loaded from list.
    pub(crate) vector_index_storage_prefix: Option<String>,
}

impl SuperfileList {
    /// Empty initial state at `manifest_id = 0`.
    pub fn empty(options: Arc<SupertableOptions>) -> Self {
        Self {
            manifest_id: 0,
            options,
            superfiles: Vec::new(),
            vector_index_storage_prefix: None,
        }
    }

    pub(crate) fn empty_with_vector_index_prefix(
        options: Arc<SupertableOptions>,
        vector_index_storage_prefix: Option<String>,
    ) -> Self {
        Self {
            manifest_id: 0,
            options,
            superfiles: Vec::new(),
            vector_index_storage_prefix,
        }
    }

    /// Build a successor SuperfileList with `new_entries` appended to
    /// the end of `superfiles`. Original is unchanged. `manifest_id`
    /// of the result is `self.manifest_id + 1`.
    pub fn with_appended(&self, new_entries: Vec<Arc<SuperfileEntry>>) -> Self {
        let mut superfiles = self.superfiles.clone();
        superfiles.extend(new_entries);
        Self {
            manifest_id: self.manifest_id + 1,
            options: self.options.clone(),
            superfiles,
            vector_index_storage_prefix: self.vector_index_storage_prefix.clone(),
        }
    }

    /// Total documents across all superfiles.
    pub fn n_docs_total(&self) -> u64 {
        self.superfiles.iter().map(|s| s.n_docs).sum()
    }
}

/// The hierarchical manifest. Outer wrapper around the
/// [`SuperfileList`] (flat in-process view) plus the
/// persistence-side metadata:
///
/// - `list`: the [`Manifest`] when this manifest was loaded
///   from / persisted to storage. `None` for in-process-only
///   supertables (no storage attached).
/// - `parts`: per-part lazy-load cache. `OnceCell` per part
///   coalesces concurrent `part(id)` calls into a single
///   `StorageProvider::get` — 100 query tasks on a cold part
///   issue exactly one load.
/// - `loader`: pulls part bytes through the storage provider
///   and verifies content hash. `None` when no storage is
///   attached (the in-process-only path).
///
/// `Deref` exposes the [`SuperfileList`] fields directly so
/// `manifest.manifest_id`, `manifest.superfiles[i]`,
/// `manifest.n_docs_total()` etc. work through a `ManifestSnapshot`
/// reference.
///
/// [`Manifest`]: list::Manifest
pub struct ManifestSnapshot {
    superfile_list: SuperfileList,
    list: Option<Manifest>,
    parts: DashMap<PartId, Arc<OnceCell<Arc<ManifestPart>>>>,
    loader: Option<Arc<ManifestPartLoader>>,
    /// Stamped partition strategy before the first list lands, or
    /// when updating strategy without rebuilding options.
    stamped_partition_strategy: Option<PartitionStrategy>,
    /// Stamped global vector grid before the first list lands (mirrors
    /// `stamped_partition_strategy`): the user commit bootstraps the grid into
    /// this on the first commit-with-vectors, and `update` reads it back via
    /// [`ManifestSnapshot::get_global_vector_index`] to persist it into the new list.
    stamped_global_vector_index: Option<list::GlobalVectorIndex>,
    /// Stamped drained-version set before the (hidden) list lands. The drain
    /// advances this via [`ManifestSnapshot::with_drained_ranges`] and `update` reads
    /// it back via [`ManifestSnapshot::get_drained_ranges`] to persist it. Hidden
    /// manifest only.
    stamped_drained_ranges: Option<list::DrainedVersionRanges>,
}

impl fmt::Debug for ManifestSnapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ManifestSnapshot")
            .field("manifest_id", &self.superfile_list.manifest_id)
            .field("n_superfiles", &self.superfile_list.superfiles.len())
            .field("has_list", &self.list.is_some())
            .field(
                "n_parts",
                &self.list.as_ref().map(|l| l.parts.len()).unwrap_or(0),
            )
            .field("n_parts_loaded", &self.parts.len())
            .field("has_loader", &self.loader.is_some())
            .finish()
    }
}

impl Deref for ManifestSnapshot {
    type Target = SuperfileList;
    fn deref(&self) -> &Self::Target {
        &self.superfile_list
    }
}

impl ManifestSnapshot {
    pub fn new(
        manifest_id: u64,
        options: Arc<SupertableOptions>,
        superfile_list: Vec<Arc<SuperfileEntry>>,
        storage: Option<Arc<dyn StorageProvider>>,
        list: Option<Manifest>,
    ) -> Self {
        let superfile_list = SuperfileList {
            manifest_id,
            options,
            superfiles: superfile_list,
            vector_index_storage_prefix: None,
        };
        if let Some(storage) = storage
            && let Some(list) = list
        {
            let manifest_cache = superfile_list.options.manifest_disk_cache.clone();
            let loader = Arc::new(ManifestPartLoader::new_with_cache(
                Arc::clone(&storage),
                &list,
                manifest_cache,
            ));
            Self {
                superfile_list,
                list: Some(list),
                parts: DashMap::new(),
                loader: Some(loader),
                stamped_partition_strategy: None,
                stamped_global_vector_index: None,
                stamped_drained_ranges: None,
            }
        } else {
            Self {
                superfile_list,
                list: None,
                parts: DashMap::new(),
                loader: None,
                stamped_partition_strategy: None,
                stamped_global_vector_index: None,
                stamped_drained_ranges: None,
            }
        }
    }

    #[cfg(test)]
    pub fn new_from_superfiles(
        opts: Arc<SupertableOptions>,
        superfiles: Vec<Arc<SuperfileEntry>>,
    ) -> Self {
        ManifestSnapshot::empty(opts).with_appended(superfiles)
    }

    /// Empty initial manifest at `manifest_id = 0`. Used by
    /// `Supertable::create` when no storage is attached.
    pub fn empty(options: Arc<SupertableOptions>) -> Self {
        Self {
            superfile_list: SuperfileList::empty(options),
            list: None,
            parts: DashMap::new(),
            loader: None,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        }
    }

    pub(crate) fn empty_with_vector_index_prefix(
        options: Arc<SupertableOptions>,
        vector_index_storage_prefix: Option<String>,
    ) -> Self {
        Self {
            superfile_list: SuperfileList::empty_with_vector_index_prefix(
                options,
                vector_index_storage_prefix,
            ),
            list: None,
            parts: dashmap::DashMap::new(),
            loader: None,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        }
    }

    /// A *materialized* empty manifest at `manifest_id = 0`, ready to persist.
    /// Unlike [`Self::empty`] (which is in-process-only, `list: None`), this
    /// carries a `Some(list)`, so [`Self::write`] emits the initial (empty)
    /// manifest list + pointer. `Supertable::create` persists this on durable
    /// storage so the table is openable immediately — before its first append,
    /// after a reopen, or from another process — without shifting the
    /// `manifest_id` sequence (the first append still commits `manifest_id 1`).
    /// `vector_index_storage_prefix` is the hidden-sibling subtree the caller
    /// generated at create (`None` for tables without vector columns, and for
    /// the hidden sibling itself).
    pub(crate) fn materialized_empty_with_vector_index_prefix(
        options: Arc<SupertableOptions>,
        vector_index_storage_prefix: Option<String>,
    ) -> Self {
        let strategy = options.effective_partition_strategy();
        let list = Self::build_list(
            &options,
            strategy,
            0,
            Vec::new(),
            vector_index_storage_prefix.clone(),
            BTreeMap::new(),
            BTreeMap::new(),
        );
        let loader = options.storage.as_ref().map(|storage| {
            Arc::new(ManifestPartLoader::new_with_cache(
                storage.clone(),
                &list,
                options.manifest_disk_cache.clone(),
            ))
        });
        Self {
            superfile_list: SuperfileList::empty_with_vector_index_prefix(
                options,
                vector_index_storage_prefix,
            ),
            list: Some(list),
            parts: DashMap::new(),
            loader,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        }
    }

    /// Build a manifest list from the supertable `options` at `manifest_id`,
    /// carrying `parts` and the tombstone-seq state. Shared by the commit path
    /// ([`Self::update`]), and the initial empty-manifest materialization
    /// ([`Self::materialized_empty_with_vector_index_prefix`]), so the
    /// options→list field mapping lives in one place.
    fn build_list(
        options: &SupertableOptions,
        strategy: PartitionStrategy,
        manifest_id: u64,
        parts: Vec<ManifestPartEntry>,
        vector_index_storage_prefix: Option<String>,
        tombstone_seqs: BTreeMap<Uuid, u64>,
        superseded_cells: BTreeMap<Uuid, BTreeSet<u32>>,
    ) -> Manifest {
        Manifest {
            format_version: LIST_FORMAT_VERSION.into(),
            manifest_id,
            options_hash: options_hash::compute_options_hash(options, &strategy),
            schema: Vec::new(),
            id_column: options.id_column.clone(),
            fts_columns: options
                .fts_columns
                .iter()
                .map(|f| list::FtsColumnInfo {
                    column: f.column.clone(),
                })
                .collect(),
            vector_columns: options
                .vector_columns
                .iter()
                .map(|v| list::VectorColumnInfo {
                    column: v.column.clone(),
                    dim: v.dim,
                    n_cent: v.n_cent,
                    rot_seed: v.rot_seed,
                    metric: format!("{:?}", v.metric).to_lowercase(),
                })
                .collect(),
            partition_strategy: strategy,
            vector_index_storage_prefix,
            global_vector_index: None,
            drained_ranges: Default::default(),
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts,
            tombstone_seqs,
            superseded_cells,
        }
    }

    pub fn get_manifest_id(&self) -> u64 {
        self.superfile_list.manifest_id
    }

    pub fn get_next_manifest_id(&self) -> u64 {
        self.get_manifest_id() + 1
    }

    pub fn get_opts(&self) -> Arc<SupertableOptions> {
        self.superfile_list.options.clone()
    }

    pub fn get_partition_strategy(&self) -> list::PartitionStrategy {
        self.partition_strategy()
            .cloned()
            .unwrap_or_else(|| self.superfile_list.options.effective_partition_strategy())
    }

    /// Borrow the resident partition strategy when one is stamped or listed.
    ///
    /// Prefer this on the query path over [`Self::get_partition_strategy`]: a
    /// `VectorCell` strategy owns [`ClusterCentroids`], and cloning it drops
    /// (or re-copies) the transposed SIMD cache used by cell ranking.
    pub(crate) fn partition_strategy(&self) -> Option<&list::PartitionStrategy> {
        self.stamped_partition_strategy
            .as_ref()
            .or_else(|| self.list.as_ref().map(|l| &l.partition_strategy))
    }

    /// [`CellRoutingParams`] from a `VectorCell` strategy, without cloning
    /// the cell-centroid grid.
    pub(crate) fn vector_cell_routing(&self) -> Option<list::CellRoutingParams> {
        match self.partition_strategy() {
            Some(list::PartitionStrategy::VectorCell { routing, .. }) => Some(*routing),
            _ => None,
        }
    }

    /// Borrow the `VectorCell` centroid grid when it matches `column`.
    pub(crate) fn vector_cell_clusters(&self, column: &str) -> Option<&ClusterCentroids> {
        match self.partition_strategy() {
            Some(list::PartitionStrategy::VectorCell {
                column: cell_column,
                clusters,
                ..
            }) if cell_column == column => Some(clusters),
            _ => None,
        }
    }

    /// The global vector cell-index grid this (user) table owns, or `None`
    /// before the first commit-with-vectors. Honors the in-memory stamp set by
    /// [`ManifestSnapshot::with_global_vector_index`] before the first list lands, then
    /// the persisted list.
    ///
    /// Cloning cost: this returns an owned [`list::GlobalVectorIndex`]. The
    /// query path must use [`Self::global_vector_index`] instead so the
    /// transposed centroid cache stays resident across warm queries.
    pub fn get_global_vector_index(&self) -> Option<list::GlobalVectorIndex> {
        self.global_vector_index().cloned()
    }

    /// Borrow the global vector cell-index (no clone — keeps the transposed
    /// SIMD cache warm on the query path).
    pub(crate) fn global_vector_index(&self) -> Option<&list::GlobalVectorIndex> {
        self.stamped_global_vector_index.as_ref().or_else(|| {
            self.list
                .as_ref()
                .and_then(|l| l.global_vector_index.as_ref())
        })
    }

    /// Drained user commit-versions recorded on this (hidden) manifest. Honors
    /// the in-memory stamp set by [`ManifestSnapshot::with_drained_ranges`] before the
    /// first list lands, then the persisted list. Empty by default.
    pub fn get_drained_ranges(&self) -> list::DrainedVersionRanges {
        if let Some(d) = &self.stamped_drained_ranges {
            return d.clone();
        }
        self.list
            .as_ref()
            .map(|l| l.drained_ranges.clone())
            .unwrap_or_default()
    }

    pub fn get_num_parts(&self) -> usize {
        self.list.as_ref().map(|l| l.parts.len()).unwrap_or(0)
    }

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

    pub fn is_in_process_only(&self) -> bool {
        self.list.is_none()
    }

    /// Return the resident flat membership only when it is known complete.
    ///
    /// In-process manifests are always flat. Part-backed user manifests are
    /// complete when the resident entry count equals the sum recorded in the
    /// list. A loaded slow-state blob is authoritative by construction:
    /// [`Manifest::load`] either hydrates it completely or fails.
    pub(crate) fn complete_flat_superfiles(&self) -> Option<&[Arc<SuperfileEntry>]> {
        match self.list.as_ref() {
            None => Some(&self.superfile_list.superfiles),
            Some(list) if list.slow_vector_state_uri.is_some() => {
                Some(&self.superfile_list.superfiles)
            }
            Some(list) => {
                let expected: u64 = list.parts.iter().map(|entry| entry.n_superfiles).sum();
                (self.superfile_list.superfiles.len() as u64 == expected)
                    .then_some(self.superfile_list.superfiles.as_slice())
            }
        }
    }

    pub(crate) fn vector_index_storage_prefix(&self) -> Option<&str> {
        if let Some(list) = self.list.as_ref()
            && let Some(prefix) = list.vector_index_storage_prefix.as_deref()
        {
            return Some(prefix);
        }
        self.superfile_list.vector_index_storage_prefix.as_deref()
    }

    fn stamp_vector_index_storage_prefix(
        &self,
        vector_columns: &[list::VectorColumnInfo],
    ) -> Option<String> {
        if vector_columns.is_empty() {
            return None;
        }
        if let Some(prefix) = self.vector_index_storage_prefix() {
            return Some(prefix.to_string());
        }
        Some(DEFAULT_VECTOR_INDEX_PREFIX.to_string())
    }

    pub fn get_cached_part_by_id(&self, part_id: &PartId) -> Option<Arc<ManifestPart>> {
        self.parts
            .get(part_id)
            .and_then(|cell| cell.value().get().cloned())
    }

    pub fn get_cached_part_by_list_idx(&self, idx: usize) -> Option<Arc<ManifestPart>> {
        let Some(list) = &self.list else {
            return None;
        };
        let part_id = list.parts[idx].part_id;
        self.get_cached_part_by_id(&part_id)
    }

    /// Load the committed manifest from storage.
    ///
    /// A genuinely absent pointer is [`ManifestLoadError::PointerNotFound`]:
    /// `Supertable::create` persists the initial (empty) pointer, so a
    /// registered table always has one. A missing pointer is therefore the
    /// open-or-create trigger for a never-created table, or a *lost* pointer
    /// on a created one — either way an error the caller sees, never a
    /// silently-empty table (which would mask committed-then-lost data). A
    /// *corrupt* pointer is a different error variant (`PointerParse`) and
    /// also propagates, so corruption is never masked.
    pub(crate) async fn load(
        current_manifest: Option<Arc<Self>>,
        storage: Arc<dyn StorageProvider>,
        options: Option<Arc<SupertableOptions>>,
    ) -> Result<Arc<Self>, ManifestLoadError> {
        // 1. Read the pointer file.
        let (pointer, _) = match read_pointer(storage.as_ref()).await? {
            Some(p) => p,
            None => return Err(ManifestLoadError::PointerNotFound),
        };
        Self::load_with_pointer(current_manifest, storage, options, pointer).await
    }

    /// [`Self::load`] with the pointer already in hand. Split out so
    /// the refresh path can read the pointer itself (conditionally,
    /// via [`probe_pointer`]) and still share the list + parts
    /// loading below.
    pub(crate) async fn load_with_pointer(
        current_manifest: Option<Arc<Self>>,
        storage: Arc<dyn StorageProvider>,
        options: Option<Arc<SupertableOptions>>,
        pointer: PointerFile,
    ) -> Result<Arc<Self>, ManifestLoadError> {
        if let Some(current_manifest) = &current_manifest
            && current_manifest.superfile_list.manifest_id >= pointer.manifest_id
        {
            // Pointer hasn't advanced past our in-memory state —
            return Err(ManifestLoadError::AlreadyLoaded);
        }

        // 2. Load + parse the manifest list.
        let (list_bytes, _) = storage
            .get(&pointer.manifest_uri)
            .await
            .map_err(ManifestLoadError::Storage)?;
        let list = list::decode(&list_bytes).map_err(ManifestLoadError::ListParse)?;

        let options = if let Some(options) = options {
            options
        } else if let Some(current) = &current_manifest {
            current.options.clone()
        } else {
            return Err(ManifestLoadError::ContentHashMismatch {
                expected: "valid options".to_string(),
                actual: "None options".to_string(),
            });
        };

        // Verify the caller's options match the
        // manifest's stamped digest. The all-zero stored
        // hash bypasses validation (legacy + synthetic
        // fixtures).
        let expected_hash = options_hash::compute_options_hash(&options, &list.partition_strategy);
        if let Err(mismatch) = options_hash::verify_options_hash(expected_hash, list.options_hash) {
            return Err(ManifestLoadError::ContentHashMismatch {
                expected: mismatch.expected,
                actual: mismatch.actual,
            });
        }

        // 3. Build the loader, superfiles & parts. Consumer memory mode
        //    loads each part's routing sibling (counts + 1-bit slab, no
        //    fp32) when the list stamps one — the user table's centroid
        //    payload stays in storage, mirroring the slow-blob sibling.
        let loader = Arc::new(ManifestPartLoader::new_with_cache_and_mode(
            Arc::clone(&storage),
            &list,
            options.manifest_disk_cache.clone(),
            options.summary_centroids_from_superfiles,
        ));
        let parts: DashMap<_, _> = DashMap::new();
        let mut all_superfiles: Vec<Arc<SuperfileEntry>> = Vec::new();

        // Slow-CAS hydration. When the list carries a slow-state ref (keyed
        // on presence, never table kind — the user table's slow section is
        // always `None`), the flat view comes from one content-addressed
        // blob instead of the part fan; parts stay lazily loadable for
        // maintenance. The ref survives list-only churn (deleted-id stamps)
        // and is cleared by every membership `update`, so ref-equality with
        // the current manifest proves membership is unchanged — reuse the
        // already-decoded entries with zero I/O. That reuse is what keeps
        // the centroid state memory-resident across manifest versions until
        // the drainer republishes.
        let expected_n_superfiles: Option<u64> = if list.slow_vector_state_uri.is_some() {
            None
        } else {
            Some(list.parts.iter().map(|e| e.n_superfiles).sum())
        };
        // Hydration precedence: (1) slow-ref reuse (zero I/O, zero decode;
        // membership unchanged by construction since every membership
        // `update` clears the ref — this keeps centroid state resident
        // across manifest churn until the drainer republishes);
        // (2) slow-state blob fetch (ref present, one GET);
        // (3) part loading (no ref — the user table always, and the
        //     hidden table mid-maintenance).
        let reused: Option<Vec<Arc<SuperfileEntry>>> = match (
            list.slow_vector_state_uri.as_deref(),
            list.slow_vector_state_content_hash,
        ) {
            (Some(uri), Some(hash)) => current_manifest.as_ref().and_then(|cur| {
                let same_ref = cur.list.as_ref().is_some_and(|cl| {
                    cl.slow_vector_state_uri.as_deref() == Some(uri)
                        && cl.slow_vector_state_content_hash == Some(hash)
                });
                let complete = expected_n_superfiles
                    .is_none_or(|expected| cur.superfile_list.superfiles.len() as u64 == expected);
                (same_ref && complete).then(|| cur.superfile_list.superfiles.clone())
            }),
            _ => None,
        };
        // No silent degradation: a list that carries a slow-state ref IS
        // the entry payload's address — if the blob fails to load, verify,
        // or agree with the list, that is corruption and the load fails
        // loudly. The part fan below serves only manifests without a ref
        // (the user table always; the hidden table mid-maintenance).
        let entries_reused = reused.is_some();
        let hydrated: Option<Vec<Arc<SuperfileEntry>>> = match reused {
            Some(entries) => Some(entries),
            None => match (
                list.slow_vector_state_uri.as_deref(),
                list.slow_vector_state_content_hash,
            ) {
                (Some(uri), Some(hash)) => {
                    // Two-object model: the primary blob is routing-shaped
                    // (counts + 1-bit admit slab, no fp32 — GiBs → MiBs at
                    // 100M docs) and EVERYONE hydrates from it; exact
                    // centroid scores come from the section.
                    let entries = slow_vector_state::load_state(storage.as_ref(), uri, &hash)
                        .await
                        .map_err(|e| ManifestLoadError::SlowStateHydration(e.to_string()))?;
                    if let Some(expected) = expected_n_superfiles
                        && entries.len() as u64 != expected
                    {
                        return Err(ManifestLoadError::SlowStateHydration(format!(
                            "blob entry count {} != list total {expected}",
                            entries.len(),
                        )));
                    }
                    Some(entries)
                }
                _ => None,
            },
        };
        if let Some(entries) = hydrated {
            // Inherit any already-loaded part cells (maintenance reuse);
            // everything else stays an empty OnceCell for on-demand loads.
            for entry in &list.parts {
                let inherited = current_manifest
                    .as_ref()
                    .and_then(|cur| cur.parts.get(&entry.part_id).map(|kv| kv.value().clone()));
                parts.insert(
                    entry.part_id,
                    inherited.unwrap_or_else(|| Arc::new(OnceCell::new())),
                );
            }
            all_superfiles = entries;
        } else if let Some(current_manifest) = &current_manifest {
            // If we have an existing manifest, populate `parts` with
            // existing entries and track missing part IDs for lazy-load.
            let mut missing_part_ids = Vec::new();
            for entry in &list.parts {
                if let Some(existing) = current_manifest.parts.get(&entry.part_id) {
                    parts.insert(entry.part_id, existing.value().clone());
                } else {
                    missing_part_ids.push(entry.part_id);
                }
            }

            let threshold = options.eager_load_threshold_parts as usize;
            let eager = list.parts.len() <= threshold;

            if eager {
                let load_futs = missing_part_ids
                    .iter()
                    .map(|id| {
                        let loader = Arc::clone(&loader);
                        let pid = *id;
                        async move { loader.load(pid).await }
                    })
                    .collect::<Vec<_>>();
                let loaded = future::join_all(load_futs).await;
                for (pid, result) in missing_part_ids.iter().zip(loaded) {
                    let part = result?;
                    let cell = OnceCell::new();
                    cell.set(part).expect("fresh cell");
                    parts.insert(*pid, Arc::new(cell));
                }
                for entry in &list.parts {
                    let cell = parts.get(&entry.part_id).expect("part inserted above");
                    let part = cell
                        .value()
                        .get()
                        .expect("eager-fetched or inherited; must be set");
                    all_superfiles.extend(part.superfiles.iter().cloned());
                }
            } else {
                for pid in &missing_part_ids {
                    parts.insert(*pid, Arc::new(OnceCell::new()));
                }
            }
        } else {
            let n_parts = list.parts.len();
            let threshold = options.eager_load_threshold_parts as usize;
            let eager = n_parts <= threshold;
            if eager {
                // eager-fetching every part (small manifests — fast first query)
                // parallel-fetch every part + populate
                // the flat superfile_list.superfiles view so the
                // iteration-style query paths (`bm25_search`,
                // `vector_search`, `query_sql`) see all superfiles
                // without going through the hierarchical iterator.
                let part_ids: Vec<_> = list.parts.iter().map(|p| p.part_id).collect();
                let load_futs = part_ids
                    .iter()
                    .map(|id| {
                        let loader = Arc::clone(&loader);
                        let pid = *id;
                        async move { loader.load(pid).await }
                    })
                    .collect::<Vec<_>>();
                let loaded = future::join_all(load_futs).await;
                for (pid, result) in part_ids.iter().zip(loaded) {
                    let part = result?;
                    all_superfiles.extend(part.superfiles.iter().cloned());
                    let cell = OnceCell::new();
                    cell.set(part).expect("fresh OnceCell");
                    parts.insert(*pid, Arc::new(cell));
                }
            } else {
                // Lazy path: each part gets an empty
                // `OnceCell`; first `ManifestSnapshot::part(id).await`
                // triggers a single storage GET for that part.
                // `superfile_list.superfiles` stays empty — legacy
                // flat-iteration queries return zero results
                // until the hierarchical query path lands.
                // Callers in lazy mode today drive
                // `ManifestSnapshot::part().await` directly.
                for entry in &list.parts {
                    parts.insert(entry.part_id, Arc::new(OnceCell::new()));
                }
            }
        }

        // HIDDEN manifests (VectorCell partitioning) never hold summary
        // fp32 in RAM — the two-object slow-CAS model stores it once, in
        // the centroid section, and every exact rescore (consumer or
        // maintenance) reads from there. New-era blobs decode straight
        // into the stripped shape (routing wire); this pass covers legacy
        // full-form blobs and inherited entries so the resident shape is
        // uniform regardless of what was fetched. Unconditional — not
        // knob-gated: hidden fine fp32 is the residency pig (~620 MB at
        // 100M). User-table summaries stay resident — they are small
        // (~100 MB at 100M, per-fragment cells), and stripping them
        // forced pre-drain and filtered routing onto 1-bit estimates,
        // which measured filtered recall 0.722 against the 0.95 bar.
        // Grid centroids in the list are untouched.
        // Skip when the entry list was reused from the current manifest:
        // those `Arc`s were stripped and prewarmed by the load that first
        // produced them, and this path also runs on the strong-consistency
        // refresh (query hot path), where a re-walk buys nothing. For fresh
        // entries the strip + slab encode is a CPU wave (rotation +
        // sign-pack per fine centroid, ~1.4 s single-threaded at 100M), so
        // it runs on the blocking pool — not inline on a runtime worker.
        if !entries_reused {
            let strip = matches!(
                list.partition_strategy,
                PartitionStrategy::VectorCell { .. }
            );
            let vector_columns = options.vector_columns.clone();
            let pool = Arc::clone(&options.reader_pool);
            let mut entries = all_superfiles;
            let prewarm = move || {
                if strip {
                    strip_summary_centroids(&mut entries, &vector_columns);
                }
                prewarm_summary_admit_slabs(&entries, &vector_columns, &pool);
                entries
            };
            all_superfiles = match spawn_blocking(prewarm).await {
                Ok(entries) => entries,
                Err(join_error) => {
                    return Err(ManifestLoadError::SlowStateHydration(format!(
                        "admit slab prewarm task failed: {join_error}"
                    )));
                }
            };
        }

        let mut new_superfile_list = SuperfileList::empty(options.clone());
        new_superfile_list.manifest_id = pointer.manifest_id;
        new_superfile_list.superfiles = all_superfiles;
        let new_manifest = ManifestSnapshot {
            superfile_list: new_superfile_list,
            list: Some(list),
            parts,
            loader: Some(loader),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        };

        Ok(Arc::new(new_manifest))
    }

    /// Commit a new manifest version.
    ///
    /// Orchestrates the four-step sequence:
    ///
    /// 1. **In parallel** — write each new manifest part + write
    ///    the new manifest list. Independent of each other; the
    ///    list references parts by URI (= blake3 of bytes,
    ///    computed before any I/O). Issued via
    ///    [`futures::future::join_all`].
    /// 2. Await all of the above (visibility barrier #1: parts
    ///    and list must be durable before the pointer publishes).
    /// 3. Build the new pointer file (manifest_id, manifest_uri,
    ///    list_content_hash).
    /// 4. Conditional pointer-PUT (visibility barrier #2: the
    ///    rename is the only thing readers observe).
    ///
    /// `parts_to_write` should contain **only the parts that need
    /// to be persisted** (i.e., new + changed). Each element is the
    /// pre-encoded (Avro+zstd) bytes produced by [`rebuild_part_and_entry`]
    /// — passing them directly avoids a second encode cycle.
    /// Reused parts from the previous manifest version are not in this
    /// list — their URIs are already in `new_list.parts[i].uri`.
    pub async fn write(
        &self,
        storage: &dyn StorageProvider,
        expected_prev_etag: Option<&str>,
        parts_to_write: &[&[u8]],
    ) -> Result<(), CommitError> {
        let Some(list_to_write) = self.list.as_ref() else {
            return Ok(());
        };
        // Step 1+2: parallel write of (list, parts).
        //
        // Both futures are independent — the list's references to
        // each part's URI are content-addressable from the
        // in-memory bytes before any I/O, so there's no
        // happens-before edge between them.
        let list_fut = write_manifest(storage, list_to_write);
        let part_futs = parts_to_write
            .iter()
            .map(|encoded| write_part_bytes(storage, encoded));
        let part_join = future::join_all(part_futs);

        let (list_res, part_results) = tokio::join!(list_fut, part_join);
        // Translate `Storage(PreconditionFailed)` from sub-writes
        // into `WriteContentionExhausted` so callers (and the
        // writer's OCC retry loop) can match on one variant
        // regardless of which CAS lost the race — list or pointer.
        let list_res = list_res.map_err(translate_contention)?;
        for part_result in part_results {
            part_result.map_err(translate_contention)?;
        }

        // Step 3: build pointer.
        let pointer = PointerFile {
            manifest_id: self.get_manifest_id(),
            manifest_uri: list_res.uri,
            content_hash: list_res.content_hash,
        };

        // Step 4: conditional pointer write — the visibility
        // barrier. Until this succeeds, no reader sees the new
        // manifest version.
        write_pointer(storage, &pointer, expected_prev_etag).await?;
        Ok(())
    }

    pub fn get_all_superfiles(&self) -> &[Arc<SuperfileEntry>] {
        &self.superfile_list.superfiles
    }

    pub(crate) async fn get_pruned_superfiles(
        &self,
        leaves: &[PruneLeaf],
    ) -> Result<Vec<Arc<SuperfileEntry>>, ManifestLoadError> {
        match &self.list {
            Some(list) => {
                if list.slow_vector_state_uri.is_some() {
                    return Ok(self.superfile_list.superfiles.clone());
                }
                // Residency fast path: when the flat view is COMPLETE, the
                // read path must issue zero metadata GETs — serve the
                // resident entries and let the per-entry skips downstream
                // (term ranges, Blooms, min/max on each `SuperfileEntry`)
                // bound the data fetches. Part-level pruning only ever
                // paid off by *avoiding part loads*; with the entries
                // already resident there is nothing to avoid.
                let expected: u64 = list.parts.iter().map(|e| e.n_superfiles).sum();
                if self.superfile_list.superfiles.len() as u64 == expected {
                    return Ok(self.superfile_list.superfiles.clone());
                }
                // Incomplete view (legacy lazy manifests): prune at part
                // granularity and load only the survivors.
                // Intersect each constraining leaf's kept-part set. A leaf
                // with no part pruner (`None`) imposes no constraint.
                let mut kept: Option<HashSet<PartId>> = None;
                for leaf in leaves {
                    if let Some(part_ids) = leaf.keep_parts(list) {
                        let set: HashSet<PartId> = part_ids.into_iter().collect();
                        kept = Some(match kept {
                            None => set,
                            Some(existing) => existing.intersection(&set).copied().collect(),
                        });
                    }
                }
                // Preserve manifest (time) order of the surviving parts.
                let ordered: Vec<PartId> = match kept {
                    Some(set) => list
                        .parts
                        .iter()
                        .map(|p| p.part_id)
                        .filter(|id| set.contains(id))
                        .collect(),
                    None => list.parts.iter().map(|p| p.part_id).collect(),
                };
                hierarchical_iter::load_and_flatten(self, &ordered).await
            }
            None => Ok(hierarchical_iter::fallback_to_flat_superfiles(self)),
        }
    }

    /// All superfile entries, loaded through the hierarchical part loader in
    /// manifest (time) order. Vector search fans over every entry — cell
    /// routing (nearest global centroids) is the selection mechanism, not a
    /// part-level prune.
    pub(crate) async fn get_all_superfiles_loaded(
        &self,
    ) -> Result<Vec<Arc<SuperfileEntry>>, ManifestLoadError> {
        match &self.list {
            Some(list) => {
                if list.slow_vector_state_uri.is_some() {
                    return Ok(self.superfile_list.superfiles.clone());
                }
                // Flat-view fast path: when the resident view is COMPLETE
                // (eager-loaded, blob-hydrated, or update-derived from a
                // complete predecessor) it is exactly the parts' content, so
                // no part loads are needed. Completeness is checked against
                // the list's per-part counts because a LAZY manifest's
                // post-commit flat view is non-empty but incomplete (the new
                // entries only) — returning it would silently drop data.
                let expected: u64 = list.parts.iter().map(|e| e.n_superfiles).sum();
                if self.superfile_list.superfiles.len() as u64 == expected {
                    return Ok(self.superfile_list.superfiles.clone());
                }
                let all: Vec<PartId> = list.parts.iter().map(|p| p.part_id).collect();
                hierarchical_iter::load_and_flatten(self, &all).await
            }
            None => Ok(hierarchical_iter::fallback_to_flat_superfiles(self)),
        }
    }

    /// User superfiles whose logical birth version is not covered by the
    /// hidden drain watermark. Part-level birth ranges prune fully drained
    /// lazy parts before any object-store read.
    pub(crate) async fn get_undrained_superfiles_loaded(
        &self,
        drained: &list::DrainedVersionRanges,
    ) -> Result<Vec<Arc<SuperfileEntry>>, ManifestLoadError> {
        let Some(list) = &self.list else {
            return Ok(self
                .superfile_list
                .superfiles
                .iter()
                .filter(|entry| !drained.contains(entry.birth_version))
                .cloned()
                .collect());
        };
        // A list carrying a slow-state ref hydrated its membership from the
        // blob (hidden manifests write no parts at all), so the resident
        // flat view is authoritative and the parts sum below would be wrong
        // — zero parts would fan out to zero entries and silently drop
        // every undrained superfile.
        if list.slow_vector_state_uri.is_some() {
            return Ok(self
                .superfile_list
                .superfiles
                .iter()
                .filter(|entry| !drained.contains(entry.birth_version))
                .cloned()
                .collect());
        }
        let expected: u64 = list.parts.iter().map(|entry| entry.n_superfiles).sum();
        if self.superfile_list.superfiles.len() as u64 == expected {
            return Ok(self
                .superfile_list
                .superfiles
                .iter()
                .filter(|entry| !drained.contains(entry.birth_version))
                .cloned()
                .collect());
        }
        let part_ids: Vec<PartId> = list
            .parts
            .iter()
            .filter(|entry| {
                entry
                    .birth_version_range()
                    .is_none_or(|(lo, hi)| !drained.covers(lo, hi))
            })
            .map(|entry| entry.part_id)
            .collect();
        let entries = hierarchical_iter::load_and_flatten(self, &part_ids).await?;
        Ok(entries
            .into_iter()
            .filter(|entry| !drained.contains(entry.birth_version))
            .collect())
    }

    pub fn get_all_list_entries(&self) -> &[ManifestPartEntry] {
        match &self.list {
            Some(list) => &list.parts,
            None => &[],
        }
    }

    /// Build a successor manifest with `new_entries` appended.
    /// Preserves the persistence-side metadata (`list`, `loader`)
    /// from the predecessor; the per-part cache is fresh (an empty
    /// `DashMap`) because the parts referenced by the new version
    /// may differ. Cross-version part inheritance via content-
    /// addressed `Arc::clone` lives in `Supertable::refresh`.
    pub fn with_appended(&self, new_entries: Vec<Arc<SuperfileEntry>>) -> Self {
        Self {
            superfile_list: self.superfile_list.with_appended(new_entries),
            list: self.list.clone(),
            parts: DashMap::new(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// The deleted-`_id` set's encoded bytes carried inline in the list
    /// (zero-GET read path); `None` on manifests stamped before the
    /// inline bytes existed.
    pub(crate) fn deleted_user_ids_inline(&self) -> Option<&[u8]> {
        self.list.as_ref()?.deleted_user_ids_inline.as_deref()
    }

    /// Slow-CAS section accessor: the content-addressed blob holding this
    /// table's superfile entries (drain-owned routing/centroid state), or
    /// `None` when no maintenance has published one — always `None` on the
    /// user table. Consumers key on presence, never on table kind.
    pub(crate) fn slow_vector_state_blob(&self) -> Option<(&str, part::ContentHash)> {
        let list = self.list.as_ref()?;
        Some((
            list.slow_vector_state_uri.as_deref()?,
            list.slow_vector_state_content_hash?,
        ))
    }

    /// Centroid-section sibling of the slow-CAS blob (contiguous fp32 fine
    /// centroids in `(entry, column, cell)` order) — the stripped-summary
    /// admit rescore hydrates it once instead of fanning per-cell superfile
    /// reads. `None` on manifests written before the sibling existed.
    pub(crate) fn slow_vector_state_centroids_blob(&self) -> Option<&RoutingRef> {
        self.list.as_ref()?.slow_vector_state_centroids.as_ref()
    }

    /// Stamp (or replace) the hidden index's consolidated deleted-user-`_id`
    /// bytes in the manifest list. Bumps `manifest_id` like a normal commit
    /// without touching superfiles or parts.
    pub fn with_deleted_user_ids(&self, encoded: Vec<u8>) -> Self {
        let next_id = self.get_next_manifest_id();
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            list.manifest_id = next_id;
            list.deleted_user_ids_inline = Some(encoded.clone());
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: next_id,
                options: Arc::clone(&self.superfile_list.options),
                superfiles: self.superfile_list.superfiles.clone(),
                vector_index_storage_prefix: self
                    .superfile_list
                    .vector_index_storage_prefix
                    .clone(),
            },
            list: new_list,
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// Stamp (or replace) the slow-CAS vector-state blob reference — the
    /// content-addressed object holding this table's superfile entries
    /// (drain-owned routing/centroid state). Bumps `manifest_id` like a
    /// normal commit without touching superfiles or parts, mirroring
    /// [`ManifestSnapshot::with_deleted_user_ids`]. Standalone restamp path
    /// (e.g. post-drain refresh); membership commits instead compose the
    /// ref via [`Self::with_slow_vector_state_ref`] before the same CAS.
    pub fn with_slow_vector_state(
        &self,
        uri: String,
        hash: part::ContentHash,
        centroids: RoutingRef,
    ) -> Self {
        let next_id = self.get_next_manifest_id();
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            list.manifest_id = next_id;
            list.slow_vector_state_uri = Some(uri);
            list.slow_vector_state_content_hash = Some(hash);
            list.slow_vector_state_centroids = Some(centroids);
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: next_id,
                options: Arc::clone(&self.superfile_list.options),
                superfiles: self.superfile_list.superfiles.clone(),
                vector_index_storage_prefix: self
                    .superfile_list
                    .vector_index_storage_prefix
                    .clone(),
            },
            list: new_list,
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// Stamp the slow-state ref onto an already-built successor **without**
    /// bumping `manifest_id`. Used when composing a membership
    /// [`crate::supertable::writer::try_commit_attempt`] so the blob PUT and
    /// list/pointer CAS publish together — closing the window where a crash
    /// leaves membership durable with a cleared slow-state ref.
    pub(crate) fn with_slow_vector_state_ref(
        &self,
        uri: String,
        hash: part::ContentHash,
        centroids: RoutingRef,
    ) -> Self {
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            list.slow_vector_state_uri = Some(uri);
            list.slow_vector_state_content_hash = Some(hash);
            list.slow_vector_state_centroids = Some(centroids);
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: self.superfile_list.manifest_id,
                options: Arc::clone(&self.superfile_list.options),
                superfiles: self.superfile_list.superfiles.clone(),
                vector_index_storage_prefix: self
                    .superfile_list
                    .vector_index_storage_prefix
                    .clone(),
            },
            list: new_list,
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// Stamp (or replace) the partition strategy on this manifest snapshot.
    /// Updates both the persisted list metadata and the in-memory options
    /// fallback used before the first list write lands.
    pub fn with_partition_strategy(&self, strategy: list::PartitionStrategy) -> Self {
        let new_list = match self.list.as_ref() {
            Some(list) => {
                let mut list = list.clone();
                list.partition_strategy = strategy.clone();
                Some(list)
            }
            None => None,
        };
        Self {
            superfile_list: SuperfileList {
                manifest_id: self.manifest_id,
                options: Arc::clone(&self.options),
                superfiles: self.superfiles.clone(),
                vector_index_storage_prefix: self.vector_index_storage_prefix.clone(),
            },
            list: new_list.or_else(|| self.list.clone()),
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: Some(strategy),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// Stamp (or replace) the global vector cell-index grid on this snapshot.
    /// Mirrors [`ManifestSnapshot::with_partition_strategy`]: updates the persisted list
    /// metadata when present, and the in-memory stamp used before the first
    /// list write lands (the first commit-with-vectors).
    pub fn with_global_vector_index(&self, index: list::GlobalVectorIndex) -> Self {
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            list.global_vector_index = Some(index.clone());
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: self.manifest_id,
                options: Arc::clone(&self.options),
                superfiles: self.superfiles.clone(),
                vector_index_storage_prefix: self.vector_index_storage_prefix.clone(),
            },
            list: new_list.or_else(|| self.list.clone()),
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: Some(index),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// Stamp the drained user commit-versions on this (hidden) snapshot, so the
    /// next `update`/commit persists them. Mirrors the other stampers: updates
    /// the list when present, and the in-memory stamp before the first hidden
    /// list lands. The drain calls this with the advanced set in the same
    /// commit that appends the batch's cells (atomic via the manifest CAS).
    pub fn with_drained_ranges(&self, ranges: list::DrainedVersionRanges) -> Self {
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            list.drained_ranges = ranges.clone();
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: self.manifest_id,
                options: Arc::clone(&self.options),
                superfiles: self.superfiles.clone(),
                vector_index_storage_prefix: self.vector_index_storage_prefix.clone(),
            },
            list: new_list.or_else(|| self.list.clone()),
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: Some(ranges),
        }
    }

    /// Successor snapshot with `additions` merged (union per superfile) into
    /// the superseded-cell map. Used to stamp a cell split's parents in the
    /// same OCC commit as the child superfiles, so the marker lands atomically
    /// with the membership that makes it meaningful. In-process-only manifests
    /// (no persisted list) have nothing to stamp and pass through unchanged.
    pub fn with_superseded_cells_added(&self, additions: &BTreeMap<Uuid, BTreeSet<u32>>) -> Self {
        let new_list = self.list.as_ref().map(|list| {
            let mut list = list.clone();
            for (superfile_id, cells) in additions {
                list.superseded_cells
                    .entry(*superfile_id)
                    .or_default()
                    .extend(cells.iter().copied());
            }
            list
        });
        Self {
            superfile_list: SuperfileList {
                manifest_id: self.manifest_id,
                options: Arc::clone(&self.options),
                superfiles: self.superfiles.clone(),
                vector_index_storage_prefix: self.vector_index_storage_prefix.clone(),
            },
            list: new_list.or_else(|| self.list.clone()),
            parts: self.parts.clone(),
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        }
    }

    /// The persisted list's per-superfile tombstone-seq map. `None`
    /// for in-process-only manifests (no persisted list ⇒ no sidecars
    /// can exist).
    pub fn get_tombstone_seqs(&self) -> Option<&BTreeMap<Uuid, u64>> {
        self.list.as_ref().map(|l| &l.tombstone_seqs)
    }

    /// The persisted list's per-superfile superseded-cell map: for each
    /// superfile, the set of cell ids whose on-disk blocks are dead
    /// (replaced in place by a cell split). Readers exclude these cells
    /// from routing so their stale blocks are never selected or fetched.
    /// `None` for in-process-only manifests (no persisted list).
    pub fn get_superseded_cells(&self) -> Option<&BTreeMap<Uuid, BTreeSet<u32>>> {
        self.list.as_ref().map(|l| &l.superseded_cells)
    }

    /// Build a successor manifest identical to `self` except that every
    /// superfile in `touched` has its tombstone seq set to the successor's
    /// `manifest_id`. This is the mutation pipeline's post-sidecar stamp:
    /// no superfile entries or parts change, so persisting the successor
    /// is a list + pointer write only (empty `parts_to_write`).
    ///
    /// Returns `None` for in-process-only manifests (no persisted list —
    /// nothing to stamp, and no cross-process readers to inform).
    pub(crate) fn with_tombstone_seqs_bumped(&self, touched: &[Uuid]) -> Option<Self> {
        let list = self.list.as_ref()?;
        let next_id = self.get_next_manifest_id();
        let mut new_list = list.clone();
        new_list.manifest_id = next_id;
        for id in touched {
            new_list.tombstone_seqs.insert(*id, next_id);
        }
        let mut superfile_list = self.superfile_list.clone();
        superfile_list.manifest_id = next_id;
        // Same parts as the predecessor — inherit the loaded-part cache
        // wholesale so the stamp never forces a part re-fetch.
        let parts = DashMap::new();
        for kv in self.parts.iter() {
            parts.insert(*kv.key(), Arc::clone(kv.value()));
        }
        Some(Self {
            superfile_list,
            list: Some(new_list),
            parts,
            loader: self.loader.clone(),
            stamped_partition_strategy: self.stamped_partition_strategy.clone(),
            stamped_global_vector_index: self.stamped_global_vector_index.clone(),
            stamped_drained_ranges: self.stamped_drained_ranges.clone(),
        })
    }

    /// Lazy-load entry point for manifest parts.
    ///
    /// Concurrent callers on the same not-yet-loaded `part_id`
    /// share a single `StorageProvider::get` via the per-part
    /// `tokio::sync::OnceCell` — 100 concurrent queries on a
    /// cold part see exactly one load.
    ///
    /// Errors:
    /// - `OpenError::Build(BuildError::Store(...))` if no loader
    ///   is attached (in-process-only manifest).
    /// - `OpenError::ContentHashMismatch` if the loaded part's
    ///   blake3 doesn't match the manifest list's recorded hash.
    /// - `OpenError::ManifestPartParse { … }` for Avro / zstd
    ///   decode failures.
    pub async fn get_part_by_id(
        &self,
        part_id: PartId,
    ) -> Result<Arc<ManifestPart>, ManifestLoadError> {
        let loader = self
            .loader
            .as_ref()
            .ok_or(ManifestLoadError::NoLoaderAttached)?;
        let cell = self
            .parts
            .entry(part_id)
            .or_insert_with(|| Arc::new(OnceCell::new()))
            .clone();
        let loaded = cell.get_or_try_init(|| loader.load(part_id)).await?;
        Ok(Arc::clone(loaded))
    }

    /// Fp32 fine centroids for this USER manifest's stripped summary
    /// cells, hydrated once per manifest generation from the FULL
    /// manifest parts — the user table's content-addressed store for
    /// summary fp32, mirroring what the slow-CAS centroid section does
    /// for the hidden table. Consumers under
    /// `summary_centroids_from_superfiles` open with routing parts
    /// (1-bit slabs, no fp32); the first rescore pays one full-part wave
    /// (bytes land in the manifest disk cache), then every query scores
    /// from RAM. User summaries are per-fragment cells, small enough to
    /// keep resident. `None` when this is a hidden (cell-partitioned)
    /// manifest — its rescore reads the spilled centroid section — or
    /// when no loader/parts exist (entries are inline and unstripped).
    pub(crate) async fn user_centroids_for_rescore(&self) -> Option<Arc<UserCentroidCache>> {
        let list = self.list.as_ref()?;
        if matches!(
            list.partition_strategy,
            PartitionStrategy::VectorCell { .. }
        ) {
            return None;
        }
        let loader = self.loader.as_ref()?;
        if list.parts.is_empty() {
            return None;
        }
        let manifest_id = self.get_manifest_id();
        let slot = Arc::clone(&self.options.user_centroid_cache);
        let mut guard = slot.lock().await;
        if let Some(cache) = guard.as_ref()
            && cache.manifest_id == manifest_id
        {
            return Some(Arc::clone(cache));
        }
        // Full-form loads bypass the snapshot's part cells (those hold the
        // routing-decoded form on knob-on consumers); the loader's disk
        // cache still dedups bytes across generations by content hash.
        let waves = list.parts.iter().map(|part| {
            let loader = Arc::clone(loader);
            let part_id = part.part_id;
            async move { loader.load_full(part_id).await }
        });
        match future::try_join_all(waves).await {
            Ok(parts) => {
                let cache = Arc::new(UserCentroidCache::from_parts(manifest_id, &parts));
                *guard = Some(Arc::clone(&cache));
                Some(cache)
            }
            Err(error) => {
                eprintln!(
                    "[supertable] full-part centroid hydration unavailable ({error}); falling \
                     back to per-superfile centroid reads"
                );
                None
            }
        }
    }

    /// Resolve one superfile by storage URI. Checks the flat
    /// [`SuperfileList::superfiles`] view first; when the entry is absent
    /// there (lazy list/parts layout), walks manifest parts until a match
    /// is found.
    pub(crate) async fn lookup_superfile_entry(
        &self,
        uri: SuperfileUri,
    ) -> Result<Option<Arc<SuperfileEntry>>, ManifestLoadError> {
        if let Some(entry) = self.superfiles.iter().find(|e| e.uri == uri) {
            return Ok(Some(Arc::clone(entry)));
        }
        let Some(list) = &self.list else {
            return Ok(None);
        };
        for part_entry in &list.parts {
            let part = self.get_part_by_id(part_entry.part_id).await?;
            if let Some(entry) = part.superfiles.iter().find(|e| e.uri == uri) {
                return Ok(Some(Arc::clone(entry)));
            }
        }
        Ok(None)
    }

    /// Returns the new ManifestPartEntries when `new_entries` are added to `old` manifest. This
    /// operation may create new ManifestParts. The function also returns the new ManifestParts that
    /// the caller can decide to write to storage.
    pub async fn update(
        &self,
        new_entries: &[Arc<SuperfileEntry>],
        entries_to_remove: &[Arc<SuperfileEntry>],
    ) -> Result<(ManifestSnapshot, Vec<EncodedPart>), ManifestError> {
        self.update_inner(new_entries, entries_to_remove, false)
            .await
    }

    /// Compaction replaces physical files without changing the logical user
    /// commits already represented by those files. Preserve each replacement
    /// entry's inherited `birth_version` so the hidden drain watermark keeps
    /// recognizing that data as drained.
    pub(crate) async fn update_preserving_birth_versions(
        &self,
        new_entries: &[Arc<SuperfileEntry>],
        entries_to_remove: &[Arc<SuperfileEntry>],
    ) -> Result<(ManifestSnapshot, Vec<EncodedPart>), ManifestError> {
        self.update_inner(new_entries, entries_to_remove, true)
            .await
    }

    async fn update_inner(
        &self,
        new_entries: &[Arc<SuperfileEntry>],
        entries_to_remove: &[Arc<SuperfileEntry>],
        preserve_birth_versions: bool,
    ) -> Result<(ManifestSnapshot, Vec<EncodedPart>), ManifestError> {
        // 1. Resolve the effective partition strategy. Locked at
        //    first commit: read from the existing manifest list
        //    if present, else use the options default.
        let opts = self.get_opts();
        let strategy = self.get_partition_strategy();
        // Part wire form follows the LOCKED strategy, not raw options: a
        // handle opened with a mismatched partition option must never flip
        // a user part to routing-only wire (that would drop its fp32
        // summaries durably, with no sibling to recover from).
        let hidden_table = matches!(&strategy, PartitionStrategy::VectorCell { .. });

        // 2. Stamp each new entry with its partition key — this also validates
        //    against the strategy (surfaces SuperfileSpansPartition /
        //    unsupported-column-type / missing-partition_hint at commit). The
        //    partition lives on the ENTRY, not the part: parts are size-bucketed
        //    at the table level (below), so a part spans partitions and carries
        //    no key of its own. A reader recovers each superfile's partition
        //    from its entry in the part, with no data-file open. Assignment
        //    runs at commit time, so e.g. IngestionTime resolves to the current
        //    day bucket.
        //
        //    Entries must arrive unstamped (empty partition_key): the key is
        //    derived here, and every source of new entries — the writer,
        //    compaction (a merged superfile is a fresh entry), WAL replay —
        //    builds them with an empty key. A non-empty key would mean an
        //    earlier stage stamped it; committing would silently re-derive and
        //    overwrite that assignment (e.g. shifting an IngestionTime entry to
        //    the current day), so reject it instead.
        //
        //    Fresh writes stamp each new entry's `birth_version` to this
        //    commit's version. Compaction instead preserves the oldest input
        //    version inherited by its replacement: changing physical files
        //    does not make already-drained logical data undrained.
        let birth_version = self.get_next_manifest_id();
        let stamped_new_entries: Vec<Arc<SuperfileEntry>> = new_entries
            .iter()
            .map(|e| {
                if !e.partition_key.is_empty() {
                    return Err(ManifestError::EntryAlreadyPartitioned {
                        detail: format!(
                            "superfile {} arrived with a partition_key already set",
                            e.superfile_id
                        ),
                    });
                }
                let pk = assign_partition(e, &strategy)?;
                let entry_birth_version = if preserve_birth_versions {
                    e.birth_version
                } else {
                    birth_version
                };
                Ok(Arc::new(SuperfileEntry {
                    partition_key: encode_partition_key(&pk),
                    birth_version: entry_birth_version,
                    ..(**e).clone()
                }))
            })
            .collect::<Result<_, ManifestError>>()?;

        // 3. One table-level lineage: new entries append to the last (latest)
        //    part — rewriting it, or splitting into a fresh part when it would
        //    exceed target_superfiles_per_part — while earlier parts carry over
        //    unchanged (same content-hash + URI, no re-encode / PUT). When there
        //    is no prior part, the new entries form the first one. The partition
        //    tag stays on each entry (routing + zone-map input); it no longer
        //    dictates part boundaries, so a query prunes parts by their
        //    aggregates and filters the surviving entries by tag in memory.
        // User tables persist membership in manifest parts. Hidden VectorCell
        // tables persist membership/routing in the slow-CAS blob instead, so
        // `None` skips part creation/rewrite entirely; the existing drain /
        // compaction slow-state publication remains the sole hidden store.
        let list_entries: Option<&[ManifestPartEntry]> =
            if matches!(&strategy, PartitionStrategy::VectorCell { .. }) {
                None
            } else {
                Some(self.get_all_list_entries())
            };
        let latest_idx = list_entries.and_then(|entries| entries.len().checked_sub(1));
        let mut out_list_entries: Vec<ManifestPartEntry> = Vec::new();
        let mut parts_to_write: Vec<EncodedPart> = Vec::new();
        let mut pending_new = list_entries
            .map(|_| stamped_new_entries.to_vec())
            .unwrap_or_default();

        for (i, entry) in list_entries.unwrap_or_default().iter().enumerate() {
            if Some(i) != latest_idx || pending_new.is_empty() {
                out_list_entries.push(entry.clone());
                continue;
            }
            let new_for_part = std::mem::take(&mut pending_new);
            let combined_n = entry.n_superfiles + new_for_part.len() as u64;
            // Freeze the latest part once it reaches either soft cap — the
            // superfile-count target or the compressed-size threshold. Absorbing
            // appends into an already-fat part re-encodes and re-PUTs its whole
            // payload every commit (O(part bytes) commit cost, plus an orphaned
            // part object per commit); splitting keeps prior parts immutable,
            // which is the point of the part scheme. Size matters independently
            // of count for vector tables: cell-packed entries carry fine
            // centroids, so a handful of entries can outweigh thousands of
            // scalar-only ones.
            let latest_at_size_cap = entry.size_bytes_compressed
                >= self.superfile_list.options.part_size_threshold_bytes;
            if combined_n > self.superfile_list.options.target_superfiles_per_part
                || latest_at_size_cap
            {
                // Split: keep the existing part, emit a fresh part for the new
                // superfiles.
                out_list_entries.push(entry.clone());
                let (fresh_entry, fresh_encoded_part) =
                    rebuild_part_and_entry(vec![], new_for_part, None, hidden_table);
                out_list_entries.push(fresh_entry);
                parts_to_write.push(fresh_encoded_part);
            } else {
                // Rewrite the latest part = its existing superfiles + the new.
                let existing_part = self.get_part_by_id(entry.part_id).await?;
                let (rebuilt_entry, rebuilt_encoded_part) = rebuild_part_and_entry(
                    existing_part.superfiles.clone(),
                    new_for_part,
                    Some(entry),
                    hidden_table,
                );
                out_list_entries.push(rebuilt_entry);
                parts_to_write.push(rebuilt_encoded_part);
            }
        }

        // Cold start: no prior parts, so the new entries form the first part.
        if !pending_new.is_empty() {
            let (fresh_entry, fresh_encoded_part) =
                rebuild_part_and_entry(vec![], pending_new, None, hidden_table);
            out_list_entries.push(fresh_entry);
            parts_to_write.push(fresh_encoded_part);
        }

        // At this point, out_list_entries contains all new ManifestListEntries that will be written.
        // If these out_list_entries i.e Vec<ManifestPartEntry> cause new ManifestParts to be created, those
        // are stored in parts_to_write.

        let mut out_list_entries_after_removal = Vec::new();
        if entries_to_remove.is_empty() {
            out_list_entries_after_removal = out_list_entries;
        } else {
            // 4. Apply removals across every part: drop the removed superfile_ids
            //    wherever they live; a part with no match is left untouched.
            let removal_ids = entries_to_remove
                .iter()
                .map(|r| r.superfile_id)
                .collect::<HashSet<_>>();
            for entry in out_list_entries {
                // TODO: Handle merging 2 parts into one if their sum is within threshold

                // Fetch the part's current superfiles — from parts_to_write (freshly
                // rebuilt this commit) or the prior manifest.
                let (superfile_entries_in_part, existing_part_to_update) = if let Some(existing) =
                    parts_to_write
                        .iter_mut()
                        .find(|ep| ep.part.part_id == entry.part_id)
                {
                    (existing.part.superfiles.clone(), Some(existing))
                } else if let Ok(existing_part) = self.get_part_by_id(entry.part_id).await {
                    (existing_part.superfiles.clone(), None)
                } else {
                    return Err(ManifestError::UnknownPartId(entry.part_id));
                };
                let final_superfile_entries = superfile_entries_in_part
                    .iter()
                    .filter(|s| !removal_ids.contains(&s.superfile_id))
                    .cloned()
                    .collect::<Vec<_>>();

                // No superfile removed from this part → keep it unchanged.
                if final_superfile_entries.len() == superfile_entries_in_part.len() {
                    out_list_entries_after_removal.push(entry);
                    continue;
                }

                let (fresh_entry, fresh_encoded_part) =
                    rebuild_part_and_entry(vec![], final_superfile_entries, None, hidden_table);

                if let Some(existing) = existing_part_to_update {
                    *existing = fresh_encoded_part;
                } else {
                    parts_to_write.push(fresh_encoded_part);
                }

                out_list_entries_after_removal.push(fresh_entry);
            }
        }

        let ids_to_remove = entries_to_remove
            .iter()
            .map(|e| e.superfile_id)
            .collect::<HashSet<_>>();

        // Carry the tombstone-seq map forward, dropping entries for
        // superfiles this commit removes — their sidecars leave the
        // manifest with them.
        let mut tombstone_seqs = self
            .list
            .as_ref()
            .map(|list| list.tombstone_seqs.clone())
            .unwrap_or_default();
        tombstone_seqs.retain(|id, _| !ids_to_remove.contains(id));
        let mut superseded_cells = self
            .list
            .as_ref()
            .map(|list| list.superseded_cells.clone())
            .unwrap_or_default();
        superseded_cells.retain(|id, _| !ids_to_remove.contains(id));

        let opts_hash = options_hash::compute_options_hash(opts.as_ref(), &strategy);
        let vector_columns: Vec<list::VectorColumnInfo> = opts
            .vector_columns
            .iter()
            .map(|v| list::VectorColumnInfo {
                column: v.column.clone(),
                dim: v.dim,
                n_cent: v.n_cent,
                rot_seed: v.rot_seed,
                metric: format!("{:?}", v.metric).to_lowercase(),
            })
            .collect();
        let new_list = Manifest {
            // Carry/advance the hidden drain watermark via the stamp (the drain
            // sets it with `with_drained_ranges` in the same commit). Empty on
            // the user manifest.
            drained_ranges: self.get_drained_ranges(),
            tombstone_seqs,
            superseded_cells,
            format_version: LIST_FORMAT_VERSION.into(),
            manifest_id: self.get_next_manifest_id(),
            options_hash: opts_hash,
            schema: Vec::new(),
            id_column: opts.id_column.clone(),
            fts_columns: opts
                .fts_columns
                .iter()
                .map(|f| list::FtsColumnInfo {
                    column: f.column.clone(),
                })
                .collect(),
            vector_columns: opts
                .vector_columns
                .iter()
                .map(|v| list::VectorColumnInfo {
                    column: v.column.clone(),
                    dim: v.dim,
                    n_cent: v.n_cent,
                    rot_seed: v.rot_seed,
                    metric: format!("{:?}", v.metric).to_lowercase(),
                })
                .collect(),
            partition_strategy: strategy,
            // Never stamp a sibling prefix onto a hidden VectorCell manifest:
            // the prefix is only ever resolved off the USER manifest to locate
            // the hidden index, and a hidden table claiming its own hidden
            // subtree would misroute maintenance/GC.
            vector_index_storage_prefix: if matches!(
                self.get_partition_strategy(),
                list::PartitionStrategy::VectorCell { .. }
            ) {
                None
            } else {
                self.stamp_vector_index_storage_prefix(&vector_columns)
            },
            global_vector_index: self.get_global_vector_index(),
            deleted_user_ids_inline: self
                .list
                .as_ref()
                .and_then(|l| l.deleted_user_ids_inline.clone()),
            // Slow-CAS section is deliberately NOT carried into the
            // successor: `update` is the membership-change path (its only
            // production caller is the commit attempt), and a membership
            // change invalidates the prior entry blob. The commit attempt
            // restamps a fresh blob onto this successor via
            // [`Self::with_slow_vector_state_ref`] before the list/pointer
            // CAS, so membership and the slow-state ref publish together.
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: out_list_entries_after_removal,
        };
        let mut new_superfile_list = self
            .get_all_superfiles()
            .iter()
            .chain(stamped_new_entries.iter())
            .map(Arc::clone)
            .collect::<Vec<_>>();
        new_superfile_list.retain(|e| !ids_to_remove.contains(&e.superfile_id));

        let new_superfile_list = SuperfileList {
            manifest_id: self.get_next_manifest_id(),
            options: self.get_opts(),
            superfiles: new_superfile_list,
            vector_index_storage_prefix: None,
        };
        let loader = opts.storage.as_ref().map(|storage| {
            Arc::new(ManifestPartLoader::new_with_cache(
                storage.clone(),
                &new_list,
                opts.manifest_disk_cache.clone(),
            ))
        });
        // Inherit only the cached parts the new list still
        // references — entries for rewritten/removed parts are
        // dropped rather than carried forward, so the in-memory
        // parts cache can't grow without bound across commits.
        // Surviving parts keep their warm cache entry (no refetch);
        // the freshly-written parts are seeded below.
        let live_part_ids: HashSet<_> = new_list.parts.iter().map(|e| e.part_id).collect();
        let parts = DashMap::new();
        for kv in self.parts.iter() {
            if live_part_ids.contains(kv.key()) {
                parts.insert(*kv.key(), kv.value().clone());
            }
        }
        for part in parts_to_write.iter() {
            let part = part.part.clone();
            parts.insert(
                part.part_id,
                Arc::new(OnceCell::new_with(Some(Arc::new(part)))),
            );
        }

        let new_manifest = ManifestSnapshot {
            superfile_list: new_superfile_list,
            list: Some(new_list),
            parts,
            loader,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        };

        Ok((new_manifest, parts_to_write))
    }
}

/// build one `ManifestPart` from `superfiles` + the
/// matching `ManifestPartEntry`. Encodes the part in both wire forms —
/// full (fp32 + slab) and routing-only (counts + slab) — content-hashes
/// each, and computes the list-level aggregate skip summaries that
/// `list_prune` reads at query time. Both encodings ship in the returned
/// [`EncodedPart`] so the commit PUTs them together; the entry carries
/// the sibling ref consumer opens select on.
/// If base_part is Some, the superfiles MUST only include the new superfiles to be added.
fn rebuild_part_and_entry(
    old_superfiles: Vec<Arc<SuperfileEntry>>,
    new_superfiles: Vec<Arc<SuperfileEntry>>,
    base_part: Option<&ManifestPartEntry>,
    hidden: bool,
) -> (ManifestPartEntry, EncodedPart) {
    // Hidden (VectorCell) manifests write parts in ROUTING wire form and
    // skip the sibling: their fp32 is stored once per generation in the
    // slow-CAS centroid section, and their entries hydrate stripped for
    // everyone — a full-form part would be a fourth fp32 copy nothing
    // reads. User manifests keep both forms: the full part is the fp32
    // store the first rescore hydrates from (3 GETs vs the measured
    // 848-GET per-superfile fan), the routing sibling is what serving
    // opens fetch. `hidden` comes from the caller's PERSISTED strategy
    // (`get_partition_strategy`), never re-derived from raw options: a
    // handle opened with mismatched options must not flip a user part to
    // routing-only wire (fp32 would be dropped durably, with no sibling).
    let aggregates = aggregates::compute(&new_superfiles, base_part);
    let superfiles = old_superfiles
        .into_iter()
        .chain(new_superfiles)
        .collect::<Vec<_>>();
    let part = ManifestPart {
        format_version: part::FORMAT_VERSION.into(),
        part_id: PartId::new_v4(),
        superfiles,
    };
    let primary_mode = if hidden {
        SummaryWireMode::RoutingOnly
    } else {
        SummaryWireMode::Full
    };
    let compressed = part::encode_with_mode(&part, primary_mode);
    let size_compressed = compressed.len() as u64;
    let content_hash = ContentHash::of(&compressed);
    let size_uncompressed = frame_content_size(&compressed, size_compressed);
    let routing = if hidden {
        None
    } else {
        let routing_encoded = part::encode_with_mode(&part, SummaryWireMode::RoutingOnly);
        let routing_hash = ContentHash::of(&routing_encoded);
        Some((routing_encoded, routing_hash))
    };
    let entry = ManifestPartEntry {
        part_id: part.part_id,
        uri: part_uri(&content_hash),
        n_superfiles: part.superfiles.len() as u64,
        size_bytes_compressed: size_compressed,
        size_bytes_uncompressed: size_uncompressed,
        content_hash,
        routing: routing.as_ref().map(|(_, hash)| RoutingRef {
            uri: part_uri(hash),
            content_hash: *hash,
        }),
        id_range: aggregates.id_range,
        scalar_stats_agg: aggregates.scalar_stats_agg,
        fts_summary_agg: aggregates.fts_summary_agg,
    };
    (
        entry,
        EncodedPart {
            part,
            encoded: compressed,
            routing_encoded: routing.map(|(bytes, _)| bytes),
        },
    )
}

/// Pulls manifest parts through a [`StorageProvider`] and verifies
/// content-hash on load.
///
/// One `ManifestPartLoader` per `ManifestSnapshot`. The same `Arc<dyn
/// StorageProvider>` is shared with the `DiskCacheStore` —
/// one auth handshake, one connection pool.
///
/// An optional [`ManifestDiskCache`] short-circuits the storage GET
/// when the part's compressed bytes are already on local disk. Because
/// parts are content-addressed, a cache hit can never be stale.
pub struct ManifestPartLoader {
    storage: Arc<dyn StorageProvider>,
    /// Maps `PartId → (expected content_hash, uri, routing sibling)`.
    /// Built from the manifest list at construction; immutable
    /// per-`ManifestSnapshot`.
    parts_index: HashMap<PartId, (ContentHash, String, Option<RoutingRef>)>,
    /// On-disk cache for compressed part bytes. `None` disables the
    /// cache (in-process-only supertables, tests, or storage attached
    /// without a `disk_cache_root` configured).
    manifest_disk_cache: Option<Arc<ManifestDiskCache>>,
    /// Consumer memory mode (`summary_centroids_from_superfiles`): load
    /// each part's routing sibling (counts + 1-bit slab, no fp32) when
    /// the list stamps one. Writer handles keep this off — part rebuilds
    /// re-encode the full form and need resident fp32.
    prefer_routing: bool,
}

impl ManifestPartLoader {
    pub fn new(storage: Arc<dyn StorageProvider>, list: &Manifest) -> Self {
        Self::new_with_cache(storage, list, None)
    }

    /// Like [`Self::new`] but attaches an on-disk part-bytes cache.
    pub fn new_with_cache(
        storage: Arc<dyn StorageProvider>,
        list: &Manifest,
        manifest_disk_cache: Option<Arc<ManifestDiskCache>>,
    ) -> Self {
        Self::new_with_cache_and_mode(storage, list, manifest_disk_cache, false)
    }

    /// Like [`Self::new_with_cache`] with the consumer routing mode
    /// explicit — `prefer_routing` selects each part's routing sibling
    /// when present.
    pub fn new_with_cache_and_mode(
        storage: Arc<dyn StorageProvider>,
        list: &Manifest,
        manifest_disk_cache: Option<Arc<ManifestDiskCache>>,
        prefer_routing: bool,
    ) -> Self {
        let mut idx = HashMap::with_capacity(list.parts.len());
        for entry in &list.parts {
            idx.insert(
                entry.part_id,
                (entry.content_hash, entry.uri.clone(), entry.routing.clone()),
            );
        }
        Self {
            storage,
            parts_index: idx,
            manifest_disk_cache,
            prefer_routing,
        }
    }

    /// Fetch + verify + decode one part. Returns the parsed
    /// `Arc<ManifestPart>`.
    ///
    /// Consults the on-disk cache first (a hit skips the storage GET);
    /// on a miss the freshly-fetched bytes are written back to the
    /// cache (best-effort) before decoding.
    pub async fn load(&self, part_id: PartId) -> Result<Arc<ManifestPart>, ManifestLoadError> {
        self.load_with_form(part_id, self.prefer_routing).await
    }

    /// [`Self::load`] forced to the FULL wire form (fp32 summaries intact)
    /// regardless of the loader's routing preference — the user-table
    /// centroid hydration reads full parts once even on consumers that
    /// open with routing siblings.
    pub async fn load_full(&self, part_id: PartId) -> Result<Arc<ManifestPart>, ManifestLoadError> {
        self.load_with_form(part_id, false).await
    }

    async fn load_with_form(
        &self,
        part_id: PartId,
        prefer_routing: bool,
    ) -> Result<Arc<ManifestPart>, ManifestLoadError> {
        let (full_hash, full_uri, routing) = self
            .parts_index
            .get(&part_id)
            .ok_or(ManifestLoadError::PartNotInList { part_id })?;
        // Routing decode lands in the stripped in-memory shape; both forms
        // are content-addressed, so the disk cache can never mix them.
        let (expected_hash, uri) = match (prefer_routing, routing) {
            (true, Some(routing)) => (&routing.content_hash, &routing.uri),
            _ => (full_hash, full_uri),
        };

        // Disk-cache hit: bytes are verified against `expected_hash`
        // inside `get`, so they're known-good here.
        if let Some(cache) = &self.manifest_disk_cache
            && let Some(bytes) = cache.get(expected_hash).await
        {
            let parsed = decode_part_off_thread(Bytes::from(bytes)).await?;
            return Ok(Arc::new(parsed));
        }

        let (bytes, _) = self
            .storage
            .get(uri)
            .await
            .map_err(ManifestLoadError::Storage)?;
        // Hash verify runs inside the same blocking task as the decode:
        // blake3 over a multi-hundred-MiB part is CPU the polling task
        // must not absorb (it serializes the nominally-concurrent part
        // fan exactly like the inline decode used to).
        let parsed = verify_and_decode_part_off_thread(bytes.clone(), *expected_hash).await?;
        // Populate the cache for next time (best-effort; the hash was
        // verified above, satisfying `put`'s contract).
        if let Some(cache) = &self.manifest_disk_cache {
            cache.put(*expected_hash, &bytes).await;
        }
        Ok(Arc::new(parsed))
    }
}

/// One consumer's hydrated user-table fp32 fine centroids, built once per
/// manifest generation from the FULL manifest parts (the user table's
/// content-addressed store for summary fp32 — the slow-CAS analog). Keyed
/// by `manifest_id`; a membership change rebuilds it on the next rescore.
/// Resident by design: user summaries are per-fragment cells (~100 MB at
/// 100M docs), unlike the hidden table's fine fp32 (spilled section).
pub(crate) struct UserCentroidCache {
    pub(crate) manifest_id: u64,
    /// `(superfile_id, column)` → per-cell `(cell_id, fp32 centroids)`.
    pub(crate) cells: HashMap<(Uuid, String), Vec<(Option<u32>, Arc<Vec<f32>>)>>,
}

impl UserCentroidCache {
    /// One cell's fp32 fine centroids, when the cache carries them.
    pub(crate) fn cell(
        &self,
        superfile_id: Uuid,
        column: &str,
        cell_id: Option<u32>,
    ) -> Option<Arc<Vec<f32>>> {
        self.cells
            .get(&(superfile_id, column.to_owned()))?
            .iter()
            .find(|(id, _)| *id == cell_id)
            .map(|(_, fp32)| Arc::clone(fp32))
    }

    /// Build from fully-loaded parts: every entry's summary cells that
    /// carry resident fp32.
    pub(crate) fn from_parts(manifest_id: u64, parts: &[Arc<ManifestPart>]) -> Self {
        let mut cells: HashMap<(Uuid, String), Vec<(Option<u32>, Arc<Vec<f32>>)>> = HashMap::new();
        for part in parts {
            for entry in &part.superfiles {
                for (column, summary) in &entry.vector_summary {
                    let list = cells
                        .entry((entry.superfile_id, column.clone()))
                        .or_default();
                    for cell in &summary.cells {
                        if cell.clusters.vectors_resident() && cell.clusters.n_cent > 0 {
                            list.push((cell.cell_id, Arc::new(cell.clusters.centroids.clone())));
                        }
                    }
                }
            }
        }
        Self { manifest_id, cells }
    }
}

/// Decode part bytes off the async runtime. Part decode is a CPU wave
/// (multi-MiB Avro payloads carrying centroid summaries and open blobs);
/// running it inline used to serialize every part behind one polling
/// task, so 18 nominally-concurrent part loads decoded one at a time.
/// `spawn_blocking` keeps the runtime free to drive the remaining
/// fetches while decodes run in parallel on the blocking pool.
async fn decode_part_off_thread(bytes: Bytes) -> Result<ManifestPart, ManifestLoadError> {
    match spawn_blocking(move || part::decode(&bytes)).await {
        Ok(result) => Ok(result?),
        Err(join_error) => Err(ManifestLoadError::Parse(part::PartParseError::Avro(
            format!("part decode task failed: {join_error}"),
        ))),
    }
}

/// [`decode_part_off_thread`] preceded by a blake3 content-hash check on
/// the same blocking task, for the storage-GET path where the bytes are
/// not yet verified.
async fn verify_and_decode_part_off_thread(
    bytes: Bytes,
    expected_hash: ContentHash,
) -> Result<ManifestPart, ManifestLoadError> {
    let verify_then_decode = move || {
        let actual_hash = ContentHash::of(&bytes);
        if actual_hash != expected_hash {
            return Err(ManifestLoadError::ContentHashMismatch {
                expected: expected_hash.to_hex(),
                actual: actual_hash.to_hex(),
            });
        }
        part::decode(&bytes).map_err(ManifestLoadError::from)
    };
    match spawn_blocking(verify_then_decode).await {
        Ok(result) => result,
        Err(join_error) => Err(ManifestLoadError::Parse(part::PartParseError::Avro(
            format!("part verify/decode task failed: {join_error}"),
        ))),
    }
}

/// Errors raised by [`ManifestSnapshot::part`] and [`ManifestPartLoader::load`].
///
/// Standalone (not folded into the supertable-level
/// `OpenError`) so the per-part load surface stays narrowly
/// testable in isolation.
#[derive(Debug, thiserror::Error)]
pub enum ManifestLoadError {
    /// Pointer not found in storage.
    #[error("pointer not found in storage")]
    PointerNotFound,
    /// The pointer this handle had been reading has since been deleted, i.e.
    /// the table was dropped and purged. Unlike [`Self::PointerNotFound`]
    /// ("never had one"), the handle must be discarded, not refreshed.
    #[error("manifest pointer was deleted while this handle was open")]
    PointerVanished,
    #[error("already loaded")]
    AlreadyLoaded,
    /// Pointer parse error.
    #[error("pointer parse error: {0}")]
    PointerParse(String),
    /// Caller invoked `ManifestSnapshot::part(...)` on an in-process-only
    /// manifest (no storage attached). The hierarchical manifest
    /// has no on-disk parts to load from.
    #[error("no storage / loader attached to this manifest")]
    NoLoaderAttached,

    #[error("list parse error: {0}")]
    ListParse(#[source] list::ListParseError),
    /// `part_id` isn't in this manifest's list. Either the caller
    /// passed a stale id (pre-refresh) or the manifest list is
    /// missing an entry.
    #[error("part_id not in manifest list: {part_id}")]
    PartNotInList { part_id: PartId },
    /// Storage backend returned an error.
    #[error("storage error during part load: {0}")]
    Storage(#[source] StorageError),
    /// Computed blake3 of the loaded bytes didn't match the
    /// manifest list's recorded `content_hash`. The bad bytes
    /// are **not** auto-refetched — a mismatch indicates
    /// corruption, not a transient race, so it's surfaced as
    /// a caller-visible failure rather than papered over.
    #[error("content-hash mismatch: expected {expected}, got {actual}")]
    ContentHashMismatch { expected: String, actual: String },
    /// Avro / zstd / version-incompat parse failure.
    #[error("part parse failed")]
    Parse(#[from] part::PartParseError),
    /// Slow-state blob hydration failed while the list carries a ref —
    /// missing object, hash mismatch, decode failure, or an entry count
    /// that disagrees with the list. Corruption, not a race: surfaced as
    /// a load failure rather than silently degrading to the part fan (a
    /// quiet fallback here concealed real defects across whole bench
    /// cycles).
    #[error("slow vector-state hydration failed: {0}")]
    SlowStateHydration(String),
}

/// One superfile's metadata + skip-pruning summaries. The bytes that
/// back the superfile live in the superfile store keyed by `uri` —
/// `superfile_id` is for debugging / observability, `uri` is for
/// store routing.
#[derive(Debug, Clone)]
pub struct SuperfileEntry {
    /// Globally unique identifier (UUID v4) for debugging /
    /// observability. Distinct from `uri` so the store routing key
    /// can evolve independently of identity.
    pub superfile_id: Uuid,
    /// Opaque key into the `SuperfileReaderCache`. v1 wraps a UUID; the
    /// trait doesn't care about the internal shape.
    pub uri: SuperfileUri,
    /// Row count.
    pub n_docs: u64,
    /// id-column min and max (the supertable-injected
    /// `Decimal128(38, 0)` id column). Stored as `i128` to
    /// carry the 128-bit Snowflake-shaped values produced by
    /// the supertable's `IdGenerator`. Signed-int comparison
    /// gives time-ordered skip-pruning because the high bit
    /// stays 0 for any plausible current-era timestamp.
    pub id_min: i128,
    pub id_max: i128,
    /// Per-scalar-column aggregate (min/max + null count, exact sum, HLL),
    /// keyed by column name, for skip pruning of SQL filters. An absent
    /// column means "no usable stats" (the pruner keeps the superfile).
    pub scalar_stats: HashMap<String, ScalarStatsAgg>,
    /// Per-FTS-column term-presence bloom + lex range. The bloom
    /// drives exact-term skip; the term-range drives prefix-query
    /// skip via `[prefix, prefix_upper_bound)` overlap. Keyed by
    /// FTS column name. Same per-column [`FtsSummaryAgg`] shape the
    /// list-level aggregate uses; built per superfile via
    /// [`FtsSummaryAgg::from_superfile`].
    pub fts_summary: HashMap<String, FtsSummaryAgg>,
    /// Per-vector-column summary centroid + per-cluster IVF centroids,
    /// driving global cluster selection at query time. Keyed by vector
    /// column name.
    pub vector_summary: HashMap<String, VectorSummary>,
    /// Partition assignment, encoded opaquely per the strategy
    /// (time_range = 8-byte LE u64 bucket index; hash = 4-byte LE
    /// u32 bucket id; column_range = 2-byte LE u16 boundary index).
    /// Empty (decoded as "unpartitioned") when no real partition
    /// strategy is configured; otherwise filled by the writer
    /// from the configured strategy at commit time.
    pub partition_key: Vec<u8>,
    /// Hash partitioning operates per-row, but at commit time we
    /// only have per-superfile summaries. Hash strategy requires
    /// superfiles to be pre-sharded — each builder-shard stamps the
    /// resulting bucket here on ingest. `None` under non-hash
    /// strategies and under the single-bucket Hash default.
    pub partition_hint: Option<u32>,
    /// precomputed superfile layout offsets so the
    /// cold-open path can fire the parquet-footer, vector
    /// subsection, and FTS subsection GETs **in parallel** in a
    /// single round-trip, without first reading the parquet KV
    /// metadata to learn where each subsection lives.
    ///
    /// Populated by the writer at commit time from the
    /// `ParquetParts` returned by `splice_index_blobs` (so
    /// the values are by construction consistent with what the
    /// parquet KV metadata would later say).
    ///
    /// `None` on superfiles produced by older writers that did not
    /// stamp this field; the cold open path falls back to the
    /// 2-RTT shape (parquet tail
    /// then vec/fts in parallel) — see
    /// `DiskCacheStore::reader_with_hints`.
    pub subsection_offsets: Option<SubsectionOffsets>,
    pub(crate) vector_layout: VectorLayout,
    /// The `manifest_id` of the commit that introduced this superfile — its
    /// **birth version**. Stamped in [`ManifestSnapshot::update`] for newly-added
    /// entries (re-derived per OCC attempt, so it always equals the winning
    /// commit's version); carried over unchanged for entries that survive a
    /// commit. The hidden-index drain uses it to track which user commits it
    /// has consumed into cells (see the hidden manifest's `drained_ranges`):
    /// because the manifest-pointer CAS serializes every commit across all
    /// writers/hosts into one gap-free version sequence, this is the only
    /// total order that's safe to watermark on. `0` on entries from before
    /// the field existed (treated as the genesis version).
    pub birth_version: u64,
}

/// superfile layout offsets cached on the manifest.
///
/// Knowing these up-front lets the cold-open path issue every
/// subsection GET in parallel against the same superfile object,
/// turning the canonical 2-RTT cold open (parquet tail → vec+fts
/// in parallel) into a single round-trip.
///
/// All offsets are absolute byte positions within the superfile
/// blob (matching `inf.vec.offset` / `inf.fts.offset` parquet KV
/// values), and `total_size` matches what an S3 `HEAD` would
/// return.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubsectionOffsets {
    /// Total byte count of the superfile blob. Lets the cold-open
    /// path skip the upfront `HEAD` round-trip too — the same
    /// information the suffix-range tail would otherwise return,
    /// but available without any I/O.
    pub total_size: u64,
    /// Absolute `(offset, length)` of the vector subsection. `None`
    /// when the superfile carries no vector subsection.
    pub vec: Option<(u64, u64)>,
    /// Absolute `(offset, length)` of the FTS subsection. `None`
    /// when the superfile carries no FTS subsection.
    pub fts: Option<(u64, u64)>,
    /// Absolute ranges that fully cover vector open-time metadata.
    /// The hinted cache path prefetches these in the first network
    /// batch so `VectorReader::open_lazy` can resolve header,
    /// directory, subheaders, and codec metadata from the overlay.
    pub vec_open_ranges: Vec<(u64, u64)>,
    /// Absolute ranges that fully cover FTS open-time metadata:
    /// header+dictionary and doc-length tables. Query-time postings
    /// stay lazy.
    pub fts_open_ranges: Vec<(u64, u64)>,
    /// the actual bytes covering the superfile's
    /// open-time batch (parquet footer tail + the
    /// `vec_open_ranges` + the `fts_open_ranges`), carried inline
    /// in the manifest part.
    ///
    /// When non-empty, the cold-fetch path installs these directly
    /// into the reader's prefetch overlay and issues **zero**
    /// open-time GETs against the superfile object — the bytes
    /// already arrived in the single part GET that `cold_open`
    /// performs. The genuine first-touch per-superfile cost then
    /// collapses from 2 RTT-batches (open metadata + cluster
    /// postings) to 1 (postings only).
    ///
    /// Each tuple is `(absolute_offset, bytes)`. Empty on superfiles
    /// produced by older writers that did not capture it, or when
    /// blob capture is disabled
    /// — the path then falls back to fetching `vec_open_ranges` /
    /// `fts_open_ranges` over the wire.
    pub open_blob: Vec<(u64, Vec<u8>)>,
}

/// Opaque store key — wraps a UUID v4. The superfile store treats
/// this as a hash-eq token and doesn't peek inside. An
/// object-store-backed variant could swap to a path-shaped URI
/// without changing any caller, since the trait shape stays the
/// same.
#[derive(Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct SuperfileUri(pub Uuid);

impl SuperfileUri {
    /// Generate a fresh URI. Called by the writer at commit time
    /// when assigning a key for a new superfile's bytes.
    pub fn new_v4() -> Self {
        Self(Uuid::new_v4())
    }

    /// Object-store / LocalFS path for committed superfile bytes.
    /// `.sf.parquet` double suffix — on disk this is still valid
    /// Parquet (row groups + optional embedded FTS/vector blobs +
    /// footer), while the `.sf` marker flags it as a Superfile
    /// superfile without making the file look non-standard.
    pub fn storage_path(self) -> String {
        format!("{SUPERFILE_DATA_DIR}/seg-{}.sf.parquet", self.0)
    }

    /// Disk-cache filename for a promoted superfile.
    pub fn cache_filename(self) -> String {
        format!("seg-{}.sf.parquet", self.0)
    }

    /// Disk-cache tempfile while a cold fetch is in flight.
    pub fn cache_tmp_filename(self) -> String {
        format!("seg-{}.sf.parquet.tmp", self.0)
    }

    /// Inverse of [`Self::cache_filename`]: recover the URI from an on-disk
    /// cache file name. The disk cache uses this to rebuild its in-memory index
    /// from files a prior run left under `cache_root`, so a restart / second
    /// handle reuses the NVMe bytes instead of cold-fetching from object
    /// storage. Returns `None` for anything that isn't exactly
    /// `seg-<uuid>.sf.parquet` — notably the `.tmp` in-flight files, whose
    /// longer `.sf.parquet.tmp` suffix must be ignored (incomplete writes).
    pub fn from_cache_filename(name: &str) -> Option<Self> {
        let body = name.strip_prefix("seg-")?.strip_suffix(".sf.parquet")?;
        Uuid::parse_str(body).ok().map(SuperfileUri)
    }
}

/// Merge min/max arrays by comparing values and keeping the actual min and max.
///
/// Takes existing (min, max) and other (min, max) arrays and returns the
/// merged (min, max) where min is the smaller value and max is the larger.
/// Both arrays are assumed to be length-1 and of the same type.
pub(crate) fn merge_min_max_arrays(
    existing_min: &ArrayRef,
    other_min: &ArrayRef,
    existing_max: &ArrayRef,
    other_max: &ArrayRef,
) -> Option<(ArrayRef, ArrayRef)> {
    // Merge two optional bounds into the surviving one — smaller for a min,
    // larger for a max. A `None` (the column is all-null on that side)
    // yields to a present value; both `None` stays `None`, so an all-null
    // column stays all-null through the fold, while a manifest part with any
    // populated superfile keeps its real bound.
    #[inline]
    fn merge_opt<T: PartialOrd>(a: Option<T>, b: Option<T>, keep_min: bool) -> Option<T> {
        match (a, b) {
            (Some(a), Some(b)) => Some(if keep_min == (a <= b) { a } else { b }),
            (Some(v), None) | (None, Some(v)) => Some(v),
            (None, None) => None,
        }
    }

    macro_rules! prim_merge {
        ($array_ty:ty) => {{
            let exn = existing_min.as_any().downcast_ref::<$array_ty>()?;
            let otn = other_min.as_any().downcast_ref::<$array_ty>()?;
            let exx = existing_max.as_any().downcast_ref::<$array_ty>()?;
            let otx = other_max.as_any().downcast_ref::<$array_ty>()?;
            let at = |a: &$array_ty| (!a.is_null(0)).then(|| a.value(0));
            Some((
                Arc::new(<$array_ty>::from(vec![merge_opt(at(exn), at(otn), true)])) as ArrayRef,
                Arc::new(<$array_ty>::from(vec![merge_opt(at(exx), at(otx), false)])) as ArrayRef,
            ))
        }};
    }

    // Same fold, re-attaching the column's timezone (the constructor drops
    // it) so the merged bound keeps the exact type its inputs carried.
    macro_rules! ts_merge {
        ($array_ty:ty, $tz:expr) => {{
            let exn = existing_min.as_any().downcast_ref::<$array_ty>()?;
            let otn = other_min.as_any().downcast_ref::<$array_ty>()?;
            let exx = existing_max.as_any().downcast_ref::<$array_ty>()?;
            let otx = other_max.as_any().downcast_ref::<$array_ty>()?;
            let at = |a: &$array_ty| (!a.is_null(0)).then(|| a.value(0));
            Some((
                Arc::new(
                    <$array_ty>::from(vec![merge_opt(at(exn), at(otn), true)])
                        .with_timezone_opt($tz.clone()),
                ) as ArrayRef,
                Arc::new(
                    <$array_ty>::from(vec![merge_opt(at(exx), at(otx), false)])
                        .with_timezone_opt($tz.clone()),
                ) as ArrayRef,
            ))
        }};
    }

    match existing_min.data_type() {
        DataType::UInt8 => prim_merge!(UInt8Array),
        DataType::UInt16 => prim_merge!(UInt16Array),
        DataType::UInt32 => prim_merge!(UInt32Array),
        DataType::UInt64 => prim_merge!(UInt64Array),
        DataType::Int8 => prim_merge!(Int8Array),
        DataType::Int16 => prim_merge!(Int16Array),
        DataType::Int32 => prim_merge!(Int32Array),
        DataType::Int64 => prim_merge!(Int64Array),
        DataType::Float32 => prim_merge!(Float32Array),
        DataType::Float64 => prim_merge!(Float64Array),
        // `false < true`, so min folds like AND and max like OR.
        DataType::Boolean => prim_merge!(BooleanArray),
        DataType::Utf8 => {
            let exn = existing_min.as_any().downcast_ref::<StringArray>()?;
            let otn = other_min.as_any().downcast_ref::<StringArray>()?;
            let exx = existing_max.as_any().downcast_ref::<StringArray>()?;
            let otx = other_max.as_any().downcast_ref::<StringArray>()?;
            let min = merge_opt(
                (!exn.is_null(0)).then(|| exn.value(0)),
                (!otn.is_null(0)).then(|| otn.value(0)),
                true,
            );
            let max = merge_opt(
                (!exx.is_null(0)).then(|| exx.value(0)),
                (!otx.is_null(0)).then(|| otx.value(0)),
                false,
            );
            Some((
                Arc::new(StringArray::from(vec![min])),
                Arc::new(StringArray::from(vec![max])),
            ))
        }
        DataType::LargeUtf8 => {
            let exn = existing_min.as_any().downcast_ref::<LargeStringArray>()?;
            let otn = other_min.as_any().downcast_ref::<LargeStringArray>()?;
            let exx = existing_max.as_any().downcast_ref::<LargeStringArray>()?;
            let otx = other_max.as_any().downcast_ref::<LargeStringArray>()?;
            let min = merge_opt(
                (!exn.is_null(0)).then(|| exn.value(0)),
                (!otn.is_null(0)).then(|| otn.value(0)),
                true,
            );
            let max = merge_opt(
                (!exx.is_null(0)).then(|| exx.value(0)),
                (!otx.is_null(0)).then(|| otx.value(0)),
                false,
            );
            Some((
                Arc::new(LargeStringArray::from(vec![min])),
                Arc::new(LargeStringArray::from(vec![max])),
            ))
        }
        DataType::Decimal128(precision, scale) => {
            let exn = existing_min.as_any().downcast_ref::<Decimal128Array>()?;
            let otn = other_min.as_any().downcast_ref::<Decimal128Array>()?;
            let exx = existing_max.as_any().downcast_ref::<Decimal128Array>()?;
            let otx = other_max.as_any().downcast_ref::<Decimal128Array>()?;
            let at = |a: &Decimal128Array| (!a.is_null(0)).then(|| a.value(0));
            let min = merge_opt(at(exn), at(otn), true);
            let max = merge_opt(at(exx), at(otx), false);
            Some((
                Arc::new(
                    Decimal128Array::from(vec![min])
                        .with_precision_and_scale(*precision, *scale)
                        .ok()?,
                ),
                Arc::new(
                    Decimal128Array::from(vec![max])
                        .with_precision_and_scale(*precision, *scale)
                        .ok()?,
                ),
            ))
        }

        // Mirror `column_min_max`'s temporal set; without these arms a
        // multi-superfile (or compacted) temporal column errors the merge and
        // drops its stat, silently regressing the fold and range prune.
        DataType::Date32 => prim_merge!(Date32Array),
        DataType::Date64 => prim_merge!(Date64Array),
        DataType::Time32(TimeUnit::Second) => prim_merge!(Time32SecondArray),
        DataType::Time32(TimeUnit::Millisecond) => prim_merge!(Time32MillisecondArray),
        DataType::Time64(TimeUnit::Microsecond) => prim_merge!(Time64MicrosecondArray),
        DataType::Time64(TimeUnit::Nanosecond) => prim_merge!(Time64NanosecondArray),
        DataType::Timestamp(TimeUnit::Second, tz) => ts_merge!(TimestampSecondArray, tz),
        DataType::Timestamp(TimeUnit::Millisecond, tz) => ts_merge!(TimestampMillisecondArray, tz),
        DataType::Timestamp(TimeUnit::Microsecond, tz) => ts_merge!(TimestampMicrosecondArray, tz),
        DataType::Timestamp(TimeUnit::Nanosecond, tz) => ts_merge!(TimestampNanosecondArray, tz),
        _ => None,
    }
}

/// Compute (min, max) for one Arrow array as length-1 `ArrayRef`s.
///
/// Returns `None` only for unsupported types. An all-null input of a
/// supported type yields length-1 *null* min/max arrays (not `None`), so
/// its null count is still recorded and `IS [NOT] NULL` can prune on it.
/// Supported set: integer (signed + unsigned, all widths), float
/// (f32, f64), boolean, Utf8, LargeUtf8, Decimal128, and temporal
/// (Date32/64, Time32/64, Timestamp). The supertable schema
/// rejects vector columns up at the SupertableOptions layer, so
/// `FixedSizeList<Float32>` won't appear here in practice.
/// Exact column sum as a length-1 array typed to match SQL `SUM`'s
/// result for the column (signed → `Int64`, unsigned → `UInt64`,
/// floats → `Float64`). `None` for non-summable types (utf8, bool,
/// decimal) or when the exact total overflows the result type —
/// consumers treat missing as "no statistics".
pub(crate) fn column_sum(col: &ArrayRef) -> Option<ArrayRef> {
    macro_rules! signed {
        ($array_ty:ty) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            let total: i128 = a.iter().flatten().map(i128::from).sum();
            let v = i64::try_from(total).ok()?;
            Some(Arc::new(Int64Array::from(vec![v])) as ArrayRef)
        }};
    }
    macro_rules! unsigned {
        ($array_ty:ty) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            let total: u128 = a.iter().flatten().map(u128::from).sum();
            let v = u64::try_from(total).ok()?;
            Some(Arc::new(UInt64Array::from(vec![v])) as ArrayRef)
        }};
    }
    macro_rules! float {
        ($array_ty:ty) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            let total: f64 = a.iter().flatten().map(f64::from).sum();
            Some(Arc::new(Float64Array::from(vec![total])) as ArrayRef)
        }};
    }

    match col.data_type() {
        DataType::Int8 => signed!(Int8Array),
        DataType::Int16 => signed!(Int16Array),
        DataType::Int32 => signed!(Int32Array),
        DataType::Int64 => signed!(Int64Array),
        DataType::UInt8 => unsigned!(UInt8Array),
        DataType::UInt16 => unsigned!(UInt16Array),
        DataType::UInt32 => unsigned!(UInt32Array),
        DataType::UInt64 => unsigned!(UInt64Array),
        DataType::Float32 => float!(Float32Array),
        DataType::Float64 => float!(Float64Array),
        _ => None,
    }
}

/// Add two length-1 sum arrays of the same type (see [`column_sum`]).
/// `None` on type mismatch or `Int64`/`UInt64` overflow. Shared with
/// the SQL provider's cross-segment statistics fold.
pub(crate) fn add_sum_arrays(a: &ArrayRef, b: &ArrayRef) -> Option<ArrayRef> {
    match (a.data_type(), b.data_type()) {
        (DataType::Int64, DataType::Int64) => {
            let x = a.as_any().downcast_ref::<Int64Array>()?.value(0);
            let y = b.as_any().downcast_ref::<Int64Array>()?.value(0);
            Some(Arc::new(Int64Array::from(vec![x.checked_add(y)?])) as ArrayRef)
        }
        (DataType::UInt64, DataType::UInt64) => {
            let x = a.as_any().downcast_ref::<UInt64Array>()?.value(0);
            let y = b.as_any().downcast_ref::<UInt64Array>()?.value(0);
            Some(Arc::new(UInt64Array::from(vec![x.checked_add(y)?])) as ArrayRef)
        }
        (DataType::Float64, DataType::Float64) => {
            let x = a.as_any().downcast_ref::<Float64Array>()?.value(0);
            let y = b.as_any().downcast_ref::<Float64Array>()?.value(0);
            Some(Arc::new(Float64Array::from(vec![x + y])) as ArrayRef)
        }
        _ => None,
    }
}

/// HyperLogLog distinct sketch over a column's non-null values.
/// `None` for types the sketch doesn't cover. Values hash by their
/// canonical byte representation (little-endian for numerics, raw
/// bytes for strings, IEEE bits for floats).
pub(crate) fn column_hll(col: &ArrayRef) -> Option<hll::HllSketch> {
    let mut sketch = hll::HllSketch::new();
    macro_rules! ints {
        ($array_ty:ty) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            for v in a.iter().flatten() {
                sketch.insert_hash(xxh3_64(&v.to_le_bytes()));
            }
        }};
    }
    match col.data_type() {
        DataType::Int8 => ints!(Int8Array),
        DataType::Int16 => ints!(Int16Array),
        DataType::Int32 => ints!(Int32Array),
        DataType::Int64 => ints!(Int64Array),
        DataType::UInt8 => ints!(UInt8Array),
        DataType::UInt16 => ints!(UInt16Array),
        DataType::UInt32 => ints!(UInt32Array),
        DataType::UInt64 => ints!(UInt64Array),
        DataType::Float32 => {
            let a = col.as_any().downcast_ref::<Float32Array>()?;
            for v in a.iter().flatten() {
                sketch.insert_hash(xxh3_64(&v.to_bits().to_le_bytes()));
            }
        }
        DataType::Float64 => {
            let a = col.as_any().downcast_ref::<Float64Array>()?;
            for v in a.iter().flatten() {
                sketch.insert_hash(xxh3_64(&v.to_bits().to_le_bytes()));
            }
        }
        DataType::Utf8 => {
            let a = col.as_any().downcast_ref::<StringArray>()?;
            for v in a.iter().flatten() {
                sketch.insert_hash(xxh3_64(v.as_bytes()));
            }
        }
        DataType::LargeUtf8 => {
            let a = col.as_any().downcast_ref::<LargeStringArray>()?;
            for v in a.iter().flatten() {
                sketch.insert_hash(xxh3_64(v.as_bytes()));
            }
        }
        _ => return None,
    }
    Some(sketch)
}

pub(crate) fn column_min_max(col: &ArrayRef) -> Option<(ArrayRef, ArrayRef)> {
    // An all-null column yields *null* min/max (not no-stat), so its null
    // count is still recorded for `IS [NOT] NULL` pruning — hence the
    // `Option` min/max are kept rather than `?`-unwrapped away.
    macro_rules! prim {
        ($array_ty:ty) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            let mn_arr: ArrayRef = Arc::new(<$array_ty>::from(vec![agg::min(a)]));
            let mx_arr: ArrayRef = Arc::new(<$array_ty>::from(vec![agg::max(a)]));
            Some((mn_arr, mx_arr))
        }};
    }

    // Timestamps are the same primitive fold, but the `from(vec![..])`
    // constructor builds a zone-less array; re-attach the column's zone so
    // the bound keeps its exact type (a naive-vs-zoned mismatch would fail
    // the cross-superfile merge and stat reconstruction).
    macro_rules! ts {
        ($array_ty:ty, $tz:expr) => {{
            let a = col.as_any().downcast_ref::<$array_ty>()?;
            let mn_arr: ArrayRef =
                Arc::new(<$array_ty>::from(vec![agg::min(a)]).with_timezone_opt($tz.clone()));
            let mx_arr: ArrayRef =
                Arc::new(<$array_ty>::from(vec![agg::max(a)]).with_timezone_opt($tz.clone()));
            Some((mn_arr, mx_arr))
        }};
    }

    match col.data_type() {
        DataType::UInt8 => prim!(UInt8Array),
        DataType::UInt16 => prim!(UInt16Array),
        DataType::UInt32 => prim!(UInt32Array),
        DataType::UInt64 => prim!(UInt64Array),
        DataType::Int8 => prim!(Int8Array),
        DataType::Int16 => prim!(Int16Array),
        DataType::Int32 => prim!(Int32Array),
        DataType::Int64 => prim!(Int64Array),
        DataType::Float32 => prim!(Float32Array),
        DataType::Float64 => prim!(Float64Array),
        DataType::Boolean => {
            let a = col.as_any().downcast_ref::<BooleanArray>()?;
            Some((
                Arc::new(BooleanArray::from(vec![agg::min_boolean(a)])),
                Arc::new(BooleanArray::from(vec![agg::max_boolean(a)])),
            ))
        }
        DataType::Utf8 => {
            let a = col.as_any().downcast_ref::<StringArray>()?;
            Some((
                Arc::new(StringArray::from(vec![agg::min_string(a)])),
                Arc::new(StringArray::from(vec![agg::max_string(a)])),
            ))
        }
        DataType::LargeUtf8 => {
            let a = col.as_any().downcast_ref::<LargeStringArray>()?;
            Some((
                Arc::new(LargeStringArray::from(vec![agg::min_string(a)])),
                Arc::new(LargeStringArray::from(vec![agg::max_string(a)])),
            ))
        }
        DataType::Decimal128(precision, scale) => {
            let a = col.as_any().downcast_ref::<Decimal128Array>()?;
            Some((
                Arc::new(
                    Decimal128Array::from(vec![agg::min(a)])
                        .with_precision_and_scale(*precision, *scale)
                        .ok()?,
                ),
                Arc::new(
                    Decimal128Array::from(vec![agg::max(a)])
                        .with_precision_and_scale(*precision, *scale)
                        .ok()?,
                ),
            ))
        }

        // Temporal columns are numeric-backed and orderable, so min/max fold
        // (DataFusion's aggregate fast-path) and prune the same as integers.
        DataType::Date32 => prim!(Date32Array),
        DataType::Date64 => prim!(Date64Array),
        DataType::Time32(TimeUnit::Second) => prim!(Time32SecondArray),
        DataType::Time32(TimeUnit::Millisecond) => prim!(Time32MillisecondArray),
        DataType::Time64(TimeUnit::Microsecond) => prim!(Time64MicrosecondArray),
        DataType::Time64(TimeUnit::Nanosecond) => prim!(Time64NanosecondArray),
        DataType::Timestamp(TimeUnit::Second, tz) => ts!(TimestampSecondArray, tz),
        DataType::Timestamp(TimeUnit::Millisecond, tz) => ts!(TimestampMillisecondArray, tz),
        DataType::Timestamp(TimeUnit::Microsecond, tz) => ts!(TimestampMicrosecondArray, tz),
        DataType::Timestamp(TimeUnit::Nanosecond, tz) => ts!(TimestampNanosecondArray, tz),
        _ => None,
    }
}

/// Per-vector-column summary: the summary centroid plus the per-cluster
/// IVF centroids. Already produced by the superfile vector builder
/// (per-column, inside the vector blob's outer header KV metadata); the
/// writer copies them into the manifest at commit time. The per-cluster
/// centroids drive global cluster selection at query time.
#[derive(Debug, Clone)]
pub struct VectorSummary {
    /// Cluster centroid; length matches the vector column's `dim`
    /// declared in `SupertableOptions::vector_columns`.
    pub centroid: Vec<f32>,
    /// Fine IVF centroids grouped by their owning global cell. Packed-file
    /// flat cluster ordinals are derived from prefix sums over this list.
    pub cells: Vec<CellVectorSummary>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CellVectorSummary {
    /// `Some(cell)` for MultiCell files; `None` for an unscoped legacy IVF.
    pub cell_id: Option<u32>,
    pub clusters: ClusterCentroids,
}

/// Bits per `u64` word in a packed centroid sign code.
pub(crate) const ADMIT_CODE_WORD_BITS: usize = 64;

/// Fraction of ranked cells the 1-bit admit prefilter keeps for exact
/// fp32 rescoring — shared by the query-side cell window and the
/// write-side assignment shortlist. 20% keeps the same coverage class as
/// the recall-validated 48-of-256 query window while scaling with the
/// population.
pub(crate) const RABITQ_ADMIT_CELL_SHORTLIST_FRACTION: f64 = 0.20;

/// Minimum meaningful prefilter window, from the recall-validated
/// 48-of-256 measurement: when the 20% slice comes out narrower than
/// this, the population is small enough that the exact scan is cheap and
/// the 1-bit estimate has nothing to buy — the query window floors here
/// (scoring everything below ~240 cells) and the write-side assignment
/// shortlist disengages entirely (exact scan).
pub(crate) const RABITQ_ADMIT_CELL_SHORTLIST_MIN: usize = 48;

/// Shared per-column state for the 1-bit admit prefilter: the column's
/// rotation, sign quantizer, and the Hamming→cosine lookup table
/// (`cos(π·h/dim)` — the standard sign-sketch angle estimator). Built
/// **once per query or per assignment batch** and shared across every
/// encoded vector — building rotation state per vector re-derives the
/// rotation thousands of times (measured ~51 ms of admit at 1M pre-drain
/// vs ~1 ms with shared state; the same waste per row on the write side).
#[derive(Debug, Clone)]
pub(crate) struct RabitqAdmitContext {
    rot_seed: u64,
    dim: usize,
    rotation: Arc<RandomRotation>,
    quant: BitQuantizer,
    /// `cos(π·h/dim)` for h in `0..=dim`.
    cos_table: Arc<Vec<f32>>,
}

impl RabitqAdmitContext {
    pub(crate) fn new(dim: usize, rot_seed: u64) -> Self {
        Self {
            rot_seed,
            dim,
            rotation: Arc::new(RandomRotation::new(dim, rot_seed)),
            quant: BitQuantizer::new(dim),
            cos_table: Arc::new(
                (0..=dim)
                    .map(|h| (PI * h as f32 / dim as f32).cos())
                    .collect(),
            ),
        }
    }

    /// Encode one vector against this context: rotate, sign-pack, and
    /// carry the norms the metric transforms need. Cheap per call (the
    /// rotation and cosine table are shared by `Arc`).
    pub(crate) fn encode(&self, vector: &[f32]) -> RabitqAdmitQuery {
        debug_assert_eq!(vector.len(), self.dim);
        let mut rotated = vec![0.0f32; self.dim];
        self.rotation.apply(vector, &mut rotated);
        let mut code = vec![0u8; self.quant.code_bytes()];
        self.quant.encode_rotated_into(&rotated, &mut code);
        let q_l2sq = dot(vector, vector);
        RabitqAdmitQuery {
            rot_seed: self.rot_seed,
            rotation: Arc::clone(&self.rotation),
            quant: self.quant.clone(),
            q_words: pack_code_bytes_to_words(&code),
            q_norm: q_l2sq.sqrt(),
            q_l2sq,
            cos_table: Arc::clone(&self.cos_table),
        }
    }
}

/// One encoded vector's 1-bit admit state: the packed sign code plus the
/// shared column context (rotation / quantizer / cosine table by `Arc`).
#[derive(Debug)]
pub(crate) struct RabitqAdmitQuery {
    rot_seed: u64,
    rotation: Arc<RandomRotation>,
    quant: BitQuantizer,
    /// Sign code packed into u64 words (zero-padded past `dim`).
    q_words: Vec<u64>,
    /// `‖q‖` and `‖q‖²` for the metric transforms.
    q_norm: f32,
    q_l2sq: f32,
    /// `cos(π·h/dim)` for h in `0..=dim`.
    cos_table: Arc<Vec<f32>>,
}

impl RabitqAdmitQuery {
    pub(crate) fn new(dim: usize, rot_seed: u64, query: &[f32]) -> Self {
        RabitqAdmitContext::new(dim, rot_seed).encode(query)
    }
}

/// One shared rotation + sign quantizer per vector column, for
/// hydration-time slab work over every summary instance.
fn admit_encoders(
    vector_columns: &[VectorConfig],
) -> HashMap<&str, (RandomRotation, BitQuantizer, u64)> {
    vector_columns
        .iter()
        .map(|vc| {
            (
                vc.column.as_str(),
                (
                    RandomRotation::new(vc.dim, vc.rot_seed),
                    BitQuantizer::new(vc.dim),
                    vc.rot_seed,
                ),
            )
        })
        .collect()
}

/// Read-only-consumer memory mode: for every uniquely-owned entry, build
/// each summary cell's 1-bit admit slab from its resident fp32 centroids
/// and then drop the fp32 vectors. One rotation + quantizer pair per
/// column, shared across all entries. Entries whose `Arc` is shared (a
/// previous snapshot or a loaded manifest part also references them) are
/// skipped — they were either stripped by the earlier load or belong to
/// maintenance state that needs fp32.
fn strip_summary_centroids(
    superfiles: &mut [Arc<SuperfileEntry>],
    vector_columns: &[VectorConfig],
) {
    let encoders = admit_encoders(vector_columns);
    if encoders.is_empty() {
        return;
    }
    for entry in superfiles.iter_mut() {
        let Some(entry) = Arc::get_mut(entry) else {
            continue;
        };
        for (column, summary) in entry.vector_summary.iter_mut() {
            let Some((rotation, quant, rot_seed)) = encoders.get(column.as_str()) else {
                continue;
            };
            for cell in &mut summary.cells {
                if cell.clusters.dim as usize != quant.dim {
                    continue;
                }
                cell.clusters
                    .strip_centroids_after_slab(rotation, quant, *rot_seed);
            }
        }
    }
}

/// Pre-build every summary cell's 1-bit admit slab at hydration, in
/// parallel on the table's reader pool. The slab is otherwise built
/// lazily on first scan, which lands the whole encode on the first
/// (cold) query — measured +1.4 s on a 100M-doc cold search (~105K fine
/// centroids, one rotation+sign-pack each, single-threaded). Open
/// absorbs the same work in a rayon pass instead. Idempotent: stripped
/// or already-warm instances are `OnceLock` no-ops.
fn prewarm_summary_admit_slabs(
    superfiles: &[Arc<SuperfileEntry>],
    vector_columns: &[VectorConfig],
    pool: &ThreadPool,
) {
    let encoders = admit_encoders(vector_columns);
    if encoders.is_empty() {
        return;
    }
    pool.install(|| {
        superfiles.par_iter().for_each(|entry| {
            for (column, summary) in &entry.vector_summary {
                let Some((rotation, quant, rot_seed)) = encoders.get(column.as_str()) else {
                    continue;
                };
                for cell in &summary.cells {
                    if cell.clusters.dim as usize != quant.dim || !cell.clusters.vectors_resident()
                    {
                        continue;
                    }
                    cell.clusters
                        .prewarm_admit_codes(rotation, quant, *rot_seed);
                }
            }
        });
    });
}

/// Pack a byte sign code into little-endian u64 words, zero-padding the
/// tail. Zero pad bits match on both sides of an XOR, so they never
/// contribute to the Hamming distance.
fn pack_code_bytes_to_words(code: &[u8]) -> Vec<u64> {
    let bytes_per_word = ADMIT_CODE_WORD_BITS / 8;
    code.chunks(bytes_per_word)
        .map(|chunk| {
            let mut word = [0u8; 8];
            word[..chunk.len()].copy_from_slice(chunk);
            u64::from_le_bytes(word)
        })
        .collect()
}

/// XOR + popcount Hamming distance over packed sign codes. Safe Rust —
/// `count_ones` lowers to the POPCNT instruction on x86-64.
#[inline]
fn hamming_words(a: &[u64], b: &[u64]) -> u32 {
    debug_assert_eq!(a.len(), b.len());
    a.iter().zip(b).map(|(x, y)| (x ^ y).count_ones()).sum()
}

/// Packed 1-bit sign codes for every centroid in a [`ClusterCentroids`],
/// plus per-centroid norms for the metric transforms. Computed wherever
/// the centroids are computed (commit staging, drain packs) and persisted
/// beside the fp32 in the summary wire blob, so hydration decodes the
/// slab instead of re-deriving one rotation per fine centroid; legacy
/// fp32-only blobs still derive it at hydration.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct RabitqAdmitCodes {
    pub(crate) rot_seed: u64,
    pub(crate) words_per_code: usize,
    /// `n_cent * words_per_code`, cluster-major.
    pub(crate) codes: Vec<u64>,
    /// `‖centroid[c]‖` per cluster.
    pub(crate) norms: Vec<f32>,
}

/// Per-cluster IVF centroids for one vector column, stored canonically as fp32
/// cluster-major (`n_cent * dim`) plus a derived block-transposed cache for hot
/// routing. Carried in the manifest so a query can rank every superfile's
/// clusters globally — without opening the superfile — and probe only the
/// globally-closest clusters. The 1-bit shortlist + rerank still run on the
/// superfile's on-disk compressed vectors; these drive cluster *selection* only.
///
/// Centroids are `n_cent * dim` (~1% of index bytes), so they are kept
/// fp32, no per-query dequant. (Rerank rows, the bulk of the index, stay
/// Sq8+ε; representation follows cardinality.) Every scan-shaped read —
/// [`Self::score_clusters_into`], [`Self::rank_cells`],
/// [`Self::nearest_cell`], boundary assignment — goes through the blocked
/// SIMD kernels in `superfile::vector::distance` over [`Self::transposed`],
/// the lazily-built block-transposed cache. [`Self::score_one`] stays a
/// zero-copy single-centroid [`distance`] probe.
///
/// The 1-bit admit prefilter ([`Self::estimate_min_admit_score`]) ranks
/// whole instances cheaply from packed sign codes; exact fp32 scoring then
/// runs only on the shortlisted cells, so final routing scores are always
/// exact.
#[derive(Debug, Default)]
pub struct ClusterCentroids {
    pub n_cent: u32,
    pub dim: u32,
    /// Per-cluster centroid, fp32, cluster-major (`n_cent * dim`).
    pub centroids: Vec<f32>,
    /// Per-cluster indexed doc count; length `n_cent`. Count-0 clusters
    /// are skipped by the selector.
    pub counts: Vec<u32>,
    /// Lazily-built block-transposed centroid cache feeding the blocked
    /// SIMD scan kernels in `superfile::vector::distance`. Built once per
    /// instance on first scan; reset by `Clone` (a clone may mutate
    /// `centroids`, so it re-derives its own cache on first use).
    transposed: OnceLock<Vec<f32>>,
    /// Lazily-built packed sign codes for the 1-bit admit prefilter.
    admit_codes: OnceLock<RabitqAdmitCodes>,
}

impl Clone for ClusterCentroids {
    fn clone(&self) -> Self {
        // Preserve warm scan caches when present. Dropping the transposed
        // cache on every clone forced the query path (which historically
        // cloned the global grid / VectorCell strategy each search) to
        // rebuild the scalar block-transpose — milliseconds at dim=1024 —
        // before the SIMD scan could run. Callers that mutate `centroids`
        // after cloning must [`Self::invalidate_transposed`].
        let transposed = OnceLock::new();
        if let Some(cache) = self.transposed.get() {
            let _ = transposed.set(cache.clone());
        }
        let admit_codes = OnceLock::new();
        if let Some(cache) = self.admit_codes.get() {
            let _ = admit_codes.set(cache.clone());
        }
        Self {
            n_cent: self.n_cent,
            dim: self.dim,
            centroids: self.centroids.clone(),
            counts: self.counts.clone(),
            transposed,
            admit_codes,
        }
    }
}

impl PartialEq for ClusterCentroids {
    fn eq(&self, other: &Self) -> bool {
        self.n_cent == other.n_cent
            && self.dim == other.dim
            && self.centroids == other.centroids
            && self.counts == other.counts
    }
}

impl Eq for ClusterCentroids {}

impl ClusterCentroids {
    /// The "no cluster centroids" value — a superfile without a vector
    /// index for the column.
    pub fn empty() -> Self {
        Self::default()
    }

    pub fn is_empty(&self) -> bool {
        self.n_cent == 0
    }

    /// Zero-copy fp32 slice of cluster `c`'s centroid (length `dim`).
    pub fn centroid(&self, c: usize) -> &[f32] {
        // Same residency guard as `transposed()` / `build_admit_codes()`:
        // on a stripped summary this would otherwise fail as an
        // out-of-bounds slice, hiding the actual invariant breach.
        assert!(
            self.vectors_resident(),
            "centroid() on a stripped summary — fp32 centroids are not resident"
        );
        let d = self.dim as usize;
        let base = c * d;
        &self.centroids[base..base + d]
    }

    /// Cluster-major fp32 centroids (`n_cent * dim`) — a clone of the
    /// stored buffer.
    pub fn to_fp32(&self) -> Vec<f32> {
        self.centroids.clone()
    }

    /// Store fp32 cluster centroids (`centroids` is cluster-major,
    /// `n_cent * dim` floats) directly. Non-finite components are clamped
    /// to zero so routing distance stays well-defined.
    pub fn from_fp32(n_cent: u32, dim: u32, centroids: &[f32], counts: Vec<u32>) -> Self {
        let stored: Vec<f32> = centroids
            .iter()
            .map(|v| if v.is_finite() { *v } else { 0.0 })
            .collect();
        Self::from_decoded(n_cent, dim, stored, counts)
    }

    /// Wrap already-validated fp32 centroids (wire decode path — bytes were
    /// written by [`Self::from_fp32`], so the clamp already happened).
    pub(crate) fn from_decoded(
        n_cent: u32,
        dim: u32,
        centroids: Vec<f32>,
        counts: Vec<u32>,
    ) -> Self {
        // A grid must carry exactly one count and one centroid per cell. A
        // mismatch (e.g. counts left at the old length after a split) silently
        // passes in memory but truncates the wire encoding — counts and centroids
        // are serialized adjacently — so the grid fails to reopen from storage.
        // Assert here to catch the malformed construction at its source.
        debug_assert_eq!(
            counts.len(),
            n_cent as usize,
            "cluster grid counts ({}) must match n_cent ({n_cent})",
            counts.len()
        );
        debug_assert_eq!(
            centroids.len(),
            n_cent as usize * dim as usize,
            "cluster grid centroids ({}) must be n_cent*dim ({}*{})",
            centroids.len(),
            n_cent,
            dim
        );
        Self {
            n_cent,
            dim,
            centroids,
            counts,
            transposed: OnceLock::new(),
            admit_codes: OnceLock::new(),
        }
    }

    /// The block-transposed centroid cache feeding the blocked SIMD scan
    /// kernels — built once per instance on first scan, shared by every
    /// scan-shaped method below and by boundary assignment. This is the ONLY
    /// sanctioned way to scan these centroids; do not hand-roll
    /// `(0..n_cent).map(distance)` loops against [`Self::centroid`].
    pub(crate) fn transposed(&self) -> &[f32] {
        assert!(
            self.vectors_resident(),
            "fp32 centroids were dropped (summary_centroids_from_superfiles); \
             exact scans must read the superfile centroid regions"
        );
        self.transposed.get_or_init(|| {
            transpose_centroids_cluster_major(
                &self.centroids,
                self.n_cent as usize,
                self.dim as usize,
            )
        })
    }

    /// Drop the transposed / admit-code scan caches after mutating
    /// [`Self::centroids`] (or `counts` / `n_cent` / `dim`) on a value that
    /// may already have been scanned. The next scan rebuilds them.
    ///
    /// Not called on the read path today (centroids are immutable after
    /// decode); kept for write/maintenance sites that mutate in place.
    #[allow(dead_code)]
    pub(crate) fn invalidate_transposed(&mut self) {
        self.transposed = OnceLock::new();
        self.admit_codes = OnceLock::new();
    }

    /// Whether the fp32 centroid vectors are resident. `false` only after
    /// [`Self::strip_centroids_after_slab`] (read-only consumer memory
    /// mode) — exact scans must then read the superfile centroid regions
    /// instead of this struct.
    pub(crate) fn vectors_resident(&self) -> bool {
        self.n_cent == 0 || !self.centroids.is_empty()
    }

    /// Build the packed 1-bit admit codes + norms from the resident fp32
    /// centroids. Shared by the lazy per-query cache fill and the eager
    /// hydration-time build that precedes a centroid strip.
    fn build_admit_codes(
        &self,
        rotation: &RandomRotation,
        quant: &BitQuantizer,
        rot_seed: u64,
    ) -> RabitqAdmitCodes {
        assert!(
            self.vectors_resident(),
            "admit codes need resident fp32 centroids; this summary was stripped"
        );
        let dim = self.dim as usize;
        let n_cent = self.n_cent as usize;
        let words_per_code = dim.div_ceil(ADMIT_CODE_WORD_BITS);
        let mut codes = vec![0u64; n_cent.saturating_mul(words_per_code)];
        let mut norms = vec![0.0f32; n_cent];
        let mut rotated = vec![0.0f32; dim];
        let mut byte_code = vec![0u8; quant.code_bytes()];
        for c in 0..n_cent {
            let centroid = self.centroid(c);
            norms[c] = dot(centroid, centroid).sqrt();
            rotation.apply(centroid, &mut rotated);
            quant.encode_rotated_into(&rotated, &mut byte_code);
            codes[c * words_per_code..(c + 1) * words_per_code]
                .copy_from_slice(&pack_code_bytes_to_words(&byte_code));
        }
        RabitqAdmitCodes {
            rot_seed,
            words_per_code,
            codes,
            norms,
        }
    }

    /// Read-only-consumer memory mode: eagerly build the 1-bit admit slab,
    /// then drop the fp32 centroid vectors (and the transposed cache) from
    /// memory. `counts`, `n_cent`, and `dim` stay resident — the flat
    /// cluster id math and posting budgets depend on them. Idempotent.
    pub(crate) fn strip_centroids_after_slab(
        &mut self,
        rotation: &RandomRotation,
        quant: &BitQuantizer,
        rot_seed: u64,
    ) {
        if self.n_cent == 0 || self.centroids.is_empty() {
            return;
        }
        let codes = self.build_admit_codes(rotation, quant, rot_seed);
        self.admit_codes = OnceLock::new();
        let _ = self.admit_codes.set(codes);
        self.centroids = Vec::new();
        self.transposed = OnceLock::new();
    }

    /// Packed sign codes for the 1-bit admit prefilter — built once per
    /// instance from the resident fp32 centroids with the query's shared
    /// rotation/quantizer (no per-instance rotation state). Pre-populated
    /// at hydration ([`prewarm_summary_admit_slabs`]) and by
    /// [`Self::strip_centroids_after_slab`] on stripped summaries.
    fn admit_codes(&self, admit: &RabitqAdmitQuery) -> &RabitqAdmitCodes {
        let cache = self.admit_codes.get_or_init(|| {
            self.build_admit_codes(admit.rotation.as_ref(), &admit.quant, admit.rot_seed)
        });
        // Release assert, mirroring the superfile reader's hard error on a
        // rot_seed mismatch: scoring a slab built under a different
        // rotation silently corrupts admit ranking (a recall bug with no
        // crash), which is strictly worse than failing loudly here.
        assert_eq!(
            cache.rot_seed, admit.rot_seed,
            "admit codes built with a different rot_seed"
        );
        cache
    }

    /// Hydration-time slab fill: build the packed admit codes now so the
    /// first query does not pay the encode. `OnceLock` no-op when already
    /// built (a reload reusing warm entries, or a stripped summary).
    pub(crate) fn prewarm_admit_codes(
        &self,
        rotation: &RandomRotation,
        quant: &BitQuantizer,
        rot_seed: u64,
    ) {
        let _ = self
            .admit_codes
            .get_or_init(|| self.build_admit_codes(rotation, quant, rot_seed));
    }

    /// The built admit slab, if any — the summary wire encoder persists it
    /// beside the fp32 centroids when present.
    pub(crate) fn admit_codes_built(&self) -> Option<&RabitqAdmitCodes> {
        self.admit_codes.get()
    }

    /// Wire-decode constructor for routing-only summary blocks (`CFR0`):
    /// no fp32 payload on the wire, so the instance lands directly in the
    /// stripped shape ([`Self::vectors_resident`] = false) with the admit
    /// slab seeded — the same state [`Self::strip_centroids_after_slab`]
    /// produces from a full instance.
    pub(crate) fn from_decoded_routing(
        n_cent: u32,
        dim: u32,
        counts: Vec<u32>,
        admit: RabitqAdmitCodes,
    ) -> Self {
        debug_assert_eq!(
            counts.len(),
            n_cent as usize,
            "routing cluster counts ({}) must match n_cent ({n_cent})",
            counts.len()
        );
        Self {
            n_cent,
            dim,
            centroids: Vec::new(),
            counts,
            transposed: OnceLock::new(),
            admit_codes: {
                let lock = OnceLock::new();
                let _ = lock.set(admit);
                lock
            },
        }
    }

    /// 1-bit prefilter: the best (smallest) estimated admit score across
    /// this instance's populated clusters, from XOR+popcount over packed
    /// sign codes. `None` when no cluster is populated. Estimates rank
    /// cells for the exact-rescore shortlist only — they never feed
    /// routing or near-tie logic directly.
    pub(crate) fn estimate_min_admit_score(
        &self,
        metric: Metric,
        admit: &RabitqAdmitQuery,
    ) -> Option<f32> {
        let mut best: Option<f32> = None;
        self.estimate_admit_scores_into(metric, admit, |_, score| {
            best = Some(best.map_or(score, |b: f32| b.min(score)));
        });
        best
    }

    /// 1-bit prefilter shortlist over this instance's clusters: rank every
    /// populated cluster by its estimated admit score (same XOR+popcount
    /// estimator as [`Self::estimate_min_admit_score`]) and return the top
    /// `window` cluster ids, ascending by estimate. The write-side
    /// assignment prefilter: exact fp32 scoring then runs only on the
    /// returned ids, so the final placement is exact within the window.
    pub(crate) fn admit_shortlist(
        &self,
        metric: Metric,
        admit: &RabitqAdmitQuery,
        window: usize,
    ) -> Vec<(u32, f32)> {
        let mut top: Vec<(u32, f32)> = Vec::with_capacity(window.saturating_add(1));
        self.estimate_admit_scores_into(metric, admit, |c, score| {
            insert_ranked(&mut top, window, c, score);
        });
        top
    }

    /// Emit every populated cluster's estimated admit score — the shared
    /// XOR+popcount estimator behind [`Self::estimate_min_admit_score`],
    /// [`Self::admit_shortlist`], and the user-path stripped-summary fine
    /// scoring (which ranks fragments on the resident 1-bit slab instead
    /// of fetching fp32 per (file, cell)).
    pub(crate) fn estimate_admit_scores_into(
        &self,
        metric: Metric,
        admit: &RabitqAdmitQuery,
        mut emit: impl FnMut(u32, f32),
    ) {
        debug_assert_eq!(admit.cos_table.len(), self.dim as usize + 1);
        let cache = self.admit_codes(admit);
        let w = cache.words_per_code;
        for c in 0..self.n_cent as usize {
            if self.counts[c] == 0 {
                continue;
            }
            let code = &cache.codes[c * w..(c + 1) * w];
            let h = hamming_words(&admit.q_words, code) as usize;
            let est_dot = admit.cos_table[h] * admit.q_norm * cache.norms[c];
            let score = match metric {
                Metric::Cosine => COSINE_DISTANCE_BASE - est_dot,
                Metric::NegDot => -est_dot,
                Metric::L2Sq => {
                    let c_norm = cache.norms[c];
                    admit.q_l2sq + c_norm * c_norm - L2_CROSS_TERM_COEFF * est_dot
                }
            };
            emit(c as u32, score);
        }
    }

    /// Score cluster `c` against `query`: [`distance`] on the fp32 centroid
    /// slice (zero-copy, no dequant). Single-centroid probe — for scans use
    /// [`Self::score_clusters_into`] / [`Self::rank_cells`].
    pub fn score_one(&self, metric: Metric, c: usize, query: &[f32]) -> f32 {
        debug_assert_eq!(query.len(), self.dim as usize);
        distance(metric, query, self.centroid(c))
    }

    /// Score every populated cluster via the blocked SIMD kernel over the
    /// cached transposed layout. Calls `emit(cluster_id, score)` in ascending
    /// cluster order for each cluster with a nonzero indexed count.
    pub fn score_clusters_into(
        &self,
        metric: Metric,
        query: &[f32],
        mut emit: impl FnMut(u32, f32),
    ) {
        debug_assert_eq!(query.len(), self.dim as usize);
        let n_cent = self.n_cent as usize;
        let scores = all_centroid_scores_transposed(
            metric,
            query,
            self.transposed(),
            n_cent,
            self.dim as usize,
        );
        for (c, &score) in scores.iter().enumerate() {
            if self.counts[c] == 0 {
                continue;
            }
            emit(c as u32, score);
        }
    }

    /// Rank every cell (including count-0 cells) against `query`: ascending
    /// score, ties broken by lower cell id. The full-ranking shape the cell
    /// routing cutoff consumes; scored by the blocked SIMD kernel.
    pub fn rank_cells(&self, metric: Metric, query: &[f32]) -> Vec<(u32, f32)> {
        debug_assert_eq!(query.len(), self.dim as usize);
        let n_cent = self.n_cent as usize;
        let scores = all_centroid_scores_transposed(
            metric,
            query,
            self.transposed(),
            n_cent,
            self.dim as usize,
        );
        let mut ranked: Vec<(u32, f32)> = scores
            .into_iter()
            .enumerate()
            .map(|(c, score)| (c as u32, score))
            .collect();
        ranked.sort_unstable_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then_with(|| a.0.cmp(&b.0))
        });
        ranked
    }

    /// Return the cell whose centroid is closest to `query` under `metric`
    /// (count-0 cells excluded; 0 when every cell is empty). Blocked SIMD
    /// top-1 over the cached transposed layout.
    pub fn nearest_cell(&self, metric: Metric, query: &[f32]) -> u32 {
        debug_assert_eq!(query.len(), self.dim as usize);
        nearest_k_centroids_transposed(
            metric,
            query,
            self.transposed(),
            self.n_cent as usize,
            self.dim as usize,
            Some(&self.counts),
            1,
        )
        .first()
        .map(|&(cell, _)| cell)
        .unwrap_or(0)
    }

    /// Assign each row in `vectors` to its nearest cell. Parallel over rows;
    /// each assignment uses [`Self::nearest_cell`].
    pub fn assign_rows(&self, metric: Metric, vectors: &[f32], assignments: &mut [u32]) {
        let dim = self.dim as usize;
        assert_eq!(vectors.len() % dim, 0, "assign_rows: vectors len mismatch");
        let n = vectors.len() / dim;
        assert_eq!(
            assignments.len(),
            n,
            "assign_rows: assignments len mismatch"
        );
        if n == 0 {
            return;
        }
        assignments
            .par_iter_mut()
            .enumerate()
            .for_each(|(d, slot)| {
                *slot = self.nearest_cell(metric, &vectors[d * dim..(d + 1) * dim]);
            });
    }
}

#[cfg(test)]
mod tests {
    use std::{hint::black_box, slice::from_ref, sync::Arc, time::Instant};

    use arrow_array::{
        Array, Date32Array, Date64Array, Int64Array, Time64MicrosecondArray,
        TimestampMicrosecondArray,
    };
    use arrow_schema::{DataType, Field, Schema, TimeUnit};
    use dashmap::DashMap;
    use datafusion::scalar::ScalarValue;
    use tempfile::TempDir;
    use tokio::sync::OnceCell;

    use super::*;
    use crate::{
        storage::LocalFsStorageProvider,
        superfile::{builder::FtsConfig, vector::distance::distance},
        supertable::manifest::{
            commit::{PartWriteResult, write_manifest_part},
            list::{Manifest, PartitionStrategy},
        },
        test_helpers::default_tokenizer,
    };

    /// Deterministic synthetic fp32 centroids for cluster-scoring tests.
    fn synth_clusters(n_cent: u32, dim: u32, seed: u64) -> (ClusterCentroids, Vec<f32>) {
        let (nc, d) = (n_cent as usize, dim as usize);
        let mut centroids = vec![0f32; nc * d];
        for c in 0..nc {
            for j in 0..d {
                let v = ((seed + (c * d + j) as u64 * 2_654_435_761) % 1000) as f32 / 250.0 - 2.0
                    + c as f32 * 0.1;
                centroids[c * d + j] = v;
            }
        }
        let counts: Vec<u32> = (0..nc).map(|c| if c == nc / 2 { 0 } else { 10 }).collect();
        let cc = ClusterCentroids::from_fp32(n_cent, dim, &centroids, counts);
        (cc, centroids)
    }

    #[test]
    fn min_max_stats_record_and_fold_all_null_columns() {
        let arr = |vals: Vec<Option<i64>>| Arc::new(Int64Array::from(vals)) as ArrayRef;
        let scalar = |a: &ArrayRef| ScalarValue::try_from_array(a, 0).expect("decode");

        // All-null column still yields a stat, with null min/max (so the
        // null count is recorded and `IS [NOT] NULL` can prune on it).
        let (mn, mx) = column_min_max(&arr(vec![None, None])).expect("all-null stat");
        assert!(mn.is_null(0) && mx.is_null(0));

        // Populated column → real min/max, nulls ignored.
        let (mn, mx) = column_min_max(&arr(vec![Some(5), Some(2), None])).expect("stat");
        assert_eq!(scalar(&mn), ScalarValue::Int64(Some(2)));
        assert_eq!(scalar(&mx), ScalarValue::Int64(Some(5)));

        // Fold: a real bound wins over a null bound; both-null stays null.
        let null1 = arr(vec![None]);
        let (mn, mx) =
            merge_min_max_arrays(&null1, &arr(vec![Some(2)]), &null1, &arr(vec![Some(9)]))
                .expect("merge real over null");
        assert_eq!(scalar(&mn), ScalarValue::Int64(Some(2)));
        assert_eq!(scalar(&mx), ScalarValue::Int64(Some(9)));
        let (mn, mx) = merge_min_max_arrays(&null1, &null1, &null1, &null1).expect("merge null");
        assert!(mn.is_null(0) && mx.is_null(0));
    }

    #[test]
    fn min_max_stats_cover_temporal_columns() {
        let scalar = |a: &ArrayRef| ScalarValue::try_from_array(a, 0).expect("decode");

        // Date32 (the ClickBench `EventDate` case that used to carry no stat):
        // numeric-backed, so min/max record and fold like an integer.
        let d: ArrayRef = Arc::new(Date32Array::from(vec![Some(20100), Some(19000), None]));
        let (mn, mx) = column_min_max(&d).expect("date stat");
        assert_eq!(scalar(&mn), ScalarValue::Date32(Some(19000)));
        assert_eq!(scalar(&mx), ScalarValue::Date32(Some(20100)));

        // Two superfiles' date bounds fold to the outer extremes.
        let lo: ArrayRef = Arc::new(Date32Array::from(vec![Some(19000)]));
        let hi: ArrayRef = Arc::new(Date32Array::from(vec![Some(20100)]));
        let olo: ArrayRef = Arc::new(Date32Array::from(vec![Some(18000)]));
        let ohi: ArrayRef = Arc::new(Date32Array::from(vec![Some(21000)]));
        let (mn, mx) = merge_min_max_arrays(&lo, &olo, &hi, &ohi).expect("date merge");
        assert_eq!(scalar(&mn), ScalarValue::Date32(Some(18000)));
        assert_eq!(scalar(&mx), ScalarValue::Date32(Some(21000)));

        // Timestamp keeps its timezone through both build and merge. A
        // naive-vs-zoned mismatch would fail the merge and silently drop the
        // stat, so assert the type survives, not just the value.
        let tz = "+05:30";
        let ts: ArrayRef = Arc::new(
            TimestampMicrosecondArray::from(vec![Some(200i64), Some(100)]).with_timezone(tz),
        );
        let zoned = DataType::Timestamp(TimeUnit::Microsecond, Some(tz.into()));
        let (mn, mx) = column_min_max(&ts).expect("ts stat");
        assert_eq!(mn.data_type(), &zoned);
        assert_eq!(
            scalar(&mn),
            ScalarValue::TimestampMicrosecond(Some(100), Some(tz.into()))
        );
        assert_eq!(
            scalar(&mx),
            ScalarValue::TimestampMicrosecond(Some(200), Some(tz.into()))
        );
        let (mmn, _mmx) = merge_min_max_arrays(&mn, &mn, &mx, &mx).expect("ts merge keeps tz");
        assert_eq!(mmn.data_type(), &zoned);

        // Date64 (ms-since-epoch) and Time64 ride the same `prim!` arm as
        // Date32; spot-check that each records and reconstructs to its type.
        let d64: ArrayRef = Arc::new(Date64Array::from(vec![Some(9i64), Some(2)]));
        assert_eq!(
            scalar(&column_min_max(&d64).expect("date64 stat").0),
            ScalarValue::Date64(Some(2))
        );
        let t64: ArrayRef = Arc::new(Time64MicrosecondArray::from(vec![Some(7i64), Some(3)]));
        assert_eq!(
            scalar(&column_min_max(&t64).expect("time64 stat").0),
            ScalarValue::Time64Microsecond(Some(3))
        );
    }

    /// Every supported column type must fold through `merge_min_max_arrays`
    /// keeping the smaller `min` and the larger `max`, and the merged bound
    /// must reconstruct to the column's own scalar type. A missing arm (or a
    /// type drift on reconstruction) fails the downcast and silently drops the
    /// stat, regressing the range prune for that type — so assert both the
    /// value fold and the reconstructed `ScalarValue` for each.
    #[test]
    fn merge_min_max_folds_every_supported_column_type() {
        let scalar = |a: &ArrayRef| ScalarValue::try_from_array(a, 0).expect("decode");
        // min-pair = (hi, lo) ⇒ lo survives; max-pair = (lo, hi) ⇒ hi survives.
        let fold =
            |lo: ArrayRef, hi: ArrayRef| merge_min_max_arrays(&hi, &lo, &lo, &hi).expect("merge");

        macro_rules! prim_case {
            ($arr:ty, $sv:path, $lo:expr, $hi:expr) => {{
                let lo: ArrayRef = Arc::new(<$arr>::from(vec![Some($lo)]));
                let hi: ArrayRef = Arc::new(<$arr>::from(vec![Some($hi)]));
                let (mn, mx) = fold(lo, hi);
                assert_eq!(
                    scalar(&mn),
                    $sv(Some($lo)),
                    concat!(stringify!($arr), " min")
                );
                assert_eq!(
                    scalar(&mx),
                    $sv(Some($hi)),
                    concat!(stringify!($arr), " max")
                );
            }};
        }

        prim_case!(UInt8Array, ScalarValue::UInt8, 1u8, 9u8);
        prim_case!(UInt16Array, ScalarValue::UInt16, 1u16, 9u16);
        prim_case!(UInt32Array, ScalarValue::UInt32, 1u32, 9u32);
        prim_case!(UInt64Array, ScalarValue::UInt64, 1u64, 9u64);
        prim_case!(Int8Array, ScalarValue::Int8, -3i8, 4i8);
        prim_case!(Int16Array, ScalarValue::Int16, -3i16, 4i16);
        prim_case!(Int32Array, ScalarValue::Int32, -3i32, 4i32);
        prim_case!(Float32Array, ScalarValue::Float32, -1.5f32, 2.5f32);
        prim_case!(Float64Array, ScalarValue::Float64, -1.5f64, 2.5f64);
        prim_case!(Date64Array, ScalarValue::Date64, 2i64, 9i64);
        prim_case!(Time32SecondArray, ScalarValue::Time32Second, 2i32, 9i32);
        prim_case!(
            Time32MillisecondArray,
            ScalarValue::Time32Millisecond,
            2i32,
            9i32
        );
        prim_case!(
            Time64NanosecondArray,
            ScalarValue::Time64Nanosecond,
            2i64,
            9i64
        );

        // `false < true`: min folds like AND, max like OR.
        let (mn, mx) = fold(
            Arc::new(BooleanArray::from(vec![Some(false)])),
            Arc::new(BooleanArray::from(vec![Some(true)])),
        );
        assert_eq!(scalar(&mn), ScalarValue::Boolean(Some(false)), "bool min");
        assert_eq!(scalar(&mx), ScalarValue::Boolean(Some(true)), "bool max");

        // Utf8 / LargeUtf8 fold lexicographically.
        let (mn, mx) = fold(
            Arc::new(StringArray::from(vec![Some("apple")])),
            Arc::new(StringArray::from(vec![Some("pear")])),
        );
        assert_eq!(
            scalar(&mn),
            ScalarValue::Utf8(Some("apple".into())),
            "utf8 min"
        );
        assert_eq!(
            scalar(&mx),
            ScalarValue::Utf8(Some("pear".into())),
            "utf8 max"
        );
        let (mn, mx) = fold(
            Arc::new(LargeStringArray::from(vec![Some("apple")])),
            Arc::new(LargeStringArray::from(vec![Some("pear")])),
        );
        assert_eq!(
            scalar(&mn),
            ScalarValue::LargeUtf8(Some("apple".into())),
            "largeutf8 min"
        );
        assert_eq!(
            scalar(&mx),
            ScalarValue::LargeUtf8(Some("pear".into())),
            "largeutf8 max"
        );

        // Decimal128 keeps precision/scale through the fold.
        let dec = |v: i128| -> ArrayRef {
            Arc::new(
                Decimal128Array::from(vec![Some(v)])
                    .with_precision_and_scale(10, 2)
                    .expect("decimal"),
            )
        };
        let (mn, mx) = fold(dec(125), dec(999));
        assert_eq!(
            scalar(&mn),
            ScalarValue::Decimal128(Some(125), 10, 2),
            "dec min"
        );
        assert_eq!(
            scalar(&mx),
            ScalarValue::Decimal128(Some(999), 10, 2),
            "dec max"
        );

        // Timestamp arms other than the Microsecond one covered above; a
        // naive (tz-less) timestamp must fold and reconstruct with `None` tz.
        let (mn, mx) = fold(
            Arc::new(TimestampSecondArray::from(vec![Some(100i64)])),
            Arc::new(TimestampSecondArray::from(vec![Some(200i64)])),
        );
        assert_eq!(
            scalar(&mn),
            ScalarValue::TimestampSecond(Some(100), None),
            "ts-sec min"
        );
        assert_eq!(
            scalar(&mx),
            ScalarValue::TimestampSecond(Some(200), None),
            "ts-sec max"
        );
        let (mn, _mx) = fold(
            Arc::new(TimestampNanosecondArray::from(vec![Some(100i64)])),
            Arc::new(TimestampNanosecondArray::from(vec![Some(200i64)])),
        );
        assert_eq!(
            scalar(&mn),
            ScalarValue::TimestampNanosecond(Some(100), None),
            "ts-nano min"
        );
    }

    /// Cloning a warm [`ClusterCentroids`] must keep the transposed cache so a
    /// subsequent scan does not pay the scalar transpose rebuild.
    #[test]
    fn clone_preserves_warm_transposed_cache() {
        let (cc, _) = synth_clusters(32, 128, 3);
        let warm = cc.transposed().as_ptr();
        assert!(cc.transposed.get().is_some());
        let cloned = cc.clone();
        assert!(
            cloned.transposed.get().is_some(),
            "clone must carry a warm transposed cache"
        );
        // Same bytes, distinct allocation (Vec clone) — pointer differs, length matches.
        assert_eq!(cloned.transposed().len(), cc.transposed().len());
        assert_ne!(
            cloned.transposed().as_ptr(),
            warm,
            "clone owns its own cache buffer"
        );
        let mut mutated = cc.clone();
        mutated.centroids[0] += 1.0;
        mutated.invalidate_transposed();
        assert!(mutated.transposed.get().is_none());
        let _ = mutated.transposed(); // rebuild
        assert!(mutated.transposed.get().is_some());
    }

    /// The 1-bit admit estimate must prefer the instance holding the
    /// query's true nearest centroid on separated fixtures, for every
    /// metric — the property the exact-rescore cell shortlist rides on.
    #[test]
    fn admit_estimate_prefers_matching_centroid_instance() {
        const DIM: usize = 128;
        const ROT_SEED: u64 = 7;
        // Two single-centroid instances on different axes.
        let mut near = vec![0.0f32; DIM];
        near[0] = 1.0;
        let mut far = vec![0.0f32; DIM];
        far[5] = 1.0;
        let a = ClusterCentroids::from_fp32(1, DIM as u32, &near, vec![1]);
        let b = ClusterCentroids::from_fp32(1, DIM as u32, &far, vec![1]);
        // Query beside `near`, small off-axis noise.
        let mut query = near.clone();
        query[1] = 0.05;
        let admit = RabitqAdmitQuery::new(DIM, ROT_SEED, &query);
        for metric in [Metric::Cosine, Metric::L2Sq, Metric::NegDot] {
            let ea = a
                .estimate_min_admit_score(metric, &admit)
                .expect("a populated");
            let eb = b
                .estimate_min_admit_score(metric, &admit)
                .expect("b populated");
            assert!(
                ea < eb,
                "{metric:?}: matching instance must rank first ({ea} vs {eb})"
            );
        }
        // Count-0 clusters are skipped: an unpopulated instance has no
        // estimate to contribute.
        let unpopulated = ClusterCentroids::from_fp32(1, DIM as u32, &near, vec![0]);
        assert!(
            unpopulated
                .estimate_min_admit_score(Metric::Cosine, &admit)
                .is_none()
        );
        // Clone carries the warm admit-code slab.
        let cloned = a.clone();
        assert!(cloned.admit_codes.get().is_some());
    }

    /// Stripping keeps `counts`/`n_cent`/`dim` and the pre-built admit
    /// slab, drops the fp32 vectors, and stays idempotent; estimates keep
    /// serving from the slab afterward.
    #[test]
    fn strip_centroids_keeps_slab_and_counts() {
        const DIM: usize = 64;
        const ROT_SEED: u64 = 7;
        let mut flat = vec![0.0f32; 2 * DIM];
        flat[0] = 1.0;
        flat[DIM + 5] = 1.0;
        let mut cc = ClusterCentroids::from_fp32(2, DIM as u32, &flat, vec![3, 4]);
        let rotation = RandomRotation::new(DIM, ROT_SEED);
        let quant = BitQuantizer::new(DIM);
        cc.strip_centroids_after_slab(&rotation, &quant, ROT_SEED);
        assert!(!cc.vectors_resident());
        assert_eq!(cc.n_cent, 2);
        assert_eq!(cc.counts, vec![3, 4]);
        assert!(cc.centroids.is_empty());
        let mut query = vec![0.0f32; DIM];
        query[0] = 1.0;
        let admit = RabitqAdmitQuery::new(DIM, ROT_SEED, &query);
        assert!(
            cc.estimate_min_admit_score(Metric::Cosine, &admit)
                .is_some(),
            "estimates must keep serving from the pre-built slab"
        );
        // Idempotent (a reload may strip already-stripped clones).
        cc.strip_centroids_after_slab(&rotation, &quant, ROT_SEED);
        assert!(!cc.vectors_resident());
        // Clone carries the stripped state + slab.
        let cloned = cc.clone();
        assert!(!cloned.vectors_resident());
        assert!(cloned.admit_codes.get().is_some());
    }

    /// Exact scans on a stripped summary must fail loudly — the caller is
    /// required to route through the superfile centroid regions instead.
    #[test]
    #[should_panic(expected = "fp32 centroids were dropped")]
    fn transposed_on_stripped_summary_panics() {
        const DIM: usize = 64;
        const ROT_SEED: u64 = 7;
        let mut flat = vec![0.0f32; DIM];
        flat[0] = 1.0;
        let mut cc = ClusterCentroids::from_fp32(1, DIM as u32, &flat, vec![1]);
        cc.strip_centroids_after_slab(
            &RandomRotation::new(DIM, ROT_SEED),
            &BitQuantizer::new(DIM),
            ROT_SEED,
        );
        let _ = cc.transposed();
    }

    /// `score_clusters_into` must match [`distance`] on the fp32 centroid slice.
    #[test]
    fn score_clusters_into_matches_centroid_distance() {
        let (n_cent, dim) = (17u32, 96u32);
        let (cc, centroids) = synth_clusters(n_cent, dim, 7);
        let query: Vec<f32> = (0..dim)
            .map(|j| ((j as u64 * 40_503 + 11) % 997) as f32 / 500.0 - 1.0)
            .collect();

        for metric in [Metric::Cosine, Metric::L2Sq, Metric::NegDot] {
            let mut scored: Vec<(u32, f32)> = Vec::new();
            cc.score_clusters_into(metric, &query, |c, s| {
                scored.push((c, s));
            });

            let mut reference: Vec<(u32, f32)> = Vec::new();
            for c in 0..n_cent as usize {
                if cc.counts[c] == 0 {
                    continue;
                }
                let d = distance(metric, &query, cc.centroid(c));
                // Single-cluster probe must equal the direct distance.
                assert_eq!(
                    cc.score_one(metric, c, &query),
                    d,
                    "{metric:?}: score_one c{c}"
                );
                reference.push((c as u32, d));
            }

            assert_eq!(
                scored.len(),
                reference.len(),
                "{metric:?}: cluster sets differ (count-0 skip)"
            );
            for ((sc, ss), (rc, rs)) in scored.iter().zip(&reference) {
                assert_eq!(sc, rc, "{metric:?}: cluster order");
                assert!(
                    (ss - rs).abs() <= 1e-5 * (1.0 + rs.abs()),
                    "{metric:?} cluster {sc}: {ss} vs {rs}"
                );
            }
        }

        // fp32 storage is lossless: to_fp32 returns the input centroids verbatim.
        let roundtrip = cc.to_fp32();
        for (i, (&got, &want)) in roundtrip.iter().zip(centroids.iter()).enumerate() {
            assert_eq!(got, want, "roundtrip[{i}]: {got} vs {want}");
        }
    }

    /// Microbench: Sq8+ε dequant + distance cluster scoring at supertable scale.
    #[test]
    #[ignore = "perf microbench, not a correctness gate"]
    fn score_clusters_microbench() {
        let (n_cent, dim) = (4096u32, 384u32);
        let iters = 50usize;
        let (cc, _) = synth_clusters(n_cent, dim, 99);
        let query: Vec<f32> = (0..dim).map(|j| (j as f32).sin()).collect();

        for metric in [Metric::Cosine, Metric::L2Sq] {
            let t0 = Instant::now();
            for _ in 0..iters {
                let mut acc = 0f32;
                cc.score_clusters_into(metric, &query, |_, s| acc += s);
                black_box(acc);
            }
            let us = t0.elapsed().as_micros() as f64 / iters as f64;
            println!("score_clusters {metric:?}: {us:.0} µs/query");
        }
    }

    fn schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![Field::new(
            "title",
            DataType::LargeUtf8,
            false,
        )]))
    }

    fn opts() -> Arc<SupertableOptions> {
        let tk = default_tokenizer();
        Arc::new(
            SupertableOptions::new(
                schema(),
                vec![FtsConfig {
                    column: "title".into(),
                    positions: false,
                }],
                vec![],
                Some(tk),
            )
            .expect("valid options"),
        )
    }

    fn seg_entry(uuid: Uuid, n_docs: u64) -> Arc<SuperfileEntry> {
        Arc::new(SuperfileEntry {
            birth_version: 0,
            superfile_id: uuid,
            uri: SuperfileUri(uuid),
            n_docs,
            id_min: 0,
            id_max: n_docs.saturating_sub(1) as i128,
            scalar_stats: HashMap::new(),
            fts_summary: HashMap::new(),
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            vector_layout: VectorLayout::Ivf,
            subsection_offsets: None,
        })
    }

    #[test]
    fn empty_manifest_starts_at_zero() {
        let m = ManifestSnapshot::empty(opts());
        assert_eq!(m.manifest_id, 0);
        assert_eq!(m.superfiles.len(), 0);
        assert_eq!(m.n_docs_total(), 0);
    }

    #[test]
    fn with_appended_increments_manifest_id_and_extends_superfiles() {
        let m0 = ManifestSnapshot::empty(opts());
        let entry = seg_entry(Uuid::new_v4(), 100);
        let m1 = m0.with_appended(vec![entry.clone()]);
        assert_eq!(m1.manifest_id, 1);
        assert_eq!(m1.superfiles.len(), 1);
        assert_eq!(m1.n_docs_total(), 100);
        // Original m0 unchanged — the immutability invariant.
        assert_eq!(m0.manifest_id, 0);
        assert_eq!(m0.superfiles.len(), 0);
        assert_eq!(m0.n_docs_total(), 0);
    }

    #[test]
    fn with_appended_chains_to_higher_manifest_ids() {
        let m0 = ManifestSnapshot::empty(opts());
        let m1 = m0.with_appended(vec![seg_entry(Uuid::new_v4(), 50)]);
        let m2 = m1.with_appended(vec![seg_entry(Uuid::new_v4(), 75)]);
        assert_eq!(m0.manifest_id, 0);
        assert_eq!(m1.manifest_id, 1);
        assert_eq!(m2.manifest_id, 2);
        assert_eq!(m0.superfiles.len(), 0);
        assert_eq!(m1.superfiles.len(), 1);
        assert_eq!(m2.superfiles.len(), 2);
        assert_eq!(m2.n_docs_total(), 50 + 75);
    }

    #[test]
    fn with_appended_shares_old_superfiles_via_arc() {
        // The new manifest's superfiles[0] should be the SAME Arc as
        // the original's superfiles[0] — copy-on-write doesn't
        // re-allocate per-superfile. (Verified by Arc::ptr_eq.)
        let entry = seg_entry(Uuid::new_v4(), 1);
        let m0 = ManifestSnapshot::empty(opts()).with_appended(vec![entry.clone()]);
        let m1 = m0.with_appended(vec![seg_entry(Uuid::new_v4(), 2)]);
        assert!(Arc::ptr_eq(&m0.superfiles[0], &m1.superfiles[0]));
    }

    #[test]
    fn with_appended_empty_input_still_bumps_manifest_id() {
        // Edge case: with_appended(vec![]) is a no-op for superfiles
        // but should still produce a new manifest_id. (Whether this
        // is a "should" decision or "ok behavior" is fine here —
        // the writer won't call it with empty input in practice;
        // the test pins the current behavior.)
        let m0 = ManifestSnapshot::empty(opts());
        let m1 = m0.with_appended(vec![]);
        assert_eq!(m1.manifest_id, 1);
        assert_eq!(m1.superfiles.len(), 0);
    }

    #[test]
    fn new_from_superfiles_builds_manifest_at_id_one_with_entries() {
        // `new_from_superfiles` is `empty(opts).with_appended(...)`:
        // one append hop off the empty manifest, so manifest_id lands
        // at 1 and the manifest carries exactly the entries handed in.
        let a = seg_entry(Uuid::new_v4(), 10);
        let b = seg_entry(Uuid::new_v4(), 20);
        let m = ManifestSnapshot::new_from_superfiles(opts(), vec![a.clone(), b.clone()]);
        assert_eq!(m.manifest_id, 1);
        assert_eq!(m.superfiles.len(), 2);
        assert_eq!(m.n_docs_total(), 30);
        // Copy-on-write shares the passed-in Arcs rather than
        // re-allocating per-superfile.
        assert!(Arc::ptr_eq(&m.superfiles[0], &a));
        assert!(Arc::ptr_eq(&m.superfiles[1], &b));
        // No storage attached, so it's an in-process-only manifest
        // (no Manifest / loader).
        assert!(m.is_in_process_only());
    }

    #[test]
    fn new_from_superfiles_with_empty_input_is_empty_at_id_one() {
        // Mirrors `with_appended(vec![])`: no superfiles, but the
        // single append hop still advances manifest_id to 1.
        let m = ManifestSnapshot::new_from_superfiles(opts(), vec![]);
        assert_eq!(m.manifest_id, 1);
        assert_eq!(m.superfiles.len(), 0);
        assert_eq!(m.n_docs_total(), 0);
    }

    #[test]
    fn get_next_manifest_id_is_current_plus_one() {
        let m0 = ManifestSnapshot::empty(opts());
        assert_eq!(m0.get_manifest_id(), 0);
        assert_eq!(m0.get_next_manifest_id(), 1);

        let m1 = m0.with_appended(vec![seg_entry(Uuid::new_v4(), 1)]);
        assert_eq!(m1.get_manifest_id(), 1);
        assert_eq!(m1.get_next_manifest_id(), 2);
    }

    #[test]
    fn get_next_manifest_id_is_a_pure_read() {
        // Querying the successor id is side-effect-free: the
        // manifest's own id is untouched and repeat calls are stable.
        let m = ManifestSnapshot::empty(opts());
        let _ = m.get_next_manifest_id();
        assert_eq!(m.get_manifest_id(), 0, "current id unchanged");
        assert_eq!(m.get_next_manifest_id(), m.get_next_manifest_id());
    }

    #[test]
    fn superfile_uri_is_distinct_per_call() {
        let a = SuperfileUri::new_v4();
        let b = SuperfileUri::new_v4();
        assert_ne!(a, b);
    }

    // ============================================================
    // In-memory `ManifestSnapshot` with lazy-load parts — content-hash-
    // verified per-part fetch through an injected
    // `StorageProvider`, OnceCell coalescing on cold cells,
    // typed errors for missing loader / missing part / hash
    // mismatch.
    // ============================================================

    mod lazy_load {
        use std::{
            collections::HashMap,
            error::Error,
            ops::Range,
            slice::from_ref,
            sync::{
                Arc,
                atomic::{AtomicUsize, Ordering},
            },
            time::SystemTime,
        };

        use arrow_schema::{DataType, Field, Schema};
        use async_trait::async_trait;
        use bytes::Bytes;
        use dashmap::DashMap;
        use tokio::spawn;
        use uuid::Uuid;

        use super::super::*;
        use crate::{
            storage::{ObjectMeta, StorageError, StorageProvider},
            supertable::{
                SupertableOptions,
                manifest::{
                    list::{FORMAT_VERSION as LIST_FORMAT_VERSION, Manifest, PartitionStrategy},
                    part::{self as part_mod, ContentHash, ManifestPart, PartId},
                },
            },
        };

        #[derive(Debug)]
        struct CountingMockStorage {
            objects: HashMap<String, Bytes>,
            get_calls: AtomicUsize,
        }

        impl CountingMockStorage {
            fn new(objects: HashMap<String, Bytes>) -> Self {
                Self {
                    objects,
                    get_calls: AtomicUsize::new(0),
                }
            }

            fn get_call_count(&self) -> usize {
                self.get_calls.load(Ordering::Acquire)
            }
        }

        #[async_trait]
        impl StorageProvider for CountingMockStorage {
            async fn head(&self, uri: &str) -> Result<ObjectMeta, StorageError> {
                match self.objects.get(uri) {
                    Some(b) => Ok(ObjectMeta {
                        size: b.len() as u64,
                        etag: Some("mock-etag".into()),
                        last_modified: SystemTime::UNIX_EPOCH,
                    }),
                    None => Err(StorageError::NotFound { uri: uri.into() }),
                }
            }

            async fn get(&self, uri: &str) -> Result<(Bytes, ObjectMeta), StorageError> {
                self.get_calls.fetch_add(1, Ordering::AcqRel);
                match self.objects.get(uri) {
                    Some(b) => Ok((
                        b.clone(),
                        ObjectMeta {
                            size: b.len() as u64,
                            etag: Some("mock-etag".into()),
                            last_modified: SystemTime::UNIX_EPOCH,
                        },
                    )),
                    None => Err(StorageError::NotFound { uri: uri.into() }),
                }
            }

            async fn get_range(
                &self,
                uri: &str,
                _range: Range<u64>,
            ) -> Result<Bytes, StorageError> {
                Err(permanent(uri, "get_range unimplemented for mock"))
            }

            async fn put_atomic(
                &self,
                uri: &str,
                _bytes: Bytes,
            ) -> Result<Option<String>, StorageError> {
                Err(permanent(uri, "put_atomic unimplemented for mock"))
            }

            async fn put_if_match(
                &self,
                uri: &str,
                _bytes: Bytes,
                _expected_etag: Option<&str>,
            ) -> Result<Option<String>, StorageError> {
                Err(permanent(uri, "put_if_match unimplemented for mock"))
            }

            async fn put_multipart(
                &self,
                uri: &str,
            ) -> Result<Box<dyn object_store::MultipartUpload>, StorageError> {
                Err(permanent(uri, "put_multipart unimplemented for mock"))
            }

            async fn delete(&self, _uri: &str) -> Result<(), StorageError> {
                Ok(())
            }
        }

        fn permanent(uri: &str, msg: &'static str) -> StorageError {
            let boxed: Box<dyn Error + Send + Sync> = msg.into();
            StorageError::Permanent {
                uri: uri.into(),
                source: boxed,
            }
        }

        fn make_test_part(seed: u8) -> ManifestPart {
            ManifestPart {
                format_version: part_mod::FORMAT_VERSION.into(),
                part_id: PartId(Uuid::from_bytes([seed; 16])),
                superfiles: vec![],
            }
        }

        fn encode_and_index(
            parts: &[ManifestPart],
        ) -> (HashMap<String, Bytes>, Vec<ManifestPartEntry>) {
            let mut objects = HashMap::new();
            let mut entries = Vec::new();
            for p in parts {
                let bytes = part_mod::encode(p);
                let hash = ContentHash::of(&bytes);
                let uri = format!("manifests/part-{}.avro.zst", hash.to_hex());
                let size_compressed = bytes.len() as u64;
                objects.insert(uri.clone(), Bytes::from(bytes));
                entries.push(ManifestPartEntry {
                    part_id: p.part_id,
                    uri,
                    n_superfiles: p.superfiles.len() as u64,
                    size_bytes_compressed: size_compressed,
                    size_bytes_uncompressed: size_compressed,
                    content_hash: hash,
                    routing: None,
                    id_range: (0, 0),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                });
            }
            (objects, entries)
        }

        fn fresh_list(entries: Vec<ManifestPartEntry>) -> Manifest {
            Manifest {
                drained_ranges: Default::default(),
                global_vector_index: None,
                tombstone_seqs: Default::default(),
                superseded_cells: Default::default(),
                format_version: LIST_FORMAT_VERSION.into(),
                manifest_id: 1,
                options_hash: ContentHash([0u8; 32]),
                schema: Vec::new(),
                id_column: "doc_id".into(),
                fts_columns: vec![],
                vector_columns: vec![],
                partition_strategy: PartitionStrategy::Hash {
                    column: "doc_id".into(),
                    n_buckets: 64,
                },
                vector_index_storage_prefix: None,
                deleted_user_ids_inline: None,
                slow_vector_state_uri: None,
                slow_vector_state_content_hash: None,
                slow_vector_state_centroids: None,
                parts: entries,
            }
        }

        fn options_for_test() -> Arc<SupertableOptions> {
            let s = Arc::new(Schema::new(vec![Field::new(
                "title",
                DataType::LargeUtf8,
                false,
            )]));
            Arc::new(SupertableOptions::new(s, vec![], vec![], None).expect("opts"))
        }

        fn build_manifest_with_loader(
            list: Manifest,
            storage: Arc<dyn StorageProvider>,
        ) -> ManifestSnapshot {
            let loader = Arc::new(ManifestPartLoader::new(Arc::clone(&storage), &list));
            ManifestSnapshot {
                superfile_list: SuperfileList::empty(options_for_test()),
                list: Some(list),
                parts: DashMap::new(),
                loader: Some(loader),
                stamped_partition_strategy: None,
                stamped_global_vector_index: None,
                stamped_drained_ranges: None,
            }
        }

        #[tokio::test]
        async fn part_first_touch_loads_and_caches() {
            let part = make_test_part(7);
            let (objects, entries) = encode_and_index(from_ref(&part));
            let storage = Arc::new(CountingMockStorage::new(objects));
            let list = fresh_list(entries);
            let manifest =
                build_manifest_with_loader(list, Arc::clone(&storage) as Arc<dyn StorageProvider>);

            let loaded = manifest.get_part_by_id(part.part_id).await.expect("load");
            assert_eq!(loaded.part_id, part.part_id);
            assert_eq!(storage.get_call_count(), 1, "exactly one storage.get");
        }

        #[tokio::test]
        async fn second_touch_hits_cache_zero_additional_gets() {
            let part = make_test_part(11);
            let (objects, entries) = encode_and_index(from_ref(&part));
            let storage = Arc::new(CountingMockStorage::new(objects));
            let list = fresh_list(entries);
            let manifest =
                build_manifest_with_loader(list, Arc::clone(&storage) as Arc<dyn StorageProvider>);

            let a = manifest
                .get_part_by_id(part.part_id)
                .await
                .expect("first load");
            let b = manifest
                .get_part_by_id(part.part_id)
                .await
                .expect("second load");
            assert!(Arc::ptr_eq(&a, &b), "second touch must return cached Arc");
            assert_eq!(storage.get_call_count(), 1, "cache hit ⇒ no extra get");
        }

        #[tokio::test]
        async fn concurrent_loaders_coalesce_to_one_get() {
            let part = make_test_part(13);
            let (objects, entries) = encode_and_index(from_ref(&part));
            let storage = Arc::new(CountingMockStorage::new(objects));
            let list = fresh_list(entries);
            let manifest = Arc::new(build_manifest_with_loader(
                list,
                Arc::clone(&storage) as Arc<dyn StorageProvider>,
            ));

            // 100 concurrent tasks on the same cold cell.
            let mut handles = Vec::with_capacity(100);
            for _ in 0..100 {
                let m = Arc::clone(&manifest);
                let pid = part.part_id;
                handles.push(spawn(async move { m.get_part_by_id(pid).await }));
            }
            let mut first: Option<Arc<ManifestPart>> = None;
            for h in handles {
                let p = h.await.expect("join").expect("load");
                match &first {
                    None => first = Some(p),
                    Some(f) => assert!(
                        Arc::ptr_eq(f, &p),
                        "all concurrent loaders must share the same Arc"
                    ),
                }
            }
            assert_eq!(
                storage.get_call_count(),
                1,
                "100 concurrent loaders on cold cell ⇒ exactly one storage.get"
            );
        }

        #[tokio::test]
        async fn content_hash_mismatch_surfaces_typed_error_without_refetch() {
            let part = make_test_part(17);
            let (mut objects, entries) = encode_and_index(from_ref(&part));
            // Tamper with the stored bytes — content_hash on
            // the list entry no longer matches.
            let bytes = objects.values().next().expect("one obj").clone();
            let mut tampered = bytes.to_vec();
            let last = tampered.len() - 1;
            tampered[last] ^= 0xff;
            let uri = entries[0].uri.clone();
            objects.insert(uri, Bytes::from(tampered));
            let (_, fresh_entries) = encode_and_index(from_ref(&part));
            let list = fresh_list(fresh_entries);

            let storage = Arc::new(CountingMockStorage::new(objects));
            let manifest =
                build_manifest_with_loader(list, Arc::clone(&storage) as Arc<dyn StorageProvider>);

            let err = manifest
                .get_part_by_id(part.part_id)
                .await
                .expect_err("must reject tampered bytes");
            assert!(
                matches!(err, ManifestLoadError::ContentHashMismatch { .. }),
                "expected ContentHashMismatch, got {err:?}"
            );
            // Bad bytes are NOT auto-refetched. Retry returns
            // the same error. OnceCell behavior on Err futures
            // is implementation-defined (cached vs re-issued);
            // load-bearing assertion is just that retry does
            // not magically succeed.
            let _pre = storage.get_call_count();
            let err2 = manifest
                .get_part_by_id(part.part_id)
                .await
                .expect_err("must reject on retry too");
            assert!(matches!(
                err2,
                ManifestLoadError::ContentHashMismatch { .. }
            ));
        }

        #[tokio::test]
        async fn part_id_not_in_list_surfaces_typed_error() {
            let part = make_test_part(19);
            let (objects, entries) = encode_and_index(&[part]);
            let storage = Arc::new(CountingMockStorage::new(objects));
            let list = fresh_list(entries);
            let manifest =
                build_manifest_with_loader(list, Arc::clone(&storage) as Arc<dyn StorageProvider>);

            let stranger = PartId(Uuid::from_bytes([0xff; 16]));
            let err = manifest
                .get_part_by_id(stranger)
                .await
                .expect_err("must reject");
            assert!(
                matches!(err, ManifestLoadError::PartNotInList { .. }),
                "expected PartNotInList, got {err:?}"
            );
            assert_eq!(
                storage.get_call_count(),
                0,
                "missing-id check happens before any storage.get"
            );
        }

        #[tokio::test]
        async fn disk_cache_hit_serves_second_loader_without_storage_get() {
            // Two independent loaders sharing one on-disk manifest cache
            // (models a fresh manifest snapshot, or a process restart):
            // the first populates the cache from storage, the second
            // reads the part bytes off local disk with zero storage GETs.
            let part = make_test_part(23);
            let (objects, entries) = encode_and_index(from_ref(&part));
            let storage = Arc::new(CountingMockStorage::new(objects));
            let storage_dyn = Arc::clone(&storage) as Arc<dyn StorageProvider>;
            let list = fresh_list(entries);

            let cache_root = std::env::temp_dir()
                .join("infino-manifest-cache-loader-test-disk_cache_hit_second_loader");
            let _ = std::fs::remove_dir_all(&cache_root);
            let cache = ManifestDiskCache::new(cache_root.clone(), 1 << 20).expect("cache");

            // Loader A: cold — one storage GET, cache populated.
            let loader_a = ManifestPartLoader::new_with_cache(
                Arc::clone(&storage_dyn),
                &list,
                Some(Arc::clone(&cache)),
            );
            let a = loader_a.load(part.part_id).await.expect("first load");
            assert_eq!(a.part_id, part.part_id);
            assert_eq!(storage.get_call_count(), 1, "first loader fetches once");
            assert_eq!(cache.stats().n_entries, 1, "part bytes cached on disk");

            // Loader B: fresh loader, same cache — disk hit, no new GET.
            let loader_b = ManifestPartLoader::new_with_cache(
                Arc::clone(&storage_dyn),
                &list,
                Some(Arc::clone(&cache)),
            );
            let b = loader_b.load(part.part_id).await.expect("second load");
            assert_eq!(b.part_id, part.part_id);
            assert_eq!(
                storage.get_call_count(),
                1,
                "disk-cache hit ⇒ no additional storage.get"
            );
            assert!(cache.stats().n_hits >= 1, "recorded a cache hit");

            let _ = std::fs::remove_dir_all(&cache_root);
        }

        #[tokio::test]
        async fn loader_without_cache_always_hits_storage() {
            // Sanity: with no cache attached, each loader load is a
            // storage GET — confirms the cache is what removes them.
            let part = make_test_part(29);
            let (objects, entries) = encode_and_index(from_ref(&part));
            let storage = Arc::new(CountingMockStorage::new(objects));
            let storage_dyn = Arc::clone(&storage) as Arc<dyn StorageProvider>;
            let list = fresh_list(entries);

            let loader = ManifestPartLoader::new(Arc::clone(&storage_dyn), &list);
            loader.load(part.part_id).await.expect("load 1");
            loader.load(part.part_id).await.expect("load 2");
            assert_eq!(
                storage.get_call_count(),
                2,
                "no cache ⇒ every load round-trips to storage"
            );
        }

        #[tokio::test]
        async fn no_loader_attached_surfaces_typed_error() {
            // In-process-only manifest — ManifestSnapshot::empty has
            // no loader. Calling part() must error cleanly,
            // not panic.
            let manifest = ManifestSnapshot::empty(options_for_test());
            let err = manifest
                .get_part_by_id(PartId(Uuid::nil()))
                .await
                .expect_err("must error");
            assert!(
                matches!(err, ManifestLoadError::NoLoaderAttached),
                "expected NoLoaderAttached, got {err:?}"
            );
        }
    }

    // ============================================================
    // SuperfileUri path helpers, Debug formatters, and the
    // `add_sum_arrays` additive-sum helper (the scalar-stats build /
    // merge logic itself is tested on `ScalarStatsAgg` in `list.rs`).
    // ============================================================

    #[test]
    fn superfile_uri_path_helpers_share_the_same_uuid() {
        let uri = SuperfileUri(Uuid::from_u128(0x1234_5678));
        let id = uri.0;
        assert_eq!(uri.storage_path(), format!("data/seg-{id}.sf.parquet"));
        assert_eq!(uri.cache_filename(), format!("seg-{id}.sf.parquet"));
        assert_eq!(uri.cache_tmp_filename(), format!("seg-{id}.sf.parquet.tmp"));
    }

    #[test]
    fn manifest_debug_reports_counts() {
        let m = ManifestSnapshot::empty(opts()).with_appended(vec![seg_entry(Uuid::new_v4(), 3)]);
        let dbg = format!("{m:?}");
        assert!(dbg.contains("ManifestSnapshot"));
        assert!(dbg.contains("manifest_id"));
        assert!(dbg.contains("n_superfiles"));
        // No storage attached ⇒ has_loader false, has_list false.
        assert!(dbg.contains("has_loader"));
    }

    #[test]
    fn manifest_debug_with_list_reports_part_count() {
        // A ManifestSnapshot carrying a `list` exercises the Some-arm of the
        // `n_parts` closure in Debug (the empty-ManifestSnapshot test above
        // only hits the `unwrap_or(0)` None-arm).
        use list::{Manifest, PartitionStrategy};
        let entry = part::PartId::new_v4();
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: part::ContentHash([0u8; 32]),
            schema: Vec::new(),
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![list::ManifestPartEntry {
                part_id: entry,
                uri: "manifests/part-x".into(),
                n_superfiles: 0,
                size_bytes_compressed: 0,
                size_bytes_uncompressed: 0,
                content_hash: part::ContentHash([0u8; 32]),
                routing: None,
                id_range: (0, 0),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let m = ManifestSnapshot {
            superfile_list: SuperfileList::empty(opts()),
            list: Some(list),
            parts: DashMap::new(),
            loader: None,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        };
        let dbg = format!("{m:?}");
        assert!(dbg.contains("n_parts: 1"), "{dbg}");
        assert!(dbg.contains("has_list: true"), "{dbg}");
    }

    #[test]
    fn cluster_centroids_empty_is_empty_and_default_matches() {
        let cc = ClusterCentroids::empty();
        assert!(cc.is_empty());
        assert_eq!(cc.n_cent, 0);
        // A populated one is not empty.
        let cc = ClusterCentroids::from_fp32(2, 4, &[0.0; 8], vec![1, 1]);
        assert!(!cc.is_empty());
        assert_eq!(cc.n_cent, 2);
        assert_eq!(cc.dim, 4);
    }

    #[test]
    fn add_sum_arrays_handles_each_type_and_overflow() {
        use arrow_array::{Float64Array, Int64Array, UInt64Array};
        // Int64 + Int64.
        let r = add_sum_arrays(
            &(Arc::new(Int64Array::from(vec![3])) as ArrayRef),
            &(Arc::new(Int64Array::from(vec![4])) as ArrayRef),
        )
        .expect("int sum");
        assert_eq!(
            r.as_any()
                .downcast_ref::<Int64Array>()
                .expect("test")
                .value(0),
            7
        );
        // UInt64 + UInt64.
        let r = add_sum_arrays(
            &(Arc::new(UInt64Array::from(vec![3u64])) as ArrayRef),
            &(Arc::new(UInt64Array::from(vec![4u64])) as ArrayRef),
        )
        .expect("uint sum");
        assert_eq!(
            r.as_any()
                .downcast_ref::<UInt64Array>()
                .expect("test")
                .value(0),
            7
        );
        // Float64 + Float64.
        let r = add_sum_arrays(
            &(Arc::new(Float64Array::from(vec![1.5])) as ArrayRef),
            &(Arc::new(Float64Array::from(vec![2.5])) as ArrayRef),
        )
        .expect("float sum");
        assert!(
            (r.as_any()
                .downcast_ref::<Float64Array>()
                .expect("test")
                .value(0)
                - 4.0)
                .abs()
                < 1e-9
        );
        // Overflow → None.
        let r = add_sum_arrays(
            &(Arc::new(Int64Array::from(vec![i64::MAX])) as ArrayRef),
            &(Arc::new(Int64Array::from(vec![1])) as ArrayRef),
        );
        assert!(r.is_none(), "i64 overflow drops the stat");
        // Type mismatch → None.
        let r = add_sum_arrays(
            &(Arc::new(Int64Array::from(vec![1])) as ArrayRef),
            &(Arc::new(UInt64Array::from(vec![1u64])) as ArrayRef),
        );
        assert!(r.is_none(), "type mismatch drops the stat");
    }

    // ---- ManifestSnapshot::update-------------------------------------------
    /// Base builder for a synthetic superfile entry. `pk` is the on-disk
    /// `partition_key`: pass a stamped key to model a prior-commit entry (as
    /// stored in an existing part), or `Vec::new()` for an unstamped entry
    /// destined for `update()`, which derives and stamps the key itself.
    fn make_entry(docs: u64, pk: Vec<u8>, hint: Option<u32>) -> Arc<SuperfileEntry> {
        Arc::new(SuperfileEntry {
            birth_version: 0,
            superfile_id: uuid::Uuid::new_v4(),
            uri: SuperfileUri::new_v4(),
            n_docs: docs,
            id_min: 0,
            id_max: docs as i128 - 1,
            scalar_stats: Default::default(),
            fts_summary: Default::default(),
            vector_summary: Default::default(),
            partition_key: pk,
            partition_hint: hint,
            vector_layout: VectorLayout::Ivf,
            subsection_offsets: None,
        })
    }

    /// Stamped prior-commit entry (non-empty `partition_key`), for placing
    /// into existing on-disk parts.
    fn make_superfile_entry(docs: u64, pk: Vec<u8>) -> Arc<SuperfileEntry> {
        make_entry(docs, pk, None)
    }

    /// Builds an UNSTAMPED entry (empty partition_key) for passing to
    /// `update()`, which derives and stamps the key. Entries that model a
    /// prior commit (placed into existing parts) keep `make_superfile_entry*`,
    /// which carries the already-stamped key.
    fn make_new_entry(docs: u64) -> Arc<SuperfileEntry> {
        make_entry(docs, Vec::new(), None)
    }

    fn hash_bucket_0_pk() -> Vec<u8> {
        // Hash partition with n_buckets=1 encodes to [0, 0, 0, 0] in little-endian
        vec![0, 0, 0, 0]
    }

    fn simple_schema() -> Arc<Schema> {
        Arc::new(Schema::new(vec![Field::new(
            "text",
            DataType::LargeUtf8,
            false,
        )]))
    }

    fn make_opts() -> Arc<SupertableOptions> {
        SupertableOptions::new(simple_schema(), vec![], vec![], None)
            .map(Arc::new)
            .expect("valid options")
    }

    fn empty_manifest(opts: &Arc<SupertableOptions>) -> Arc<ManifestSnapshot> {
        Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList::empty(opts.clone()),
            list: Some(Manifest {
                drained_ranges: Default::default(),
                global_vector_index: None,
                tombstone_seqs: Default::default(),
                superseded_cells: Default::default(),
                format_version: list::FORMAT_VERSION.into(),
                manifest_id: 0,
                options_hash: ContentHash([0u8; 32]),
                schema: vec![],
                id_column: "_id".into(),
                fts_columns: vec![],
                vector_columns: vec![],
                partition_strategy: PartitionStrategy::Hash {
                    column: "_id".into(),
                    n_buckets: 1,
                },
                vector_index_storage_prefix: None,
                deleted_user_ids_inline: None,
                slow_vector_state_uri: None,
                slow_vector_state_content_hash: None,
                slow_vector_state_centroids: None,
                parts: vec![],
            }),
            parts: DashMap::new(),
            loader: None,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        })
    }

    /// Slow-CAS section semantics: `with_slow_vector_state` stamps the ref
    /// (bumping manifest_id, preserving membership); `update` — the
    /// membership-change path — CLEARS it in the successor list; the
    /// deleted-ids stamper preserves it (list-only churn keeps residency).
    #[tokio::test]
    async fn slow_vector_state_ref_stamp_clear_and_preserve() {
        let opts = make_opts();
        let manifest = empty_manifest(&opts);
        assert!(manifest.slow_vector_state_blob().is_none());

        let hash = ContentHash([3u8; 32]);
        let centroids = RoutingRef {
            uri: "slow-vector-state/state-c.bin".into(),
            content_hash: ContentHash([5u8; 32]),
        };
        let stamped = manifest.with_slow_vector_state(
            "slow-vector-state/state-x.bin".into(),
            hash,
            centroids.clone(),
        );
        let (uri, got_hash) = stamped.slow_vector_state_blob().expect("ref stamped");
        assert_eq!(uri, "slow-vector-state/state-x.bin");
        assert_eq!(got_hash, hash);
        assert_eq!(
            stamped.slow_vector_state_centroids_blob(),
            Some(&centroids),
            "centroid-section sibling stamped with the state ref"
        );
        assert_eq!(stamped.get_manifest_id(), manifest.get_next_manifest_id());
        assert_eq!(
            stamped.get_all_superfiles().len(),
            manifest.get_all_superfiles().len(),
            "stamp must not change membership"
        );

        // A deleted-ids stamp (list-only churn: the user-delete path) must
        // NOT disturb the slow-state ref — this is the residency invariant.
        let deleted_stamped = stamped.with_deleted_user_ids(Vec::new());
        assert!(
            deleted_stamped.slow_vector_state_blob().is_some(),
            "deleted-ids stamp must preserve the slow-state ref"
        );
        assert!(
            deleted_stamped.slow_vector_state_centroids_blob().is_some(),
            "deleted-ids stamp must preserve the centroid-section ref"
        );

        // A membership change (update) must CLEAR the ref: the blob no
        // longer describes the new membership; only maintenance restamps.
        let new_entry = make_new_entry(100);
        let (updated, _parts) = stamped
            .update(from_ref(&new_entry), &[])
            .await
            .expect("update");
        assert!(
            updated.slow_vector_state_blob().is_none(),
            "membership change must clear the slow-state ref"
        );
        assert!(
            updated.slow_vector_state_centroids_blob().is_none(),
            "membership change must clear the centroid-section ref"
        );
    }

    /// A manifest whose list carries a slow-state ref hydrated its
    /// membership from the blob — hidden manifests write NO parts. The
    /// undrained load must trust the resident flat view there: summing
    /// `parts[].n_superfiles` gives zero, and the part fan it used to
    /// trigger flattened zero parts into zero entries, silently dropping
    /// every undrained superfile.
    #[tokio::test]
    async fn undrained_load_trusts_resident_view_with_slow_state_ref() {
        let opts = make_opts();
        let entries = vec![
            make_superfile_entry(100, hash_bucket_0_pk()),
            make_superfile_entry(50, hash_bucket_0_pk()),
        ];
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: Some("slow-vector-state/state-abc.bin".into()),
            slow_vector_state_content_hash: Some(ContentHash([7u8; 32])),
            slow_vector_state_centroids: None,
            parts: Vec::new(),
        };
        // Storage must be attached: `new` only keeps the list (and builds
        // the part loader this test's fan path needs) when it is.
        let (_dir, storage) = local_storage();
        let manifest = ManifestSnapshot::new(1, opts, entries.clone(), Some(storage), Some(list));
        let drained = list::DrainedVersionRanges::default();
        let got = manifest
            .get_undrained_superfiles_loaded(&drained)
            .await
            .expect("undrained load");
        assert_eq!(
            got.len(),
            entries.len(),
            "resident blob-hydrated membership must be returned, not the empty part fan"
        );
    }

    #[tokio::test]
    async fn update_fresh_start_cold_partition_should_create_entry() {
        let opts = make_opts();
        let old_manifest = empty_manifest(&opts);

        let new_entry = make_new_entry(100);
        let new_entries = vec![new_entry];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(parts[0].part.superfiles.len(), 1);
        assert_eq!(parts[0].part.superfiles[0].n_docs, 100);
    }

    #[tokio::test]
    async fn update_fresh_start_multiple_cold_partitions_should_create_entries() {
        // Multiple new entries in a single commit all land in one table-level
        // part (well under the default target).
        let opts = make_opts();
        let old_manifest = empty_manifest(&opts);

        let entry1 = make_new_entry(100);
        let entry2 = make_new_entry(200);
        let new_entries = vec![entry1, entry2];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, 2);
        assert_eq!(parts[0].part.superfiles.len(), 2);
        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 300);
    }

    fn local_storage() -> (TempDir, Arc<dyn StorageProvider>) {
        let dir = TempDir::new().expect("tempdir");
        let store: Arc<dyn StorageProvider> =
            Arc::new(LocalFsStorageProvider::new(dir.path()).expect("local"));
        (dir, store)
    }

    /// Number of parts whose `OnceCell` actually holds decoded bytes —
    /// distinct from `get_num_parts_loaded()`, which counts map slots (the
    /// lazy and hydrated branches pre-insert empty cells for every part).
    fn n_parts_initialized(m: &ManifestSnapshot) -> usize {
        m.parts
            .iter()
            .filter(|kv| kv.value().get().is_some())
            .count()
    }

    /// Persist one part (two entries) + a list referencing it + the pointer.
    /// `slow_ref` optionally stamps the list's slow-CAS section, letting the
    /// hydration tests choose a valid ref, a corrupt one, or none.
    async fn persist_two_entry_table(
        storage: &Arc<dyn StorageProvider>,
        slow_ref: Option<(String, ContentHash)>,
    ) -> Vec<Arc<SuperfileEntry>> {
        let entries = vec![
            make_superfile_entry(100, hash_bucket_0_pk()),
            make_superfile_entry(50, hash_bucket_0_pk()),
        ];
        let part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: entries.clone(),
        };
        let pw = write_manifest_part(storage.as_ref(), &part)
            .await
            .expect("write part");
        let (slow_uri, slow_hash) = match slow_ref {
            Some((u, h)) => (Some(u), Some(h)),
            None => (None, None),
        };
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: slow_uri,
            slow_vector_state_content_hash: slow_hash,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 99),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let lw = write_manifest(storage.as_ref(), &list)
            .await
            .expect("write list");
        write_pointer(
            storage.as_ref(),
            &PointerFile {
                manifest_id: 1,
                manifest_uri: lw.uri,
                content_hash: lw.content_hash,
            },
            None,
        )
        .await
        .expect("write pointer");
        entries
    }

    /// Hydration: a list carrying a verified slow-state ref builds the flat
    /// view from the blob with ZERO part loads; the parts stay lazily
    /// loadable for maintenance.
    #[tokio::test]
    async fn load_hydrates_flat_view_from_slow_state_blob() {
        let opts = make_opts();
        let (_dir, storage) = local_storage();
        let entries = vec![
            make_superfile_entry(100, hash_bucket_0_pk()),
            make_superfile_entry(50, hash_bucket_0_pk()),
        ];
        let published = slow_vector_state::write_state(storage.as_ref(), &entries, None)
            .await
            .expect("write blob");
        let (blob_uri, blob_hash) = (published.uri, published.content_hash);
        // Rebuild the same membership durably with the ref stamped.
        let (_dir2, storage2) = local_storage();
        let _ = _dir2;
        drop(storage2); // single-storage test; helper writes to `storage`.
        let persisted = persist_two_entry_table(&storage, Some((blob_uri, blob_hash))).await;

        let loaded = ManifestSnapshot::load(None, Arc::clone(&storage), Some(opts))
            .await
            .expect("load");
        assert_eq!(loaded.superfiles.len(), 2);
        let want: HashSet<Uuid> = persisted.iter().map(|e| e.superfile_id).collect();
        let got: HashSet<Uuid> = loaded.superfiles.iter().map(|e| e.superfile_id).collect();
        assert_eq!(
            got.len(),
            2,
            "blob-hydrated flat view must carry both entries"
        );
        assert_eq!(want.len(), 2);
        assert_eq!(
            n_parts_initialized(&loaded),
            0,
            "hydration must not fetch any manifest part"
        );
        assert!(loaded.slow_vector_state_blob().is_some());
    }

    /// Dim for the routing-hydration fixture summaries.
    const ROUTING_TEST_DIM: usize = 16;
    /// Rot seed for the routing-hydration fixture slabs.
    const ROUTING_TEST_ROT_SEED: u64 = 5;

    /// Stamped entry carrying a one-cell vector summary whose admit slab is
    /// built — the shape drain-published entries have at republish time.
    fn make_summary_entry(docs: u64) -> Arc<SuperfileEntry> {
        let base = make_superfile_entry(docs, hash_bucket_0_pk());
        let mut entry = (*base).clone();
        let mut flat = vec![0.0f32; 2 * ROUTING_TEST_DIM];
        flat[0] = 1.0;
        flat[ROUTING_TEST_DIM + 1] = -1.0;
        let clusters = ClusterCentroids::from_fp32(2, ROUTING_TEST_DIM as u32, &flat, vec![3, 4]);
        clusters.prewarm_admit_codes(
            &RandomRotation::new(ROUTING_TEST_DIM, ROUTING_TEST_ROT_SEED),
            &BitQuantizer::new(ROUTING_TEST_DIM),
            ROUTING_TEST_ROT_SEED,
        );
        entry.vector_summary.insert(
            "emb".into(),
            VectorSummary {
                centroid: vec![0.5; ROUTING_TEST_DIM],
                cells: vec![CellVectorSummary {
                    cell_id: Some(0),
                    clusters,
                }],
            },
        );
        Arc::new(entry)
    }

    /// Two-object slow-CAS model: the state blob is routing-shaped, so
    /// EVERY consumer — knob on or off — hydrates stripped entries (no
    /// fp32) with the write-time slab seeded; exact centroid scores come
    /// from the published section. The fixture options carry no vector
    /// columns, so the hydration-time strip pass is inert — stripped
    /// entries can only have come from the blob's wire form itself.
    #[tokio::test]
    async fn state_blob_hydrates_stripped_for_all_consumers() {
        let (_dir, storage) = local_storage();
        let entries = vec![make_summary_entry(100), make_summary_entry(50)];
        let published = slow_vector_state::write_state(storage.as_ref(), &entries, None)
            .await
            .expect("write blobs");
        persist_two_entry_table(
            &storage,
            Some((published.uri.clone(), published.content_hash)),
        )
        .await;

        let consumer_opts = |knob: bool| {
            Arc::new(
                SupertableOptions::new(simple_schema(), vec![], vec![], None)
                    .expect("valid options")
                    .with_summary_centroids_from_superfiles(knob),
            )
        };
        let resident = |m: &ManifestSnapshot| {
            m.superfiles
                .iter()
                .map(|e| {
                    let clusters = &e.vector_summary["emb"].cells[0].clusters;
                    assert!(clusters.admit_codes_built().is_some(), "slab always rides");
                    clusters.vectors_resident()
                })
                .collect::<Vec<bool>>()
        };

        let knob_on = ManifestSnapshot::load(None, Arc::clone(&storage), Some(consumer_opts(true)))
            .await
            .expect("knob-on load");
        assert_eq!(
            resident(&knob_on),
            vec![false, false],
            "the routing-shaped state blob hydrates stripped entries"
        );

        let knob_off =
            ManifestSnapshot::load(None, Arc::clone(&storage), Some(consumer_opts(false)))
                .await
                .expect("knob-off load");
        assert_eq!(
            resident(&knob_off),
            vec![false, false],
            "knob-off hydrates the same routing-shaped blob — fp32 lives in the section"
        );
    }

    /// Persist a one-part table whose entries carry vector summaries. The
    /// part ships in both wire forms; `with_routing` controls whether the
    /// list entry stamps the sibling ref (absent models a pre-sibling
    /// manifest). No slow-state ref, so hydration goes through the part
    /// loader — the user-table shape.
    async fn persist_summary_part_table(storage: &Arc<dyn StorageProvider>, with_routing: bool) {
        let entries = vec![make_summary_entry(100), make_summary_entry(50)];
        let part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: entries,
        };
        let full = part::encode(&part);
        let full_hash = ContentHash::of(&full);
        let routing = part::encode_with_mode(&part, SummaryWireMode::RoutingOnly);
        let routing_hash = ContentHash::of(&routing);
        assert!(
            routing.len() < full.len(),
            "routing part must shed the fp32 payload ({} vs {} bytes)",
            routing.len(),
            full.len()
        );
        write_part_bytes(storage.as_ref(), &full)
            .await
            .expect("put full part");
        write_part_bytes(storage.as_ref(), &routing)
            .await
            .expect("put routing part");
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: part.part_id,
                uri: part_uri(&full_hash),
                content_hash: full_hash,
                routing: with_routing.then(|| RoutingRef {
                    uri: part_uri(&routing_hash),
                    content_hash: routing_hash,
                }),
                size_bytes_compressed: full.len() as u64,
                size_bytes_uncompressed: full.len() as u64,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let lw = write_manifest(storage.as_ref(), &list)
            .await
            .expect("write list");
        write_pointer(
            storage.as_ref(),
            &PointerFile {
                manifest_id: 1,
                manifest_uri: lw.uri,
                content_hash: lw.content_hash,
            },
            None,
        )
        .await
        .expect("write pointer");
    }

    /// The user-table shape (no slow-state ref, hydration through the part
    /// loader): consumer memory mode loads each part's routing sibling —
    /// summaries arrive stripped with the write-time slab, without
    /// downloading the fp32 payload. Knob-off loads (writers) and knob-on
    /// loads of pre-sibling manifests keep the full part. The fixture
    /// options carry no vector columns, so the strip pass is inert and a
    /// stripped summary can only have come from the routing part.
    #[tokio::test]
    async fn consumer_mode_hydrates_parts_from_routing_sibling() {
        let consumer_opts = |knob: bool| {
            Arc::new(
                SupertableOptions::new(simple_schema(), vec![], vec![], None)
                    .expect("valid options")
                    .with_summary_centroids_from_superfiles(knob),
            )
        };
        // (fp32 resident, slab present) per entry. The routing wire is the
        // slab's ONLY home now: full-part decodes carry fp32 and no slab
        // (real tables rebuild writer-side slabs in the hydration prewarm;
        // this fixture declares no vector columns, so the prewarm is inert).
        let shape = |m: &ManifestSnapshot| {
            m.superfiles
                .iter()
                .map(|e| {
                    let clusters = &e.vector_summary["emb"].cells[0].clusters;
                    (
                        clusters.vectors_resident(),
                        clusters.admit_codes_built().is_some(),
                    )
                })
                .collect::<Vec<(bool, bool)>>()
        };

        let (_dir, storage) = local_storage();
        persist_summary_part_table(&storage, true).await;
        let knob_on = ManifestSnapshot::load(None, Arc::clone(&storage), Some(consumer_opts(true)))
            .await
            .expect("knob-on load");
        assert_eq!(
            shape(&knob_on),
            vec![(false, true), (false, true)],
            "knob-on consumer must hydrate stripped entries (slab riding) from the routing part"
        );
        let knob_off =
            ManifestSnapshot::load(None, Arc::clone(&storage), Some(consumer_opts(false)))
                .await
                .expect("knob-off load");
        assert_eq!(
            shape(&knob_off),
            vec![(true, false), (true, false)],
            "knob-off load must keep the full part's resident fp32 (slab rebuilt by prewarm \
             on real tables)"
        );

        // Pre-sibling manifest: knob-on falls back to the full part.
        let (_dir2, storage2) = local_storage();
        persist_summary_part_table(&storage2, false).await;
        let fallback =
            ManifestSnapshot::load(None, Arc::clone(&storage2), Some(consumer_opts(true)))
                .await
                .expect("fallback load");
        assert_eq!(
            shape(&fallback),
            vec![(true, false), (true, false)],
            "knob-on without a routing ref must fall back to the full part"
        );
    }

    /// [`rebuild_part_and_entry`] stamps the routing sibling on USER
    /// manifests: the entry's ref addresses exactly the sibling bytes
    /// returned for the commit PUT. Hidden (VectorCell) manifests write
    /// the part routing-shaped and skip the sibling entirely — the
    /// primary IS the slim form and fp32 lives in the slow-CAS section.
    #[tokio::test]
    async fn rebuild_part_and_entry_stamps_routing_sibling() {
        let (entry, encoded_part) =
            rebuild_part_and_entry(vec![], vec![make_summary_entry(10)], None, false);
        let routing = entry.routing.expect("sibling stamped");
        let routing_encoded = encoded_part
            .routing_encoded
            .as_ref()
            .expect("user part carries sibling bytes");
        assert_eq!(
            routing.content_hash,
            ContentHash::of(routing_encoded),
            "entry ref must address the returned sibling bytes"
        );
        assert_eq!(routing.uri, part_uri(&routing.content_hash));
        assert_eq!(entry.content_hash, ContentHash::of(&encoded_part.encoded));
        assert!(
            routing_encoded.len() < encoded_part.encoded.len(),
            "sibling must shed the fp32 payload"
        );

        let (hidden_entry, hidden_encoded) =
            rebuild_part_and_entry(vec![], vec![make_summary_entry(10)], None, true);
        assert!(
            hidden_entry.routing.is_none(),
            "hidden part must not stamp a sibling — its primary is routing-shaped"
        );
        assert!(
            hidden_encoded.routing_encoded.is_none(),
            "hidden part must not carry sibling bytes"
        );
        assert!(
            hidden_encoded.encoded.len() < encoded_part.encoded.len(),
            "hidden primary must shed the fp32 payload ({} vs {} bytes)",
            hidden_encoded.encoded.len(),
            encoded_part.encoded.len()
        );
    }

    /// Residency invariant: a refresh whose slow-state ref is unchanged
    /// (list-only churn — here a deleted-ids stamp) reuses the decoded
    /// entries — same `Arc`s, zero part loads, zero blob refetch.
    #[tokio::test]
    async fn refresh_with_unchanged_slow_ref_reuses_entries() {
        let opts = make_opts();
        let (_dir, storage) = local_storage();
        let entries = vec![
            make_superfile_entry(100, hash_bucket_0_pk()),
            make_superfile_entry(50, hash_bucket_0_pk()),
        ];
        let published = slow_vector_state::write_state(storage.as_ref(), &entries, None)
            .await
            .expect("write blob");
        let (blob_uri, blob_hash) = (published.uri, published.content_hash);
        persist_two_entry_table(&storage, Some((blob_uri, blob_hash))).await;

        let a = ManifestSnapshot::load(None, Arc::clone(&storage), Some(Arc::clone(&opts)))
            .await
            .expect("load A");
        // List-only churn: stamp deleted-ids (preserves the slow ref) and
        // publish it so the pointer advances past A.
        let (_, meta) = read_pointer(storage.as_ref())
            .await
            .expect("read pointer")
            .expect("pointer present");
        let etag = meta.etag.expect("localfs pointer etag");
        let stamped = a.with_deleted_user_ids(Vec::new());
        stamped
            .write(storage.as_ref(), Some(etag.as_str()), &[])
            .await
            .expect("stamp publish");

        let b = ManifestSnapshot::load(Some(Arc::clone(&a)), Arc::clone(&storage), None)
            .await
            .expect("refresh");
        assert_eq!(b.get_manifest_id(), a.get_manifest_id() + 1);
        assert!(b.slow_vector_state_blob().is_some(), "ref preserved");
        assert_eq!(b.superfiles.len(), a.superfiles.len());
        for (be, ae) in b.superfiles.iter().zip(a.superfiles.iter()) {
            assert!(
                Arc::ptr_eq(be, ae),
                "unchanged ref must reuse the SAME decoded entries — \
                 the centroid state never leaves memory on list-only churn"
            );
        }
        assert_eq!(
            n_parts_initialized(&b),
            0,
            "refresh with unchanged ref must not fetch parts"
        );
    }

    /// A list that carries a slow-state ref whose blob is missing or
    /// corrupt is a CORRUPT manifest: the load must raise
    /// [`ManifestLoadError::SlowStateHydration`] — never silently degrade
    /// to the part fan. (The old quiet fallback concealed hydration
    /// defects behind normal-looking, slower opens.)
    #[tokio::test]
    async fn load_with_corrupt_slow_ref_raises_hydration_error() {
        let opts = make_opts();
        let (_dir, storage) = local_storage();
        let bogus = (
            "slow-vector-state/state-missing.bin".to_string(),
            ContentHash([9u8; 32]),
        );
        persist_two_entry_table(&storage, Some(bogus)).await;

        let err = ManifestSnapshot::load(None, Arc::clone(&storage), Some(opts))
            .await
            .expect_err("corrupt slow-state ref must fail the load loudly");
        assert!(
            matches!(err, ManifestLoadError::SlowStateHydration(_)),
            "expected SlowStateHydration, got: {err:?}"
        );
    }

    #[tokio::test]
    async fn update_add_to_existing_partition_rewrites_part() {
        // Adding a new entry to the single existing part rewrites it in place.
        let opts = make_opts();

        let (_dir, storage) = local_storage();

        let old_superfile = make_superfile_entry(100, hash_bucket_0_pk());
        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![old_superfile.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 1,
                id_range: (0, 99),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);

        let parts = DashMap::new();
        parts.insert(
            pw.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![old_superfile],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Add new entry to the SAME partition (not a new/cold partition)
        let new_entry = make_new_entry(50);
        let new_entries = vec![new_entry];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Should have 1 list entry (rewritten old one)
        assert_eq!(list_entries.len(), 1);
        // Should have 1 new part (the rewritten one)
        assert_eq!(parts.len(), 1);

        assert_eq!(list_entries[0].n_superfiles, 2);

        // Part should have combined superfiles
        assert_eq!(parts[0].part.superfiles.len(), 2);
        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 150);
    }

    #[tokio::test]
    async fn update_leaves_unchanged_parts_untouched() {
        // Start with three parts, two superfiles each, forming one table-level
        // lineage in list order: [part_0, part_1, part_2]. New entries append to
        // the LAST part (the latest), which here has room for one more superfile
        // (target = 3, so 2 + 1 = 3 stays within target → rewrite, no split). We
        // commit a single new superfile. After update ONLY the last part
        // changes; the two earlier parts must carry over byte-for-byte — same
        // part_id, uri, and content_hash — and must NOT be re-emitted into
        // `parts_to_write` (no re-encode, no PUT).
        const SUPERFILES_PER_PART: u64 = 2;
        const TARGET_SUPERFILES_PER_PART: u64 = 3;

        let (_dir, storage) = local_storage();

        // Attach storage so the manifests `update` derives also carry
        // a loader — the second (removal) phase loads carried-over parts
        // (part_0, part_1) back from storage.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = TARGET_SUPERFILES_PER_PART;
        let opts = Arc::new(base_opts.with_storage(storage.clone()));

        let pk_a = hash2_pk(0);
        let pk_b = hash2_pk(1);

        // Helper: build a 2-superfile part for a partition and persist it.
        async fn two_superfile_part(
            storage: &dyn StorageProvider,
            pk: &[u8],
            hint: u32,
            docs: [u64; 2],
        ) -> (ManifestPart, PartWriteResult) {
            let part = ManifestPart {
                format_version: part::FORMAT_VERSION.into(),
                part_id: PartId::new_v4(),
                superfiles: vec![
                    make_superfile_entry_hinted(docs[0], pk.to_vec(), hint),
                    make_superfile_entry_hinted(docs[1], pk.to_vec(), hint),
                ],
            };
            let pw = write_manifest_part(storage, &part)
                .await
                .expect("write part");
            (part, pw)
        }

        let (part_a_old, pw_a_old) =
            two_superfile_part(storage.as_ref(), &pk_a, 0, [100, 110]).await;
        let (_part_a_latest, pw_a_latest) =
            two_superfile_part(storage.as_ref(), &pk_a, 0, [120, 130]).await;
        let (part_b, pw_b) = two_superfile_part(storage.as_ref(), &pk_b, 1, [200, 210]).await;

        // Build a list entry mirroring a persisted part.
        let entry_for = |pw: &PartWriteResult| -> ManifestPartEntry {
            ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri.clone(),
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: SUPERFILES_PER_PART,
                id_range: (0, 0),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }
        };

        // List order: [part_0, part_1, part_2]. part_2 is the last
        // (rewrite candidate under option-B); part_0 and part_1 are
        // frozen earlier parts.
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 2,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                entry_for(&pw_a_old),
                entry_for(&pw_a_latest),
                entry_for(&pw_b),
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);

        // Only the latest (last) part is needed in-cache for the rewrite to
        // load + combine; the loader serves the rest from storage.
        let parts_map = DashMap::new();
        parts_map.insert(
            part_b.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_b.clone())))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: part_a_old
                    .superfiles
                    .iter()
                    .chain(part_b.superfiles.iter())
                    .cloned()
                    .collect(),
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Commit one new superfile. Keep `new_entry` around — the second
        // phase below removes it again.
        let new_entry = make_new_entry_hinted(140, 0);
        let (new_manifest, parts_to_write) = old_manifest
            .update(from_ref(&new_entry), &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Three list entries remain (part_0 carried over, part_1 carried over,
        // part_2 rewritten in place), and only ONE part is re-emitted for
        // writing — the rewritten last part.
        assert_eq!(list_entries.len(), 3, "list entry count");
        assert_eq!(
            parts_to_write.len(),
            1,
            "only the rewritten last part should be re-emitted; \
             unchanged parts must not be re-encoded/PUT",
        );

        // Locate the carried-over entries by their original part_id and
        // assert they are byte-for-byte identical to what was persisted.
        let find = |part_id: PartId| {
            list_entries
                .iter()
                .find(|e| e.part_id == part_id)
                .unwrap_or_else(|| panic!("entry for part {part_id:?} missing after update"))
        };

        // part_0 carries over unchanged.
        let part0_after = find(pw_a_old.part_id);
        assert_eq!(part0_after.uri, pw_a_old.uri, "carried-over part_0 uri");
        assert_eq!(
            part0_after.content_hash, pw_a_old.content_hash,
            "carried-over part_0 content_hash",
        );
        assert_eq!(part0_after.n_superfiles, SUPERFILES_PER_PART);

        // part_1 carries over unchanged too.
        let part1_after = find(pw_a_latest.part_id);
        assert_eq!(part1_after.uri, pw_a_latest.uri, "carried-over part_1 uri");
        assert_eq!(
            part1_after.content_hash, pw_a_latest.content_hash,
            "carried-over part_1 content_hash",
        );
        assert_eq!(part1_after.n_superfiles, SUPERFILES_PER_PART);

        // The one re-emitted part is the rewritten last part: it now holds the
        // original two superfiles plus the new one.
        assert_eq!(
            parts_to_write[0].part.superfiles.len(),
            (SUPERFILES_PER_PART + 1) as usize,
            "rewritten last part should hold its 2 superfiles + the new one",
        );
        // And the original last part_id is gone from the list (it was
        // rewritten, not carried over).
        assert!(
            !list_entries.iter().any(|e| e.part_id == pw_b.part_id),
            "the rewritten last part is replaced, so its old part_id must not survive",
        );
        // The rewritten (last) entry is the one that is neither of the two
        // carried-over parts — it holds the combined superfiles.
        let rewritten_after = list_entries
            .iter()
            .find(|e| e.part_id != pw_a_old.part_id && e.part_id != pw_a_latest.part_id)
            .expect("rewritten last entry present after the add");
        assert_eq!(rewritten_after.n_superfiles, SUPERFILES_PER_PART + 1);

        // ---- Second phase: remove the superfile we just added --------
        //
        // The new superfile lives in the rewritten last part. Remove it. Only
        // that part should change. The two earlier parts never held the removed
        // superfile — both must carry over byte-for-byte.
        //
        // Capture the rewritten last part's identity (the part the removal will
        // legitimately rebuild).
        let rewritten_v1_part_id = rewritten_after.part_id;

        let (after_removal, removal_parts) = new_manifest
            .update(&[], from_ref(&new_entry))
            .await
            .expect("update removal");
        let entries_after = after_removal.get_all_list_entries();

        assert_eq!(entries_after.len(), 3, "list entry count after removal");

        // The part we removed from MUST change: its v1 part_id is gone.
        assert!(
            !entries_after
                .iter()
                .any(|e| e.part_id == rewritten_v1_part_id),
            "the part we removed a superfile from must be rebuilt (new part_id)",
        );

        // part_0 held none of the removed superfile — same part identity.
        let part0_after_removal = entries_after
            .iter()
            .find(|e| e.part_id == pw_a_old.part_id)
            .expect("part_0 must survive the removal unchanged");
        assert_eq!(
            part0_after_removal.uri, pw_a_old.uri,
            "part_0 uri after removal",
        );
        assert_eq!(
            part0_after_removal.content_hash, pw_a_old.content_hash,
            "part_0 content_hash after removal",
        );

        // part_1 held none of the removed superfile — same part identity.
        let part1_after_removal = entries_after
            .iter()
            .find(|e| e.part_id == pw_a_latest.part_id)
            .expect("part_1 must survive the removal unchanged");
        assert_eq!(
            part1_after_removal.uri, pw_a_latest.uri,
            "part_1 uri after removal",
        );
        assert_eq!(
            part1_after_removal.content_hash, pw_a_latest.content_hash,
            "part_1 content_hash after removal",
        );

        // Only the part that actually lost a superfile should be
        // re-emitted for writing.
        assert_eq!(
            removal_parts.len(),
            1,
            "only the part we removed from should be rewritten; unchanged parts \
             must not be re-encoded/PUT",
        );
    }

    #[tokio::test]
    async fn update_rewrite_partition_within_target() {
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 3;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf1 = make_superfile_entry(100, hash_bucket_0_pk());
        let sf2 = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf1.clone(), sf2.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);

        let parts = DashMap::new();
        parts.insert(
            pw.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf1, sf2],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Add 1 new superfile to same partition (2 + 1 = 3, within target)
        let new_entry = make_new_entry(75);
        let new_entries = vec![new_entry];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Rewrite case: 1 list entry (old entry replaced), 1 new part
        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);

        assert_eq!(list_entries[0].n_superfiles, 3);

        // Part should have all 3 superfiles combined
        let part = &parts[0];
        assert_eq!(part.part.superfiles.len(), 3);
        // Verify combined doc count
        let total_docs: u64 = part.part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 325); // 100 + 150 + 75
    }

    #[tokio::test]
    async fn update_split_partition_exceeds_target() {
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 2;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf1 = make_superfile_entry(100, hash_bucket_0_pk());
        let sf2 = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf1.clone(), sf2.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);

        let parts = DashMap::new();
        parts.insert(
            pw.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf1, sf2],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Add 2 new superfiles to same partition (2 + 2 = 4, exceeds target of 2)
        let new_entry1 = make_new_entry(75);
        let new_entry2 = make_new_entry(80);
        let new_entries = vec![new_entry1, new_entry2];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Split case: 2 list entries (old + fresh for split), 1 new part (fresh)
        assert_eq!(list_entries.len(), 2);
        assert_eq!(parts.len(), 1);

        // First entry is the carried-over existing part; the second is the
        // fresh split part holding the new superfiles.

        // First entry (old) should still have original superfiles
        assert_eq!(list_entries[0].n_superfiles, 2);

        // Second entry (fresh) should have the new superfiles
        assert_eq!(list_entries[1].n_superfiles, 2);

        // The one new part should have exactly the 2 new superfiles
        let part = &parts[0];
        assert_eq!(part.part.superfiles.len(), 2);
        let total_docs: u64 = part.part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 155); // 75 + 80
    }

    /// Hinted counterpart to [`make_superfile_entry`] — stamped, with a
    /// routing `partition_hint`.
    fn make_superfile_entry_hinted(docs: u64, pk: Vec<u8>, hint: u32) -> Arc<SuperfileEntry> {
        make_entry(docs, pk, Some(hint))
    }

    /// Hinted counterpart to [`make_new_entry`] — UNSTAMPED (empty
    /// partition_key), carrying a routing `partition_hint` for `update()` to
    /// derive the key from.
    fn make_new_entry_hinted(docs: u64, hint: u32) -> Arc<SuperfileEntry> {
        make_entry(docs, Vec::new(), Some(hint))
    }

    fn hash2_pk(bucket: u32) -> Vec<u8> {
        bucket.to_le_bytes().to_vec()
    }

    #[tokio::test]
    async fn update_split_partition_exceeds_size_threshold() {
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 10_000;
        // Any real encoded part is bigger than 1 byte, so the latest part is
        // always considered at-cap.
        base_opts.part_size_threshold_bytes = 1;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf1 = make_superfile_entry(100, hash_bucket_0_pk());
        let sf2 = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf1.clone(), sf2.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri.clone(),
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let frozen_part_id = pw.part_id;
        let frozen_hash = pw.content_hash;
        let loader = ManifestPartLoader::new(storage, &list);

        let parts = DashMap::new();
        parts.insert(
            pw.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf1, sf2],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // 2 + 1 = 3 superfiles — far under the 10_000 count target, so only
        // the size cap can force the split.
        let new_entries = vec![make_new_entry(75)];
        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(
            list_entries.len(),
            2,
            "size-capped latest part must freeze; new entries go to a fresh part"
        );
        assert_eq!(parts.len(), 1, "only the fresh part is encoded + written");

        // The frozen part carries over byte-identical: same id, same hash —
        // no re-encode, no re-PUT.
        assert_eq!(list_entries[0].part_id, frozen_part_id);
        assert_eq!(list_entries[0].content_hash, frozen_hash);
        assert_eq!(list_entries[0].n_superfiles, 2);

        // The fresh part holds exactly the new superfile.
        assert_eq!(list_entries[1].n_superfiles, 1);
        assert_eq!(parts[0].part.superfiles.len(), 1);
        assert_eq!(parts[0].part.superfiles[0].n_docs, 75);
    }

    #[tokio::test]
    async fn update_older_entry_preserved_when_latest_rewritten() {
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 2;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf_old = make_superfile_entry(100, hash_bucket_0_pk());
        let sf_latest = make_superfile_entry(150, hash_bucket_0_pk());

        let part_old = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_old.clone()],
        };
        let pw_old = write_manifest_part(storage.as_ref(), &part_old)
            .await
            .expect("write part_old");

        let part_latest = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_latest.clone()],
        };
        let pw_latest = write_manifest_part(storage.as_ref(), &part_latest)
            .await
            .expect("write part_latest");

        // Old manifest with TWO entries for same partition (result of prior split)
        // Second one is the "latest" for that partition
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_old.part_id,
                    uri: pw_old.uri.clone(),
                    content_hash: pw_old.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_old.size_bytes_compressed,
                    size_bytes_uncompressed: pw_old.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 99),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_latest.part_id,
                    uri: pw_latest.uri,
                    content_hash: pw_latest.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_latest.size_bytes_compressed,
                    size_bytes_uncompressed: pw_latest.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 149),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);

        let parts = DashMap::new();
        parts.insert(
            part_latest.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_latest)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_old, sf_latest],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Add one new entry for the partition
        let new_entries = vec![make_new_entry(75)];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Expect: old entry (preserved) + latest entry (rewritten) = 2 list entries
        // Expect: 1 new part (latest rewrite)
        assert_eq!(list_entries.len(), 2);
        assert_eq!(parts.len(), 1);

        // First entry should carry over the old one unchanged
        assert_eq!(list_entries[0].n_superfiles, 1);
        // URI should be exactly the same as the original written part
        assert_eq!(list_entries[0].uri, pw_old.uri);

        // Second entry should be the rewritten latest (1 + 1 = 2 superfiles)
        assert_eq!(list_entries[1].n_superfiles, 2);

        // New part should have the combined latest + new
        assert_eq!(parts[0].part.superfiles.len(), 2);
        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 225); // 150 + 75
    }

    // ---- cross-partition tests --------------------------------------------

    #[tokio::test]
    async fn update_two_partitions_both_touched() {
        // Two existing parts hold superfiles tagged with different partition
        // hints. New entries carrying both hints are added in one commit. Parts
        // are a single table-level lineage now, so the new entries all append to
        // the LAST existing part (the latest); the earlier part carries over
        // unchanged. Each superfile keeps its own partition_hint/partition_key.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 3;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf_a = make_superfile_entry_hinted(100, hash2_pk(0), 0);
        let part_a = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a.clone()],
        };
        let pw_a = write_manifest_part(storage.as_ref(), &part_a)
            .await
            .expect("write part_a");

        let sf_b = make_superfile_entry_hinted(200, hash2_pk(1), 1);
        let part_b = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_b.clone()],
        };
        let pw_b = write_manifest_part(storage.as_ref(), &part_b)
            .await
            .expect("write part_b");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 2,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a.part_id,
                    uri: pw_a.uri,
                    content_hash: pw_a.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 99),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_b.part_id,
                    uri: pw_b.uri,
                    content_hash: pw_b.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_b.size_bytes_compressed,
                    size_bytes_uncompressed: pw_b.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a)))),
        );
        parts_map.insert(
            part_b.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_b)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_a, sf_b],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let new_entries = vec![make_new_entry_hinted(50, 0), make_new_entry_hinted(80, 1)];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Single lineage: the earlier part carries over, the latest part is
        // rewritten with both new entries appended. 2 list entries, 1 new part.
        assert_eq!(list_entries.len(), 2);
        assert_eq!(parts.len(), 1);

        // [0] The earlier part carries over unchanged — original part_id
        // preserved, still its single original superfile.
        assert_eq!(list_entries[0].part_id, pw_a.part_id);
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(list_entries[0].content_hash, pw_a.content_hash);

        // [1] The latest part is rewritten: its 1 existing superfile + both new
        // ones = 3 superfiles.
        assert_eq!(list_entries[1].n_superfiles, 3);
        assert_eq!(parts[0].part.superfiles.len(), 3);
        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 330); // 200 (existing B) + 50 + 80

        // Each new superfile kept its own partition_hint (the routing tag is
        // independent of the part's table-level grouping key).
        let hints: Vec<_> = parts[0]
            .part
            .superfiles
            .iter()
            .map(|s| s.partition_hint)
            .collect();
        assert!(hints.contains(&Some(0)), "hint-0 new entry preserved");
        assert!(hints.contains(&Some(1)), "hint-1 new entry preserved");
    }

    #[tokio::test]
    async fn update_two_partitions_one_touched_exact_carry_over() {
        // One new entry is committed. Parts are a single table-level lineage, so
        // it appends to the LAST existing part (the latest), which is rewritten;
        // the earlier part carries over with the exact URI and content_hash that
        // were written — no re-encode, no PUT.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 3;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf_a = make_superfile_entry_hinted(100, hash2_pk(0), 0);
        let part_a = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a.clone()],
        };
        let pw_a = write_manifest_part(storage.as_ref(), &part_a)
            .await
            .expect("write part_a");

        let sf_b = make_superfile_entry_hinted(200, hash2_pk(1), 1);
        let part_b = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_b.clone()],
        };
        let pw_b = write_manifest_part(storage.as_ref(), &part_b)
            .await
            .expect("write part_b");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 2,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a.part_id,
                    uri: pw_a.uri.clone(),
                    content_hash: pw_a.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 99),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_b.part_id,
                    uri: pw_b.uri,
                    content_hash: pw_b.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_b.size_bytes_compressed,
                    size_bytes_uncompressed: pw_b.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a)))),
        );
        parts_map.insert(
            part_b.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_b)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_a, sf_b],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // Only touch partition A
        let new_entries = vec![make_new_entry_hinted(50, 0)];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // 2 list entries (earlier part carried over, latest rewritten), 1 new part
        assert_eq!(list_entries.len(), 2);
        assert_eq!(parts.len(), 1);

        // [0] Earlier part: exact carry-over — part_id and content_hash unchanged.
        assert_eq!(list_entries[0].part_id, pw_a.part_id);
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(list_entries[0].content_hash, pw_a.content_hash);

        // [1] Latest part: rewritten with its existing superfile + the new one =
        // 2 superfiles, 250 docs.
        assert_eq!(list_entries[1].n_superfiles, 2);
        assert_eq!(parts[0].part.superfiles.len(), 2);
        let docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(docs, 250); // 200 (existing B) + 50 (new)
    }

    #[tokio::test]
    async fn update_two_partitions_each_with_prior_split() {
        // Four existing parts from prior splits, in one table-level lineage:
        // [p0, p1, p2, p3]. Two new entries (different partition hints) are
        // committed. They append to the LAST part (p3, the latest); p3 already
        // holds 1 superfile so 1 + 2 = 3 exceeds the target of 2 — a split. The
        // split keeps p3 as-is and emits a fresh part holding just the 2 new
        // entries. p0..p2 carry over unchanged. Each superfile keeps its own
        // partition_hint.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 2;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        // Partition A: two parts
        let sf_a_old = make_superfile_entry_hinted(100, hash2_pk(0), 0);
        let part_a_old = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_old.clone()],
        };
        let pw_a_old = write_manifest_part(storage.as_ref(), &part_a_old)
            .await
            .expect("write part_a_old");

        let sf_a_latest = make_superfile_entry_hinted(150, hash2_pk(0), 0);
        let part_a_latest = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_latest.clone()],
        };
        let pw_a_latest = write_manifest_part(storage.as_ref(), &part_a_latest)
            .await
            .expect("write part_a_latest");

        // Partition B: two parts
        let sf_b_old = make_superfile_entry_hinted(200, hash2_pk(1), 1);
        let part_b_old = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_b_old.clone()],
        };
        let pw_b_old = write_manifest_part(storage.as_ref(), &part_b_old)
            .await
            .expect("write part_b_old");

        let sf_b_latest = make_superfile_entry_hinted(250, hash2_pk(1), 1);
        let part_b_latest = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_b_latest.clone()],
        };
        let pw_b_latest = write_manifest_part(storage.as_ref(), &part_b_latest)
            .await
            .expect("write part_b_latest");

        // List order: [a_old, a_latest, b_old, b_latest]
        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 2,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a_old.part_id,
                    uri: pw_a_old.uri.clone(),
                    content_hash: pw_a_old.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_old.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_old.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 99),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_a_latest.part_id,
                    uri: pw_a_latest.uri.clone(),
                    content_hash: pw_a_latest.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_latest.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_latest.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 149),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_b_old.part_id,
                    uri: pw_b_old.uri.clone(),
                    content_hash: pw_b_old.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_b_old.size_bytes_compressed,
                    size_bytes_uncompressed: pw_b_old.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_b_latest.part_id,
                    uri: pw_b_latest.uri.clone(),
                    content_hash: pw_b_latest.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_b_latest.size_bytes_compressed,
                    size_bytes_uncompressed: pw_b_latest.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 249),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a_latest.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a_latest)))),
        );
        parts_map.insert(
            part_b_latest.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_b_latest)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_a_old, sf_a_latest, sf_b_old, sf_b_latest],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let new_entries = vec![make_new_entry_hinted(75, 0), make_new_entry_hinted(90, 1)];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // 5 list entries: the 4 existing parts all carry over, plus 1 fresh
        // split part holding the 2 new entries.
        assert_eq!(list_entries.len(), 5);
        // 1 new part: the fresh split.
        assert_eq!(parts.len(), 1);

        // [0..=3] The four existing parts carry over unchanged — original
        // part_ids preserved, one superfile each.
        assert_eq!(list_entries[0].part_id, pw_a_old.part_id);
        assert_eq!(list_entries[0].uri, pw_a_old.uri);
        assert_eq!(list_entries[0].content_hash, pw_a_old.content_hash);

        assert_eq!(list_entries[1].part_id, pw_a_latest.part_id);

        assert_eq!(list_entries[2].part_id, pw_b_old.part_id);
        assert_eq!(list_entries[2].uri, pw_b_old.uri);
        assert_eq!(list_entries[2].content_hash, pw_b_old.content_hash);

        assert_eq!(list_entries[3].part_id, pw_b_latest.part_id);

        for e in &list_entries[0..4] {
            assert_eq!(e.n_superfiles, 1);
        }

        // [4] Fresh split part: the 2 new entries. 165 docs (75 + 90).
        assert_eq!(list_entries[4].n_superfiles, 2);
        assert_eq!(parts[0].part.superfiles.len(), 2);
        let docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(docs, 165); // 75 (hint 0) + 90 (hint 1)

        // The new superfiles kept their own partition hints.
        let hints: Vec<_> = parts[0]
            .part
            .superfiles
            .iter()
            .map(|s| s.partition_hint)
            .collect();
        assert!(hints.contains(&Some(0)), "hint-0 new entry preserved");
        assert!(hints.contains(&Some(1)), "hint-1 new entry preserved");
    }

    #[tokio::test]
    async fn update_multiple_partitions_land_in_one_lineage() {
        // A single commit of several new superfiles carrying DIFFERENT
        // partition hints (well under the target) produces ONE table-level part
        // holding ALL of them. Each superfile keeps its own partition_hint —
        // the routing tag is independent of the part's table-level grouping.
        // Under the default single-bucket Hash strategy the commit-time
        // partition_key stamped on every entry is bucket 0, regardless of hint.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 8;
        let opts = Arc::new(base_opts);

        let old_manifest = empty_manifest(&opts);

        let hints = [0u32, 1, 2, 3];
        let new_entries: Vec<_> = hints
            .iter()
            .enumerate()
            .map(|(i, &h)| make_new_entry_hinted(100 + i as u64, h))
            .collect();

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, &[])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // One part holding all four.
        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, hints.len() as u64);
        assert_eq!(parts[0].part.superfiles.len(), hints.len());

        // Every original superfile is present, each kept its own
        // partition_hint, and each carries the commit-time partition_key
        // stamped by `update` — bucket 0 under the single-bucket Hash default.
        for (&h, expected) in hints.iter().zip(new_entries.iter()) {
            let landed = parts[0]
                .part
                .superfiles
                .iter()
                .find(|s| s.superfile_id == expected.superfile_id)
                .unwrap_or_else(|| panic!("superfile with hint {h} landed in the part"));
            assert_eq!(landed.partition_hint, Some(h), "partition_hint preserved");
            assert_eq!(
                landed.partition_key,
                hash2_pk(0),
                "single-bucket Hash stamps bucket 0 on every entry"
            );
        }
    }

    // ---- removal tests ---------------------------------------------------

    #[tokio::test]
    async fn update_remove_one_superfile_from_partition() {
        // Partition has 2 superfiles; remove one. Verifies the part is
        // rewritten containing only the superfile that was not removed.
        let opts = make_opts();
        let (_dir, storage) = local_storage();

        let sf_keep = make_superfile_entry(100, hash_bucket_0_pk());
        let sf_remove = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_keep.clone(), sf_remove.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            existing_part.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_keep.clone(), sf_remove.clone()],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let (new_manifest, parts) = old_manifest
            .update(&[], from_ref(&sf_remove))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Part rewritten with 1 superfile; no cold entries.
        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(parts[0].part.superfiles.len(), 1);
        assert_eq!(
            parts[0].part.superfiles[0].superfile_id,
            sf_keep.superfile_id
        );
        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 100);
    }

    #[tokio::test]
    async fn update_add_and_remove_in_same_partition() {
        // One new superfile is added while one existing superfile is removed
        // in the same partition. The resulting part should contain the
        // surviving existing superfile plus the new one — not the removed one.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 3;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        let sf_keep = make_superfile_entry(100, hash_bucket_0_pk());
        let sf_remove = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_keep.clone(), sf_remove.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            existing_part.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_keep.clone(), sf_remove.clone()],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let sf_new = make_new_entry(75);
        let new_entries = vec![sf_new.clone()];

        let (new_manifest, parts) = old_manifest
            .update(&new_entries, from_ref(&sf_remove))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Net result: 1 list entry, 1 part — sf_keep + sf_new, sf_remove absent
        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, 2);
        assert_eq!(parts[0].part.superfiles.len(), 2);

        let ids: Vec<_> = parts[0]
            .part
            .superfiles
            .iter()
            .map(|s| s.superfile_id)
            .collect();
        assert!(ids.contains(&sf_keep.superfile_id));
        assert!(ids.contains(&sf_new.superfile_id));
        assert!(!ids.contains(&sf_remove.superfile_id));

        let total_docs: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(total_docs, 175); // 100 + 75
    }

    #[tokio::test]
    async fn update_remove_from_one_partition_other_carried_over_exactly() {
        // Two parts. The removed superfile lives only in the first part. Removals
        // are checked against every part: the first part matches and is
        // rewritten; the second holds no matching id and carries over with the
        // exact URI and content_hash — no re-encode, no PUT.
        let opts = make_opts();
        let (_dir, storage) = local_storage();

        let sf_a_keep = make_superfile_entry_hinted(100, hash2_pk(0), 0);
        let sf_a_remove = make_superfile_entry_hinted(150, hash2_pk(0), 0);
        let part_a = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_keep.clone(), sf_a_remove.clone()],
        };
        let pw_a = write_manifest_part(storage.as_ref(), &part_a)
            .await
            .expect("write part_a");

        let sf_b = make_superfile_entry_hinted(200, hash2_pk(1), 1);
        let part_b = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_b.clone()],
        };
        let pw_b = write_manifest_part(storage.as_ref(), &part_b)
            .await
            .expect("write part_b");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 2,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a.part_id,
                    uri: pw_a.uri,
                    content_hash: pw_a.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a.size_bytes_uncompressed,
                    n_superfiles: 2,
                    id_range: (0, 149),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_b.part_id,
                    uri: pw_b.uri.clone(),
                    content_hash: pw_b.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_b.size_bytes_compressed,
                    size_bytes_uncompressed: pw_b.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a)))),
        );
        parts_map.insert(
            part_b.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_b)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf_a_keep.clone(), sf_a_remove.clone(), sf_b.clone()],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let (new_manifest, parts) = old_manifest
            .update(&[], from_ref(&sf_a_remove))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // 2 list entries, 1 new part (only the first part was rewritten)
        assert_eq!(list_entries.len(), 2);
        assert_eq!(parts.len(), 1);

        // First part: rewritten with 1 surviving superfile.
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(parts[0].part.superfiles.len(), 1);
        assert_eq!(
            parts[0].part.superfiles[0].superfile_id,
            sf_a_keep.superfile_id
        );
        let docs_a: u64 = parts[0].part.superfiles.iter().map(|s| s.n_docs).sum();
        assert_eq!(docs_a, 100);

        // Second part: exact carry-over — URI and content_hash unchanged.
        assert_eq!(list_entries[1].n_superfiles, 1);
        assert_eq!(list_entries[1].uri, pw_b.uri);
        assert_eq!(list_entries[1].content_hash, pw_b.content_hash);
    }

    #[tokio::test]
    async fn update_remove_from_latest_part_in_split_partition() {
        // Two parts from a prior split: part_a_old (1 sf) and part_a_latest
        // (2 sfs). We remove sf_a_latest_remove, which lives in the SECOND
        // (latest) part. The removal set is checked against every part:
        // part_a_old holds no matching id and carries over unchanged, while
        // part_a_latest matches and is rewritten with only its surviving
        // superfile.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 2;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        // part_a_old: frozen entry from a prior split
        let sf_a_old = make_superfile_entry(100, hash_bucket_0_pk());
        let part_a_old = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_old.clone()],
        };
        let pw_a_old = write_manifest_part(storage.as_ref(), &part_a_old)
            .await
            .expect("write part_a_old");

        // part_a_latest: current mutable entry; contains the sf to remove
        let sf_a_latest_keep = make_superfile_entry(150, hash_bucket_0_pk());
        let sf_a_latest_remove = make_superfile_entry(200, hash_bucket_0_pk());
        let part_a_latest = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_latest_keep.clone(), sf_a_latest_remove.clone()],
        };
        let pw_a_latest = write_manifest_part(storage.as_ref(), &part_a_latest)
            .await
            .expect("write part_a_latest");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a_old.part_id,
                    uri: pw_a_old.uri.clone(),
                    content_hash: pw_a_old.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_old.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_old.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 99),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_a_latest.part_id,
                    uri: pw_a_latest.uri.clone(),
                    content_hash: pw_a_latest.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_latest.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_latest.size_bytes_uncompressed,
                    n_superfiles: 2,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a_old.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a_old)))),
        );
        parts_map.insert(
            part_a_latest.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a_latest)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![
                    sf_a_old.clone(),
                    sf_a_latest_keep.clone(),
                    sf_a_latest_remove.clone(),
                ],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let (new_manifest, parts_to_write) = old_manifest
            .update(&[], from_ref(&sf_a_latest_remove))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(list_entries.len(), 2);
        // Only the part that actually held the removed superfile is rewritten;
        // the other carries over untouched.
        assert_eq!(parts_to_write.len(), 1);

        // [0] part_a_old carried over. [1] part_a_latest rewritten.

        // sf_a_old survives (in one of the output parts)
        // sf_a_latest_keep survives (in one of the output parts)
        // sf_a_latest_remove is absent from every output part
        let all_ids: Vec<_> = parts_to_write
            .iter()
            .flat_map(|ep| ep.part.superfiles.iter())
            .map(|s| s.superfile_id)
            .collect();
        assert!(
            all_ids.contains(&sf_a_latest_keep.superfile_id),
            "sf_a_latest_keep must survive"
        );
        assert!(
            !all_ids.contains(&sf_a_latest_remove.superfile_id),
            "sf_a_latest_remove must be absent"
        );

        // Each rewritten part has exactly 1 superfile
        assert_eq!(list_entries[0].n_superfiles, 1);
        assert_eq!(list_entries[1].n_superfiles, 1);
    }

    #[tokio::test]
    async fn update_remove_all_superfiles_empties_partition() {
        // All superfiles in a partition are removed. Documents the current
        // behavior: the list entry survives with n_superfiles=0 and the
        // part has no superfiles (empty partition).
        let opts = make_opts();
        let (_dir, storage) = local_storage();

        let sf1 = make_superfile_entry(100, hash_bucket_0_pk());
        let sf2 = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf1.clone(), sf2.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            existing_part.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf1.clone(), sf2.clone()],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let (new_manifest, parts) = old_manifest
            .update(&[], &[sf1.clone(), sf2.clone()])
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        // Both superfiles removed: list entry remains with n_superfiles=0.
        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts.len(), 1);
        assert_eq!(list_entries[0].n_superfiles, 0);
        assert_eq!(parts[0].part.superfiles.len(), 0);
    }

    #[tokio::test]
    async fn update_remove_nonexistent_superfile_id_is_noop() {
        // entries_to_remove contains a superfile_id that is not present in any
        // part. The filter matches nothing and both original superfiles survive.
        // The part is still rewritten (the removal loop doesn't skip parts where
        // no removal matched), so n_superfiles stays at 2.
        let opts = make_opts();
        let (_dir, storage) = local_storage();

        let sf1 = make_superfile_entry(100, hash_bucket_0_pk());
        let sf2 = make_superfile_entry(150, hash_bucket_0_pk());

        let existing_part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf1.clone(), sf2.clone()],
        };
        let pw = write_manifest_part(storage.as_ref(), &existing_part)
            .await
            .expect("write part");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![ManifestPartEntry {
                part_id: pw.part_id,
                uri: pw.uri,
                content_hash: pw.content_hash,
                routing: None,
                size_bytes_compressed: pw.size_bytes_compressed,
                size_bytes_uncompressed: pw.size_bytes_uncompressed,
                n_superfiles: 2,
                id_range: (0, 149),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            }],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            existing_part.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(existing_part)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![sf1.clone(), sf2.clone()],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        // sf_ghost was never added to any part; its superfile_id won't match anything
        let sf_ghost = make_superfile_entry(50, hash_bucket_0_pk());

        let (new_manifest, parts_to_write) = old_manifest
            .update(&[], from_ref(&sf_ghost))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(list_entries.len(), 1);
        assert_eq!(parts_to_write.len(), 0);
        assert_eq!(list_entries[0].n_superfiles, 2);
    }

    #[tokio::test]
    async fn update_remove_from_older_frozen_part_in_split_partition() {
        // Two parts from a prior split: part_a_old (2 sfs: sf_a_old_keep +
        // sf_a_old_remove) and part_a_latest (1 sf). We remove sf_a_old_remove,
        // which lives in the FIRST (older) part. The removal set is checked
        // against every part: part_a_old matches and is rewritten without the
        // removed superfile; part_a_latest holds no matching id and carries over
        // unchanged. sf_a_old_keep and sf_a_latest survive.
        let mut base_opts =
            SupertableOptions::new(simple_schema(), vec![], vec![], None).expect("valid options");
        base_opts.target_superfiles_per_part = 2;
        let opts = Arc::new(base_opts);

        let (_dir, storage) = local_storage();

        // part_a_old: frozen entry — contains the sf to remove
        let sf_a_old_keep = make_superfile_entry(100, hash_bucket_0_pk());
        let sf_a_old_remove = make_superfile_entry(150, hash_bucket_0_pk());
        let part_a_old = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_old_keep.clone(), sf_a_old_remove.clone()],
        };
        let pw_a_old = write_manifest_part(storage.as_ref(), &part_a_old)
            .await
            .expect("write part_a_old");

        // part_a_latest: mutable entry — does not contain the sf to remove
        let sf_a_latest = make_superfile_entry(200, hash_bucket_0_pk());
        let part_a_latest = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![sf_a_latest.clone()],
        };
        let pw_a_latest = write_manifest_part(storage.as_ref(), &part_a_latest)
            .await
            .expect("write part_a_latest");

        let list = Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 0,
            options_hash: ContentHash([0u8; 32]),
            schema: vec![],
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts: vec![
                ManifestPartEntry {
                    part_id: pw_a_old.part_id,
                    uri: pw_a_old.uri,
                    content_hash: pw_a_old.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_old.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_old.size_bytes_uncompressed,
                    n_superfiles: 2,
                    id_range: (0, 149),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
                ManifestPartEntry {
                    part_id: pw_a_latest.part_id,
                    uri: pw_a_latest.uri,
                    content_hash: pw_a_latest.content_hash,
                    routing: None,
                    size_bytes_compressed: pw_a_latest.size_bytes_compressed,
                    size_bytes_uncompressed: pw_a_latest.size_bytes_uncompressed,
                    n_superfiles: 1,
                    id_range: (0, 199),
                    scalar_stats_agg: Default::default(),
                    fts_summary_agg: Default::default(),
                },
            ],
        };
        let loader = ManifestPartLoader::new(storage, &list);
        let parts_map = DashMap::new();
        parts_map.insert(
            part_a_old.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a_old)))),
        );
        parts_map.insert(
            part_a_latest.part_id,
            Arc::new(OnceCell::new_with(Some(Arc::new(part_a_latest)))),
        );
        let old_manifest = Arc::new(ManifestSnapshot {
            superfile_list: SuperfileList {
                manifest_id: 0,
                options: opts.clone(),
                superfiles: vec![
                    sf_a_old_keep.clone(),
                    sf_a_old_remove.clone(),
                    sf_a_latest.clone(),
                ],
                vector_index_storage_prefix: None,
            },
            list: Some(list),
            parts: parts_map,
            loader: Some(Arc::new(loader)),
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        });

        let (new_manifest, parts_to_write) = old_manifest
            .update(&[], from_ref(&sf_a_old_remove))
            .await
            .expect("update");
        let list_entries = new_manifest.get_all_list_entries();

        assert_eq!(list_entries.len(), 2);
        // Only the part that held the removed superfile is rewritten; the other
        // carries over untouched.
        assert_eq!(parts_to_write.len(), 1);

        // [0] part_a_old rewritten. [1] part_a_latest carried over.

        // sf_a_old_keep and sf_a_latest survive; sf_a_old_remove is absent
        let all_ids: Vec<_> = parts_to_write
            .iter()
            .flat_map(|ep| ep.part.superfiles.iter())
            .map(|s| s.superfile_id)
            .collect();
        assert!(
            all_ids.contains(&sf_a_old_keep.superfile_id),
            "sf_a_old_keep must survive"
        );
        assert!(
            !all_ids.contains(&sf_a_old_remove.superfile_id),
            "sf_a_old_remove must be absent"
        );

        // Old part now has 1 sf (sf_a_old_remove was removed)
        assert_eq!(list_entries[0].n_superfiles, 1);
        // Latest part still has 1 sf (removal did not touch it)
        assert_eq!(list_entries[1].n_superfiles, 1);
    }

    /// Build a single-part `Manifest` carrying `n_parts` placeholder
    /// entries — enough to exercise the list-aware `ManifestSnapshot` accessors
    /// without attaching storage.
    fn list_with_parts(n_parts: usize) -> list::Manifest {
        use list::{Manifest, ManifestPartEntry, PartitionStrategy};
        let parts = (0..n_parts)
            .map(|i| ManifestPartEntry {
                part_id: part::PartId(Uuid::from_u128(i as u128 + 1)),
                uri: format!("manifests/part-{i}"),
                n_superfiles: 0,
                size_bytes_compressed: 0,
                size_bytes_uncompressed: 0,
                content_hash: part::ContentHash([0u8; 32]),
                routing: None,
                id_range: (0, 0),
                scalar_stats_agg: Default::default(),
                fts_summary_agg: Default::default(),
            })
            .collect();
        Manifest {
            drained_ranges: Default::default(),
            global_vector_index: None,
            tombstone_seqs: Default::default(),
            superseded_cells: Default::default(),
            format_version: list::FORMAT_VERSION.into(),
            manifest_id: 1,
            options_hash: part::ContentHash([0u8; 32]),
            schema: Vec::new(),
            id_column: "_id".into(),
            fts_columns: vec![],
            vector_columns: vec![],
            partition_strategy: PartitionStrategy::Hash {
                column: "_id".into(),
                n_buckets: 1,
            },
            vector_index_storage_prefix: None,
            deleted_user_ids_inline: None,
            slow_vector_state_uri: None,
            slow_vector_state_content_hash: None,
            slow_vector_state_centroids: None,
            parts,
        }
    }

    fn manifest_with_list(list: list::Manifest) -> ManifestSnapshot {
        ManifestSnapshot {
            superfile_list: SuperfileList::empty(opts()),
            list: Some(list),
            parts: DashMap::new(),
            loader: None,
            stamped_partition_strategy: None,
            stamped_global_vector_index: None,
            stamped_drained_ranges: None,
        }
    }

    /// `get_num_parts` / `get_all_list_entries` read straight off the
    /// attached `Manifest` (the Some-arm of both accessors).
    #[test]
    fn list_accessors_read_from_attached_list() {
        let m = manifest_with_list(list_with_parts(3));
        assert_eq!(m.get_num_parts(), 3);
        assert_eq!(m.get_all_list_entries().len(), 3);
        assert_eq!(m.get_num_parts_loaded(), 0, "nothing eagerly loaded");
        assert!(!m.is_in_process_only(), "a list is attached");

        // No-list manifest takes the None-arms.
        let empty = ManifestSnapshot::empty(opts());
        assert_eq!(empty.get_num_parts(), 0);
        assert!(empty.get_all_list_entries().is_empty());
        assert!(empty.is_in_process_only());
    }

    #[test]
    fn complete_flat_superfiles_rejects_partial_part_view() {
        let mut list = list_with_parts(1);
        list.parts[0].n_superfiles = 1;
        let mut manifest = manifest_with_list(list);
        assert!(
            manifest.complete_flat_superfiles().is_none(),
            "empty resident view cannot represent one listed superfile"
        );

        manifest
            .superfile_list
            .superfiles
            .push(seg_entry(Uuid::new_v4(), 4));
        let complete = manifest
            .complete_flat_superfiles()
            .expect("resident count now matches list");
        assert_eq!(complete.len(), 1);
    }

    /// `get_cached_part_by_id` / `get_cached_part_by_list_idx` return
    /// `None` before any part is fetched into the per-part cache; the
    /// list-index variant resolves the index to a `PartId` first.
    #[test]
    fn cached_part_lookups_miss_before_load() {
        let m = manifest_with_list(list_with_parts(2));
        let known_id = part::PartId(Uuid::from_u128(1));
        assert!(m.get_cached_part_by_id(&known_id).is_none());
        assert!(m.get_cached_part_by_list_idx(0).is_none());
        assert!(m.get_cached_part_by_list_idx(1).is_none());

        // A manifest with no list has no parts to resolve by index.
        let empty = ManifestSnapshot::empty(opts());
        assert!(empty.get_cached_part_by_list_idx(0).is_none());
    }

    /// `ManifestSnapshot::new` with no storage/list takes the in-process-only
    /// constructor branch (loader + list both `None`).
    #[test]
    fn manifest_new_without_storage_is_in_process_only() {
        let m = ManifestSnapshot::new(7, opts(), vec![seg_entry(Uuid::new_v4(), 4)], None, None);
        assert_eq!(m.get_manifest_id(), 7);
        assert!(m.is_in_process_only());
        assert_eq!(m.get_num_parts(), 0);
        assert_eq!(m.superfiles.len(), 1);
    }

    /// `ClusterCentroids::from_fp32` clamps non-finite components to zero.
    #[test]
    fn from_fp32_handles_non_finite_components() {
        let centroids = [f32::INFINITY, f32::NEG_INFINITY, 0.0, 1.0];
        let cc = ClusterCentroids::from_fp32(1, 4, &centroids, vec![1]);
        let out = cc.centroid(0);
        assert!(out.iter().all(|v| v.is_finite()));
        assert_eq!(out[0], 0.0);
        assert_eq!(out[1], 0.0);
        assert_eq!(out[2], 0.0);
        assert_eq!(out[3], 1.0);
    }

    /// `decode_part_off_thread` decodes valid part bytes on the blocking pool
    /// back to a part equal to the original, and surfaces a typed error (not a
    /// panic / task-join failure) on garbage input.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn decode_part_off_thread_roundtrips_and_rejects_garbage() {
        let id = Uuid::new_v4();
        let seg = Arc::new(SuperfileEntry {
            birth_version: 0,
            superfile_id: id,
            uri: SuperfileUri(id),
            n_docs: 3,
            id_min: -5,
            id_max: 7,
            scalar_stats: HashMap::new(),
            fts_summary: HashMap::new(),
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            vector_layout: VectorLayout::Ivf,
            subsection_offsets: None,
        });
        let part = ManifestPart {
            format_version: part::FORMAT_VERSION.into(),
            part_id: PartId::new_v4(),
            superfiles: vec![seg],
        };
        let bytes = part::encode(&part);

        let decoded = decode_part_off_thread(Bytes::from(bytes))
            .await
            .expect("valid part decodes off-thread");
        assert_eq!(decoded.part_id, part.part_id, "part_id round-trips");
        assert_eq!(decoded.superfiles.len(), 1);
        assert_eq!(decoded.superfiles[0].superfile_id, id);
        assert_eq!(decoded.superfiles[0].id_min, -5);
        assert_eq!(decoded.superfiles[0].id_max, 7);

        // Garbage bytes surface a typed error, not a panic.
        let err = decode_part_off_thread(Bytes::from_static(b"not-a-valid-part-blob"))
            .await
            .expect_err("garbage bytes must fail to decode");
        assert!(
            matches!(err, ManifestLoadError::Parse(_)),
            "expected a parse error, got {err:?}"
        );
    }
}