laurus 0.10.0

Unified search library for lexical, vector, and semantic retrieval
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
use crate::error::Result;
use crate::storage::StorageConfig;
use crate::storage::StorageFactory;
use crate::storage::memory::MemoryStorageConfig;
use crate::vector::core::distance::DistanceMetric;
use crate::vector::core::rerank::RerankStorageKind;
use crate::vector::core::vector::Vector;
use crate::vector::index::VectorIndex;
use crate::vector::index::config::HnswIndexConfig;
use crate::vector::index::hnsw::HnswIndex;
use crate::vector::index::hnsw::reader::HnswIndexReader;
use crate::vector::index::hnsw::writer::HnswIndexWriter;
use crate::vector::index::rerank_sidecar::read_sidecar;
use crate::vector::writer::{VectorIndexWriter, VectorIndexWriterConfig};
use std::sync::Arc;

#[test]
fn test_hnsw_integration() -> Result<()> {
    let storage_config = StorageConfig::Memory(MemoryStorageConfig::default());
    let storage = StorageFactory::create(storage_config)?;

    // HNSW Config
    let config = HnswIndexConfig {
        dimension: 3,
        m: 16,
        ef_construction: 100,
        distance_metric: DistanceMetric::Cosine,
        ..Default::default()
    };

    let index = HnswIndex::create(storage.clone(), "default_index", config.clone())?;
    let mut writer = index.writer()?;

    // Add vectors
    let vectors = vec![
        (1, "test".to_string(), Vector::new(vec![1.0, 0.0, 0.0])), // A
        (2, "test".to_string(), Vector::new(vec![0.0, 1.0, 0.0])), // B
        (3, "test".to_string(), Vector::new(vec![0.0, 0.0, 1.0])), // C
        (4, "test".to_string(), Vector::new(vec![0.707, 0.707, 0.0])), // Between A and B
    ];

    writer.build(vectors.clone())?;
    writer.finalize()?;
    // Note: commit is handled by VectorIndexWriter trait default which calls write("default_index")
    // Since we are using HnswIndexWriter directly via trait object or concrete?
    // index.writer() returns Box<dyn VectorIndexWriter>.
    writer.commit()?;

    // Read back
    let reader = index.reader()?;

    // Check graph loading
    use crate::vector::index::hnsw::reader::HnswIndexReader;
    let hnsw_reader = reader
        .as_any()
        .downcast_ref::<HnswIndexReader>()
        .expect("Should be HnswIndexReader");
    assert!(hnsw_reader.graph.is_some());

    // Search using Graph
    use crate::vector::index::hnsw::searcher::HnswSearcher;
    use crate::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

    let searcher = HnswSearcher::new(reader.clone())?;

    // Query close to A (1,0,0)
    let query = Vector::new(vec![0.9, 0.1, 0.0]);
    let request = VectorIndexQuery::new(query)
        .top_k(1)
        .field_name("test".to_string());

    let results = searcher.search(&request)?;

    assert_eq!(results.results.len(), 1);
    assert_eq!(results.results[0].doc_id, 1);

    // Stage 2 (Issue #481): rerank_factor against a Stage 1 segment
    // (no sidecar) must silently degrade to Stage 1 ranking — there
    // is no f32 information to recover, so returning an error would
    // be a worse experience than just returning the int8 ranking.
    let rerank_request = VectorIndexQuery::new(Vector::new(vec![0.9, 0.1, 0.0]))
        .top_k(1)
        .field_name("test".to_string())
        .rerank_factor(2);
    let degraded = searcher.search(&rerank_request)?;
    assert_eq!(degraded.results.len(), 1);
    assert_eq!(degraded.results[0].doc_id, 1);

    Ok(())
}

#[test]
fn writer_omits_sidecar_when_rerank_storage_is_none() -> Result<()> {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let config = HnswIndexConfig {
        dimension: 3,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        rerank_storage: None,
        ..Default::default()
    };
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "stage1_only",
        Arc::clone(&storage),
    )?;
    writer.add_vectors(vec![
        (1, "f".to_string(), Vector::new(vec![1.0, 0.0, 0.0])),
        (2, "f".to_string(), Vector::new(vec![0.0, 1.0, 0.0])),
    ])?;
    writer.finalize()?;
    writer.write()?;

    assert!(
        storage.file_exists("stage1_only.hnsw"),
        "main LVS1 segment must exist"
    );
    assert!(
        !storage.file_exists("stage1_only.hnsw.f32"),
        "no sidecar should be written when rerank_storage is None"
    );
    Ok(())
}

#[test]
fn writer_emits_sidecar_with_matching_header_when_rerank_storage_is_f32() -> Result<()> {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 3;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        rerank_storage: Some(RerankStorageKind::F32),
        ..Default::default()
    };
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "stage2_f32",
        Arc::clone(&storage),
    )?;

    let originals = vec![
        (1u64, "f".to_string(), Vector::new(vec![0.1, 0.2, 0.3])),
        (2u64, "f".to_string(), Vector::new(vec![-1.0, 0.5, 0.25])),
        (3u64, "f".to_string(), Vector::new(vec![0.7, -0.7, 0.0])),
    ];
    writer.add_vectors(originals.clone())?;
    writer.finalize()?;
    writer.write()?;

    assert!(
        storage.file_exists("stage2_f32.hnsw.f32"),
        "sidecar must be written when rerank_storage is Some(F32)"
    );
    let mut sidecar_in = storage.open_input("stage2_f32.hnsw.f32")?;
    let sidecar_size = sidecar_in.size()?;
    let (header, payload) = read_sidecar(&mut sidecar_in, sidecar_size)?;
    assert_eq!(header.dim as usize, dim);
    assert_eq!(header.vector_count as usize, originals.len());
    assert_eq!(header.storage_kind, RerankStorageKind::F32);
    assert_eq!(payload.len(), originals.len() * dim * 4);

    // Sidecar order matches the LVS1 sort-by-doc_id order, so the
    // first record must be doc_id 1 with values [0.1, 0.2, 0.3].
    let first_x = f32::from_le_bytes([payload[0], payload[1], payload[2], payload[3]]);
    let first_y = f32::from_le_bytes([payload[4], payload[5], payload[6], payload[7]]);
    let first_z = f32::from_le_bytes([payload[8], payload[9], payload[10], payload[11]]);
    assert!((first_x - 0.1).abs() < f32::EPSILON);
    assert!((first_y - 0.2).abs() < f32::EPSILON);
    assert!((first_z - 0.3).abs() < f32::EPSILON);
    Ok(())
}

#[test]
fn reader_loads_rerank_storage_when_sidecar_present() -> Result<()> {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 3;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        rerank_storage: Some(RerankStorageKind::F32),
        ..Default::default()
    };
    let originals = vec![
        (1u64, "f".to_string(), Vector::new(vec![0.1, 0.2, 0.3])),
        (2u64, "f".to_string(), Vector::new(vec![-1.0, 0.5, 0.25])),
    ];
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "stage2_reader",
        Arc::clone(&storage),
    )?;
    writer.add_vectors(originals.clone())?;
    writer.finalize()?;
    writer.write()?;

    let reader = HnswIndexReader::load(
        Arc::clone(&storage),
        "stage2_reader",
        DistanceMetric::Cosine,
    )?;
    let pool = reader
        .rerank_storage()
        .expect("rerank_storage must be Some when sidecar exists in Eager mode");
    assert_eq!(pool.dim, dim);
    assert_eq!(pool.vector_count, originals.len());
    assert_eq!(pool.kind, RerankStorageKind::F32);
    let v0 = pool
        .get_f32_slice(1, "f")
        .expect("doc 1 must be present in rerank pool");
    assert_eq!(v0, &[0.1, 0.2, 0.3]);
    let v1 = pool
        .get_f32_slice(2, "f")
        .expect("doc 2 must be present in rerank pool");
    assert_eq!(v1, &[-1.0, 0.5, 0.25]);
    Ok(())
}

#[test]
fn reader_rerank_storage_is_none_for_stage1_segment() -> Result<()> {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 3;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        rerank_storage: None,
        ..Default::default()
    };
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "stage1_reader",
        Arc::clone(&storage),
    )?;
    writer.add_vectors(vec![
        (1u64, "f".to_string(), Vector::new(vec![1.0, 0.0, 0.0])),
        (2u64, "f".to_string(), Vector::new(vec![0.0, 1.0, 0.0])),
    ])?;
    writer.finalize()?;
    writer.write()?;

    let reader = HnswIndexReader::load(
        Arc::clone(&storage),
        "stage1_reader",
        DistanceMetric::Cosine,
    )?;
    assert!(
        reader.rerank_storage().is_none(),
        "Stage 1 segment (no sidecar) must yield rerank_storage = None"
    );
    Ok(())
}

#[test]
fn searcher_returns_exact_f32_distance_when_rerank_storage_is_loaded() -> Result<()> {
    use crate::vector::index::hnsw::searcher::HnswSearcher;
    use crate::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 4;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 32,
        distance_metric: DistanceMetric::Euclidean,
        normalize_vectors: false,
        rerank_storage: Some(RerankStorageKind::F32),
        ..Default::default()
    };
    let originals = vec![
        (
            1u64,
            "f".to_string(),
            Vector::new(vec![0.123_456, 0.234_567, 0.345_678, 0.456_789]),
        ),
        (
            2u64,
            "f".to_string(),
            Vector::new(vec![0.987_654, 0.876_543, 0.765_432, 0.654_321]),
        ),
        (
            3u64,
            "f".to_string(),
            Vector::new(vec![-0.111_111, -0.222_222, -0.333_333, -0.444_444]),
        ),
    ];

    let index = HnswIndex::create(Arc::clone(&storage), "rerank_search", config)?;
    let mut writer = index.writer()?;
    writer.build(originals.clone())?;
    writer.finalize()?;
    writer.commit()?;

    let reader = index.reader()?;
    let mut searcher = HnswSearcher::new(reader)?;
    searcher.set_ef_search(50);

    // Query equal to doc 1's vector -> exact f32 distance must be 0.
    let request = VectorIndexQuery::new(Vector::new(vec![
        0.123_456, 0.234_567, 0.345_678, 0.456_789,
    ]))
    .top_k(1)
    .field_name("f".to_string())
    .rerank_factor(3);
    let results = searcher.search(&request)?;

    assert_eq!(results.results.len(), 1);
    assert_eq!(results.results[0].doc_id, 1, "doc 1 must be top match");
    // Stage 1 (int8) returns a small but non-zero approximation for
    // the self-distance because of quantization noise. Stage 2 with
    // rerank rescores against the original f32 vectors and must
    // recover the exact zero.
    assert_eq!(
        results.results[0].distance, 0.0,
        "rerank must restore the exact f32 self-distance, got {}",
        results.results[0].distance
    );
    Ok(())
}

#[test]
fn searcher_silently_falls_back_to_stage1_when_rerank_storage_absent() -> Result<()> {
    use crate::vector::index::hnsw::searcher::HnswSearcher;
    use crate::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 3;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        rerank_storage: None,
        ..Default::default()
    };
    let index = HnswIndex::create(Arc::clone(&storage), "stage1_with_rerank_request", config)?;
    let mut writer = index.writer()?;
    writer.build(vec![
        (1u64, "f".to_string(), Vector::new(vec![1.0, 0.0, 0.0])),
        (2u64, "f".to_string(), Vector::new(vec![0.0, 1.0, 0.0])),
    ])?;
    writer.finalize()?;
    writer.commit()?;

    let reader = index.reader()?;
    let searcher = HnswSearcher::new(reader)?;

    // Stage 1 segment + rerank_factor request must succeed (no
    // NotImplemented) and return the int8 ranking.
    let request = VectorIndexQuery::new(Vector::new(vec![1.0, 0.0, 0.0]))
        .top_k(1)
        .field_name("f".to_string())
        .rerank_factor(5);
    let results = searcher.search(&request)?;
    assert_eq!(results.results.len(), 1);
    assert_eq!(results.results[0].doc_id, 1);
    Ok(())
}

#[test]
fn writer_load_round_trips_byte_exact_via_sidecar() -> Result<()> {
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let dim = 4;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        rerank_storage: Some(RerankStorageKind::F32),
        ..Default::default()
    };
    let originals = vec![
        (
            10u64,
            "f".to_string(),
            Vector::new(vec![0.123_456, -0.987_654, 1.111_222, 0.000_001]),
        ),
        (
            20u64,
            "f".to_string(),
            Vector::new(vec![-1.5, 0.5, 0.25, -0.75]),
        ),
    ];

    {
        let mut writer = HnswIndexWriter::with_storage(
            config.clone(),
            VectorIndexWriterConfig::default(),
            "stage2_round_trip",
            Arc::clone(&storage),
        )?;
        writer.add_vectors(originals.clone())?;
        writer.finalize()?;
        writer.write()?;
    }

    let loaded = HnswIndexWriter::load(
        config,
        VectorIndexWriterConfig::default(),
        Arc::clone(&storage),
        "stage2_round_trip",
    )?;
    let loaded_vectors = loaded.vectors();
    assert_eq!(loaded_vectors.len(), originals.len());

    for (orig, got) in originals.iter().zip(loaded_vectors.iter()) {
        assert_eq!(orig.0, got.0, "doc_id");
        assert_eq!(orig.1, got.1, "field name");
        assert_eq!(
            orig.2.data, got.2.data,
            "f32 payload must round-trip byte-exact via the LRS1 sidecar"
        );
    }
    Ok(())
}

/// A corrupted sidecar must fail `HnswIndexWriter::load` (Issue #788).
///
/// This is the anti-laundering guarantee: if the writer-reload path
/// accepted a corrupted sidecar, the broken f32 values would enter the
/// writer's in-memory state and be re-emitted with a fresh, valid CRC
/// on the next commit — silently converting detectable corruption into
/// undetectable corruption.
#[test]
fn writer_load_rejects_corrupted_sidecar() -> Result<()> {
    use std::io::{Read, Write};

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let config = HnswIndexConfig {
        dimension: 4,
        m: 4,
        ef_construction: 16,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        rerank_storage: Some(RerankStorageKind::F32),
        ..Default::default()
    };
    {
        let mut writer = HnswIndexWriter::with_storage(
            config.clone(),
            VectorIndexWriterConfig::default(),
            "stage2_corrupt",
            Arc::clone(&storage),
        )?;
        writer.add_vectors(vec![
            (1u64, "f".to_string(), Vector::new(vec![1.0, 0.0, 0.0, 0.0])),
            (2u64, "f".to_string(), Vector::new(vec![0.0, 1.0, 0.0, 0.0])),
        ])?;
        writer.finalize()?;
        writer.write()?;
    }

    // Flip one byte in the middle of the sidecar payload.
    let sidecar_name = "stage2_corrupt.hnsw.f32";
    let mut bytes = Vec::new();
    storage
        .open_input(sidecar_name)?
        .read_to_end(&mut bytes)
        .map_err(crate::error::LaurusError::from)?;
    let payload_mid = crate::vector::index::rerank_sidecar::HEADER_SIZE
        + (bytes.len()
            - crate::vector::index::rerank_sidecar::HEADER_SIZE
            - crate::vector::index::rerank_sidecar::FOOTER_SIZE)
            / 2;
    bytes[payload_mid] ^= 0xff;
    let mut out = storage.create_output(sidecar_name)?;
    out.write_all(&bytes)
        .map_err(crate::error::LaurusError::from)?;
    out.close()?;

    let result = HnswIndexWriter::load(
        config,
        VectorIndexWriterConfig::default(),
        Arc::clone(&storage),
        "stage2_corrupt",
    );
    // Pin the failure to the CRC check: the load path also has
    // dim/count-mismatch (InvalidOperation) and Io exits right after
    // read_sidecar, and those must not satisfy this test.
    match result {
        Err(crate::error::LaurusError::Index(msg)) => {
            assert!(
                msg.contains("checksum mismatch"),
                "expected a checksum mismatch, got: {msg}"
            );
        }
        Err(other) => panic!(
            "a corrupted .hnsw.f32 must fail the writer reload path \
             with a checksum error, got {other:?}"
        ),
        Ok(_) => panic!(
            "a corrupted .hnsw.f32 must be rejected by the writer \
             reload path, got Ok"
        ),
    }
    Ok(())
}

#[test]
fn test_hnsw_pq_search_returns_corpus_neighbour() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;
    use crate::vector::index::hnsw::searcher::HnswSearcher;
    use crate::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

    let storage_config = StorageConfig::Memory(MemoryStorageConfig::default());
    let storage = StorageFactory::create(storage_config)?;

    // Two well-separated clusters in 4-D Euclidean space. M=2 → sub_dim=2.
    let config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantization { subvector_count: 2 },
        ..Default::default()
    };

    let index = HnswIndex::create(storage.clone(), "pq_round_trip", config.clone())?;
    let mut writer = index.writer()?;

    // Two widely-separated clusters with 128 points each — 256 total,
    // meeting the PQ min-train threshold (#880: segments with fewer vectors
    // than the 256 k-means centroids are written as Scalar8Bit, so a
    // smaller corpus would silently stop exercising the PQ path this test
    // exists for). The large cluster separation keeps the quantiser stable
    // across platforms (issue #730: platform-dependent f32 reduction order
    // in k-means could otherwise flip a near/far quantisation code).
    //
    // Near cluster (doc_ids 1..=128) sits around (10, 10, 20, 20); far
    // cluster (doc_ids 129..=256) sits around (-100, -100, -200, -200).
    const POINTS_PER_CLUSTER: usize = 128;
    let offset = |i: usize| -> [f32; 4] {
        [
            ((i % 8) as f32) * 0.04 - 0.14,
            ((i / 8 % 8) as f32) * 0.04 - 0.14,
            ((i / 64 % 8) as f32) * 0.04 - 0.14,
            ((i % 16) as f32) * 0.04 - 0.32,
        ]
    };
    let near_base = [10.0_f32, 10.0, 20.0, 20.0];
    let far_base = [-100.0_f32, -100.0, -200.0, -200.0];

    let mut vectors = Vec::with_capacity(2 * POINTS_PER_CLUSTER);
    for i in 0..POINTS_PER_CLUSTER {
        let off = offset(i);
        let v: Vec<f32> = near_base.iter().zip(&off).map(|(b, o)| b + o).collect();
        vectors.push(((i + 1) as u64, "embedding".to_string(), Vector::new(v)));
    }
    for i in 0..POINTS_PER_CLUSTER {
        let off = offset(i);
        let v: Vec<f32> = far_base.iter().zip(&off).map(|(b, o)| b + o).collect();
        vectors.push((
            (i + 1 + POINTS_PER_CLUSTER) as u64,
            "embedding".to_string(),
            Vector::new(v),
        ));
    }

    writer.build(vectors.clone())?;
    writer.finalize()?;
    writer.commit()?;

    let reader = index.reader()?;
    let searcher = HnswSearcher::new(reader)?;

    // Query at the near cluster centre — every top-3 result must come from
    // the near cluster (doc_ids 1..=128), never the far cluster. The exact
    // ordering within the near cluster is not asserted because PQ is
    // approximate; only cluster membership is guaranteed by the large
    // separation.
    let query = Vector::new(vec![10.0, 10.0, 20.0, 20.0]);
    let request = VectorIndexQuery::new(query)
        .top_k(3)
        .field_name("embedding".to_string());
    let results = searcher.search(&request)?;
    assert_eq!(results.results.len(), 3, "expected top-3 results");
    let ids: std::collections::HashSet<u64> = results.results.iter().map(|r| r.doc_id).collect();
    for id in &ids {
        assert!(
            (1..=POINTS_PER_CLUSTER as u64).contains(id),
            "top-3 must all be near-cluster doc_ids (1..=128); got {ids:?}",
        );
    }
    Ok(())
}

/// Issue #631: a segment below `PQ_MIN_TRAIN_VECTORS` (256) normally
/// degrades to Scalar8Bit (nothing to train k-means on) -- but when a
/// shared codebook is configured there is nothing to train either way,
/// so the degradation rationale no longer applies and the segment
/// should stay Product Quantization, encoding directly against the
/// shared codebook.
#[test]
fn shared_pq_codebook_keeps_small_segment_on_pq() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;
    use crate::vector::index::pq_codebook::train_and_write_pq_codebook;
    use crate::vector::index::storage::VectorStorage;

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;

    // Train the shared codebook on a representative sample well above the
    // min-train threshold, entirely separate from the tiny corpus the
    // segment itself will hold.
    let mut state: u64 = 0xABCD_EF01_2345_6789;
    let training_sample: Vec<Vector> = (0..300)
        .map(|_| {
            let data: Vec<f32> = (0..4)
                .map(|_| {
                    state = state
                        .wrapping_mul(6_364_136_223_846_793_005)
                        .wrapping_add(1_442_695_040_888_963_407);
                    ((state >> 33) as f32 / u32::MAX as f32) * 2.0 - 1.0
                })
                .collect();
            Vector::new(data)
        })
        .collect();
    train_and_write_pq_codebook(
        storage.as_ref(),
        "shared.pqcb",
        4,
        2,
        256,
        false,
        &training_sample,
    )?;

    let mut config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantization { subvector_count: 2 },
        pq_codebook_path: Some("shared.pqcb".to_string()),
        ..Default::default()
    };
    config.resolve_pq_codebook(storage.as_ref())?;
    assert!(
        config.pq_codebook.is_some(),
        "the shared codebook must resolve from the file just written"
    );

    let index = HnswIndex::create(storage.clone(), "small_segment", config)?;
    let mut writer = index.writer()?;
    // Only 10 vectors -- far below PQ_MIN_TRAIN_VECTORS (256).
    let vectors: Vec<_> = (0..10u64)
        .map(|i| {
            (
                i + 1,
                "embedding".to_string(),
                Vector::new(vec![i as f32, i as f32, i as f32 * 2.0, i as f32 * 2.0]),
            )
        })
        .collect();
    writer.build(vectors)?;
    writer.finalize()?;
    writer.commit()?;

    let reader = index.reader()?;
    let hnsw_reader = reader
        .as_any()
        .downcast_ref::<HnswIndexReader>()
        .expect("HnswIndex::reader() always returns an HnswIndexReader");
    assert!(
        matches!(hnsw_reader.vectors(), VectorStorage::OwnedPq(_)),
        "a 10-vector segment with a shared codebook configured must stay \
         Product Quantization, not degrade to Scalar8Bit"
    );
    Ok(())
}

/// Issue #631: a `pq_codebook_path` naming a file that has not been
/// trained yet must not block opening the index (the schema may be
/// created before `laurus train pq-codebook` runs) -- but `write()`
/// must fail loudly, with the exact command to fix it, rather than
/// silently falling back to per-segment training (which would defeat
/// the point of configuring a shared codebook at all).
#[test]
fn missing_shared_pq_codebook_is_lenient_at_open_but_errors_at_write() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let mut config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantization { subvector_count: 2 },
        pq_codebook_path: Some("not-yet-trained.pqcb".to_string()),
        ..Default::default()
    };
    // Lenient at open: no file exists yet, but resolution must not error.
    config.resolve_pq_codebook(storage.as_ref())?;
    assert!(config.pq_codebook.is_none());

    let index = HnswIndex::create(storage.clone(), "no_codebook_yet", config)?;
    let mut writer = index.writer()?;
    let vectors: Vec<_> = (0..300u64)
        .map(|i| {
            (
                i + 1,
                "embedding".to_string(),
                Vector::new(vec![i as f32, i as f32, i as f32 * 2.0, i as f32 * 2.0]),
            )
        })
        .collect();
    writer.build(vectors)?;
    writer.finalize()?;

    // A path is configured but never resolved to an actual codebook --
    // `write()` must fail loudly (naming the configured path and the fix)
    // rather than silently falling back to training a fresh codebook,
    // which would defeat the point of configuring a shared codebook.
    let err = writer.write().expect_err(
        "write() must reject a configured-but-unresolved pq_codebook_path instead of \
         silently training a fresh codebook",
    );
    let message = err.to_string();
    assert!(
        message.contains("not-yet-trained.pqcb"),
        "error must name the configured path, got: {message}"
    );
    Ok(())
}

/// Issue #920 (FastScan mirror of `shared_pq_codebook_keeps_small_segment_on_pq`):
/// a segment below `PQ_FASTSCAN_MIN_TRAIN_VECTORS` (16) normally
/// degrades to Scalar8Bit, but with a shared k=16 codebook configured
/// nothing is trained, so the segment must stay FastScan and encode
/// against the shared codebook.
#[cfg(feature = "pq-fastscan")]
#[test]
fn shared_pq_codebook_keeps_small_segment_on_fastscan() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;
    use crate::vector::index::pq_codebook::train_and_write_pq_codebook;
    use crate::vector::index::storage::VectorStorage;

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;

    let mut state: u64 = 0xABCD_EF01_2345_6789;
    let training_sample: Vec<Vector> = (0..300)
        .map(|_| {
            let data: Vec<f32> = (0..4)
                .map(|_| {
                    state = state
                        .wrapping_mul(6_364_136_223_846_793_005)
                        .wrapping_add(1_442_695_040_888_963_407);
                    ((state >> 33) as f32 / u32::MAX as f32) * 2.0 - 1.0
                })
                .collect();
            Vector::new(data)
        })
        .collect();
    train_and_write_pq_codebook(
        storage.as_ref(),
        "shared-fs.pqcb",
        4,
        2,
        16,
        false,
        &training_sample,
    )?;

    let mut config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantizationFastScan { subvector_count: 2 },
        pq_codebook_path: Some("shared-fs.pqcb".to_string()),
        ..Default::default()
    };
    config.resolve_pq_codebook(storage.as_ref())?;
    assert!(
        config.pq_codebook.is_some(),
        "the shared k=16 codebook must resolve from the file just written"
    );

    let index = HnswIndex::create(storage.clone(), "small_fs_segment", config)?;
    let mut writer = index.writer()?;
    // Only 10 vectors -- below PQ_FASTSCAN_MIN_TRAIN_VECTORS (16).
    let vectors: Vec<_> = (0..10u64)
        .map(|i| {
            (
                i + 1,
                "embedding".to_string(),
                Vector::new(vec![i as f32, i as f32, i as f32 * 2.0, i as f32 * 2.0]),
            )
        })
        .collect();
    writer.build(vectors)?;
    writer.finalize()?;
    writer.commit()?;

    let reader = index.reader()?;
    let hnsw_reader = reader
        .as_any()
        .downcast_ref::<HnswIndexReader>()
        .expect("HnswIndex::reader() always returns an HnswIndexReader");
    assert!(
        matches!(hnsw_reader.vectors(), VectorStorage::OwnedPqFastScan(_)),
        "a 10-vector segment with a shared k=16 codebook configured must stay \
         FastScan, not degrade to Scalar8Bit"
    );
    Ok(())
}

/// Issue #920 (FastScan mirror of
/// `missing_shared_pq_codebook_is_lenient_at_open_but_errors_at_write`):
/// a configured-but-untrained `pq_codebook_path` on a FastScan field
/// must fail loudly at write — before this fix it was silently ignored
/// and every segment retrained k-means.
#[cfg(feature = "pq-fastscan")]
#[test]
fn missing_shared_pq_codebook_errors_at_write_for_fastscan() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let mut config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantizationFastScan { subvector_count: 2 },
        pq_codebook_path: Some("not-yet-trained-fs.pqcb".to_string()),
        ..Default::default()
    };
    config.resolve_pq_codebook(storage.as_ref())?;
    assert!(config.pq_codebook.is_none());

    let index = HnswIndex::create(storage.clone(), "no_fs_codebook_yet", config)?;
    let mut writer = index.writer()?;
    let vectors: Vec<_> = (0..300u64)
        .map(|i| {
            (
                i + 1,
                "embedding".to_string(),
                Vector::new(vec![i as f32, i as f32, i as f32 * 2.0, i as f32 * 2.0]),
            )
        })
        .collect();
    writer.build(vectors)?;
    writer.finalize()?;

    let err = writer.write().expect_err(
        "write() must reject a configured-but-unresolved pq_codebook_path on a \
         FastScan field instead of silently training a fresh codebook",
    );
    let message = err.to_string();
    assert!(
        message.contains("not-yet-trained-fs.pqcb"),
        "error must name the configured path, got: {message}"
    );
    Ok(())
}

/// Issue #920: a k=256 (standard PQ) codebook configured on a FastScan
/// field must be rejected loudly at write with the k mismatch named.
#[cfg(feature = "pq-fastscan")]
#[test]
fn k256_codebook_on_fastscan_field_errors_at_write() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;
    use crate::vector::index::pq_codebook::train_and_write_pq_codebook;

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;

    let mut state: u64 = 0xABCD_EF01_2345_6789;
    let training_sample: Vec<Vector> = (0..300)
        .map(|_| {
            let data: Vec<f32> = (0..4)
                .map(|_| {
                    state = state
                        .wrapping_mul(6_364_136_223_846_793_005)
                        .wrapping_add(1_442_695_040_888_963_407);
                    ((state >> 33) as f32 / u32::MAX as f32) * 2.0 - 1.0
                })
                .collect();
            Vector::new(data)
        })
        .collect();
    // Standard-PQ (k=256) codebook...
    train_and_write_pq_codebook(
        storage.as_ref(),
        "wrong-k.pqcb",
        4,
        2,
        256,
        false,
        &training_sample,
    )?;

    // ...configured on a FastScan (k=16) field.
    let mut config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 50,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantizationFastScan { subvector_count: 2 },
        pq_codebook_path: Some("wrong-k.pqcb".to_string()),
        ..Default::default()
    };
    config.resolve_pq_codebook(storage.as_ref())?;

    let index = HnswIndex::create(storage.clone(), "wrong_k_fs", config)?;
    let mut writer = index.writer()?;
    let vectors: Vec<_> = (0..300u64)
        .map(|i| {
            (
                i + 1,
                "embedding".to_string(),
                Vector::new(vec![i as f32, i as f32, i as f32 * 2.0, i as f32 * 2.0]),
            )
        })
        .collect();
    writer.build(vectors)?;
    writer.finalize()?;

    let err = writer
        .write()
        .expect_err("a k=256 codebook must not encode a FastScan field");
    let message = err.to_string();
    assert!(
        message.contains("k = 256") && message.contains("k = 16"),
        "error must name both the stored and required k, got: {message}"
    );
    Ok(())
}

/// Regression / parity test for Issue #644: HNSW `ef_search` must honour
/// the per-query override and the schema-level default, instead of being
/// permanently capped at the historical `50` constant.
#[test]
fn hnsw_searcher_honours_per_query_and_schema_ef_search() -> Result<()> {
    use crate::vector::index::hnsw::searcher::HnswSearcher;
    use crate::vector::search::searcher::{VectorIndexQuery, VectorIndexSearcher};

    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let config = HnswIndexConfig {
        dimension: 3,
        m: 16,
        ef_construction: 64,
        // Schema-level default lifts the searcher's fallback well above
        // the legacy hardcoded `50`.
        default_ef_search: Some(300),
        distance_metric: DistanceMetric::Cosine,
        ..Default::default()
    };

    let index = HnswIndex::create(storage.clone(), "ef_test", config)?;
    let mut writer = index.writer()?;
    let vectors = (0..32u64)
        .map(|i| {
            let mut v = vec![0.0_f32; 3];
            v[(i as usize) % 3] = 1.0 + (i as f32) * 0.01;
            (i, "vec".to_string(), Vector::new(v))
        })
        .collect::<Vec<_>>();
    writer.build(vectors)?;
    writer.finalize()?;
    writer.commit()?;

    // The searcher built via `HnswIndex::searcher()` must pick up the
    // schema-level `default_ef_search` (Issue #644).
    let searcher = index.searcher()?;
    let request = VectorIndexQuery::new(Vector::new(vec![1.0, 0.0, 0.0]))
        .top_k(5)
        .field_name("vec".to_string());
    let results = searcher.search(&request)?;
    assert!(
        !results.results.is_empty(),
        "expected non-empty results from schema-default search path"
    );

    // Construct a direct HnswSearcher and exercise the per-query override.
    let reader = index.reader()?;
    let direct = HnswSearcher::new(reader.clone())?;
    let override_request = VectorIndexQuery::new(Vector::new(vec![1.0, 0.0, 0.0]))
        .top_k(5)
        .field_name("vec".to_string())
        .ef_search(400);
    let with_override = direct.search(&override_request)?;
    assert!(
        !with_override.results.is_empty(),
        "expected non-empty results from per-query override search path"
    );

    Ok(())
}

/// A [`StorageInput`] that counts every byte read from its inner stream, used to
/// measure the I/O an Eager `.hnsw` load performs (Issue #789).
#[derive(Debug)]
struct CountingInput {
    inner: Box<dyn crate::storage::StorageInput>,
    counter: Arc<std::sync::atomic::AtomicU64>,
}

impl std::io::Read for CountingInput {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let n = self.inner.read(buf)?;
        self.counter
            .fetch_add(n as u64, std::sync::atomic::Ordering::Relaxed);
        Ok(n)
    }
}

impl std::io::Seek for CountingInput {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.inner.seek(pos)
    }
}

impl crate::storage::StorageInput for CountingInput {
    fn size(&self) -> Result<u64> {
        self.inner.size()
    }

    fn clone_input(&self) -> Result<Box<dyn crate::storage::StorageInput>> {
        Ok(Box::new(CountingInput {
            inner: self.inner.clone_input()?,
            counter: Arc::clone(&self.counter),
        }))
    }

    fn close(&mut self) -> Result<()> {
        self.inner.close()
    }

    // Force every read through `read` (and thus the counter); the HNSW reader
    // never takes the zero-copy `as_slice` path, so this matches production.
    fn as_slice(&self) -> Option<&[u8]> {
        None
    }
}

/// A [`Storage`] that wraps another and counts the bytes read from files whose
/// name ends in `.hnsw`, so a test can assert how many passes a load makes over
/// the segment (Issue #789). All other operations delegate unchanged.
#[derive(Debug)]
struct CountingStorage {
    inner: Arc<dyn crate::storage::Storage>,
    hnsw_bytes_read: Arc<std::sync::atomic::AtomicU64>,
}

impl crate::storage::Storage for CountingStorage {
    fn open_input(&self, name: &str) -> Result<Box<dyn crate::storage::StorageInput>> {
        let input = self.inner.open_input(name)?;
        if name.ends_with(".hnsw") {
            Ok(Box::new(CountingInput {
                inner: input,
                counter: Arc::clone(&self.hnsw_bytes_read),
            }))
        } else {
            Ok(input)
        }
    }

    fn create_output(&self, name: &str) -> Result<Box<dyn crate::storage::StorageOutput>> {
        self.inner.create_output(name)
    }

    fn create_output_append(&self, name: &str) -> Result<Box<dyn crate::storage::StorageOutput>> {
        self.inner.create_output_append(name)
    }

    fn file_exists(&self, name: &str) -> bool {
        self.inner.file_exists(name)
    }

    fn delete_file(&self, name: &str) -> Result<()> {
        self.inner.delete_file(name)
    }

    fn list_files(&self) -> Result<Vec<String>> {
        self.inner.list_files()
    }

    fn file_size(&self, name: &str) -> Result<u64> {
        self.inner.file_size(name)
    }

    fn metadata(&self, name: &str) -> Result<crate::storage::FileMetadata> {
        self.inner.metadata(name)
    }

    fn rename_file(&self, old_name: &str, new_name: &str) -> Result<()> {
        self.inner.rename_file(old_name, new_name)
    }

    fn create_temp_output(
        &self,
        prefix: &str,
    ) -> Result<(String, Box<dyn crate::storage::StorageOutput>)> {
        self.inner.create_temp_output(prefix)
    }

    fn sync(&self) -> Result<()> {
        self.inner.sync()
    }

    fn close(&mut self) -> Result<()> {
        // The inner storage is shared behind an `Arc`; nothing to close here.
        Ok(())
    }
}

/// Eager load must read the `.hnsw` segment exactly once (Issue #789).
///
/// The integrity CRC is folded into the single structural pass, so the only
/// `.hnsw` reads are the 8-byte footer probe plus one sequential pass over the
/// content — `file_size` bytes total. Before #789, verification ran as a
/// separate full pass, so a footer-carrying segment was read ~twice
/// (`2 * content_len + 8`). Asserting the exact single-pass byte count is a
/// deterministic regression guard against the double-read returning.
#[test]
fn eager_load_reads_hnsw_segment_exactly_once() -> Result<()> {
    let inner = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let config = HnswIndexConfig {
        dimension: 4,
        m: 8,
        ef_construction: 32,
        distance_metric: DistanceMetric::Cosine,
        normalize_vectors: false,
        ..Default::default()
    };
    // A handful of vectors makes the segment comfortably larger than the
    // 8-byte footer, so a single pass and a double pass differ unambiguously.
    let vectors: Vec<(u64, String, Vector)> = (0..32)
        .map(|i| {
            let f = i as f32;
            (
                i,
                "f".to_string(),
                Vector::new(vec![f, f + 1.0, f + 2.0, f + 3.0]),
            )
        })
        .collect();
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "count_seg",
        Arc::clone(&inner),
    )?;
    writer.add_vectors(vectors)?;
    writer.finalize()?;
    writer.write()?;

    let file_size = inner.file_size("count_seg.hnsw")?;
    assert!(
        file_size > crate::vector::index::hnsw::HNSW_FOOTER_LEN,
        "segment must carry a footer for this measurement"
    );

    let hnsw_bytes_read = Arc::new(std::sync::atomic::AtomicU64::new(0));
    let counting: Arc<dyn crate::storage::Storage> = Arc::new(CountingStorage {
        inner: Arc::clone(&inner),
        hnsw_bytes_read: Arc::clone(&hnsw_bytes_read),
    });
    // Default loading_mode() is Eager, which is the folded path under test.
    assert!(matches!(
        counting.loading_mode(),
        crate::storage::LoadingMode::Eager
    ));

    let _reader = HnswIndexReader::load(counting, "count_seg", DistanceMetric::Cosine)?;

    let read = hnsw_bytes_read.load(std::sync::atomic::Ordering::Relaxed);
    assert_eq!(
        read, file_size,
        "Eager load must read the segment exactly once (footer probe + one \
         folded pass = file_size); a double-read would be ~2x content_len"
    );
    Ok(())
}

/// A corrupted pq-fastscan Eager segment must be rejected by the folded CRC
/// (Issue #789).
///
/// The default-quantizer corruption tests only cover Scalar8Bit. The
/// `OwnedPqFastScan` branch of [`HnswIndexReader::load`] also reads purely
/// sequentially (`read_pq_fastscan_record` is `Read`-bound, never seeks), so
/// `is_sequential()` stays true and the CRC is folded into the single Eager
/// pass on this branch too. This test locks in that a byte flip on a
/// non-Scalar8Bit segment is still detected.
#[cfg(feature = "pq-fastscan")]
#[test]
fn eager_load_rejects_corrupted_pq_fastscan_segment() -> Result<()> {
    use crate::vector::core::quantization::QuantizationMethod;
    use std::io::{Read, Write};

    let dim = 8usize;
    let sub = 4usize;
    let n = 64u64;
    let storage = StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
    let config = HnswIndexConfig {
        dimension: dim,
        m: 16,
        ef_construction: 100,
        distance_metric: DistanceMetric::Euclidean,
        quantization_method: QuantizationMethod::ProductQuantizationFastScan {
            subvector_count: sub,
        },
        ..Default::default()
    };
    // Deterministic, broadly-spread vectors so the K=16 codebook trainer
    // converges to non-degenerate centroids (mirrors pq_fastscan_search_test).
    let vectors: Vec<(u64, String, Vector)> = (0..n)
        .map(|i| {
            let s = i as usize;
            let v: Vec<f32> = (0..dim)
                .map(|d| ((s * 31 + d * 17) % 257) as f32 - 128.0)
                .collect();
            (i, "f".to_string(), Vector::new(v))
        })
        .collect();
    let mut writer = HnswIndexWriter::with_storage(
        config,
        VectorIndexWriterConfig::default(),
        "pqfs_seg",
        Arc::clone(&storage),
    )?;
    writer.add_vectors(vectors)?;
    writer.finalize()?;
    writer.write()?;

    // Sanity: the clean segment loads.
    HnswIndexReader::load(Arc::clone(&storage), "pqfs_seg", DistanceMetric::Euclidean)?;

    // Flip a byte deep in the content (well before the 8-byte footer); the
    // folded CRC must reject the segment on the next load.
    let mut bytes = {
        let mut input = storage.open_input("pqfs_seg.hnsw")?;
        let mut buf = Vec::new();
        input
            .read_to_end(&mut buf)
            .expect("read pq-fastscan segment");
        buf
    };
    let mid = bytes.len() / 2;
    bytes[mid] ^= 0xff;
    {
        let mut out = storage.create_output("pqfs_seg.hnsw")?;
        out.write_all(&bytes).expect("rewrite corrupted segment");
        out.close()?;
    }

    let result = HnswIndexReader::load(Arc::clone(&storage), "pqfs_seg", DistanceMetric::Euclidean);
    assert!(
        result.is_err(),
        "a corrupted pq-fastscan .hnsw must be rejected on Eager load, got Ok"
    );
    Ok(())
}

/// Issue #841: HNSW **level assignment** must be deterministic — the
/// level RNG is seeded with a fixed constant ([`LEVEL_RNG_SEED`] in the
/// writer), so building the same vector set twice yields the same entry
/// point, max level, and per-node layer counts.
///
/// Neighbor lists are deliberately NOT compared: graph insertion runs in
/// parallel (`ConcurrentHnswGraph` + rayon), so neighbor selection still
/// depends on thread interleaving. That residual nondeterminism is
/// documented on #841; this test pins exactly the invariant the seeded
/// RNG guarantees.
#[test]
fn graph_build_levels_are_deterministic_across_writers() -> Result<()> {
    /// Loaded level shape: `(entry_point, max_level, per-node layer counts)`.
    type LevelShape = (Option<u64>, usize, Vec<(u64, usize)>);

    /// Build one segment from a fixed 64-vector set and return its
    /// loaded [`LevelShape`].
    fn build(name: &str) -> Result<LevelShape> {
        let storage =
            StorageFactory::create(StorageConfig::Memory(MemoryStorageConfig::default()))?;
        let config = HnswIndexConfig {
            dimension: 4,
            m: 4,
            ef_construction: 16,
            distance_metric: DistanceMetric::Cosine,
            ..Default::default()
        };
        let mut writer = HnswIndexWriter::with_storage(
            config,
            VectorIndexWriterConfig::default(),
            name,
            Arc::clone(&storage),
        )?;
        // Deterministic non-trivial vectors; 64 nodes give the level
        // RNG room to produce multiple layers.
        let vectors: Vec<(u64, String, Vector)> = (0..64u64)
            .map(|i| {
                let t = i as f32;
                (
                    i,
                    "f".to_string(),
                    Vector::new(vec![
                        (t * 0.37).sin(),
                        (t * 0.73).cos(),
                        (t * 0.11).sin(),
                        (t * 0.53).cos(),
                    ]),
                )
            })
            .collect();
        writer.add_vectors(vectors)?;
        writer.finalize()?;
        writer.write()?;

        let reader = HnswIndexReader::load(storage, name, DistanceMetric::Cosine)?;
        let graph = reader.graph.as_ref().expect("segment must carry a graph");
        // `iter_nodes` yields nodes in ordinal (= ascending doc id) order,
        // so the collected shape is deterministic by construction. The
        // entry point is compared as a doc id (stable across builds),
        // not as an ordinal.
        let levels = graph
            .iter_nodes()
            .map(|(id, layers)| (id, layers.len()))
            .collect();
        Ok((
            graph.entry_point().map(|ord| graph.doc_id(ord)),
            graph.max_level(),
            levels,
        ))
    }

    let a = build("determinism_a")?;
    let b = build("determinism_b")?;
    assert_eq!(a.0, b.0, "entry point must be identical across builds");
    assert_eq!(a.1, b.1, "max level must be identical across builds");
    assert_eq!(
        a.2, b.2,
        "every node's layer count must be identical across builds"
    );
    Ok(())
}