-
Notifications
You must be signed in to change notification settings - Fork 14
fix(search): make equal-score ranking deterministic #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -268,6 +268,7 @@ pub fn search_with_intelligence( | |
| b.score | ||
| .partial_cmp(&a.score) | ||
| .unwrap_or(std::cmp::Ordering::Equal) | ||
| .then_with(|| a.file_path.cmp(&b.file_path)) | ||
| }); | ||
| true | ||
| } else { | ||
|
|
@@ -311,6 +312,7 @@ pub fn search_with_intelligence( | |
| b.score | ||
| .partial_cmp(&a.score) | ||
| .unwrap_or(std::cmp::Ordering::Equal) | ||
| .then_with(|| a.file_path.cmp(&b.file_path)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For temporal queries where several candidates fall inside the requested date range, Useful? React with 👍 / 👎. |
||
| }); | ||
|
|
||
| // 5-lane RRF (rerank_results is empty when reranker absent, weight 0) | ||
|
|
@@ -374,10 +376,14 @@ fn dedup_by_file(results: Vec<RankedResult>) -> Vec<RankedResult> { | |
| } | ||
| } | ||
| let mut deduped: Vec<RankedResult> = by_file.into_values().collect(); | ||
| // Tiebreak on file_path: the map's iteration order is random per process, | ||
| // and equal scores otherwise land in random rank order (nondeterministic | ||
| // RRF ranks downstream). | ||
| deduped.sort_by(|a, b| { | ||
| b.score | ||
| .partial_cmp(&a.score) | ||
| .unwrap_or(std::cmp::Ordering::Equal) | ||
| .then_with(|| a.file_path.cmp(&b.file_path)) | ||
| }); | ||
| deduped | ||
| } | ||
|
|
@@ -393,7 +399,15 @@ fn merge_seeds(semantic: &[RankedResult], fts: &[RankedResult]) -> Vec<RankedRes | |
| by_file.insert(r.file_path.clone(), r.clone()); | ||
| } | ||
| } | ||
| by_file.into_values().collect() | ||
| let mut seeds: Vec<RankedResult> = by_file.into_values().collect(); | ||
| // Deterministic seed order (map iteration order is random per process). | ||
| seeds.sort_by(|a, b| { | ||
| b.score | ||
| .partial_cmp(&a.score) | ||
| .unwrap_or(std::cmp::Ordering::Equal) | ||
| .then_with(|| a.file_path.cmp(&b.file_path)) | ||
| }); | ||
| seeds | ||
| } | ||
|
|
||
| /// Run a search query and print results. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //! Deterministic, model-free search regression battery. | ||
| //! | ||
| //! The old integration suites exercised deleted HNSW/Embedder modules. This | ||
| //! fixture uses the current Store + MockLlm path and repeats the same queries | ||
| //! to catch ranking nondeterminism without downloading a GGUF model. | ||
|
|
||
| use engraph::docid::generate_docid; | ||
| use engraph::llm::MockLlm; | ||
| use engraph::search::search_internal; | ||
| use engraph::store::Store; | ||
|
|
||
| fn fixture_store() -> Store { | ||
| let store = Store::open_memory().unwrap(); | ||
| let embedder = MockLlm::new(256); | ||
| let documents = [ | ||
| ( | ||
| "notes/alpha.md", | ||
| "retrieval ranking architecture", | ||
| "retrieval ranking", | ||
| ), | ||
| ( | ||
| "notes/beta.md", | ||
| "retrieval ranking operations", | ||
| "retrieval ranking", | ||
| ), | ||
| ("notes/gamma.md", "unrelated gardening notes", "unrelated"), | ||
| ]; | ||
|
|
||
| for (path, snippet, vector_seed) in documents { | ||
| let file_id = store | ||
| .insert_file(path, path, 0, &[], &generate_docid(path), None, None) | ||
| .unwrap(); | ||
| let vector_id = store.next_vector_id().unwrap(); | ||
| let vector = embedder.hash_to_vector(vector_seed); | ||
| store | ||
| .insert_chunk_with_vector(file_id, "# Fixture", snippet, vector_id, 3, &vector) | ||
| .unwrap(); | ||
| store.insert_vec(vector_id, &vector).unwrap(); | ||
| store.insert_fts_chunk(file_id, 0, snippet).unwrap(); | ||
| } | ||
| store | ||
| } | ||
|
|
||
| #[test] | ||
| fn golden_search_battery_is_stable() { | ||
| let store = fixture_store(); | ||
| let mut embedder = MockLlm::new(256); | ||
|
|
||
| for _ in 0..10 { | ||
| let retrieval = search_internal("retrieval ranking", 2, &store, &mut embedder).unwrap(); | ||
| let retrieval_paths: Vec<&str> = retrieval | ||
| .results | ||
| .iter() | ||
| .map(|result| result.file_path.as_str()) | ||
| .collect(); | ||
| assert_eq!( | ||
| retrieval_paths, | ||
| vec!["notes/alpha.md", "notes/beta.md"], | ||
| "retrieval golden result changed" | ||
| ); | ||
|
|
||
| let unrelated = search_internal("unrelated", 1, &store, &mut embedder).unwrap(); | ||
| assert_eq!( | ||
| unrelated.results[0].file_path, "notes/gamma.md", | ||
| "unrelated golden result changed" | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the reranker assigns the same score to multiple candidates, including the existing
unwrap_or(0.0)path when scoring fails, this tie-break turns the rerank lane into an alphabetical ranking. Because the rerank lane is fused with a nonzero weight, equal reranker scores can now promote alphabetically earlier files over candidates that were ranked higher by pass 1; the deterministic tie-break here should preserve the existingfused_pass1candidate order instead of usingfile_path.Useful? React with 👍 / 👎.