From 195285b00f95f56b996f48305cba7abb15ffa521 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 21 Jan 2026 13:50:20 +0400 Subject: [PATCH] fix: handle NaN scores in sort_by comparison This fixes a non-deterministic sorting behavior where NaN scores from the embedding engine (or database queries) would cause unstable result ordering. NaNs are now explicitly handled and pushed to the end of the results. --- src/core/db.rs | 16 ++++++++++++---- src/core/search.rs | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/core/db.rs b/src/core/db.rs index 080e586..0873004 100644 --- a/src/core/db.rs +++ b/src/core/db.rs @@ -187,11 +187,19 @@ impl Database { .filter_map(Result::ok) .collect(); - // Sort by similarity (highest first) + // Sort by similarity (highest first), handling NaNs results.sort_by(|a, b| { - b.similarity - .partial_cmp(&a.similarity) - .unwrap_or(std::cmp::Ordering::Equal) + if b.similarity.is_nan() && a.similarity.is_nan() { + std::cmp::Ordering::Equal + } else if b.similarity.is_nan() { + std::cmp::Ordering::Less + } else if a.similarity.is_nan() { + std::cmp::Ordering::Greater + } else { + b.similarity + .partial_cmp(&a.similarity) + .unwrap_or(std::cmp::Ordering::Equal) + } }); results.truncate(limit * 3); // Get more for reranking diff --git a/src/core/search.rs b/src/core/search.rs index 1690ba4..7e1f24e 100644 --- a/src/core/search.rs +++ b/src/core/search.rs @@ -78,11 +78,19 @@ impl SearchEngine { }) .collect(); - // Sort by score descending + // Sort by score descending, handling NaNs by pushing them to the end results.sort_by(|a, b| { - b.score - .partial_cmp(&a.score) - .unwrap_or(std::cmp::Ordering::Equal) + if b.score.is_nan() && a.score.is_nan() { + std::cmp::Ordering::Equal + } else if b.score.is_nan() { + std::cmp::Ordering::Less + } else if a.score.is_nan() { + std::cmp::Ordering::Greater + } else { + b.score + .partial_cmp(&a.score) + .unwrap_or(std::cmp::Ordering::Equal) + } }); results.truncate(max_results);