use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use anyhow::Result;
use tokio::sync::OnceCell;
use tracing::warn;
use crate::config::{InferenceBackend, VeraConfig};
use crate::embedding::{CachedEmbeddingProvider, DynamicProvider, EmbeddingProvider};
use crate::retrieval::dynamic_reranker::DynamicReranker;
use crate::retrieval::exact_matches::augment_exact_match_candidates_with_store;
pub use crate::retrieval::exact_matches::augment_multi_query_exact_matches;
use crate::retrieval::hybrid::{
SearchStores, compute_vector_candidates, search_hybrid_reranked_with_stores,
search_hybrid_with_stores,
};
use crate::retrieval::query_classifier::{QueryType, classify_query, params_for_query_type};
use crate::retrieval::ranking::{RankingStage, is_path_weighted_query};
use crate::retrieval::{apply_filters, search_bm25_with_stores_and_filters};
use crate::types::{SearchFilters, SearchResult};
#[derive(Debug, Default)]
pub struct SearchTimings {
pub embedding: Option<Duration>,
pub bm25: Option<Duration>,
pub vector: Option<Duration>,
pub fusion: Option<Duration>,
pub reranking: Option<Duration>,
pub augmentation: Option<Duration>,
pub total: Option<Duration>,
}
impl From<crate::retrieval::hybrid::HybridTimings> for SearchTimings {
fn from(t: crate::retrieval::hybrid::HybridTimings) -> Self {
SearchTimings {
embedding: t.embedding,
bm25: t.bm25,
vector: t.vector,
fusion: t.fusion,
reranking: t.reranking,
augmentation: None,
total: None,
}
}
}
pub struct SearchContext {
provider: Option<CachedEmbeddingProvider<DynamicProvider>>,
model_name: Option<String>,
provider_error: Option<String>,
backend: InferenceBackend,
stores: Mutex<Option<(PathBuf, Arc<SearchStores>)>>,
reranker: OnceCell<Option<DynamicReranker>>,
#[cfg(test)]
reranker_builds: std::sync::atomic::AtomicUsize,
}
impl SearchContext {
pub async fn new(config: &VeraConfig, backend: InferenceBackend) -> Self {
let (provider, model_name, provider_error) =
match crate::embedding::create_dynamic_provider(config, backend).await {
Ok((provider, model_name)) => (
Some(CachedEmbeddingProvider::with_namespace(
provider,
512,
&model_name,
)),
Some(model_name),
None,
),
Err(err) => {
warn!(
"Failed to create embedding provider ({}), using BM25-only search",
err
);
(None, None, Some(err.to_string()))
}
};
Self {
provider,
model_name,
provider_error,
backend,
stores: Mutex::new(None),
reranker: OnceCell::new(),
#[cfg(test)]
reranker_builds: std::sync::atomic::AtomicUsize::new(0),
}
}
pub fn bm25_only() -> Self {
Self {
provider: None,
model_name: None,
provider_error: None,
backend: InferenceBackend::Api,
stores: Mutex::new(None),
reranker: OnceCell::new(),
#[cfg(test)]
reranker_builds: std::sync::atomic::AtomicUsize::new(0),
}
}
async fn reranker_for_query(
&self,
config: &VeraConfig,
has_intent: bool,
query: &str,
filters: &SearchFilters,
) -> Option<&DynamicReranker> {
if !reranking_wanted(has_intent, query, filters) {
return None;
}
self.reranker(config).await
}
#[cfg(test)]
fn reranker_build_count(&self) -> usize {
self.reranker_builds
.load(std::sync::atomic::Ordering::Relaxed)
}
async fn reranker(&self, config: &VeraConfig) -> Option<&DynamicReranker> {
self.reranker
.get_or_init(|| async {
#[cfg(test)]
self.reranker_builds
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
crate::retrieval::create_dynamic_reranker(config, self.backend)
.await
.unwrap_or_else(|err| {
warn!("Failed to create reranker ({})", err);
None
})
})
.await
.as_ref()
}
pub fn embedding_provider(&self) -> Option<&CachedEmbeddingProvider<DynamicProvider>> {
self.provider.as_ref()
}
pub fn model_name(&self) -> Option<&str> {
self.model_name.as_deref()
}
fn search_stores(&self, index_dir: &Path) -> Result<Arc<SearchStores>> {
let mut cached = self
.stores
.lock()
.map_err(|_| anyhow::anyhow!("search store cache lock poisoned"))?;
if let Some((cached_dir, stores)) = cached.as_ref()
&& cached_dir == index_dir
{
return Ok(Arc::clone(stores));
}
let stores = Arc::new(SearchStores::open(index_dir)?);
*cached = Some((index_dir.to_path_buf(), Arc::clone(&stores)));
Ok(stores)
}
pub async fn search(
&self,
index_dir: &Path,
query: &str,
intent: Option<&str>,
config: &VeraConfig,
filters: &SearchFilters,
result_limit: usize,
) -> Result<(Vec<SearchResult>, SearchTimings)> {
let total_start = Instant::now();
let vector_query = build_query_with_intent(query, intent);
let has_intent = matches!(vector_query, std::borrow::Cow::Owned(_));
let fetch_limit = compute_fetch_limit(query, filters, result_limit);
let stores = self.search_stores(index_dir)?;
let Some(provider) = self.provider.as_ref() else {
if let Some(error) = self.provider_error.as_deref() {
warn!(
"embedding provider unavailable ({}), using BM25-only search",
error
);
}
return run_bm25_only(
query,
filters,
fetch_limit,
result_limit,
total_start,
&stores,
);
};
let mut stored_dim = config.embedding.max_stored_dim;
let index_meta = stores.bm25_metadata.lock().ok().map(|metadata_store| {
(
metadata_store.get_index_meta("model_name").unwrap_or(None),
metadata_store
.get_index_meta("embedding_dim")
.unwrap_or(None),
metadata_store
.get_index_meta("document_prefix")
.unwrap_or(None),
)
});
if let Some((Some(s_model), Some(s_dim), s_prefix)) = index_meta {
if let Some(model_name) = self.model_name.as_deref()
&& !crate::config::model_names_match_with_aliases(
&s_model,
model_name,
&config.embedding.model_aliases,
)
{
warn!(
"Index model '{}' does not match active model '{}'; using BM25-only search",
s_model, model_name
);
return run_bm25_only(
query,
filters,
fetch_limit,
result_limit,
total_start,
&stores,
);
}
let active_prefix = provider.document_prefix_identity();
if s_prefix.as_deref().unwrap_or("") != active_prefix {
warn!(
"Index document prefix '{}' does not match active prefix '{}'; using BM25-only search (re-index to restore vector search)",
s_prefix.as_deref().unwrap_or(""),
active_prefix
);
return run_bm25_only(
query,
filters,
fetch_limit,
result_limit,
total_start,
&stores,
);
}
if let Ok(dim) = s_dim.parse::<usize>() {
if let Some(provider_dim) = provider.expected_dim()
&& provider_dim < dim
{
warn!(
"Index dimension {} exceeds provider dimension {}; using BM25-only search",
dim, provider_dim
);
return run_bm25_only(
query,
filters,
fetch_limit,
result_limit,
total_start,
&stores,
);
}
stored_dim = dim;
}
}
let reranker = self
.reranker_for_query(config, has_intent, query, filters)
.await;
let reranker_enabled = reranker.is_some();
let query_type = classify_query(query);
let query_params = params_for_query_type(query_type);
let rrf_k = query_params.rrf_k;
let vector_candidates = effective_vector_candidates(fetch_limit, query_params);
let rerank_candidates =
effective_rerank_candidates(config.retrieval.rerank_candidates, result_limit);
let ranking_stage = if reranker_enabled {
RankingStage::PostRerank
} else {
RankingStage::Initial
};
let (results, hybrid_timings) = if let Some(reranker) = reranker {
search_hybrid_reranked_with_stores(
index_dir,
provider,
reranker,
query,
vector_query.as_ref(),
filters,
fetch_limit,
result_limit,
rrf_k,
stored_dim,
rerank_candidates,
vector_candidates,
crate::config::graph_augmentation_enabled(),
Arc::clone(&stores),
)
.await?
} else {
search_hybrid_with_stores(
index_dir,
provider,
query,
vector_query.as_ref(),
filters,
fetch_limit,
rrf_k,
stored_dim,
vector_candidates,
Arc::clone(&stores),
)
.await?
};
let mut timings = SearchTimings::from(hybrid_timings);
let aug_start = Instant::now();
let indexed_files = stores.indexed_files()?;
let metadata_store = stores
.bm25_metadata
.lock()
.map_err(|_| anyhow::anyhow!("BM25 metadata store lock poisoned"))?;
let results = augment_exact_match_candidates_with_store(
&metadata_store,
&indexed_files,
query,
results,
ranking_stage,
filters,
)?;
timings.augmentation = Some(aug_start.elapsed());
timings.total = Some(total_start.elapsed());
Ok((apply_filters(results, filters, result_limit), timings))
}
}
pub fn execute_search(
index_dir: &Path,
query: &str,
intent: Option<&str>,
config: &VeraConfig,
filters: &SearchFilters,
result_limit: usize,
backend: InferenceBackend,
) -> Result<(Vec<SearchResult>, SearchTimings)> {
let rt = tokio::runtime::Runtime::new()?;
let context = rt.block_on(SearchContext::new(config, backend));
rt.block_on(context.search(index_dir, query, intent, config, filters, result_limit))
}
pub(crate) fn build_query_with_intent<'a>(
query: &'a str,
intent: Option<&str>,
) -> std::borrow::Cow<'a, str> {
let intent = intent
.map(|value| value.split_whitespace().collect::<Vec<_>>().join(" "))
.filter(|value| !value.is_empty());
match intent {
Some(intent) => std::borrow::Cow::Owned(format!("intent: {intent} | {query}")),
None => std::borrow::Cow::Borrowed(query),
}
}
fn compute_fetch_limit(query: &str, filters: &SearchFilters, result_limit: usize) -> usize {
let mut fetch_limit = if filters.is_empty() {
result_limit
} else {
result_limit.saturating_mul(3).max(result_limit + 20)
};
if !filters.path_glob.is_empty() {
fetch_limit = fetch_limit.max(result_limit.saturating_mul(10).max(result_limit + 100));
}
if filters.exact_paths.is_some() {
fetch_limit = fetch_limit.max(result_limit.saturating_mul(12).max(result_limit + 200));
}
if needs_structural_overfetch(query, filters) {
fetch_limit = fetch_limit.max(result_limit.saturating_mul(8).max(result_limit + 140));
} else if matches!(classify_query(query), QueryType::NaturalLanguage) {
fetch_limit = fetch_limit.max(result_limit.saturating_mul(3).max(result_limit + 40));
}
fetch_limit
}
fn needs_structural_overfetch(query: &str, filters: &SearchFilters) -> bool {
matches!(classify_query(query), QueryType::NaturalLanguage)
&& query.split_whitespace().count() >= 4
&& filters.path_glob.is_empty()
&& filters.exact_paths.is_none()
&& filters.symbol_type.is_none()
&& !is_path_weighted_query(query)
}
fn effective_vector_candidates(
fetch_limit: usize,
query_params: crate::retrieval::query_classifier::QueryParams,
) -> usize {
compute_vector_candidates(fetch_limit, query_params.vector_candidate_multiplier)
}
fn effective_rerank_candidates(configured: usize, result_limit: usize) -> usize {
configured.max(result_limit)
}
fn reranking_wanted(has_intent: bool, query: &str, filters: &SearchFilters) -> bool {
has_intent || !should_skip_reranking(query, filters)
}
fn should_skip_reranking(query: &str, filters: &SearchFilters) -> bool {
let word_count = query.split_whitespace().count();
filters.exact_paths.is_some()
|| filters.symbol_type.is_some()
|| is_path_weighted_query(query)
|| (matches!(classify_query(query), QueryType::Identifier) && word_count <= 2)
}
fn run_bm25_only(
query: &str,
filters: &SearchFilters,
fetch_limit: usize,
result_limit: usize,
total_start: Instant,
stores: &Arc<SearchStores>,
) -> Result<(Vec<SearchResult>, SearchTimings)> {
let bm25_start = Instant::now();
let metadata_store = stores
.bm25_metadata
.lock()
.map_err(|_| anyhow::anyhow!("BM25 metadata store lock poisoned"))?;
let results = search_bm25_with_stores_and_filters(
&stores.bm25,
&metadata_store,
query,
filters,
fetch_limit,
)?;
let bm25_elapsed = bm25_start.elapsed();
let aug_start = Instant::now();
let indexed_files = stores.indexed_files()?;
let results = augment_exact_match_candidates_with_store(
&metadata_store,
&indexed_files,
query,
results,
RankingStage::Initial,
filters,
)?;
let timings = SearchTimings {
bm25: Some(bm25_elapsed),
augmentation: Some(aug_start.elapsed()),
total: Some(total_start.elapsed()),
..Default::default()
};
Ok((apply_filters(results, filters, result_limit), timings))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::retrieval::exact_matches::augment_exact_match_candidates;
use crate::storage::bm25::{Bm25Document, Bm25Index};
use crate::storage::metadata::MetadataStore;
use crate::test_env::run_env_test;
use crate::types::Language;
use crate::types::{Chunk, SymbolType};
use tempfile::tempdir;
#[test]
fn test_dimension_mismatch_and_inference() {
run_env_test(
"retrieval::search_service::tests::test_dimension_mismatch_and_inference_probe",
&[
("EMBEDDING_MODEL_BASE_URL", Some("http://127.0.0.1:0")),
("EMBEDDING_MODEL_ID", Some("dummy-api-model")),
("EMBEDDING_MODEL_API_KEY", Some("dummy-key")),
],
);
}
#[test]
#[ignore = "driven by test_dimension_mismatch_and_inference"]
fn test_dimension_mismatch_and_inference_probe() {
crate::init_tls();
let dir = tempdir().unwrap();
let index_dir = dir.path();
let metadata_path = index_dir.join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
store
.set_index_meta("model_name", "jina-embeddings-v5-text-nano-retrieval")
.unwrap();
store.set_index_meta("embedding_dim", "1024").unwrap();
let config = VeraConfig::default();
let filters = SearchFilters::default();
{
let res = execute_search(
index_dir,
"test",
None,
&config,
&filters,
10,
crate::config::InferenceBackend::OnnxJina(
crate::config::OnnxExecutionProvider::Cpu,
),
);
if let Err(err) = res {
let err_msg = err.to_string();
assert!(
err_msg.contains("tantivy")
|| err_msg.contains("Failed to initialize local embedding provider")
|| err_msg.contains("No such file")
|| err_msg.contains("not found"),
"{}",
err_msg
);
}
}
store
.set_index_meta("model_name", "dummy-api-model")
.unwrap();
store.set_index_meta("embedding_dim", "123").unwrap();
let res = execute_search(
index_dir,
"test",
None,
&config,
&filters,
10,
crate::config::InferenceBackend::Api,
);
assert!(res.is_ok(), "Expected Ok but got {:?}", res);
}
#[test]
fn model_metadata_mismatch_falls_back_to_bm25() {
run_env_test(
"retrieval::search_service::tests::model_metadata_mismatch_falls_back_to_bm25_probe",
&[
("EMBEDDING_MODEL_BASE_URL", Some("http://127.0.0.1:0")),
("EMBEDDING_MODEL_ID", Some("active-api-model")),
("EMBEDDING_MODEL_API_KEY", Some("dummy-key")),
],
);
}
#[test]
#[ignore = "driven by model_metadata_mismatch_falls_back_to_bm25"]
fn model_metadata_mismatch_falls_back_to_bm25_probe() {
crate::init_tls();
let dir = tempdir().unwrap();
let index_dir = dir.path();
let store = MetadataStore::open(&index_dir.join("metadata.db")).unwrap();
store
.insert_chunks(&[Chunk {
id: "auth:0".to_string(),
file_path: "src/auth.rs".to_string(),
line_start: 1,
line_end: 4,
content: "pub fn authenticate_user() -> bool { true }".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("authenticate_user".to_string()),
}])
.unwrap();
store.set_index_meta("model_name", "indexed-model").unwrap();
store.set_index_meta("embedding_dim", "64").unwrap();
let bm25 = Bm25Index::open(&index_dir.join("bm25")).unwrap();
bm25.insert_batch(&[Bm25Document {
chunk_id: "auth:0",
file_path: "src/auth.rs",
content: "pub fn authenticate_user() -> bool { true }",
symbol_name: Some("authenticate_user"),
language: "rust",
}])
.unwrap();
let mut config = VeraConfig::default();
config.embedding.timeout_secs = 1;
config.embedding.max_retries = 0;
let filters = SearchFilters::default();
let rt = tokio::runtime::Runtime::new().unwrap();
let context = rt.block_on(SearchContext::new(
&config,
crate::config::InferenceBackend::Api,
));
let (results, timings) = rt
.block_on(context.search(index_dir, "authenticate user", None, &config, &filters, 10))
.unwrap();
assert_eq!(results[0].file_path, "src/auth.rs");
assert!(timings.bm25.is_some());
assert!(timings.vector.is_none());
}
#[test]
fn document_prefix_mismatch_falls_back_to_bm25() {
run_env_test(
"retrieval::search_service::tests::document_prefix_mismatch_falls_back_to_bm25_probe",
&[
("EMBEDDING_MODEL_BASE_URL", Some("http://127.0.0.1:0")),
("EMBEDDING_MODEL_ID", Some("indexed-model")),
("EMBEDDING_MODEL_API_KEY", Some("dummy-key")),
],
);
}
#[test]
#[ignore = "driven by document_prefix_mismatch_falls_back_to_bm25"]
fn document_prefix_mismatch_falls_back_to_bm25_probe() {
crate::init_tls();
let dir = tempdir().unwrap();
let index_dir = dir.path();
let store = MetadataStore::open(&index_dir.join("metadata.db")).unwrap();
store
.insert_chunks(&[Chunk {
id: "auth:0".to_string(),
file_path: "src/auth.rs".to_string(),
line_start: 1,
line_end: 4,
content: "pub fn authenticate_user() -> bool { true }".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("authenticate_user".to_string()),
}])
.unwrap();
store.set_index_meta("model_name", "indexed-model").unwrap();
store.set_index_meta("embedding_dim", "64").unwrap();
store
.set_index_meta("document_prefix", "passage: ")
.unwrap();
let bm25 = Bm25Index::open(&index_dir.join("bm25")).unwrap();
bm25.insert_batch(&[Bm25Document {
chunk_id: "auth:0",
file_path: "src/auth.rs",
content: "pub fn authenticate_user() -> bool { true }",
symbol_name: Some("authenticate_user"),
language: "rust",
}])
.unwrap();
let mut config = VeraConfig::default();
config.embedding.timeout_secs = 1;
config.embedding.max_retries = 0;
let filters = SearchFilters::default();
let rt = tokio::runtime::Runtime::new().unwrap();
let context = rt.block_on(SearchContext::new(
&config,
crate::config::InferenceBackend::Api,
));
let (results, timings) = rt
.block_on(context.search(index_dir, "authenticate user", None, &config, &filters, 10))
.unwrap();
assert_eq!(results[0].file_path, "src/auth.rs");
assert!(timings.bm25.is_some());
assert!(timings.vector.is_none());
}
#[test]
fn bm25_only_search_fills_path_scoped_results_from_deeper_pool() {
let dir = tempdir().unwrap();
let index_dir = dir.path();
let metadata_store = MetadataStore::open(&index_dir.join("metadata.db")).unwrap();
let bm25 = Bm25Index::open(&index_dir.join("bm25")).unwrap();
let mut chunks = Vec::new();
for i in 0..160 {
chunks.push(Chunk {
id: format!("noise:{i}"),
file_path: format!("other/dependency_injection_work_{i}.py"),
line_start: 1,
line_end: 4,
content:
"def dependency_injection_work():\n dependency injection work dependency injection work"
.to_string(),
language: Language::Python,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("dependency_injection_work".to_string()),
});
}
chunks.push(Chunk {
id: "fastapi:dependency".to_string(),
file_path: "fastapi/dependencies/utils.py".to_string(),
line_start: 42,
line_end: 55,
content: "def solve_dependencies():\n \"\"\"Resolve dependency injection for request handlers.\"\"\"\n return values"
.to_string(),
language: Language::Python,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("solve_dependencies".to_string()),
});
metadata_store.insert_chunks(&chunks).unwrap();
let lang_strings: Vec<String> = chunks.iter().map(|c| c.language.to_string()).collect();
let docs: Vec<Bm25Document<'_>> = chunks
.iter()
.zip(lang_strings.iter())
.map(|(chunk, language)| Bm25Document {
chunk_id: &chunk.id,
file_path: &chunk.file_path,
content: &chunk.content,
symbol_name: chunk.symbol_name.as_deref(),
language,
})
.collect();
bm25.insert_batch(&docs).unwrap();
let filters = SearchFilters {
path_glob: vec!["fastapi/**".to_string()],
..Default::default()
};
let rt = tokio::runtime::Runtime::new().unwrap();
let context = SearchContext::bm25_only();
let (results, timings) = rt
.block_on(context.search(
index_dir,
"how does dependency injection work",
None,
&VeraConfig::default(),
&filters,
5,
))
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].file_path, "fastapi/dependencies/utils.py");
assert!(timings.bm25.is_some());
assert_eq!(
context.reranker_build_count(),
0,
"a context with no embedding provider must never construct a reranker"
);
}
#[test]
fn build_query_with_intent_formats_and_normalizes() {
let none = build_query_with_intent("test query", None);
assert_eq!(none.as_ref(), "test query");
assert!(matches!(none, std::borrow::Cow::Borrowed(_)));
let empty = build_query_with_intent("test query", Some(" "));
assert_eq!(empty.as_ref(), "test query");
assert!(matches!(empty, std::borrow::Cow::Borrowed(_)));
let some = build_query_with_intent("test query", Some("find auth\n handlers"));
assert_eq!(some.as_ref(), "intent: find auth handlers | test query");
assert!(matches!(some, std::borrow::Cow::Owned(_)));
}
#[test]
fn reranking_runs_for_intent_searches_with_short_queries() {
let filters = SearchFilters::default();
assert!(should_skip_reranking("authenticate", &filters));
assert!(!reranking_wanted(false, "authenticate", &filters));
assert!(reranking_wanted(true, "authenticate", &filters));
assert!(reranking_wanted(false, "how does auth work", &filters));
}
#[test]
fn reranker_is_built_only_for_queries_that_rerank() {
run_env_test(
"retrieval::search_service::tests::reranker_is_built_only_for_queries_that_rerank_probe",
&[
("EMBEDDING_MODEL_BASE_URL", Some("http://127.0.0.1:0")),
("EMBEDDING_MODEL_ID", Some("dummy-api-model")),
("EMBEDDING_MODEL_API_KEY", Some("dummy-key")),
("RERANKER_MODEL_BASE_URL", Some("http://127.0.0.1:0")),
("RERANKER_MODEL_ID", Some("dummy-reranker-model")),
("RERANKER_MODEL_API_KEY", Some("dummy-key")),
],
);
}
#[tokio::test]
#[ignore = "driven by reranker_is_built_only_for_queries_that_rerank"]
async fn reranker_is_built_only_for_queries_that_rerank_probe() {
crate::init_tls();
let mut config = VeraConfig::default();
config.retrieval.reranking_enabled = true;
let filters = SearchFilters::default();
let context = SearchContext::new(&config, crate::config::InferenceBackend::Api).await;
assert_eq!(context.reranker_build_count(), 0);
assert!(!reranking_wanted(false, "Bm25Index", &filters));
assert!(
context
.reranker_for_query(&config, false, "Bm25Index", &filters)
.await
.is_none()
);
assert_eq!(
context.reranker_build_count(),
0,
"a query that skips reranking must not construct the reranker"
);
assert!(
context
.reranker_for_query(
&config,
false,
"how are embeddings batched during indexing",
&filters
)
.await
.is_some()
);
assert_eq!(context.reranker_build_count(), 1);
assert!(
context
.reranker_for_query(&config, false, "how does auth work", &filters)
.await
.is_some()
);
assert_eq!(context.reranker_build_count(), 1);
}
#[test]
fn path_glob_filters_do_not_skip_reranking() {
let filters = SearchFilters {
path_glob: vec!["crates/vera-core/**".to_string()],
..Default::default()
};
assert!(!should_skip_reranking(
"how does dependency injection work",
&filters
));
}
#[test]
fn rerank_candidate_depth_is_independent_of_fetch_depth() {
assert_eq!(effective_rerank_candidates(20, 5), 20);
assert_eq!(
effective_rerank_candidates(4, 5),
5,
"reranking must cover enough candidates to return the requested result count"
);
let nl_params =
params_for_query_type(crate::retrieval::query_classifier::QueryType::NaturalLanguage);
let vc = effective_vector_candidates(10, nl_params);
assert!(vc >= 50); }
#[test]
fn broad_nl_queries_overfetch_before_ranking() {
let filters = SearchFilters::default();
assert_eq!(compute_fetch_limit("Config", &filters, 20), 20);
assert_eq!(
compute_fetch_limit("file type detection and filtering", &filters, 20),
160
);
assert_eq!(
compute_fetch_limit(
"how are HTTP errors handled and returned to clients",
&filters,
5
),
145
);
}
#[test]
fn exact_identifier_queries_skip_reranking() {
assert!(should_skip_reranking("Config", &SearchFilters::default()));
assert!(should_skip_reranking(
"src/config.ts",
&SearchFilters::default()
));
assert!(!should_skip_reranking(
"how are HTTP errors handled",
&SearchFilters::default()
));
}
#[test]
fn exact_identifier_lookup_finds_matching_symbol() {
let dir = tempdir().unwrap();
let metadata_path = dir.path().join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
store
.insert_chunks(&[Chunk {
id: "sink:0".to_string(),
file_path: "crates/searcher/src/sink.rs".to_string(),
line_start: 102,
line_end: 223,
content: "pub trait Sink {}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Trait),
symbol_name: Some("Sink".to_string()),
}])
.unwrap();
let augmented = augment_exact_match_candidates(
dir.path(),
"Sink trait and its implementations",
Vec::new(),
RankingStage::Initial,
&SearchFilters::default(),
)
.unwrap();
assert!(
augmented
.iter()
.any(|result| result.symbol_name.as_deref() == Some("Sink"))
);
}
#[test]
fn exact_identifier_prefers_public_type_definition() {
let dir = tempdir().unwrap();
let metadata_path = dir.path().join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
store
.insert_chunks(&[
Chunk {
id: "config:0".to_string(),
file_path: "crates/core/search.rs".to_string(),
line_start: 19,
line_end: 25,
content: "struct Config {\n search_zip: bool,\n}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Struct),
symbol_name: Some("Config".to_string()),
},
Chunk {
id: "config:1".to_string(),
file_path: "crates/regex/src/config.rs".to_string(),
line_start: 25,
line_end: 43,
content: "pub(crate) struct Config {\n pub(crate) multi_line: bool,\n}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Struct),
symbol_name: Some("Config".to_string()),
},
Chunk {
id: "config:2".to_string(),
file_path: "crates/searcher/src/searcher/mod.rs".to_string(),
line_start: 151,
line_end: 185,
content: "pub struct Config {\n line_term: LineTerminator,\n multi_line: bool,\n}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Struct),
symbol_name: Some("Config".to_string()),
},
])
.unwrap();
let augmented = augment_exact_match_candidates(
dir.path(),
"Config",
Vec::new(),
RankingStage::Initial,
&SearchFilters::default(),
)
.unwrap();
assert_eq!(
augmented[0].file_path,
"crates/searcher/src/searcher/mod.rs"
);
}
#[test]
fn multi_query_exact_matches_are_promoted_after_fusion() {
let dir = tempdir().unwrap();
let metadata_path = dir.path().join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
store
.insert_chunks(&[
Chunk {
id: "kimi:0".to_string(),
file_path: "backend/crates/omnigate-auth/src/kimi.rs".to_string(),
line_start: 265,
line_end: 275,
content: "pub fn persist_kimi_auth_record() {}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("persist_kimi_auth_record".to_string()),
},
Chunk {
id: "factory:0".to_string(),
file_path: "backend/crates/omnigate-auth/src/factory.rs".to_string(),
line_start: 286,
line_end: 296,
content: "pub fn persist_factory_auth_record() {}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("persist_factory_auth_record".to_string()),
},
Chunk {
id: "lib:0".to_string(),
file_path: "backend/crates/omnigate-auth/src/lib.rs".to_string(),
line_start: 10,
line_end: 40,
content: "pub use crate::kimi::persist_kimi_auth_record;".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Module),
symbol_name: Some("omnigate_auth".to_string()),
},
])
.unwrap();
let fused = vec![
SearchResult {
file_path: "backend/crates/omnigate-auth/src/lib.rs".to_string(),
line_start: 10,
line_end: 40,
content: "pub use crate::kimi::persist_kimi_auth_record;".to_string(),
language: Language::Rust,
score: 0.0,
symbol_name: Some("omnigate_auth".to_string()),
symbol_type: Some(SymbolType::Module),
},
SearchResult {
file_path: "backend/crates/omnigate-auth/src/factory.rs".to_string(),
line_start: 286,
line_end: 296,
content: "pub fn persist_factory_auth_record() {}".to_string(),
language: Language::Rust,
score: 0.0,
symbol_name: Some("persist_factory_auth_record".to_string()),
symbol_type: Some(SymbolType::Function),
},
SearchResult {
file_path: "backend/crates/omnigate-auth/src/kimi.rs".to_string(),
line_start: 265,
line_end: 275,
content: "pub fn persist_kimi_auth_record() {}".to_string(),
language: Language::Rust,
score: 0.0,
symbol_name: Some("persist_kimi_auth_record".to_string()),
symbol_type: Some(SymbolType::Function),
},
];
let queries = vec![
"persist_kimi_auth_record".to_string(),
"persist_factory_auth_record".to_string(),
];
let augmented = augment_multi_query_exact_matches(
dir.path(),
&queries,
fused,
&SearchFilters::default(),
5,
)
.unwrap();
assert_eq!(
augmented[0].symbol_name.as_deref(),
Some("persist_kimi_auth_record")
);
assert_eq!(
augmented[1].symbol_name.as_deref(),
Some("persist_factory_auth_record")
);
}
#[test]
fn multi_query_exact_matches_interleave_across_queries() {
let dir = tempdir().unwrap();
let metadata_path = dir.path().join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
let mut chunks: Vec<Chunk> = (0..5)
.map(|i| Chunk {
id: format!("common:{i}"),
file_path: format!("src/common_{i}.rs"),
line_start: 1,
line_end: 10,
content: "pub fn common_fn() {}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("common_fn".to_string()),
})
.collect();
chunks.push(Chunk {
id: "rare:0".to_string(),
file_path: "src/rare.rs".to_string(),
line_start: 1,
line_end: 10,
content: "pub fn rare_fn() {}".to_string(),
language: Language::Rust,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("rare_fn".to_string()),
});
store.insert_chunks(&chunks).unwrap();
let augmented = augment_multi_query_exact_matches(
dir.path(),
&["common_fn".to_string(), "rare_fn".to_string()],
Vec::new(),
&SearchFilters::default(),
3,
)
.unwrap();
assert_eq!(augmented.len(), 3);
assert_eq!(augmented[0].symbol_name.as_deref(), Some("common_fn"));
assert_eq!(augmented[1].symbol_name.as_deref(), Some("rare_fn"));
}
#[test]
fn bare_filename_query_gets_exact_file_augmentation() {
let dir = tempdir().unwrap();
let metadata_path = dir.path().join("metadata.db");
let store = MetadataStore::open(&metadata_path).unwrap();
store
.insert_chunks(&[
Chunk {
id: "deep:0".to_string(),
file_path: "deep/nested/handler.py".to_string(),
line_start: 1,
line_end: 20,
content: "def handle(): pass".to_string(),
language: Language::Python,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("handle".to_string()),
},
Chunk {
id: "shallow:0".to_string(),
file_path: "src/handler.py".to_string(),
line_start: 1,
line_end: 20,
content: "def handle(): pass".to_string(),
language: Language::Python,
symbol_type: Some(SymbolType::Function),
symbol_name: Some("handle".to_string()),
},
])
.unwrap();
let augmented = augment_multi_query_exact_matches(
dir.path(),
&["handler.py".to_string()],
Vec::new(),
&SearchFilters::default(),
5,
)
.unwrap();
assert!(
!augmented.is_empty(),
"a bare filename query must inject exact file chunks"
);
assert_eq!(augmented[0].file_path, "src/handler.py");
}
}