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
//! Hybrid embedding backend (unified TF-IDF + neural/remote).
use super::*;
// ============================================================================
// HYBRID EMBEDDING BACKEND (Unified TF-IDF + Neural/Remote)
// ============================================================================
/// Neural embedding dimension for the Qwen3-Embedding-0.6B ONNX model.
///
/// This dimension is used for local ONNX-backed neural embeddings.
/// The value must match the output dimension of the deployed model.
#[cfg(feature = "onnx")]
pub(crate) const NEURAL_EMBEDDING_DIMENSION: usize = 1024;
/// Hybrid embedding backend that always uses TF-IDF as the base signal and
/// combines configured neural/remote embeddings for semantic retrieval.
#[derive(Debug, Clone)]
pub enum HybridEmbedder {
/// TF-IDF only (base signal always available)
TfIdfOnly(TfIdfEmbedder),
/// TF-IDF + Local ONNX neural embeddings (via worker process)
#[cfg(feature = "onnx")]
HybridLocal {
/// TF-IDF embedder for keyword-based search
tfidf: TfIdfEmbedder,
/// Worker client for neural embedding via leindex-embed process
neural: EmbeddingClient,
/// Weight for neural embeddings in hybrid scoring (0.0-1.0)
neural_weight: f32,
},
/// TF-IDF + Remote embeddings (OpenAI, Cohere, custom)
#[cfg(feature = "remote-embeddings")]
HybridRemote {
/// TF-IDF embedder for keyword-based search
tfidf: TfIdfEmbedder,
/// Remote embedding provider for semantic search
remote: GenericRemoteProvider,
/// Weight for remote embeddings in hybrid scoring (0.0-1.0)
remote_weight: f32,
},
}
/// Scoring weights for hybrid embedding combination
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct HybridScoringWeights {
/// Weight for TF-IDF signal (0.0-1.0)
pub tfidf: f32,
/// Weight for neural/remote signal (0.0-1.0)
pub neural: f32,
/// Weight for structural signal (0.0-1.0)
pub structural: f32,
/// Weight for text match signal (0.0-1.0)
pub text_match: f32,
}
impl Default for HybridScoringWeights {
fn default() -> Self {
Self {
tfidf: 0.30,
neural: 0.40,
structural: 0.15,
text_match: 0.15,
}
}
}
impl HybridScoringWeights {
/// Create weights when neural embedding is unavailable
pub fn without_neural() -> Self {
Self {
tfidf: 0.60,
neural: 0.00,
structural: 0.20,
text_match: 0.20,
}
}
/// Normalize weights to sum to 1.0
pub fn normalize(&self) -> Self {
let sum = self.tfidf + self.neural + self.structural + self.text_match;
if sum == 0.0 {
return Self::default();
}
Self {
tfidf: self.tfidf / sum,
neural: self.neural / sum,
structural: self.structural / sum,
text_match: self.text_match / sum,
}
}
}
impl HybridEmbedder {
/// Create an explicit TF-IDF-only embedder for disabled/terminal-failure paths.
pub fn tfidf_only(embedder: TfIdfEmbedder) -> Self {
Self::TfIdfOnly(embedder)
}
/// Create a hybrid embedder with local ONNX neural embeddings via worker
#[cfg(feature = "onnx")]
pub fn hybrid_local(tfidf: TfIdfEmbedder, neural_weight: Option<f32>) -> Result<Self, String> {
Ok(Self::HybridLocal {
tfidf,
neural: EmbeddingClient::new(),
neural_weight: neural_weight.unwrap_or(0.40),
})
}
/// Create a hybrid embedder with remote embeddings
#[cfg(feature = "remote-embeddings")]
pub fn hybrid_remote(
tfidf: TfIdfEmbedder,
remote_config: RemoteEmbeddingConfig,
remote_weight: Option<f32>,
) -> Result<Self, RemoteEmbeddingError> {
let remote = GenericRemoteProvider::from_config(remote_config)?;
Ok(Self::HybridRemote {
tfidf,
remote,
remote_weight: remote_weight.unwrap_or(0.40),
})
}
/// Get the TF-IDF embedder (always available)
pub fn tfidf(&self) -> &TfIdfEmbedder {
match self {
Self::TfIdfOnly(embedder) => embedder,
#[cfg(feature = "onnx")]
Self::HybridLocal { tfidf, .. } => tfidf,
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { tfidf, .. } => tfidf,
}
}
/// Get the TF-IDF embedder mutably (always available)
pub fn tfidf_mut(&mut self) -> &mut TfIdfEmbedder {
match self {
Self::TfIdfOnly(embedder) => embedder,
#[cfg(feature = "onnx")]
Self::HybridLocal { tfidf, .. } => tfidf,
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { tfidf, .. } => tfidf,
}
}
/// Get the configured TF-IDF embedding dimension.
pub fn tfidf_dimension(&self) -> usize {
self.tfidf().dimension()
}
/// Get the neural/remote dimension (if available)
pub fn neural_dimension(&self) -> Option<usize> {
match self {
Self::TfIdfOnly(_) => None,
#[cfg(feature = "onnx")]
Self::HybridLocal { .. } => Some(NEURAL_EMBEDDING_DIMENSION),
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { remote, .. } => Some(remote.dimension()),
}
}
/// Check if neural/remote enhancement is available
pub fn has_neural(&self) -> bool {
match self {
Self::TfIdfOnly(_) => false,
#[cfg(feature = "onnx")]
Self::HybridLocal { .. } => true,
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => true,
}
}
/// If a GPU provider was requested but the worker fell back to CPU, return
/// a reason the caller can act on (skip neural enrichment → TF-IDF only).
///
/// Honors the user's intent: a deliberate `cpu` (or `auto`) configuration
/// always returns `None` so the CPU neural path stays fully operational;
/// only an explicit `migraphx`/`cuda`/`rocm` request that actually degraded
/// to CPU is flagged. See [`EmbeddingClient::cpu_fallback_reason`].
#[cfg(feature = "onnx")]
pub fn cpu_fallback_reason(&self) -> Option<String> {
match self {
Self::HybridLocal { neural, .. } => neural.cpu_fallback_reason(),
_ => None,
}
}
/// Whether neural inference is already ready for a query.
///
pub fn neural_ready(&self) -> bool {
match self {
Self::TfIdfOnly(_) => false,
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => neural.is_ready(),
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => true,
}
}
/// Report the readiness state used by MCP retrieval metadata.
pub fn neural_status(&self) -> &'static str {
match self {
Self::TfIdfOnly(_) => "absent",
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => match neural.availability() {
crate::search::WorkerAvailability::Ready => "ready",
crate::search::WorkerAvailability::Initializing(_) => "initializing",
crate::search::WorkerAvailability::Failed(_) => "failed",
crate::search::WorkerAvailability::Absent => "absent",
},
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => "ready",
}
}
/// Get the neural weight for scoring
pub fn neural_weight(&self) -> f32 {
match self {
Self::TfIdfOnly(_) => 0.0,
#[cfg(feature = "onnx")]
Self::HybridLocal { neural_weight, .. } => *neural_weight,
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { remote_weight, .. } => *remote_weight,
}
}
/// Get recommended scoring weights
pub fn scoring_weights(&self) -> HybridScoringWeights {
if self.has_neural() {
HybridScoringWeights::default()
} else {
HybridScoringWeights::without_neural()
}
}
/// Generate TF-IDF embedding for pre-tokenized content (always available)
pub fn embed_tfidf(&self, tokens: &[String]) -> Vec<f32> {
self.tfidf().embed_tokens(tokens)
}
/// Generate neural/remote embedding for text (if available)
///
/// Uses `embed_with_fallback` for retry-once semantics:
/// - VAL-CPHASE-017: Retries once on worker failure
/// - VAL-CPHASE-018: Falls back to TF-IDF for the affected batch after second failure
/// - VAL-CPHASE-019: Emits actionable warning on fallback
/// - VAL-CPHASE-020: Worker failure does not crash the main daemon
/// - VAL-CPHASE-021: Fresh worker can be spawned after fallback
#[cfg(any(feature = "onnx", feature = "remote-embeddings"))]
pub async fn embed_neural_async(&self, text: &str) -> Option<Result<Vec<f32>, String>> {
match self {
Self::TfIdfOnly(_) => None,
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => {
// Clone shares the worker handle via Arc (EmbeddingClient::clone is cheap).
// Required because spawn_blocking requires ownership.
let neural = neural.clone();
let texts = vec![text.to_string()];
let result = task::spawn_blocking(move || {
neural.embed_with_fallback(&texts, NEURAL_EMBEDDING_DIMENSION)
})
.await
.ok()?;
match result {
EmbedResult::Success(response) => {
let vectors = response.into_vectors();
if vectors.len() != 1 {
return Some(Err(format!(
"worker returned {} vectors for one input",
vectors.len()
)));
}
// VAL-CPHASE-016: write from flat buffer directly. The decoded
// vector count, not response.count, is the storage contract.
Some(Ok(vectors
.into_iter()
.next()
.expect("validated vector count")))
}
EmbedResult::Fallback { batch_id, error } => {
// VAL-CPHASE-018/019: Fallback already logged actionable warning.
tracing::warn!(
batch_id = %batch_id,
error = %error,
"Neural embedding degraded to TF-IDF for node (async path)"
);
None
}
}
}
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { remote, .. } => Some(
remote
.embed(text)
.await
.map_err(|e| format!("Remote embedding failed: {}", e)),
),
}
}
/// Blocking cross-encoder rerank of candidate documents via the worker's
/// on-demand reranker (bge-reranker-base). Takes (id, content, initial_score)
/// tuples and returns (id, combined_score) ranked by the cross-encoder.
/// Returns None when no neural worker is available (TfIdfOnly, or a
/// no-onnx build) — caller keeps the original ordering. (VAL-RERANK.)
pub fn rerank_blocking(
&self,
query: &str,
docs: Vec<(String, String, f32)>,
) -> Option<Result<Vec<(String, f32)>, String>> {
#[cfg(feature = "onnx")]
#[allow(clippy::needless_return)]
// return is required so the cfg(not(onnx)) fallthrough block can follow
{
use leindex_embed::protocol::RerankDocument;
return match self {
Self::TfIdfOnly(_) => None,
Self::HybridLocal { neural, .. } => {
let documents: Vec<RerankDocument> = docs
.into_iter()
.map(|(id, content, initial_score)| RerankDocument {
id,
content,
initial_score,
})
.collect();
match neural.rerank(query, documents) {
Ok(resp) => Some(Ok(resp
.results
.into_iter()
.map(|r| (r.id, r.combined_score))
.collect())),
Err(e) => Some(Err(e.to_string())),
}
}
// Remote-only embedders provide embeddings, not a local reranker;
// reranking is a local-ONNX capability. (Required for match
// exhaustiveness under the onnx+remote-embeddings feature combo.)
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => None,
};
}
#[cfg(not(feature = "onnx"))]
{
let _ = (query, docs);
None
}
}
/// Embed a single text with the neural embedder (blocking). Returns `None`
/// for the TF-IDF-only variant; for the hybrid-local variant returns the
/// embedding result, with fallback applied by `embed_with_fallback`.
#[cfg(any(feature = "onnx", feature = "remote-embeddings"))]
pub fn embed_neural_blocking(&self, text: &str) -> Option<Result<Vec<f32>, String>> {
match self {
Self::TfIdfOnly(_) => None,
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => {
let texts = vec![text.to_string()];
let result = neural.embed_with_fallback(&texts, NEURAL_EMBEDDING_DIMENSION);
match result {
EmbedResult::Success(response) => {
let vectors = response.into_vectors();
if vectors.len() != 1 {
return Some(Err(format!(
"worker returned {} vectors for one input",
vectors.len()
)));
}
// VAL-CPHASE-016: write from flat buffer directly. The decoded
// vector count, not response.count, is the storage contract.
Some(Ok(vectors
.into_iter()
.next()
.expect("validated vector count")))
}
EmbedResult::Fallback { batch_id, error } => {
// VAL-CPHASE-018/019: Fallback already logged actionable warning.
// Return None so the caller falls back to TF-IDF for this batch.
tracing::warn!(
batch_id = %batch_id,
error = %error,
"Neural embedding degraded to TF-IDF for node"
);
None
}
}
}
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => {
// Remote requires async runtime, this is a blocking wrapper
// In practice, the indexing pipeline should use the async version
Some(Err("Remote embeddings require async runtime".to_string()))
}
}
}
/// Generate neural/remote embeddings for a batch of texts (blocking wrapper).
///
/// Returns `Vec<Option<Vec<f32>>>` — one entry per input text.
/// `Some(vec)` on success, `None` only when the provider is unavailable or
/// the affected request enters the explicit fallback path.
///
/// This batches all texts into a single IPC call to the ONNX worker,
/// reducing N round-trips to 1 per chunk.
#[cfg(any(feature = "onnx", feature = "remote-embeddings"))]
pub fn embed_neural_batch_blocking(&self, texts: &[String]) -> Vec<Option<Vec<f32>>> {
match self {
Self::TfIdfOnly(_) => vec![None; texts.len()],
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => {
if texts.is_empty() {
return Vec::new();
}
let result = neural.embed_with_fallback(texts, NEURAL_EMBEDDING_DIMENSION);
match result {
EmbedResult::Success(response) => {
let vectors = response.into_vectors();
if vectors.len() == texts.len() {
vectors.into_iter().map(Some).collect()
} else {
tracing::warn!(
expected = texts.len(),
got = vectors.len(),
"Neural batch returned wrong vector count, falling back to None for all"
);
vec![None; texts.len()]
}
}
EmbedResult::Fallback { batch_id, error } => {
tracing::warn!(
batch_id = %batch_id,
error = %error,
"Neural batch embedding degraded to TF-IDF for {} texts",
texts.len()
);
vec![None; texts.len()]
}
}
}
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => {
// Remote requires async runtime; not supported in blocking context
vec![None; texts.len()]
}
}
}
/// Persist the TF-IDF embedder to storage
///
/// Delegates to the inner TfIdfEmbedder's persist_to_storage method
pub fn persist_to_storage(
&self,
project_path: &Path,
pdg: &ProgramDependenceGraph,
) -> Result<()> {
self.tfidf().persist_to_storage(project_path, pdg)
}
/// Unload the ONNX session if the hybrid backend uses one (A+ idle-unload).
///
/// After an indexing batch completes, calling this drops the live ONNX
/// session so it does not remain resident indefinitely (VAL-APLUS-024).
/// With the worker architecture, this signals the worker to shut down.
pub fn unload_onnx(&self) {
match self {
Self::TfIdfOnly(_) => {}
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => {
// Kill the worker process; the client can spawn a fresh one later.
neural.kill_worker();
}
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => {}
}
}
/// Check whether the ONNX session is currently loaded.
#[must_use]
pub fn is_onnx_loaded(&self) -> bool {
match self {
Self::TfIdfOnly(_) => false,
#[cfg(feature = "onnx")]
Self::HybridLocal { neural, .. } => neural.is_ready(),
#[cfg(feature = "remote-embeddings")]
Self::HybridRemote { .. } => false,
}
}
}
impl Default for HybridEmbedder {
fn default() -> Self {
Self::TfIdfOnly(TfIdfEmbedder::build_from_tokens(&[]))
}
}