khive-retrieval 0.21.0

Hybrid retrieval composer (HNSW + BM25 + fusion + graph + cross-encoder) with deterministic scoring
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
use super::*;

struct FakeScorer {
    scores: Vec<f32>,
}

impl CrossEncoderScorer for FakeScorer {
    fn score_batch(&self, _query: &str, _documents: &[&str]) -> Vec<f32> {
        self.scores.clone()
    }
}

struct FakeResolver {
    documents: Vec<Option<String>>,
}

#[async_trait]
impl RerankDocumentResolver<u32> for FakeResolver {
    async fn resolve_documents(&self, _ids: &[u32]) -> Result<Vec<Option<String>>> {
        Ok(self.documents.clone())
    }
}

fn make_reranker(
    scores: Vec<f32>,
    documents: Vec<Option<String>>,
) -> NativeCrossEncoderReranker<u32, FakeResolver, FakeScorer> {
    NativeCrossEncoderReranker::new(
        Arc::new(FakeScorer { scores }),
        Arc::new(FakeResolver { documents }),
    )
}

#[tokio::test]
async fn test_top_k_zero_returns_empty() {
    let reranker = make_reranker(vec![0.9, 0.1], vec![Some("a".into()), Some("b".into())]);
    let results = vec![(1u32, DeterministicScore::from_f64(0.5))];
    let out = reranker.rerank("q", results, 0).await.unwrap();
    assert!(out.is_empty());
}

#[tokio::test]
async fn test_empty_input_returns_empty() {
    let reranker = make_reranker(vec![], vec![]);
    let out = reranker.rerank("q", vec![], 5).await.unwrap();
    assert!(out.is_empty());
}

#[tokio::test]
async fn test_descending_sort() {
    let reranker = make_reranker(
        vec![0.1, 0.9, 0.5],
        vec![Some("a".into()), Some("b".into()), Some("c".into())],
    );
    let results = vec![
        (1u32, DeterministicScore::from_f64(0.3)),
        (2u32, DeterministicScore::from_f64(0.3)),
        (3u32, DeterministicScore::from_f64(0.3)),
    ];
    let out = reranker.rerank("q", results, 3).await.unwrap();
    assert_eq!(out[0].0, 2u32); // score 0.9
    assert_eq!(out[1].0, 3u32); // score 0.5
    assert_eq!(out[2].0, 1u32); // score 0.1
}

#[tokio::test]
async fn test_tie_preserves_original_order() {
    let reranker = make_reranker(
        vec![0.5, 0.5, 0.5],
        vec![Some("a".into()), Some("b".into()), Some("c".into())],
    );
    let results = vec![
        (10u32, DeterministicScore::from_f64(0.3)),
        (20u32, DeterministicScore::from_f64(0.3)),
        (30u32, DeterministicScore::from_f64(0.3)),
    ];
    let out = reranker.rerank("q", results, 3).await.unwrap();
    assert_eq!(out[0].0, 10u32);
    assert_eq!(out[1].0, 20u32);
    assert_eq!(out[2].0, 30u32);
}

#[tokio::test]
async fn missing_documents_keep_surviving_ids_and_scores_aligned() {
    let scorer = Arc::new(RecordingScorer::default());
    let reranker = NativeCrossEncoderReranker::new(
        scorer.clone(),
        Arc::new(FakeResolver {
            documents: vec![Some("low".into()), None, Some("high".into())],
        }),
    );
    let out = reranker
        .rerank("query", candidates(&[10, 20, 30]), 3)
        .await
        .unwrap();
    assert_eq!(
        out,
        vec![
            (30, DeterministicScore::from_f64(0.9_f32 as f64)),
            (10, DeterministicScore::from_f64(0.1_f32 as f64))
        ]
    );
    assert_eq!(
        *scorer.calls.lock().unwrap(),
        vec![(
            "query".to_string(),
            vec!["low".to_string(), "high".to_string()]
        )]
    );
    let all_present = NativeCrossEncoderReranker::new(
        Arc::new(RecordingScorer::default()),
        Arc::new(FakeResolver {
            documents: vec![Some("low".into()), Some("high".into())],
        }),
    );
    assert_eq!(
        all_present
            .rerank("query", candidates(&[10, 30]), 3)
            .await
            .unwrap(),
        out
    );
}

#[derive(Default)]
struct RecordingScorer {
    calls: std::sync::Mutex<Vec<(String, Vec<String>)>>,
}

impl CrossEncoderScorer for RecordingScorer {
    fn score_batch(&self, query: &str, documents: &[&str]) -> Vec<f32> {
        self.calls.lock().unwrap().push((
            query.into(),
            documents.iter().map(|text| (*text).into()).collect(),
        ));
        documents
            .iter()
            .map(|text| match *text {
                "low" => 0.1,
                "high" => 0.9,
                _ => panic!("unexpected document text"),
            })
            .collect()
    }
}

fn candidates(ids: &[u32]) -> Vec<(u32, DeterministicScore)> {
    ids.iter()
        .map(|id| (*id, DeterministicScore::from_f64(0.5)))
        .collect()
}

#[tokio::test]
async fn all_missing_documents_do_not_call_the_scorer() {
    let scorer = Arc::new(RecordingScorer::default());
    let reranker = NativeCrossEncoderReranker::new(
        scorer.clone(),
        Arc::new(FakeResolver {
            documents: vec![None, None],
        }),
    );
    assert!(reranker
        .rerank("query", candidates(&[10, 20]), 2)
        .await
        .unwrap()
        .is_empty());
    assert!(scorer.calls.lock().unwrap().is_empty());
}

#[tokio::test]
async fn limit_applies_after_missing_documents_and_scoring() {
    let reranker = NativeCrossEncoderReranker::new(
        Arc::new(RecordingScorer::default()),
        Arc::new(FakeResolver {
            documents: vec![None, Some("low".into()), Some("high".into())],
        }),
    );
    assert_eq!(
        reranker
            .rerank("query", candidates(&[10, 20, 30]), 1)
            .await
            .unwrap(),
        vec![(30, DeterministicScore::from_f64(0.9_f32 as f64))]
    );
}

#[tokio::test]
async fn missing_documents_preserve_tie_order_and_duplicate_ids() {
    let reranker = make_reranker(
        vec![0.5, 0.5, 0.5],
        vec![Some("a".into()), None, Some("b".into()), Some("a".into())],
    );
    assert_eq!(
        reranker
            .rerank("q", candidates(&[30, 99, 10, 30]), 4)
            .await
            .unwrap(),
        candidates(&[30, 10, 30])
    );
}

#[tokio::test]
async fn scorer_length_must_match_retained_documents() {
    let reranker = make_reranker(
        vec![0.1, 0.2, 0.3],
        vec![Some("a".into()), None, Some("b".into())],
    );
    let error = reranker
        .rerank("q", candidates(&[10, 20, 30]), 3)
        .await
        .unwrap_err();
    assert!(
        matches!(error, RetrievalError::Rerank(message) if message == "model returned 3 scores for 2 candidates")
    );
}

#[tokio::test]
async fn test_resolver_length_mismatch_returns_error() {
    struct BadResolver;

    #[async_trait]
    impl RerankDocumentResolver<u32> for BadResolver {
        async fn resolve_documents(&self, _ids: &[u32]) -> Result<Vec<Option<String>>> {
            Ok(vec![]) // wrong length
        }
    }

    let reranker = NativeCrossEncoderReranker::new(
        Arc::new(FakeScorer { scores: vec![0.5] }),
        Arc::new(BadResolver),
    );
    let results = vec![(1u32, DeterministicScore::from_f64(0.5))];
    let err = reranker.rerank("q", results, 1).await.unwrap_err();
    assert!(matches!(err, RetrievalError::Rerank(_)));
}

#[tokio::test]
async fn test_top_k_limits_output() {
    let reranker = make_reranker(
        vec![0.9, 0.8, 0.7],
        vec![Some("a".into()), Some("b".into()), Some("c".into())],
    );
    let results = vec![
        (1u32, DeterministicScore::from_f64(0.3)),
        (2u32, DeterministicScore::from_f64(0.3)),
        (3u32, DeterministicScore::from_f64(0.3)),
    ];
    let out = reranker.rerank("q", results, 2).await.unwrap();
    assert_eq!(out.len(), 2);
}

#[tokio::test]
async fn test_top_k_larger_than_results_returns_all() {
    // top_k=10 with only 2 candidates — should return all 2, sorted by score
    let reranker = make_reranker(vec![0.1, 0.9], vec![Some("a".into()), Some("b".into())]);
    let results = vec![
        (1u32, DeterministicScore::from_f64(0.5)),
        (2u32, DeterministicScore::from_f64(0.3)),
    ];
    let out = reranker.rerank("q", results, 10).await.unwrap();
    assert_eq!(out.len(), 2);
    assert_eq!(out[0].0, 2u32); // score 0.9
    assert_eq!(out[1].0, 1u32); // score 0.1
}

#[tokio::test]
async fn test_single_result_passes_through() {
    let reranker = make_reranker(vec![0.75], vec![Some("only doc".into())]);
    let results = vec![(42u32, DeterministicScore::from_f64(0.5))];
    let out = reranker.rerank("q", results, 1).await.unwrap();
    assert_eq!(out.len(), 1);
    assert_eq!(out[0].0, 42u32);
}

#[derive(Default)]
struct HookAwareScorer {
    calls: std::sync::atomic::AtomicUsize,
    inputs: std::sync::Mutex<Vec<(String, Vec<String>)>>,
}

impl CrossEncoderScorer for HookAwareScorer {
    fn score_batch(&self, query: &str, documents: &[&str]) -> Vec<f32> {
        self.calls.fetch_add(1, Ordering::Relaxed);
        self.inputs.lock().unwrap().push((
            query.to_string(),
            documents
                .iter()
                .map(|document| (*document).to_string())
                .collect(),
        ));
        vec![0.25; documents.len()]
    }

    fn score_batch_with_hook(
        &self,
        query: &str,
        documents: &[&str],
        hook: Option<&dyn LoraHook>,
    ) -> Result<CrossEncoderScores> {
        let mut scores = self.score_batch(query, documents);
        let observer = hook.map(ObservedHook::new);
        if let Some(observer) = &observer {
            for score in &mut scores {
                if observer.is_active(0, "ffn_output") {
                    observer.apply(0, "ffn_output", &[1.0], std::slice::from_mut(score));
                }
            }
        }
        Ok(CrossEncoderScores {
            scores,
            hook_applied: observer
                .as_ref()
                .is_some_and(|observer| observer.applied.load(Ordering::Relaxed)),
        })
    }
}

struct BorrowedHook<'a> {
    delta: &'a f32,
    calls: &'a std::sync::atomic::AtomicUsize,
    target: &'a str,
}

impl LoraHook for BorrowedHook<'_> {
    fn apply(&self, _layer_idx: usize, _module: &str, _x: &[f32], output: &mut [f32]) {
        self.calls.fetch_add(1, Ordering::Relaxed);
        for value in output {
            *value += self.delta;
        }
    }

    fn is_active(&self, _layer_idx: usize, module: &str) -> bool {
        module == self.target
    }
}

#[tokio::test]
async fn per_call_hook_reaches_scorer_and_is_not_retained() {
    let scorer = Arc::new(HookAwareScorer::default());
    let reranker = NativeCrossEncoderReranker::new(
        scorer.clone(),
        Arc::new(FakeResolver {
            documents: vec![Some("a".into()), None, Some("b".into())],
        }),
    );
    let baseline = reranker
        .rerank("q", candidates(&[1, 2, 3]), 2)
        .await
        .unwrap();
    let delta = 0.5;
    let calls = std::sync::atomic::AtomicUsize::new(0);
    let hook = BorrowedHook {
        delta: &delta,
        calls: &calls,
        target: "ffn_output",
    };
    let adapted = reranker
        .rerank_with_hook("q", candidates(&[1, 2, 3]), 1, Some(&hook))
        .await
        .unwrap();
    assert!(adapted.hook_applied, "per-call hook must reach the scorer");
    assert_eq!(calls.load(Ordering::Relaxed), 2);
    assert_eq!(
        adapted.results,
        vec![(1, DeterministicScore::from_f64(0.75))]
    );
    let subsequent = reranker
        .rerank_with_hook("q", candidates(&[1, 2, 3]), 2, None)
        .await
        .unwrap();
    assert_eq!(
        subsequent.results, baseline,
        "hook must not survive its call"
    );
    assert!(!subsequent.hook_applied);
    assert_eq!(calls.load(Ordering::Relaxed), 2);
    assert_eq!(scorer.calls.load(Ordering::Relaxed), 3);
    assert_eq!(
        *scorer.inputs.lock().unwrap(),
        vec![("q".to_string(), vec!["a".to_string(), "b".to_string()]); 3],
        "per-call scoring must preserve the literal query and retained document order"
    );
}

#[tokio::test]
async fn noop_hook_matches_none_without_applied_evidence() {
    let reranker = NativeCrossEncoderReranker::new(
        Arc::new(HookAwareScorer::default()),
        Arc::new(FakeResolver {
            documents: vec![Some("a".into())],
        }),
    );
    let none = reranker
        .rerank_with_hook("q", candidates(&[1]), 1, None)
        .await
        .unwrap();
    let noop = reranker
        .rerank_with_hook("q", candidates(&[1]), 1, Some(&NoopLoraHook))
        .await
        .unwrap();
    assert_eq!(noop.results, none.results);
    assert!(!none.hook_applied);
    assert!(
        !noop.hook_applied,
        "hook presence is not application evidence"
    );
    let delta = 0.5;
    let calls = std::sync::atomic::AtomicUsize::new(0);
    let unreachable = BorrowedHook {
        delta: &delta,
        calls: &calls,
        target: "unreached_projection",
    };
    let result = reranker
        .rerank_with_hook("q", candidates(&[1]), 1, Some(&unreachable))
        .await
        .unwrap();
    assert_eq!(result.results, none.results);
    assert!(!result.hook_applied);
    assert_eq!(calls.load(Ordering::Relaxed), 0);
}

#[tokio::test]
async fn unsupported_hook_is_not_silently_ignored() {
    let scorer = Arc::new(RecordingScorer::default());
    let reranker = NativeCrossEncoderReranker::new(
        scorer.clone(),
        Arc::new(FakeResolver {
            documents: vec![Some("low".into())],
        }),
    );
    let delta = 0.5;
    let calls = std::sync::atomic::AtomicUsize::new(0);
    let active = BorrowedHook {
        delta: &delta,
        calls: &calls,
        target: "ffn_output",
    };
    for hook in [&NoopLoraHook as &dyn LoraHook, &active] {
        let result = reranker
            .rerank_with_hook("q", candidates(&[1]), 1, Some(hook))
            .await;
        assert!(
            matches!(result, Err(RetrievalError::Rerank(ref message))
                if message == "cross-encoder scorer does not support per-call hooks"),
            "opaque scorer must refuse every supplied hook, including Noop"
        );
    }
    assert!(scorer.calls.lock().unwrap().is_empty());
    assert_eq!(calls.load(Ordering::Relaxed), 0);
    let none = reranker
        .rerank_with_hook("q", candidates(&[1]), 1, None)
        .await
        .unwrap();
    assert_eq!(
        none.results,
        vec![(1, DeterministicScore::from_f64(0.1_f32 as f64))]
    );
    assert!(!none.hook_applied);
    assert_eq!(scorer.calls.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn skipped_batches_do_not_score_or_claim_hook_application() {
    let scorer = Arc::new(RecordingScorer::default());
    let reranker = NativeCrossEncoderReranker::new(
        scorer.clone(),
        Arc::new(FakeResolver {
            documents: vec![None],
        }),
    );
    for (input, limit) in [(candidates(&[1]), 0), (vec![], 1), (candidates(&[1]), 1)] {
        let result = reranker
            .rerank_with_hook("q", input, limit, Some(&NoopLoraHook))
            .await
            .unwrap();
        assert!(result.results.is_empty());
        assert!(!result.hook_applied);
    }
    assert!(scorer.calls.lock().unwrap().is_empty());
}

#[test]
fn observer_preserves_validation_activity_and_actual_apply() {
    struct RefusingHook {
        calls: std::sync::Mutex<Vec<&'static str>>,
    }
    impl LoraHook for RefusingHook {
        fn apply(&self, _layer: usize, _module: &str, _x: &[f32], output: &mut [f32]) {
            self.calls.lock().unwrap().push("apply");
            output[0] += 0.5;
        }
        fn validate_geometry(
            &self,
            _geometry: &dyn LoraTargetGeometry,
        ) -> std::result::Result<(), String> {
            self.calls.lock().unwrap().push("geometry");
            Err("geometry refusal".into())
        }
        fn validate_against_bert(
            &self,
            layers: usize,
            hidden: usize,
            intermediate: usize,
        ) -> std::result::Result<(), String> {
            assert_eq!((layers, hidden, intermediate), (1, 2, 4));
            self.calls.lock().unwrap().push("legacy");
            Err("legacy refusal".into())
        }
        fn is_active(&self, layer: usize, module: &str) -> bool {
            assert_eq!((layer, module), (0, "ffn_output"));
            self.calls.lock().unwrap().push("active");
            true
        }
    }
    let hook = RefusingHook {
        calls: std::sync::Mutex::new(Vec::new()),
    };
    let observer = ObservedHook::new(&hook);
    assert_eq!(
        observer.validate_geometry(&BertLoraGeometry::new(1, 2, 4)),
        Err("geometry refusal".into())
    );
    #[allow(deprecated)]
    let legacy = observer.validate_against_bert(1, 2, 4);
    assert_eq!(legacy, Err("legacy refusal".into()));
    assert!(observer.is_active(0, "ffn_output"));
    assert!(
        !observer.applied.load(Ordering::Relaxed),
        "validation and activity must not count as apply"
    );
    let mut output = [0.25];
    observer.apply(0, "ffn_output", &[1.0], &mut output);
    assert_eq!(output, [0.75]);
    assert!(observer.applied.load(Ordering::Relaxed));
    assert_eq!(
        *hook.calls.lock().unwrap(),
        vec!["geometry", "legacy", "active", "apply"]
    );
}