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
//! Multi-strategy ensemble query (Reciprocal Rank Fusion) for [`RagClient`].
use super::RagClient;
use crate::rag::embedding::EmbeddingProvider;
use crate::rag::types::*;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::time::Instant;
impl RagClient {
/// Multi-strategy ensemble query: fan out across all requested strategies
/// concurrently, fuse results via Reciprocal Rank Fusion (RRF), and
/// optionally apply spectral diversity reranking as a final pass.
///
/// ## Strategies
///
/// - `Semantic` — vector similarity search
/// - `Keyword` — BM25 keyword / hybrid search
/// - `GitHistory` — semantic search over commit history
/// - `CodeNavigation` — AST-based relations search (requires `code-analysis`)
///
/// ## Fusion
///
/// Results from each strategy are deduplicated by `file_path:start_line` and
/// fused using RRF so that items appearing near the top of multiple strategy
/// lists rank highest overall.
pub async fn query_ensemble(&self, request: EnsembleRequest) -> Result<EnsembleResponse> {
use brainwires_storage::bm25_search::reciprocal_rank_fusion_generic;
let start = Instant::now();
// Determine active strategies.
let active: Vec<SearchStrategy> = if request.strategies.is_empty() {
#[allow(unused_mut)]
let mut s = vec![
SearchStrategy::Semantic,
SearchStrategy::Keyword,
SearchStrategy::GitHistory,
];
#[cfg(feature = "code-analysis")]
s.push(SearchStrategy::CodeNavigation);
s
} else {
request.strategies.clone()
};
// Embed the query once.
let query_embedding = self
.embedding_provider
.embed_batch(vec![request.query.clone()])
.context("Failed to generate query embedding for ensemble")?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("No embedding generated for ensemble query"))?;
// Fan out across strategies concurrently.
// Each strategy returns (strategy_name, Vec<SearchResult>).
let path = request.path.clone();
let project = request.project.clone();
let query = request.query.clone();
let limit = request.limit;
let min_score = request.min_score;
let file_extensions = request.file_extensions.clone();
let languages = request.languages.clone();
// Build strategy futures as boxed async closures resolved concurrently.
let mut strategy_futures = Vec::new();
for strategy in &active {
match strategy {
SearchStrategy::Semantic => {
let qe = query_embedding.clone();
let q = query.clone();
let pa = path.clone();
let pr = project.clone();
let db = self.vector_db.clone();
strategy_futures.push(tokio::spawn(async move {
let results = db
.search(qe, &q, limit * 2, min_score, pr, pa, false)
.await
.unwrap_or_default();
("semantic".to_string(), results)
}));
}
SearchStrategy::Keyword => {
let qe = query_embedding.clone();
let q = query.clone();
let pa = path.clone();
let pr = project.clone();
let db = self.vector_db.clone();
let exts = file_extensions.clone();
let langs = languages.clone();
strategy_futures.push(tokio::spawn(async move {
let results = if exts.is_empty() && langs.is_empty() {
db.search(qe, &q, limit * 2, min_score, pr, pa, true)
.await
.unwrap_or_default()
} else {
db.search_filtered(
qe,
&q,
limit * 2,
min_score,
pr,
pa,
true,
exts,
langs,
Vec::new(),
)
.await
.unwrap_or_default()
};
("keyword".to_string(), results)
}));
}
SearchStrategy::GitHistory => {
let ep = self.embedding_provider.clone();
let db = self.vector_db.clone();
let gc = self.git_cache.clone();
let gp = self.git_cache_path.clone();
let q = query.clone();
let pa = path.clone().unwrap_or_else(|| ".".to_string());
let pr = project.clone();
strategy_futures.push(tokio::spawn(async move {
use crate::rag::client::git_indexing;
use brainwires_core::SearchResult;
let git_req = SearchGitHistoryRequest {
query: q,
path: pa,
project: pr,
branch: None,
max_commits: 200,
limit: limit * 2,
min_score,
author: None,
since: None,
until: None,
file_pattern: None,
};
let resp: SearchGitHistoryResponse =
git_indexing::do_search_git_history(ep, db, gc, &gp, git_req)
.await
.unwrap_or(SearchGitHistoryResponse {
results: Vec::new(),
commits_indexed: 0,
total_cached_commits: 0,
duration_ms: 0,
});
let results: Vec<SearchResult> = resp
.results
.into_iter()
.map(|g| SearchResult {
file_path: g.commit_hash.clone(),
root_path: None,
content: format!("{}\n{}", g.commit_message, g.diff_snippet),
score: g.score,
vector_score: g.vector_score,
keyword_score: g.keyword_score,
start_line: 0,
end_line: 0,
language: "git".to_string(),
project: None,
indexed_at: g.commit_date,
})
.collect();
("git_history".to_string(), results)
}));
}
#[cfg(feature = "code-analysis")]
SearchStrategy::CodeNavigation => {
let qe = query_embedding.clone();
let db = self.vector_db.clone();
let q = query.clone();
let pa = path.clone();
let pr = project.clone();
strategy_futures.push(tokio::spawn(async move {
let results = db
.search(qe, &q, limit * 2, min_score, pr, pa, false)
.await
.unwrap_or_default();
("code_navigation".to_string(), results)
}));
}
}
}
// Collect strategy results.
let mut all_results: HashMap<String, SearchResult> = HashMap::new();
let mut strategy_lists: Vec<Vec<(String, f32)>> = Vec::new();
let mut strategies_used: Vec<String> = Vec::new();
let mut per_strategy_counts: HashMap<String, usize> = HashMap::new();
for handle in strategy_futures {
match handle.await {
Ok((name, results)) => {
per_strategy_counts.insert(name.clone(), results.len());
let ranked: Vec<(String, f32)> = results
.iter()
.map(|r| {
let key = format!("{}:{}", r.file_path, r.start_line);
all_results.entry(key.clone()).or_insert_with(|| r.clone());
(key, r.score)
})
.collect();
if !ranked.is_empty() {
strategies_used.push(name);
strategy_lists.push(ranked);
}
}
Err(e) => {
tracing::warn!("Ensemble strategy task failed: {e}");
}
}
}
// RRF fusion across all strategy ranked lists.
let fused: Vec<(String, f32)> = reciprocal_rank_fusion_generic(strategy_lists, limit);
// Resolve fused keys back to SearchResult, overriding score with RRF score.
let mut results: Vec<SearchResult> = fused
.into_iter()
.filter_map(|(key, rrf_score)| {
all_results.get(&key).map(|r| {
let mut result = r.clone();
result.score = rrf_score;
result
})
})
.collect();
// Optional spectral reranking as a final diversity pass.
#[cfg(feature = "spectral-select")]
if request.spectral_rerank && results.len() > limit {
use crate::spectral::{DiversityReranker, SpectralReranker, SpectralSelectConfig};
let keys: Vec<String> = results
.iter()
.map(|r| format!("{}:{}", r.file_path, r.start_line))
.collect();
// Re-fetch embeddings for the fused candidates.
if let Ok((_, embeddings)) = self
.vector_db
.search_with_embeddings(
query_embedding.clone(),
&request.query,
results.len(),
0.0,
request.project.clone(),
request.path.clone(),
false,
)
.await
{
// Build a key→embedding map from the re-fetched results.
let _ = keys; // suppress unused warning
if embeddings.len() == results.len() {
let reranker = SpectralReranker::new(SpectralSelectConfig::default());
let indices = reranker.rerank(&results, &embeddings, limit);
results = indices.into_iter().map(|i| results[i].clone()).collect();
} else {
results.truncate(limit);
}
} else {
results.truncate(limit);
}
}
results.truncate(limit);
Ok(EnsembleResponse {
results,
duration_ms: start.elapsed().as_millis() as u64,
strategies_used,
per_strategy_counts,
})
}
}