vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
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
//! Vector similarity search over indexed chunks.
//!
//! Provides semantic search using the embedding pipeline and sqlite-vec
//! vector store. Generates a query embedding via the configured API provider,
//! then performs nearest-neighbor lookup, and hydrates results from the
//! metadata store. Finds semantically related code even when query terms
//! don't appear literally in results (e.g., "memory allocation" finds `alloc`).

use std::time::{Duration, Instant};

use anyhow::Result;
use tracing::{debug, warn};

use crate::embedding::{EmbeddingError, EmbeddingProvider};
use crate::storage::metadata::MetadataStore;
use crate::storage::vector::{MAX_KNN_K, VectorStore};
use crate::types::SearchResult;

/// Errors specific to vector search.
#[derive(Debug, thiserror::Error)]
pub enum VectorSearchError {
    /// The embedding API is unavailable (connection, auth, etc.).
    #[error("embedding API unavailable: {source}")]
    EmbeddingUnavailable {
        #[from]
        source: EmbeddingError,
    },

    /// Storage or metadata error.
    #[error("storage error: {0:#}")]
    StorageError(#[from] anyhow::Error),
}

/// Perform vector search using pre-opened stores (useful for testing and reuse).
///
/// Generates a query embedding, runs nearest-neighbor search in the vector
/// store, then hydrates each result with full chunk metadata. Results are
/// returned sorted by similarity score in descending order.
///
/// The similarity score is derived from the distance: `score = 1.0 / (1.0 + distance)`.
/// This transforms the raw distance (lower = closer) into a 0–1 similarity score
/// (higher = more similar), which is consistent with other scoring conventions
/// in the retrieval pipeline.
pub async fn search_vector_with_stores(
    vector_store: &VectorStore,
    metadata_store: &MetadataStore,
    provider: &impl EmbeddingProvider,
    query: &str,
    limit: usize,
) -> Result<Vec<SearchResult>, VectorSearchError> {
    search_vector_with_stores_timed(vector_store, metadata_store, provider, query, limit)
        .await
        .map(|(results, _)| results)
}

/// Like [`search_vector_with_stores`], but also reports how long the query
/// embedding took.
///
/// The embedding call is nested inside the vector search, so a caller that
/// reports per-stage timings cannot otherwise separate model cost from storage
/// cost. The returned duration covers only [`generate_query_embedding`]; it is
/// [`Duration::ZERO`] when `limit` is 0 and no embedding is generated.
pub async fn search_vector_with_stores_timed(
    vector_store: &VectorStore,
    metadata_store: &MetadataStore,
    provider: &impl EmbeddingProvider,
    query: &str,
    limit: usize,
) -> Result<(Vec<SearchResult>, Duration), VectorSearchError> {
    if limit == 0 {
        return Ok((Vec::new(), Duration::ZERO));
    }

    // 1. Generate query embedding.
    let embed_start = Instant::now();
    let query_embedding = generate_query_embedding(provider, query, vector_store.dim()).await?;
    let embed_elapsed = embed_start.elapsed();

    debug!(
        query = query,
        dim = query_embedding.len(),
        "generated query embedding"
    );

    let results =
        search_vector_from_embedding(vector_store, metadata_store, query, limit, &query_embedding)?;

    Ok((results, embed_elapsed))
}

/// Perform vector search with stores cached behind short-lived mutex guards.
///
/// The embedding call happens before either lock is acquired. The locks are
/// held only across the synchronous sqlite-vec search and metadata hydration,
/// never across the provider await.
pub(crate) async fn search_vector_with_cached_stores_timed(
    vector_store: &std::sync::Mutex<VectorStore>,
    metadata_store: &std::sync::Mutex<MetadataStore>,
    provider: &impl EmbeddingProvider,
    query: &str,
    limit: usize,
) -> Result<(Vec<SearchResult>, Duration), VectorSearchError> {
    if limit == 0 {
        return Ok((Vec::new(), Duration::ZERO));
    }

    let stored_dim = vector_store
        .lock()
        .map_err(|_| {
            VectorSearchError::StorageError(anyhow::anyhow!("vector store lock poisoned"))
        })?
        .dim();
    let embed_start = Instant::now();
    let query_embedding = generate_query_embedding(provider, query, stored_dim).await?;
    let embed_elapsed = embed_start.elapsed();

    debug!(
        query = query,
        dim = query_embedding.len(),
        "generated query embedding"
    );

    let vector_store = vector_store.lock().map_err(|_| {
        VectorSearchError::StorageError(anyhow::anyhow!("vector store lock poisoned"))
    })?;
    let metadata_store = metadata_store.lock().map_err(|_| {
        VectorSearchError::StorageError(anyhow::anyhow!("metadata store lock poisoned"))
    })?;
    let results = search_vector_from_embedding(
        &vector_store,
        &metadata_store,
        query,
        limit,
        &query_embedding,
    )?;

    Ok((results, embed_elapsed))
}

fn search_vector_from_embedding(
    vector_store: &VectorStore,
    metadata_store: &MetadataStore,
    query: &str,
    limit: usize,
    query_embedding: &[f32],
) -> Result<Vec<SearchResult>, VectorSearchError> {
    // 1. Search the vector store for nearest neighbors.
    let (requested, candidates) = candidate_pool(limit);
    if requested > candidates {
        // Deliberately without the query text: the surrounding `debug!` calls
        // carry it, but this one is on by default, and a search query is user
        // content that should not be raised into default-visible logs.
        warn!(
            requested,
            fetched = candidates,
            "candidate pool exceeds the sqlite-vec KNN cap; the filter and \
             metadata over-fetch are inert above it"
        );
    }

    let vector_results = vector_store
        .search(query_embedding, candidates)
        .map_err(|e| VectorSearchError::StorageError(e.context("vector search failed")))?;

    debug!(
        query = query,
        raw_results = vector_results.len(),
        "vector search returned candidates"
    );

    // 2. Hydrate results from metadata store and convert distances to scores.
    // Fetched in a single batch query instead of one query per candidate
    // (N+1). SQLite does not preserve `IN (...)` row order, so results are
    // re-projected back into the original vector-distance order below.
    let ids: Vec<String> = vector_results
        .iter()
        .map(|vr| vr.chunk_id.clone())
        .collect();
    let mut chunk_by_id = metadata_store.get_chunks_by_ids(&ids).map_err(|e| {
        VectorSearchError::StorageError(e.context("failed to batch-fetch chunk metadata"))
    })?;

    let mut results = Vec::with_capacity(vector_results.len());

    for vr in &vector_results {
        let Some(chunk) = chunk_by_id.remove(&vr.chunk_id) else {
            debug!(
                chunk_id = %vr.chunk_id,
                "chunk metadata not found, skipping"
            );
            continue;
        };

        // Convert distance to similarity score: higher is better.
        let score = distance_to_similarity(vr.distance);

        results.push(chunk.into_search_result(score));

        if results.len() >= limit {
            break;
        }
    }

    debug!(
        query = query,
        returned = results.len(),
        "vector search complete"
    );

    Ok(results)
}

/// Size the KNN request for a caller-selected pool, bounded by the backend cap.
///
/// Returns `(requested, fetched)`. Extra candidates are fetched to absorb chunks
/// whose metadata has gone missing, without doubling an already-large pool.
///
/// The two values differ once the compounded over-fetch runs past
/// [`MAX_KNN_K`]. Callers stack several multipliers before reaching here (query
/// type, then a filter over-fetch, then this one), so a natural-language query
/// with an active filter can ask for more than sqlite-vec will serve. Bounding
/// it here rather than letting the storage layer clamp keeps the ceiling
/// visible to the one place that can report it; the same `k` reaches sqlite-vec
/// either way, so ranking is unchanged.
fn candidate_pool(limit: usize) -> (usize, usize) {
    let requested = limit
        .saturating_add(limit / 2)
        .max(limit.saturating_add(10));
    (requested, requested.min(MAX_KNN_K))
}

/// Generate a query embedding, truncating to match stored dimensionality.
///
/// The embedding provider may return vectors of higher dimensionality than
/// what is stored (e.g., Qwen3 produces 4096-dim but we store 1024-dim
/// via Matryoshka truncation). This function truncates the query vector
/// to match the stored vector dimensionality.
async fn generate_query_embedding(
    provider: &impl EmbeddingProvider,
    query: &str,
    stored_dim: usize,
) -> Result<Vec<f32>, EmbeddingError> {
    let texts = vec![provider.prepare_query_text(query)];
    let mut vectors = provider.embed_batch(&texts).await?;

    if vectors.is_empty() {
        return Err(EmbeddingError::ResponseError {
            message: "embedding API returned no vectors for query".to_string(),
        });
    }

    let mut vector = vectors.swap_remove(0);

    // Truncate to stored dimensionality if needed (Matryoshka embedding).
    if vector.len() > stored_dim {
        vector.truncate(stored_dim);
    }

    // Validate dimensionality matches.
    if vector.len() != stored_dim {
        return Err(EmbeddingError::ResponseError {
            message: format!(
                "query embedding dimension {} does not match stored dimension {}",
                vector.len(),
                stored_dim
            ),
        });
    }

    Ok(vector)
}
/// Convert a distance score to a similarity score.
///
/// sqlite-vec returns L2 (Euclidean) distances where lower is better.
/// We convert to a similarity score in (0, 1] where higher is better:
///   similarity = 1.0 / (1.0 + distance)
///
/// This is a standard transformation that:
/// - Maps distance=0 to similarity=1 (perfect match)
/// - Maps distance→∞ to similarity→0 (no similarity)
/// - Is monotonically decreasing (preserves ranking)
fn distance_to_similarity(distance: f64) -> f64 {
    1.0 / (1.0 + distance)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embedding::test_helpers::MockProvider;
    use crate::types::{Chunk, Language, SymbolType};
    use std::collections::HashMap;

    /// The pool must never ask sqlite-vec for more than it will serve, and must
    /// report the shortfall so a degraded pool is diagnosable rather than silent.
    #[test]
    fn candidate_pool_is_bounded_by_the_knn_cap() {
        // Below the cap the metadata over-fetch is untouched.
        assert_eq!(candidate_pool(10), (20, 20));
        assert_eq!(candidate_pool(100), (150, 150));

        // At and above it, the request is reported but not made.
        let (requested, fetched) = candidate_pool(MAX_KNN_K);
        assert!(
            requested > MAX_KNN_K,
            "over-fetch should exceed the cap here"
        );
        assert_eq!(fetched, MAX_KNN_K);

        // No caller can push the fetch past the cap, and none can overflow it.
        assert_eq!(candidate_pool(usize::MAX).1, MAX_KNN_K);
    }

    /// Create sample chunks with semantic variety for testing.
    fn sample_chunks() -> Vec<Chunk> {
        vec![
            Chunk {
                id: "src/alloc.rs:0".to_string(),
                file_path: "src/alloc.rs".to_string(),
                line_start: 1,
                line_end: 15,
                content: "pub fn alloc_page(size: usize) -> *mut u8 {\n    \
                           let layout = Layout::from_size_align(size, 4096).unwrap();\n    \
                           unsafe { std::alloc::alloc(layout) }\n}"
                    .to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("alloc_page".to_string()),
            },
            Chunk {
                id: "src/alloc.rs:1".to_string(),
                file_path: "src/alloc.rs".to_string(),
                line_start: 17,
                line_end: 30,
                content: "pub fn dealloc_page(ptr: *mut u8, size: usize) {\n    \
                           let layout = Layout::from_size_align(size, 4096).unwrap();\n    \
                           unsafe { std::alloc::dealloc(ptr, layout) }\n}"
                    .to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("dealloc_page".to_string()),
            },
            Chunk {
                id: "src/auth.rs:0".to_string(),
                file_path: "src/auth.rs".to_string(),
                line_start: 1,
                line_end: 12,
                content: "pub fn authenticate(user: &str, password: &str) -> Result<Token> {\n    \
                           let hash = hash_password(password);\n    \
                           verify_credentials(user, &hash)\n}"
                    .to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("authenticate".to_string()),
            },
            Chunk {
                id: "src/db.py:0".to_string(),
                file_path: "src/db.py".to_string(),
                line_start: 1,
                line_end: 10,
                content: "class DatabaseConnection:\n    \
                           def __init__(self, host, port):\n        \
                           self.conn = psycopg2.connect(host=host, port=port)\n    \
                           def execute_query(self, sql):\n        \
                           return self.conn.execute(sql)"
                    .to_string(),
                language: Language::Python,
                symbol_type: Some(SymbolType::Class),
                symbol_name: Some("DatabaseConnection".to_string()),
            },
            Chunk {
                id: "src/cache.go:0".to_string(),
                file_path: "src/cache.go".to_string(),
                line_start: 1,
                line_end: 15,
                content: "func NewLRUCache(capacity int) *LRUCache {\n    \
                           return &LRUCache{\n        \
                           capacity: capacity,\n        \
                           items: make(map[string]*list.Element),\n        \
                           order: list.New(),\n    }\n}"
                    .to_string(),
                language: Language::Go,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("NewLRUCache".to_string()),
            },
            Chunk {
                id: "src/server.ts:0".to_string(),
                file_path: "src/server.ts".to_string(),
                line_start: 1,
                line_end: 10,
                content: "function handleRequest(req: Request): Response {\n    \
                           const auth = authenticate(req.headers);\n    \
                           if (!auth) return new Response('Unauthorized', { status: 401 });\n    \
                           return processRequest(req);\n}"
                    .to_string(),
                language: Language::TypeScript,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("handleRequest".to_string()),
            },
        ]
    }

    /// Embed sample chunks with MockProvider and store in vector + metadata stores.
    async fn setup_test_stores(dim: usize) -> (VectorStore, MetadataStore) {
        let chunks = sample_chunks();
        let provider = MockProvider::new(dim);

        // Store metadata.
        let metadata_store = MetadataStore::open_in_memory().unwrap();
        metadata_store.insert_chunks(&chunks).unwrap();

        // Generate embeddings and store vectors.
        let vector_store = VectorStore::open_in_memory(dim).unwrap();
        crate::embedding::test_helpers::embed_and_insert_vectors(&vector_store, &provider, &chunks)
            .await;

        (vector_store, metadata_store)
    }

    // ── distance_to_similarity tests ─────────────────────────────────

    #[test]
    fn distance_zero_gives_similarity_one() {
        let score = distance_to_similarity(0.0);
        assert!((score - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn distance_positive_gives_score_between_zero_and_one() {
        let score = distance_to_similarity(1.0);
        assert!(score > 0.0);
        assert!(score < 1.0);
        assert!((score - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn distance_large_gives_score_near_zero() {
        let score = distance_to_similarity(1000.0);
        assert!(score > 0.0);
        assert!(score < 0.01);
    }

    #[test]
    fn similarity_is_monotonically_decreasing() {
        let distances = [0.0, 0.1, 0.5, 1.0, 2.0, 5.0, 10.0, 100.0];
        let scores: Vec<f64> = distances
            .iter()
            .map(|d| distance_to_similarity(*d))
            .collect();
        for i in 1..scores.len() {
            assert!(
                scores[i - 1] > scores[i],
                "similarity must decrease as distance increases: {} > {} at index {i}",
                scores[i - 1],
                scores[i],
            );
        }
    }

    #[test]
    fn storage_error_display_includes_error_chain() {
        let error = anyhow::anyhow!("root cause").context("outer context");
        let display = VectorSearchError::StorageError(error).to_string();

        assert!(display.contains("outer context"));
        assert!(display.contains("root cause"));
    }

    // ── generate_query_embedding tests ───────────────────────────────

    #[tokio::test]
    async fn query_embedding_has_correct_dimension() {
        let provider = MockProvider::new(8);
        let embedding = generate_query_embedding(&provider, "test query", 8)
            .await
            .unwrap();
        assert_eq!(embedding.len(), 8);
    }

    #[tokio::test]
    async fn query_embedding_truncates_to_stored_dim() {
        // Provider produces 16-dim, but stored is 8-dim.
        let provider = MockProvider::new(16);
        let embedding = generate_query_embedding(&provider, "test query", 8)
            .await
            .unwrap();
        assert_eq!(embedding.len(), 8);
    }

    #[tokio::test]
    async fn query_embedding_rejects_dim_mismatch() {
        // Provider produces 4-dim, but stored is 8-dim.
        let provider = MockProvider::new(4);
        let result = generate_query_embedding(&provider, "test query", 8).await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("dimension"),
            "error should mention dimension: {err}"
        );
    }

    #[tokio::test]
    async fn query_embedding_propagates_auth_error() {
        let provider = MockProvider::failing(EmbeddingError::AuthError {
            message: "invalid key".to_string(),
        });
        let result = generate_query_embedding(&provider, "test query", 4).await;
        assert!(result.is_err());
        assert!(
            matches!(result.unwrap_err(), EmbeddingError::AuthError { .. }),
            "should propagate auth error"
        );
    }

    #[tokio::test]
    async fn query_embedding_propagates_connection_error() {
        let provider = MockProvider::failing(EmbeddingError::ConnectionError {
            message: "unreachable".to_string(),
        });
        let result = generate_query_embedding(&provider, "test query", 4).await;
        assert!(result.is_err());
        assert!(
            matches!(result.unwrap_err(), EmbeddingError::ConnectionError { .. }),
            "should propagate connection error"
        );
    }

    // ── search_vector_with_stores tests ──────────────────────────────

    #[tokio::test]
    async fn search_returns_results_for_indexed_content() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "alloc", 10)
                .await
                .unwrap();

        assert!(!results.is_empty(), "should find results for 'alloc'");
    }

    #[tokio::test]
    async fn results_sorted_by_score_descending() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results = search_vector_with_stores(
            &vector_store,
            &metadata_store,
            &provider,
            "database connection query",
            10,
        )
        .await
        .unwrap();

        assert!(
            results.len() >= 2,
            "need multiple results to verify ordering"
        );
        for i in 1..results.len() {
            assert!(
                results[i - 1].score >= results[i].score,
                "scores must be descending: {} >= {} at position {i}",
                results[i - 1].score,
                results[i].score,
            );
        }
    }

    #[tokio::test]
    async fn results_include_full_metadata() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "cache", 10)
                .await
                .unwrap();

        assert!(!results.is_empty());
        let top = &results[0];
        // Every result should have required fields populated.
        assert!(!top.file_path.is_empty(), "file_path should be set");
        assert!(top.line_start > 0, "line_start should be 1-based");
        assert!(top.line_end >= top.line_start, "line_end >= line_start");
        assert!(!top.content.is_empty(), "content should be present");
        assert!(top.score > 0.0, "score should be positive");
        assert!(top.score <= 1.0, "similarity score should be <= 1.0");
    }

    #[tokio::test]
    async fn search_respects_limit() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "function", 2)
                .await
                .unwrap();

        assert!(results.len() <= 2, "results should respect the limit of 2");
    }

    #[tokio::test]
    async fn search_zero_limit_skips_embedding() {
        let dim = 8;
        let vector_store = VectorStore::open_in_memory(dim).unwrap();
        let metadata_store = MetadataStore::open_in_memory().unwrap();
        let provider = MockProvider::failing(EmbeddingError::ConnectionError {
            message: "should not be called".to_string(),
        });

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "function", 0)
                .await
                .unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn scores_are_positive_and_bounded() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results = search_vector_with_stores(
            &vector_store,
            &metadata_store,
            &provider,
            "authenticate",
            10,
        )
        .await
        .unwrap();

        for result in &results {
            assert!(result.score > 0.0, "score should be positive");
            assert!(result.score <= 1.0, "score should be <= 1.0");
        }
    }

    #[tokio::test]
    async fn search_with_embedding_error_returns_error() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::failing(EmbeddingError::ConnectionError {
            message: "API down".to_string(),
        });

        let result =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "test", 10).await;

        assert!(result.is_err(), "should return error when embedding fails");
        assert!(
            matches!(
                result.unwrap_err(),
                VectorSearchError::EmbeddingUnavailable { .. }
            ),
            "should be an embedding unavailable error"
        );
    }

    #[tokio::test]
    async fn search_returns_results_from_multiple_languages() {
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        // Get all results.
        let results = search_vector_with_stores(
            &vector_store,
            &metadata_store,
            &provider,
            "code function",
            10,
        )
        .await
        .unwrap();

        let languages: std::collections::HashSet<_> = results.iter().map(|r| r.language).collect();
        assert!(
            languages.len() >= 2,
            "should return results from multiple languages, got: {languages:?}"
        );
    }

    #[tokio::test]
    async fn search_with_truncation() {
        // Provider returns 16-dim vectors, but store uses 8-dim.
        let dim = 8;
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        // Use a provider that produces larger vectors than stored.
        let query_provider = MockProvider::new(16);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &query_provider, "cache", 10)
                .await
                .unwrap();

        assert!(
            !results.is_empty(),
            "should work with truncated query embeddings"
        );
    }

    #[tokio::test]
    async fn empty_vector_store_returns_empty_results() {
        let dim = 8;
        let vector_store = VectorStore::open_in_memory(dim).unwrap();
        let metadata_store = MetadataStore::open_in_memory().unwrap();
        let provider = MockProvider::new(dim);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "anything", 10)
                .await
                .unwrap();

        assert!(results.is_empty(), "empty store should return no results");
    }

    #[tokio::test]
    async fn batch_chunk_fetch_preserves_vector_distance_order() {
        // Regression test for the N+1 -> batch fetch change: insert chunks
        // in an order different from the requested id order, and assert the
        // search results come back in vector-distance order (the order `vr`
        // came in), not whatever order SQLite happens to return for
        // `IN (...)` (unspecified) or insertion order.
        let dim = 8;
        let metadata_store = MetadataStore::open_in_memory().unwrap();

        // Inserted in id order, which is also the order `WHERE id IN (...)`
        // returns rows in (the chunks table keys on id). The mock provider
        // ranks these contents c, a, b by distance, so the expected result
        // order differs from both — which is what makes the assertions below
        // able to fail.
        let chunks = vec![
            Chunk {
                id: "a".to_string(),
                file_path: "src/a.rs".to_string(),
                line_start: 1,
                line_end: 2,
                content: "fn a() {}".to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("a".to_string()),
            },
            Chunk {
                id: "b".to_string(),
                file_path: "src/b.rs".to_string(),
                line_start: 1,
                line_end: 2,
                content: "fn b() {}".to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("b".to_string()),
            },
            Chunk {
                id: "c".to_string(),
                file_path: "src/c.rs".to_string(),
                line_start: 1,
                line_end: 2,
                content: "fn c() {}".to_string(),
                language: Language::Rust,
                symbol_type: Some(SymbolType::Function),
                symbol_name: Some("c".to_string()),
            },
        ];
        metadata_store.insert_chunks(&chunks).unwrap();

        let vector_store = VectorStore::open_in_memory(dim).unwrap();
        let provider = MockProvider::new(dim);
        crate::embedding::test_helpers::embed_and_insert_vectors(&vector_store, &provider, &chunks)
            .await;

        // Ground truth: the distance order the vector store itself reports.
        // Every candidate must come back paired with its own distance.
        let query_embedding = generate_query_embedding(&provider, "fn", dim)
            .await
            .unwrap();
        let vector_results = vector_store.search(&query_embedding, chunks.len()).unwrap();
        assert_eq!(vector_results.len(), chunks.len());

        let chunk_by_id: HashMap<&str, &Chunk> =
            chunks.iter().map(|c| (c.id.as_str(), c)).collect();
        let expected: Vec<(&str, &str, f64)> = vector_results
            .iter()
            .map(|vr| {
                let chunk = chunk_by_id[vr.chunk_id.as_str()];
                (
                    chunk.file_path.as_str(),
                    chunk.content.as_str(),
                    distance_to_similarity(vr.distance),
                )
            })
            .collect();

        // Guard that the fixture actually discriminates. The chunks are
        // inserted in id order, so insertion order and the `IN (...)`
        // primary-key order are the same sequence here; one comparison covers
        // both. Without this, a fixture whose distance order matched storage
        // order would satisfy the assertions below without testing anything —
        // which is exactly what the first version of this test did.
        let expected_paths: Vec<&str> = expected.iter().map(|(path, _, _)| *path).collect();
        let storage_order_paths: Vec<&str> = chunks.iter().map(|c| c.file_path.as_str()).collect();
        assert_ne!(
            expected_paths, storage_order_paths,
            "fixture is not discriminating: distance order equals insertion and primary-key order"
        );

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "fn", 10)
                .await
                .unwrap();

        // Assert the pairing, not just that scores descend. Scores descend for
        // any implementation that emits one result per candidate in candidate
        // order, because the vector store already returns rows sorted by
        // distance — so a monotonicity check cannot catch chunks being paired
        // with the wrong neighbour's distance.
        assert_eq!(
            results.len(),
            expected.len(),
            "batch fetch dropped candidates"
        );
        let actual: Vec<(&str, &str, f64)> = results
            .iter()
            .map(|r| (r.file_path.as_str(), r.content.as_str(), r.score))
            .collect();
        assert_eq!(
            actual, expected,
            "each chunk must come back paired with its own distance, in vector-distance order"
        );
    }

    #[tokio::test]
    async fn result_content_matches_source_chunk() {
        let dim = 8;
        let chunks = sample_chunks();
        let (vector_store, metadata_store) = setup_test_stores(dim).await;
        let provider = MockProvider::new(dim);

        let results =
            search_vector_with_stores(&vector_store, &metadata_store, &provider, "alloc page", 10)
                .await
                .unwrap();

        assert!(!results.is_empty());
        // Find a result from alloc.rs and verify content matches original.
        let alloc_result = results.iter().find(|r| r.file_path == "src/alloc.rs");
        assert!(alloc_result.is_some(), "should find result from alloc.rs");
        let alloc_result = alloc_result.unwrap();
        // Verify content matches one of the original chunks.
        let matching_chunk = chunks
            .iter()
            .find(|c| c.file_path == alloc_result.file_path && c.content == alloc_result.content);
        assert!(
            matching_chunk.is_some(),
            "result content should match a source chunk"
        );
    }
}