prolly-map 0.3.0

Content-addressed versioned map storage primitives.
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
use super::super::builder::BatchBuilder;
use super::super::canonical_splice::{canonical_splice, CanonicalSpliceStats};
use super::super::cid::Cid;
use super::super::config::Config;
use super::super::error::{Error, Mutation as TreeMutation};
use super::super::store::Store;
use super::super::Prolly;
use super::builder::{build_hierarchy, build_hierarchy_parallel, IndexedRecord};
use super::cache::{ContentCache, DEFAULT_PROXIMITY_CACHE_NODES};
use super::distance::{prepare_vector, query_score, score};
use super::mutation::{mutate_hierarchy, LogicalEdit};
use super::search::{
    adaptive_should_stop, insert_top_k, AdaptiveContext, FrontierEntry, PreparedFilter,
    SearchCandidate,
};
use super::storage::quantized::ScalarQuantized;
use super::storage::vector::ExternalVector;
use super::storage::{Descriptor, PhysicalNodeKind, ProximityNode, StoredRecord, VectorRef};
use super::vector::promotion_level;
use super::{
    BuildParallelism, DistanceMetric, ExactProximityRecord, Neighbor, ProximityBuildStats,
    ProximityConfig, ProximityMutation, ProximityMutationStats, ProximityRecord,
    ProximitySearchStats, ProximityTree, ProximityVerification, SearchBackend, SearchCompletion,
    SearchPolicy, SearchRequest, SearchResult,
};
use std::collections::{BTreeMap, BinaryHeap, HashSet};
use std::sync::{Arc, Mutex};

/// Immutable exact-key directory plus deterministic ANN hierarchy.
pub struct ProximityMap<S: Store> {
    store: S,
    directory: Prolly<S>,
    tree: ProximityTree,
    node_cache: Mutex<ContentCache<ProximityNode>>,
}

impl<S> ProximityMap<S>
where
    S: Store + Clone + Send + Sync,
    S::Error: Send + Sync,
{
    /// Build a canonical proximity map from logical records.
    pub fn build(
        store: S,
        config: ProximityConfig,
        records: impl IntoIterator<Item = ProximityRecord>,
    ) -> Result<Self, Error> {
        Self::build_with_parallelism(store, config, records, BuildParallelism::default())
            .map(|(map, _)| map)
    }

    /// Build with an explicit runtime worker limit and canonical logical stats.
    pub fn build_with_parallelism(
        store: S,
        config: ProximityConfig,
        records: impl IntoIterator<Item = ProximityRecord>,
        parallelism: BuildParallelism,
    ) -> Result<(Self, ProximityBuildStats), Error> {
        config.validate()?;
        let mut records: Vec<_> = records.into_iter().collect();
        records.sort_by(|left, right| left.key.cmp(&right.key));
        for pair in records.windows(2) {
            if pair[0].key == pair[1].key {
                return Err(Error::DuplicateProximityKey {
                    key: pair[0].key.clone(),
                });
            }
        }

        let directory_config = Config::default();
        let mut directory_builder = BatchBuilder::new(store.clone(), directory_config.clone());
        let mut indexed = Vec::with_capacity(records.len());
        for record in records {
            let stored = StoredRecord::new(
                &record.vector,
                record.value,
                config.metric,
                config.dimensions,
            )?;
            indexed.push(IndexedRecord {
                key: record.key.clone(),
                vector: stored.vector.clone(),
            });
            directory_builder.add(record.key, stored.encode());
        }
        let directory_tree = directory_builder.build()?;
        let hierarchy = build_hierarchy_parallel(&indexed, &config, parallelism.threads())?;
        let objects_written = put_missing_nodes(&store, &hierarchy.nodes)?;

        let descriptor = Descriptor {
            config: config.clone(),
            count: indexed.len() as u64,
            directory: directory_tree.clone(),
            proximity_root: hierarchy.root.clone(),
        };
        let descriptor_bytes = descriptor.encode();
        let descriptor_cid = Cid::from_bytes(&descriptor_bytes);
        store
            .put(descriptor_cid.as_bytes(), &descriptor_bytes)
            .map_err(|error| Error::Store(Box::new(error)))?;

        let stats = ProximityBuildStats {
            distance_evaluations: hierarchy.distance_evaluations,
            proximity_objects: hierarchy.nodes.len(),
            proximity_objects_written: objects_written,
        };
        Ok((
            Self {
                directory: Prolly::new(store.clone(), directory_config),
                store,
                tree: ProximityTree {
                    directory: directory_tree,
                    proximity_root: hierarchy.root,
                    descriptor: descriptor_cid,
                    count: indexed.len() as u64,
                    config,
                },
                node_cache: Mutex::new(ContentCache::new(DEFAULT_PROXIMITY_CACHE_NODES)),
            },
            stats,
        ))
    }

    /// Load and locally validate a persisted proximity descriptor.
    pub fn load(store: S, descriptor_cid: Cid) -> Result<Self, Error> {
        let descriptor_bytes = load_content(&store, &descriptor_cid)?;
        let descriptor = Descriptor::decode(&descriptor_bytes)?;
        let root_bytes = load_content(&store, &descriptor.proximity_root)?;
        if root_bytes.len() > descriptor.config.overflow.max_page_bytes as usize {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "root exceeds descriptor max_node_bytes".to_owned(),
            });
        }
        let root = ProximityNode::decode(&root_bytes, descriptor.config.dimensions)?;
        if root.subtree_count != descriptor.count {
            return Err(Error::InvalidProximityObject {
                kind: "descriptor",
                reason: "record count disagrees with proximity root".to_owned(),
            });
        }
        let directory_config = descriptor.directory.config.clone();
        Ok(Self {
            directory: Prolly::new(store.clone(), directory_config),
            store,
            tree: ProximityTree {
                directory: descriptor.directory,
                proximity_root: descriptor.proximity_root,
                descriptor: descriptor_cid,
                count: descriptor.count,
                config: descriptor.config,
            },
            node_cache: Mutex::new(ContentCache::new(DEFAULT_PROXIMITY_CACHE_NODES)),
        })
    }

    /// The immutable roots and shape configuration committed by this map.
    pub fn tree(&self) -> &ProximityTree {
        &self.tree
    }

    /// Invalidate process-local decoded proximity objects after an external
    /// content sweep. This does not mutate or remove persisted content.
    pub fn clear_content_cache(&self) -> Result<(), Error> {
        self.node_cache
            .lock()
            .map_err(|_| Error::InvalidProximityObject {
                kind: "cache",
                reason: "node cache lock poisoned".to_owned(),
            })?
            .clear();
        Ok(())
    }

    /// Exact key lookup through the authoritative ordered directory.
    pub fn get(&self, key: &[u8]) -> Result<Option<ExactProximityRecord>, Error> {
        self.directory
            .get(&self.tree.directory, key)?
            .map(|bytes| {
                let record = StoredRecord::decode(&bytes, self.tree.config.dimensions)?;
                Ok((record.vector, record.value))
            })
            .transpose()
    }

    /// Exact key membership through the authoritative ordered directory.
    pub fn contains_key(&self, key: &[u8]) -> Result<bool, Error> {
        Ok(self.get(key)?.is_some())
    }

    /// Canonical full rebuild after applying a sorted-unique logical mutation batch.
    pub fn rebuild_batch(
        &self,
        mutations: impl IntoIterator<Item = ProximityMutation>,
    ) -> Result<Self, Error> {
        let mutations = validate_mutations(mutations)?;
        let mut records = self.collect_records()?;
        apply_mutations(&mut records, &mutations, &self.tree.config)?;
        Self::build(
            self.store.clone(),
            self.tree.config.clone(),
            records.into_values(),
        )
    }

    /// Copy-on-write mutation with exact clean-rebuild CID equivalence.
    pub fn mutate_batch(
        &self,
        mutations: impl IntoIterator<Item = ProximityMutation>,
    ) -> Result<(Self, ProximityMutationStats), Error> {
        let mutations = validate_mutations(mutations)?;
        if mutations.is_empty() {
            return Ok((
                Self::load(self.store.clone(), self.tree.descriptor.clone())?,
                Default::default(),
            ));
        }
        let keys: Vec<_> = mutations
            .iter()
            .map(|mutation| mutation.key.clone())
            .collect();
        let old_values = self.directory.get_many(&self.tree.directory, &keys)?;
        let mut logical_edits = Vec::new();
        let mut directory_mutations = Vec::with_capacity(mutations.len());
        let mut count = self.tree.count;
        for (mutation, old_bytes) in mutations.iter().zip(old_values) {
            let old = old_bytes
                .as_deref()
                .map(|bytes| StoredRecord::decode(bytes, self.tree.config.dimensions))
                .transpose()?;
            let new = mutation
                .value
                .as_ref()
                .map(|(vector, value)| {
                    StoredRecord::new(
                        vector,
                        value.clone(),
                        self.tree.config.metric,
                        self.tree.config.dimensions,
                    )
                })
                .transpose()?;
            match (&old, &new) {
                (None, Some(_)) => {
                    count = count
                        .checked_add(1)
                        .ok_or_else(|| Error::InvalidProximityObject {
                            kind: "mutation",
                            reason: "record count overflow".to_owned(),
                        })?
                }
                (Some(_), None) => count -= 1,
                _ => {}
            }
            let old_vector = old.as_ref().map(|record| record.vector.clone());
            let new_vector = new.as_ref().map(|record| record.vector.clone());
            if old_vector != new_vector {
                logical_edits.push(LogicalEdit {
                    key: mutation.key.clone(),
                    old: old_vector,
                    new: new_vector,
                    level: promotion_level(
                        &mutation.key,
                        self.tree.config.hierarchy.log_chunk_size,
                        self.tree.config.hierarchy.level_hash_seed,
                    ),
                });
            }
            directory_mutations.push(match new {
                Some(record) => TreeMutation::Upsert {
                    key: mutation.key.clone(),
                    val: record.encode(),
                },
                None => TreeMutation::Delete {
                    key: mutation.key.clone(),
                },
            });
        }
        let (directory_tree, directory_stats) =
            canonical_splice(&self.directory, &self.tree.directory, directory_mutations)?;

        if logical_edits.is_empty() {
            let descriptor = Descriptor {
                config: self.tree.config.clone(),
                count,
                directory: directory_tree.clone(),
                proximity_root: self.tree.proximity_root.clone(),
            };
            let bytes = descriptor.encode();
            let descriptor_cid = Cid::from_bytes(&bytes);
            self.store
                .put(descriptor_cid.as_bytes(), &bytes)
                .map_err(|error| Error::Store(Box::new(error)))?;
            let map = Self {
                directory: Prolly::new(self.store.clone(), directory_tree.config.clone()),
                store: self.store.clone(),
                tree: ProximityTree {
                    directory: directory_tree,
                    proximity_root: self.tree.proximity_root.clone(),
                    descriptor: descriptor_cid,
                    count,
                    config: self.tree.config.clone(),
                },
                node_cache: Mutex::new(ContentCache::new(DEFAULT_PROXIMITY_CACHE_NODES)),
            };
            return Ok((
                map,
                ProximityMutationStats {
                    nodes_reused: 1,
                    directory_entries_scanned: directory_stats.entries_scanned,
                    directory_nodes_read: directory_stats.nodes_read,
                    directory_nodes_rebuilt: directory_stats.nodes_rebuilt,
                    directory_nodes_written: directory_stats.nodes_written,
                    directory_nodes_reused: directory_stats.nodes_reused,
                    directory_levels_rebuilt: directory_stats.levels_rebuilt,
                    directory_right_edge_rebuilt: directory_stats.right_edge_rebuilt,
                    ..Default::default()
                },
            ));
        }

        let (old_root, _) = self.load_node(&self.tree.proximity_root)?;
        let max_edit_level = logical_edits
            .iter()
            .map(|edit| edit.level)
            .max()
            .unwrap_or(0);
        let (proximity_root, nodes, mut stats) =
            if old_root.entries.is_empty() || max_edit_level >= old_root.level {
                let records = self.collect_records_from(&directory_tree)?;
                let indexed: Vec<_> = records
                    .values()
                    .map(|record| IndexedRecord {
                        key: record.key.clone(),
                        vector: record.vector.clone(),
                    })
                    .collect();
                let built = build_hierarchy(&indexed, &self.tree.config)?;
                let stats = ProximityMutationStats {
                    records_rebuilt: indexed.len(),
                    distance_evaluations: built.distance_evaluations,
                    full_proximity_rebuild: true,
                    ..Default::default()
                };
                (built.root, built.nodes, stats)
            } else {
                let local = mutate_hierarchy(
                    &self.store,
                    &self.tree.proximity_root,
                    &self.tree.config,
                    &logical_edits,
                )?;
                (local.root, local.nodes, local.stats)
            };
        let pending_count = nodes.len();
        let nodes_written = put_missing_nodes(&self.store, &nodes)?;
        stats.nodes_written = nodes_written;
        stats.nodes_reused += pending_count.saturating_sub(nodes_written);
        apply_directory_stats(&mut stats, directory_stats);

        let descriptor = Descriptor {
            config: self.tree.config.clone(),
            count,
            directory: directory_tree.clone(),
            proximity_root: proximity_root.clone(),
        };
        let descriptor_bytes = descriptor.encode();
        let descriptor_cid = Cid::from_bytes(&descriptor_bytes);
        self.store
            .put(descriptor_cid.as_bytes(), &descriptor_bytes)
            .map_err(|error| Error::Store(Box::new(error)))?;
        Ok((
            Self {
                directory: Prolly::new(self.store.clone(), directory_tree.config.clone()),
                store: self.store.clone(),
                tree: ProximityTree {
                    directory: directory_tree,
                    proximity_root,
                    descriptor: descriptor_cid,
                    count,
                    config: self.tree.config.clone(),
                },
                node_cache: Mutex::new(ContentCache::new(DEFAULT_PROXIMITY_CACHE_NODES)),
            },
            stats,
        ))
    }

    /// Deterministic global best-first search over the authoritative hierarchy.
    pub fn search(&self, request: SearchRequest<'_>) -> Result<SearchResult, Error> {
        self.search_with_trace(request, None)
    }

    pub(super) fn search_with_trace(
        &self,
        request: SearchRequest<'_>,
        mut trace: Option<&mut Vec<super::proof::ProximitySearchEvent>>,
    ) -> Result<SearchResult, Error> {
        request.validate()?;
        if matches!(
            request.backend,
            SearchBackend::ProductQuantized | SearchBackend::Hnsw
        ) {
            return Err(Error::InvalidProximitySearch {
                reason: "requested backend requires a validated accelerator sidecar".to_owned(),
            });
        }
        let filter = PreparedFilter::new(request.filter.clone(), &self.tree.directory)?;
        let query = prepare_vector(
            self.tree.config.metric,
            request.query,
            self.tree.config.dimensions,
        )?;
        let use_scalar_quantization =
            super::accelerator::sq8::enabled(&self.tree.config, request.policy);
        let mut stats = ProximitySearchStats::default();
        let mut frontier = BinaryHeap::new();
        frontier.push(FrontierEntry {
            bound: 0.0,
            score: 0.0,
            key: Vec::new(),
            cid: self.tree.proximity_root.clone(),
            expected_level: None,
        });
        if let Some(trace) = trace.as_deref_mut() {
            trace.push(super::proof::ProximitySearchEvent::FrontierPushed {
                cid: self.tree.proximity_root.clone(),
                bound_bits: 0.0f64.to_bits(),
            });
        }
        let mut candidates = Vec::<SearchCandidate>::new();
        let mut score_cache = BTreeMap::<Vec<u8>, f64>::new();
        let mut visited = HashSet::new();
        let mut levels = HashSet::new();
        let mut last_fanout = 0usize;
        let mut completion = SearchCompletion::Exact;

        while let Some(next) = frontier.peek() {
            if !use_scalar_quantization
                && self.tree.config.metric == DistanceMetric::L2Squared
                && candidates.len() == request.k
                && next.bound > candidates.last().expect("full top-k").score
            {
                break;
            }
            if let SearchPolicy::Adaptive(quality) = request.policy {
                if candidates.last().is_some_and(|worst| {
                    let overlapping = frontier
                        .iter()
                        .filter(|entry| entry.bound <= worst.score)
                        .count();
                    adaptive_should_stop(
                        quality,
                        AdaptiveContext {
                            results: candidates.len(),
                            k: request.k,
                            frontier_bound: next.bound,
                            worst_score: worst.score,
                            overlapping_clusters: overlapping,
                            logical_level: next.expected_level.unwrap_or(u8::MAX),
                            last_fanout,
                            cluster_count: frontier.len(),
                        },
                    )
                }) {
                    completion = SearchCompletion::ApproximatePolicySatisfied;
                    break;
                }
            }
            if request
                .budget
                .max_nodes
                .is_some_and(|maximum| stats.nodes_read >= maximum)
            {
                completion = SearchCompletion::BudgetExhausted;
                break;
            }
            let next = frontier.pop().expect("peeked frontier");
            if let Some(trace) = trace.as_deref_mut() {
                trace.push(super::proof::ProximitySearchEvent::FrontierPopped {
                    cid: next.cid.clone(),
                    bound_bits: next.bound.to_bits(),
                });
            }
            if !visited.insert(next.cid.clone()) {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "cycle or repeated child ownership".to_owned(),
                });
            }
            let (node, mut bytes) = self.load_node(&next.cid)?;
            if let Some(trace) = trace.as_deref_mut() {
                trace.push(super::proof::ProximitySearchEvent::VisitedObject(
                    next.cid.clone(),
                ));
            }
            let quantizer = if use_scalar_quantization && node.kind.has_children(node.level) {
                let (quantizer, quantizer_bytes) = self.load_scalar_quantizer(&node)?;
                bytes = bytes.saturating_add(quantizer_bytes);
                Some(quantizer)
            } else {
                None
            };
            if next
                .expected_level
                .is_some_and(|expected| node.level != expected)
            {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "child has an unexpected logical level".to_owned(),
                });
            }
            stats.bytes_read = stats.bytes_read.saturating_add(bytes);
            if request
                .budget
                .max_committed_bytes
                .is_some_and(|maximum| stats.committed_bytes.saturating_add(bytes) > maximum)
            {
                completion = SearchCompletion::BudgetExhausted;
                break;
            }
            stats.nodes_read += 1;
            stats.committed_bytes += bytes;
            last_fanout = node.entries.len();
            levels.insert(node.level);
            stats.levels_visited = levels.len();

            for (entry_index, entry) in node.entries.iter().enumerate() {
                if node.kind.has_children(node.level) {
                    if !filter.intersects(&entry.min_key, &entry.max_key) {
                        continue;
                    }
                    let Some(child) = &entry.child else {
                        return Err(Error::InvalidProximityObject {
                            kind: "node",
                            reason: "internal entry has no child".to_owned(),
                        });
                    };
                    let representative_score = if let Some(quantizer) = &quantizer {
                        if distance_budget_exhausted(&request, &stats) {
                            completion = SearchCompletion::BudgetExhausted;
                            break;
                        }
                        stats.quantized_distance_evaluations += 1;
                        quantizer.approximate_score(self.tree.config.metric, &query, entry_index)?
                    } else {
                        match score_cache.get(&entry.key) {
                            Some(score) => *score,
                            None => {
                                if distance_budget_exhausted(&request, &stats) {
                                    completion = SearchCompletion::BudgetExhausted;
                                    break;
                                }
                                stats.distance_evaluations += 1;
                                let value = query_score(
                                    request.kernel,
                                    self.tree.config.metric,
                                    &query,
                                    entry.vector.inline()?,
                                );
                                score_cache.insert(entry.key.clone(), value);
                                value
                            }
                        }
                    };
                    let bound = if quantizer.is_none()
                        && self.tree.config.metric == DistanceMetric::L2Squared
                    {
                        super::distance::canonical::l2_lower_bound_down(
                            representative_score,
                            entry.covering_radius,
                        )
                    } else {
                        representative_score
                    };
                    if request
                        .budget
                        .max_frontier_entries
                        .is_some_and(|maximum| frontier.len() >= maximum)
                    {
                        completion = SearchCompletion::BudgetExhausted;
                        break;
                    }
                    frontier.push(FrontierEntry {
                        bound,
                        score: representative_score,
                        key: entry.key.clone(),
                        cid: child.clone(),
                        expected_level: Some(if node.kind == PhysicalNodeKind::OverflowDirectory {
                            node.level
                        } else {
                            node.level - 1
                        }),
                    });
                    if let Some(trace) = trace.as_deref_mut() {
                        trace.push(super::proof::ProximitySearchEvent::FrontierPushed {
                            cid: child.clone(),
                            bound_bits: bound.to_bits(),
                        });
                    }
                    stats.frontier_peak = stats.frontier_peak.max(frontier.len());
                } else if filter.contains(&entry.key) {
                    let leaf_score = match score_cache.get(&entry.key) {
                        Some(score) => *score,
                        None => {
                            if distance_budget_exhausted(&request, &stats) {
                                completion = SearchCompletion::BudgetExhausted;
                                break;
                            }
                            stats.distance_evaluations += 1;
                            let value = query_score(
                                request.kernel,
                                self.tree.config.metric,
                                &query,
                                entry.vector.inline()?,
                            );
                            score_cache.insert(entry.key.clone(), value);
                            value
                        }
                    };
                    if let Some(trace) = trace.as_deref_mut() {
                        trace.push(super::proof::ProximitySearchEvent::CandidateScored {
                            key: entry.key.clone(),
                            distance_bits: leaf_score.to_bits(),
                        });
                    }
                    insert_top_k(
                        &mut candidates,
                        SearchCandidate {
                            key: entry.key.clone(),
                            vector: entry.vector.inline()?.to_vec(),
                            score: leaf_score,
                        },
                        request.k,
                    );
                }
            }
            if completion == SearchCompletion::BudgetExhausted {
                break;
            }
        }

        let keys: Vec<_> = candidates
            .iter()
            .map(|candidate| candidate.key.clone())
            .collect();
        let values = self.directory.get_many(&self.tree.directory, &keys)?;
        if use_scalar_quantization {
            stats.reranked_candidates = candidates.len();
        }
        let mut neighbors = Vec::with_capacity(candidates.len());
        for (candidate, stored) in candidates.into_iter().zip(values) {
            let bytes = stored.ok_or_else(|| Error::InvalidProximityObject {
                kind: "node",
                reason: "leaf key is absent from exact directory".to_owned(),
            })?;
            let record = StoredRecord::decode(&bytes, self.tree.config.dimensions)?;
            if record.vector != candidate.vector {
                return Err(Error::InvalidProximityObject {
                    kind: "node",
                    reason: "leaf vector disagrees with exact directory".to_owned(),
                });
            }
            neighbors.push(Neighbor {
                key: candidate.key,
                value: record.value,
                distance: candidate.score,
            });
        }
        if let Some(trace) = trace {
            trace.push(super::proof::ProximitySearchEvent::Completed(completion));
        }
        Ok(SearchResult {
            neighbors,
            stats,
            completion,
        })
    }

    /// Traverse and validate the descriptor, directory, hierarchy, and routing invariants.
    pub fn verify(&self) -> Result<ProximityVerification, Error> {
        let records = self.collect_records()?;
        let root_bytes = load_content(&self.store, &self.tree.proximity_root)?;
        let root = ProximityNode::decode(&root_bytes, self.tree.config.dimensions)?;
        let mut state = VerificationState {
            records: &records,
            seen_nodes: HashSet::new(),
            seen_external_vectors: HashSet::new(),
            seen_scalar_quantizers: HashSet::new(),
            seen_leaf_keys: HashSet::new(),
            summary: ProximityVerification {
                record_count: self.tree.count,
                maximum_level: root.level,
                ..Default::default()
            },
        };
        let verified = self.verify_node(
            &self.tree.proximity_root,
            Some(root.level),
            None,
            &mut state,
        )?;
        if verified.count != self.tree.count || records.len() as u64 != self.tree.count {
            return Err(Error::InvalidProximityObject {
                kind: "descriptor",
                reason: "logical counts disagree".to_owned(),
            });
        }
        if state.seen_leaf_keys.len() != records.len()
            || records
                .keys()
                .any(|key| !state.seen_leaf_keys.contains(key))
        {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "leaf identities do not match the exact directory".to_owned(),
            });
        }
        Ok(state.summary)
    }

    fn load_node(&self, cid: &Cid) -> Result<(Arc<ProximityNode>, usize), Error> {
        if let Some((node, bytes)) = self
            .node_cache
            .lock()
            .map_err(|_| Error::InvalidProximityObject {
                kind: "cache",
                reason: "node cache lock poisoned".to_owned(),
            })?
            .get(cid)
        {
            return Ok((node, bytes));
        }
        let bytes = load_content(&self.store, cid)?;
        if bytes.len() > self.tree.config.overflow.max_page_bytes as usize {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "node exceeds descriptor max_node_bytes".to_owned(),
            });
        }
        let len = bytes.len();
        let mut node = ProximityNode::decode(&bytes, self.tree.config.dimensions)?;
        let vector_bytes = self.resolve_external_vectors(&mut node)?;
        let node = Arc::new(node);
        self.node_cache
            .lock()
            .map_err(|_| Error::InvalidProximityObject {
                kind: "cache",
                reason: "node cache lock poisoned".to_owned(),
            })?
            .insert(cid.clone(), node.clone(), len + vector_bytes);
        Ok((node, len + vector_bytes))
    }

    fn resolve_external_vectors(&self, node: &mut ProximityNode) -> Result<usize, Error> {
        let mut bytes_read = 0usize;
        for entry in &mut node.entries {
            let VectorRef::External(cid) = &entry.vector else {
                continue;
            };
            let bytes = load_content(&self.store, cid)?;
            let external = ExternalVector::decode(&bytes)?;
            if external.vector.len() != self.tree.config.dimensions as usize {
                return Err(Error::InvalidProximityObject {
                    kind: "vector",
                    reason: "external vector dimension mismatch".to_owned(),
                });
            }
            bytes_read += bytes.len();
            entry.vector = VectorRef::Inline(external.vector);
        }
        Ok(bytes_read)
    }

    fn load_scalar_quantizer(
        &self,
        node: &ProximityNode,
    ) -> Result<(ScalarQuantized, usize), Error> {
        let config = self
            .tree
            .config
            .scalar_quantization
            .as_ref()
            .ok_or_else(|| Error::InvalidProximityObject {
                kind: "quantizer",
                reason: "quantized search requires descriptor configuration".to_owned(),
            })?;
        let cid = node
            .quantizer
            .as_ref()
            .ok_or_else(|| Error::InvalidProximityObject {
                kind: "quantizer",
                reason: "configured node has no scalar quantizer".to_owned(),
            })?;
        let bytes = load_content(&self.store, cid)?;
        let quantizer = ScalarQuantized::decode(&bytes)?;
        if quantizer.dimensions != self.tree.config.dimensions
            || quantizer.group_size != config.group_size
        {
            return Err(Error::InvalidProximityObject {
                kind: "quantizer",
                reason: "quantizer configuration disagrees with descriptor".to_owned(),
            });
        }
        if quantizer.entry_count != node.entries.len() as u64 {
            return Err(Error::InvalidProximityObject {
                kind: "quantizer",
                reason: "quantizer entry count disagrees with node".to_owned(),
            });
        }
        Ok((quantizer, bytes.len()))
    }

    pub(crate) fn collect_records(&self) -> Result<BTreeMap<Vec<u8>, ProximityRecord>, Error> {
        self.collect_records_from(&self.tree.directory)
    }

    pub(crate) fn store_clone(&self) -> S {
        self.store.clone()
    }

    pub(super) fn directory_manager(&self) -> &Prolly<S> {
        &self.directory
    }

    pub(super) fn load_descriptor_bytes(&self) -> Result<Vec<u8>, Error> {
        load_content(&self.store, &self.tree.descriptor)
    }

    fn collect_records_from(
        &self,
        directory: &super::super::tree::Tree,
    ) -> Result<BTreeMap<Vec<u8>, ProximityRecord>, Error> {
        let mut records = BTreeMap::new();
        for entry in self.directory.range(directory, &[], None)? {
            let (key, bytes) = entry?;
            let stored = StoredRecord::decode(&bytes, self.tree.config.dimensions)?;
            records.insert(
                key.clone(),
                ProximityRecord {
                    key,
                    vector: stored.vector,
                    value: stored.value,
                },
            );
        }
        Ok(records)
    }

    fn verify_node(
        &self,
        cid: &Cid,
        expected_level: Option<u8>,
        parent: Option<(
            &super::storage::ProximityEntry,
            &[super::storage::ProximityEntry],
        )>,
        state: &mut VerificationState<'_>,
    ) -> Result<VerifiedSubtree, Error> {
        if !state.seen_nodes.insert(cid.clone()) {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "cycle or repeated child ownership".to_owned(),
            });
        }
        let bytes = load_content(&self.store, cid)?;
        if bytes.len() > self.tree.config.overflow.max_page_bytes as usize {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "node exceeds descriptor max_node_bytes".to_owned(),
            });
        }
        let mut node = ProximityNode::decode(&bytes, self.tree.config.dimensions)?;
        for entry in &node.entries {
            if let VectorRef::External(vector) = &entry.vector {
                if state.seen_external_vectors.insert(vector.clone()) {
                    state.summary.external_vector_count += 1;
                }
            }
        }
        self.resolve_external_vectors(&mut node)?;
        match (&self.tree.config.scalar_quantization, &node.quantizer) {
            (None, None) => {}
            (Some(config), Some(cid)) => {
                let quantizer_bytes = load_content(&self.store, cid)?;
                let quantizer = ScalarQuantized::decode(&quantizer_bytes)?;
                if quantizer.dimensions != self.tree.config.dimensions
                    || quantizer.group_size != config.group_size
                {
                    return Err(Error::InvalidProximityObject {
                        kind: "quantizer",
                        reason: "quantizer configuration disagrees with descriptor".to_owned(),
                    });
                }
                let vectors = node
                    .entries
                    .iter()
                    .map(|entry| entry.vector.inline())
                    .collect::<Result<Vec<_>, _>>()?;
                quantizer.verify(&vectors)?;
                state.summary.quantized_node_count += 1;
                if state.seen_scalar_quantizers.insert(cid.clone()) {
                    state.summary.scalar_quantizer_count += 1;
                }
            }
            _ => {
                return Err(Error::InvalidProximityObject {
                    kind: "quantizer",
                    reason: "node quantizer presence disagrees with descriptor".to_owned(),
                })
            }
        }
        if expected_level != Some(node.level) {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "unexpected logical level".to_owned(),
            });
        }
        state.summary.proximity_node_count += 1;
        match node.kind {
            PhysicalNodeKind::OverflowPage => state.summary.overflow_page_count += 1,
            PhysicalNodeKind::OverflowDirectory => state.summary.overflow_directory_count += 1,
            PhysicalNodeKind::Leaf | PhysicalNodeKind::Route => {}
        }
        state.summary.maximum_node_bytes = state.summary.maximum_node_bytes.max(bytes.len());

        if node.kind != PhysicalNodeKind::OverflowDirectory {
            if let Some((selected, candidates)) = parent {
                for entry in &node.entries {
                    // A promoted representative is deterministically retained in
                    // its own route even when another representative has an
                    // equal vector and a lexicographically smaller key.
                    if entry.key == selected.key {
                        continue;
                    }
                    let selected_distance = score(
                        self.tree.config.metric,
                        entry.vector.inline()?,
                        selected.vector.inline()?,
                    );
                    for candidate in candidates {
                        state.summary.distance_checks += 1;
                        let candidate_distance = score(
                            self.tree.config.metric,
                            entry.vector.inline()?,
                            candidate.vector.inline()?,
                        );
                        let candidate_is_better = candidate_distance
                            .total_cmp(&selected_distance)
                            .then_with(|| candidate.key.cmp(&selected.key))
                            .is_lt();
                        if candidate_is_better {
                            return Err(Error::InvalidProximityObject {
                                kind: "node",
                                reason: "nearest-representative invariant violated".to_owned(),
                            });
                        }
                    }
                }
            }
        }

        if node.kind != PhysicalNodeKind::OverflowDirectory {
            for entry in &node.entries {
                if super::vector::promotion_level(
                    &entry.key,
                    self.tree.config.hierarchy.log_chunk_size,
                    self.tree.config.hierarchy.level_hash_seed,
                ) < node.level
                {
                    return Err(Error::InvalidProximityObject {
                        kind: "node",
                        reason: "entry appears above its deterministic promotion level".to_owned(),
                    });
                }
            }
        }

        let verified = if node.kind.is_logical_leaf(node.level) {
            let mut points = Vec::with_capacity(node.entries.len());
            for entry in &node.entries {
                if !state.seen_leaf_keys.insert(entry.key.clone()) {
                    return Err(Error::InvalidProximityObject {
                        kind: "node",
                        reason: "duplicate leaf identity".to_owned(),
                    });
                }
                let record =
                    state
                        .records
                        .get(&entry.key)
                        .ok_or_else(|| Error::InvalidProximityObject {
                            kind: "node",
                            reason: "leaf key is absent from exact directory".to_owned(),
                        })?;
                if record.vector.as_slice() != entry.vector.inline()? {
                    return Err(Error::InvalidProximityObject {
                        kind: "node",
                        reason: "leaf vector disagrees with exact directory".to_owned(),
                    });
                }
                points.push((entry.key.clone(), entry.vector.inline()?.to_vec()));
            }
            VerifiedSubtree::from_points(node.entries.len() as u64, points)
        } else {
            let mut count = 0u64;
            let mut points = Vec::new();
            let mut minimum: Option<Vec<u8>> = None;
            let mut maximum: Option<Vec<u8>> = None;
            for entry in &node.entries {
                let child = entry
                    .child
                    .as_ref()
                    .ok_or_else(|| Error::InvalidProximityObject {
                        kind: "node",
                        reason: "internal entry has no child".to_owned(),
                    })?;
                let child_verified = self.verify_node(
                    child,
                    Some(if node.kind == PhysicalNodeKind::OverflowDirectory {
                        node.level
                    } else {
                        node.level - 1
                    }),
                    if node.kind == PhysicalNodeKind::OverflowDirectory {
                        parent
                    } else {
                        Some((entry, &node.entries))
                    },
                    state,
                )?;
                if child_verified.count != entry.child_count {
                    return Err(Error::InvalidProximityObject {
                        kind: "node",
                        reason: "child count summary mismatch".to_owned(),
                    });
                }
                if child_verified.minimum.as_deref() != Some(entry.min_key.as_slice())
                    || child_verified.maximum.as_deref() != Some(entry.max_key.as_slice())
                {
                    return Err(Error::InvalidProximityObject {
                        kind: "node",
                        reason: "child key-bound summary mismatch".to_owned(),
                    });
                }
                for (_, vector) in &child_verified.points {
                    let required = super::distance::euclidean_radius_up(
                        score(
                            super::DistanceMetric::L2Squared,
                            entry.vector.inline()?,
                            vector,
                        ),
                        0.0,
                    );
                    if required > entry.covering_radius {
                        return Err(Error::InvalidProximityObject {
                            kind: "node",
                            reason: "covering-radius summary is not conservative".to_owned(),
                        });
                    }
                }
                count = count.checked_add(child_verified.count).ok_or_else(|| {
                    Error::InvalidProximityObject {
                        kind: "node",
                        reason: "subtree count overflow".to_owned(),
                    }
                })?;
                if minimum.as_ref().map_or(true, |key| entry.min_key < *key) {
                    minimum = Some(entry.min_key.clone());
                }
                if maximum.as_ref().map_or(true, |key| entry.max_key > *key) {
                    maximum = Some(entry.max_key.clone());
                }
                points.extend(child_verified.points);
            }
            VerifiedSubtree {
                count,
                minimum,
                maximum,
                points,
            }
        };
        if verified.count != node.subtree_count {
            return Err(Error::InvalidProximityObject {
                kind: "node",
                reason: "subtree count mismatch".to_owned(),
            });
        }
        Ok(verified)
    }
}

struct VerificationState<'a> {
    records: &'a BTreeMap<Vec<u8>, ProximityRecord>,
    seen_nodes: HashSet<Cid>,
    seen_external_vectors: HashSet<Cid>,
    seen_scalar_quantizers: HashSet<Cid>,
    seen_leaf_keys: HashSet<Vec<u8>>,
    summary: ProximityVerification,
}

struct VerifiedSubtree {
    count: u64,
    minimum: Option<Vec<u8>>,
    maximum: Option<Vec<u8>>,
    points: Vec<(Vec<u8>, Vec<f32>)>,
}

impl VerifiedSubtree {
    fn from_points(count: u64, points: Vec<(Vec<u8>, Vec<f32>)>) -> Self {
        let minimum = points.iter().map(|(key, _)| key).min().cloned();
        let maximum = points.iter().map(|(key, _)| key).max().cloned();
        Self {
            count,
            minimum,
            maximum,
            points,
        }
    }
}

fn load_content<S: Store>(store: &S, cid: &Cid) -> Result<Vec<u8>, Error> {
    let bytes = store
        .get(cid.as_bytes())
        .map_err(|error| Error::Store(Box::new(error)))?
        .ok_or_else(|| Error::NotFound(cid.clone()))?;
    let actual = Cid::from_bytes(&bytes);
    if actual != *cid {
        return Err(Error::CidMismatch {
            expected: cid.clone(),
            actual,
        });
    }
    Ok(bytes)
}

fn distance_budget_exhausted(request: &SearchRequest<'_>, stats: &ProximitySearchStats) -> bool {
    request
        .budget
        .max_distance_evaluations
        .is_some_and(|maximum| {
            stats
                .distance_evaluations
                .saturating_add(stats.quantized_distance_evaluations)
                >= maximum
        })
}

fn put_missing_nodes<S: Store>(store: &S, nodes: &[(Cid, Vec<u8>)]) -> Result<usize, Error> {
    let keys: Vec<_> = nodes.iter().map(|(cid, _)| cid.as_bytes()).collect();
    let existing = store
        .batch_get_ordered_unique(&keys)
        .map_err(|error| Error::Store(Box::new(error)))?;
    for ((expected, _), value) in nodes.iter().zip(&existing) {
        if let Some(bytes) = value {
            let actual = Cid::from_bytes(bytes);
            if actual != *expected {
                return Err(Error::CidMismatch {
                    expected: expected.clone(),
                    actual,
                });
            }
        }
    }
    let missing: Vec<_> = nodes
        .iter()
        .zip(existing)
        .filter_map(|((cid, bytes), value)| {
            value
                .is_none()
                .then_some((cid.as_bytes(), bytes.as_slice()))
        })
        .collect();
    store
        .batch_put(&missing)
        .map_err(|error| Error::Store(Box::new(error)))?;
    Ok(missing.len())
}

fn apply_directory_stats(target: &mut ProximityMutationStats, source: CanonicalSpliceStats) {
    target.directory_entries_scanned = source.entries_scanned;
    target.directory_nodes_read = source.nodes_read;
    target.directory_nodes_rebuilt = source.nodes_rebuilt;
    target.directory_nodes_written = source.nodes_written;
    target.directory_nodes_reused = source.nodes_reused;
    target.directory_levels_rebuilt = source.levels_rebuilt;
    target.directory_right_edge_rebuilt = source.right_edge_rebuilt;
}

fn validate_mutations(
    mutations: impl IntoIterator<Item = ProximityMutation>,
) -> Result<Vec<ProximityMutation>, Error> {
    let mut mutations: Vec<_> = mutations.into_iter().collect();
    mutations.sort_by(|left, right| left.key.cmp(&right.key));
    for pair in mutations.windows(2) {
        if pair[0].key == pair[1].key {
            return Err(Error::DuplicateProximityKey {
                key: pair[0].key.clone(),
            });
        }
    }
    Ok(mutations)
}

fn apply_mutations(
    records: &mut BTreeMap<Vec<u8>, ProximityRecord>,
    mutations: &[ProximityMutation],
    config: &ProximityConfig,
) -> Result<(), Error> {
    for mutation in mutations {
        match &mutation.value {
            Some((vector, value)) => {
                records.insert(
                    mutation.key.clone(),
                    ProximityRecord {
                        key: mutation.key.clone(),
                        vector: prepare_vector(config.metric, vector, config.dimensions)?,
                        value: value.clone(),
                    },
                );
            }
            None => {
                records.remove(&mutation.key);
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prolly::proximity::distance::{query_kernel_calls, reset_query_kernel_calls};
    use crate::prolly::store::MemStore;

    fn config() -> ProximityConfig {
        let mut config = ProximityConfig::new(1);
        config.hierarchy.log_chunk_size = 1;
        config.hierarchy.level_hash_seed = 7;
        config.overflow.max_page_bytes = 256 * 1024;
        config
    }

    fn two_representative_map() -> (Arc<MemStore>, ProximityMap<Arc<MemStore>>) {
        let keys: Vec<_> = (0..10_000)
            .map(|index| format!("candidate-{index}").into_bytes())
            .filter(|key| promotion_level(key, 1, 7) == 1)
            .take(2)
            .collect();
        assert_eq!(keys.len(), 2);
        let store = Arc::new(MemStore::new());
        let map = ProximityMap::build(
            store.clone(),
            config(),
            keys.into_iter()
                .enumerate()
                .map(|(index, key)| ProximityRecord {
                    key,
                    vector: vec![index as f32],
                    value: Vec::new(),
                }),
        )
        .unwrap();
        (store, map)
    }

    #[test]
    fn construction_and_mutation_never_enter_a_query_kernel() {
        reset_query_kernel_calls();
        let store = Arc::new(MemStore::new());
        let map = ProximityMap::build(
            store,
            config(),
            (0..64).map(|index| ProximityRecord {
                key: format!("key-{index:03}").into_bytes(),
                vector: vec![index as f32],
                value: Vec::new(),
            }),
        )
        .unwrap();
        let (map, _) = map
            .mutate_batch([ProximityMutation {
                key: b"key-017".to_vec(),
                value: Some((vec![17.25], b"updated".to_vec())),
            }])
            .unwrap();
        assert_eq!(query_kernel_calls(), 0);

        let mut request = SearchRequest::exact(&[17.0], 3);
        request.kernel = super::super::QueryKernel::SimdDeterministic;
        map.search(request).unwrap();
        assert!(query_kernel_calls() > 0);
    }

    fn publish_root_descriptor(
        store: &Arc<MemStore>,
        map: &ProximityMap<Arc<MemStore>>,
        root: ProximityNode,
    ) -> Cid {
        let root_bytes = root.encode().unwrap();
        let root_cid = Cid::from_bytes(&root_bytes);
        store.put(root_cid.as_bytes(), &root_bytes).unwrap();

        let descriptor_bytes = store.get(map.tree.descriptor.as_bytes()).unwrap().unwrap();
        let mut descriptor = Descriptor::decode(&descriptor_bytes).unwrap();
        descriptor.proximity_root = root_cid;
        let descriptor_bytes = descriptor.encode();
        let descriptor_cid = Cid::from_bytes(&descriptor_bytes);
        store
            .put(descriptor_cid.as_bytes(), &descriptor_bytes)
            .unwrap();
        descriptor_cid
    }

    fn publish_replacement_root(
        store: &Arc<MemStore>,
        map: &ProximityMap<Arc<MemStore>>,
        root: ProximityNode,
    ) -> ProximityMap<Arc<MemStore>> {
        let descriptor_cid = publish_root_descriptor(store, map, root);
        ProximityMap::load(store.clone(), descriptor_cid).unwrap()
    }

    #[test]
    fn verify_rejects_a_leaf_vector_that_disagrees_with_the_exact_directory() {
        let store = Arc::new(MemStore::new());
        let mut leaf_config = config();
        leaf_config.hierarchy.log_chunk_size = 63;
        let map = ProximityMap::build(
            store.clone(),
            leaf_config,
            [ProximityRecord {
                key: b"key".to_vec(),
                vector: vec![1.0],
                value: Vec::new(),
            }],
        )
        .unwrap();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        root.entries[0].vector = super::super::storage::VectorRef::Inline(vec![2.0]);
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "leaf vector disagrees with exact directory"
        ));
    }

    #[test]
    fn verify_rejects_repeated_child_ownership() {
        let (store, map) = two_representative_map();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        assert_eq!(root.level, 1);
        assert_eq!(root.entries.len(), 2);
        root.entries[1].child = root.entries[0].child.clone();
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "cycle or repeated child ownership"
        ));
    }

    #[test]
    fn verify_rejects_an_invalid_child_level() {
        let (store, map) = two_representative_map();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        root.entries[0].child = Some(map.tree.proximity_root.clone());
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "unexpected logical level"
        ));
    }

    #[test]
    fn verify_rejects_a_representative_below_its_node_level() {
        let (store, map) = two_representative_map();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        let replacement = (0..10_000)
            .map(|index| format!("!invalid-{index}").into_bytes())
            .find(|key| promotion_level(key, 1, 7) == 0 && key < &root.entries[1].key)
            .unwrap();
        root.entries[0].key = replacement.clone();
        root.entries[0].min_key = replacement;
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "entry appears above its deterministic promotion level"
        ));
    }

    #[test]
    fn verify_rejects_a_non_nearest_parent_route() {
        let (store, map) = two_representative_map();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        let first = root.entries[0].child.clone();
        root.entries[0].child = root.entries[1].child.clone();
        root.entries[1].child = first;
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "nearest-representative invariant violated"
        ));
    }

    #[test]
    fn load_rejects_a_root_subtree_count_that_disagrees_with_the_descriptor() {
        let (store, map) = two_representative_map();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        root.subtree_count += 1;
        root.entries[0].child_count += 1;
        let descriptor = publish_root_descriptor(&store, &map, root);

        assert!(matches!(
            ProximityMap::load(store, descriptor),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "record count disagrees with proximity root"
        ));
    }

    #[test]
    fn verify_rejects_a_non_conservative_covering_radius() {
        let store = Arc::new(MemStore::new());
        let map = ProximityMap::build(
            store.clone(),
            config(),
            (0..128).map(|index| ProximityRecord {
                key: format!("radius-{index:04}").into_bytes(),
                vector: vec![index as f32],
                value: Vec::new(),
            }),
        )
        .unwrap();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        let entry = root
            .entries
            .iter_mut()
            .find(|entry| entry.covering_radius > 0.0)
            .expect("test hierarchy has a nontrivial cluster");
        entry.covering_radius = 0.0;
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { reason, .. })
                if reason == "covering-radius summary is not conservative"
        ));
    }

    #[test]
    fn verify_rejects_a_scalar_quantizer_that_disagrees_with_its_node() {
        let store = Arc::new(MemStore::new());
        let mut quantized_config = config();
        quantized_config.scalar_quantization =
            Some(super::super::ScalarQuantizationConfig { group_size: 1 });
        let map = ProximityMap::build(
            store.clone(),
            quantized_config,
            (0..64).map(|index| ProximityRecord {
                key: format!("quantized-{index:03}").into_bytes(),
                vector: vec![index as f32],
                value: Vec::new(),
            }),
        )
        .unwrap();
        let bytes = store
            .get(map.tree.proximity_root.as_bytes())
            .unwrap()
            .unwrap();
        let mut root = ProximityNode::decode(&bytes, 1).unwrap();
        let fake_vectors = vec![vec![999.0]; root.entries.len()];
        let fake_refs: Vec<_> = fake_vectors.iter().map(Vec::as_slice).collect();
        let fake = ScalarQuantized::build(&fake_refs, 1, 1).unwrap();
        let fake_bytes = fake.encode().unwrap();
        let fake_cid = Cid::from_bytes(&fake_bytes);
        store.put(fake_cid.as_bytes(), &fake_bytes).unwrap();
        root.quantizer = Some(fake_cid);
        let corrupt = publish_replacement_root(&store, &map, root);

        assert!(matches!(
            corrupt.verify(),
            Err(Error::InvalidProximityObject { kind: "quantizer", reason })
                if reason.contains("disagree")
        ));
    }
}