From 8ce2b26f5f407e077422a06dfdffe77f18f5dfd7 Mon Sep 17 00:00:00 2001 From: Garm Date: Tue, 27 Oct 2026 16:38:07 +0100 Subject: [PATCH] test(relevance): score past the embed batch cap against the real model Forty texts of mixed lengths span three batches of 16 with the one related item in the last, in both the Rust and the Python scorer: it must still rank first, and every batched score must match the item scored on its own. Run with RUN_FASTEMBED_TESTS=1 (Rust) or fastembed installed (Python). Co-Authored-By: Claude Opus 5.5 --- .../headroom-core/src/relevance/embedding.rs | 35 +++++++++++++++++++ tests/test_relevance.py | 14 ++++++++ 2 files changed, 49 insertions(+) diff --git a/crates/headroom-core/src/relevance/embedding.rs b/crates/headroom-core/src/relevance/embedding.rs index a0c7c42d2..c64fc21f4 100644 --- a/crates/headroom-core/src/relevance/embedding.rs +++ b/crates/headroom-core/src/relevance/embedding.rs @@ -511,4 +511,39 @@ mod tests { assert!((0.0..=1.0).contains(&sc.score)); } } + + #[cfg(feature = "ml")] + #[test] + fn fastembed_batches_past_the_cap_keep_each_score_with_its_item() { + if !fastembed_enabled() { + return; + } + // Three EMBED_BATCH_SIZE batches of mixed lengths, the one related item + // in the last: a misaligned batch boundary would hand its score away. + let s = EmbeddingScorer::try_new().expect("model loads"); + let mut items: Vec = (0..40) + .map(|i| { + format!( + "inventory row {i}: {}", + "pallets of garden furniture ".repeat(i % 5 + 1) + ) + }) + .collect(); + items[37] = "authentication failed for user admin: invalid password".to_string(); + let refs: Vec<&str> = items.iter().map(String::as_str).collect(); + let batch = s.score_batch(&refs, "login error"); + assert_eq!(batch.len(), 40); + let top = (0..40) + .max_by(|&a, &b| batch[a].score.total_cmp(&batch[b].score)) + .unwrap(); + assert_eq!(top, 37); + for (item, sc) in refs.iter().zip(&batch) { + let single = s.score(item, "login error").score; + assert!( + (sc.score - single).abs() < 1e-3, + "{item}: batched {} vs single {single}", + sc.score + ); + } + } } diff --git a/tests/test_relevance.py b/tests/test_relevance.py index 162b5c39d..0563eda25 100644 --- a/tests/test_relevance.py +++ b/tests/test_relevance.py @@ -186,6 +186,20 @@ class TestEmbeddingScorer: # "failed"/"error" semantically relates to "errors" assert scores[0].score > scores[1].score + def test_batches_past_the_cap_keep_each_score_with_its_item(self, scorer): + """Three EMBED_BATCH_SIZE batches of mixed lengths, the related item in + the last: a misaligned batch boundary would hand its score away.""" + items = [ + f"inventory row {i}: " + "pallets of garden furniture " * (i % 5 + 1) for i in range(40) + ] + items[37] = "authentication failed for user admin: invalid password" + + scores = [s.score for s in scorer.score_batch(items, "login error")] + assert len(scores) == 40 + assert max(range(40), key=scores.__getitem__) == 37 + for item, batched in zip(items, scores): + assert batched == pytest.approx(scorer.score(item, "login error").score, abs=1e-3) + def test_paraphrase_match(self, scorer): """Embeddings match paraphrases.""" items = [