From 598eed92cb6eab1a338c10e42b0460241f3869bd Mon Sep 17 00:00:00 2001 From: catlog22 Date: Sun, 28 Dec 2025 20:55:03 +0800 Subject: [PATCH] fix(ranking): add explicit NaN check in normalize_weights Add math.isnan() check before math.isfinite() to properly catch NaN values in weight totals. Prevents division by NaN which could produce unexpected results in RRF fusion calculations. Solution-ID: SOL-20251228113631 Issue-ID: ISS-1766921318981-0 Task-ID: T1 --- codex-lens/src/codexlens/search/ranking.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/codex-lens/src/codexlens/search/ranking.py b/codex-lens/src/codexlens/search/ranking.py index 3e0bfd18..6f59eaf2 100644 --- a/codex-lens/src/codexlens/search/ranking.py +++ b/codex-lens/src/codexlens/search/ranking.py @@ -22,12 +22,23 @@ class QueryIntent(str, Enum): MIXED = "mixed" -def normalize_weights(weights: Dict[str, float]) -> Dict[str, float]: +def normalize_weights(weights: Dict[str, float | None]) -> Dict[str, float | None]: """Normalize weights to sum to 1.0 (best-effort).""" total = sum(float(v) for v in weights.values() if v is not None) - if not math.isfinite(total) or total <= 0: - return {k: float(v) for k, v in weights.items()} - return {k: float(v) / total for k, v in weights.items()} + + # NaN total: do not attempt to normalize (division would propagate NaNs). + if math.isnan(total): + return dict(weights) + + # Infinite total: do not attempt to normalize (division yields 0 or NaN). + if not math.isfinite(total): + return dict(weights) + + # Zero/negative total: do not attempt to normalize (invalid denominator). + if total <= 0: + return dict(weights) + + return {k: (float(v) / total if v is not None else None) for k, v in weights.items()} def detect_query_intent(query: str) -> QueryIntent: