From 6486c5685035264b388e4c8b38091e8062f3cc0f Mon Sep 17 00:00:00 2001 From: catlog22 Date: Sun, 28 Dec 2025 21:11:30 +0800 Subject: [PATCH] fix(vector-store): add epsilon tolerance for norm checks Replace exact zero comparison with epsilon-based check (< 1e-10) in _cosine_similarity to handle floating point precision issues. Also check for product underflow to prevent inf/nan from division by very small numbers. Solution-ID: SOL-20251228113619 Issue-ID: ISS-1766921318981-11 Task-ID: T1 --- codex-lens/src/codexlens/semantic/vector_store.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/codex-lens/src/codexlens/semantic/vector_store.py b/codex-lens/src/codexlens/semantic/vector_store.py index c7133e42..0e7b6925 100644 --- a/codex-lens/src/codexlens/semantic/vector_store.py +++ b/codex-lens/src/codexlens/semantic/vector_store.py @@ -46,10 +46,16 @@ def _cosine_similarity(a: List[float], b: List[float]) -> float: norm_a = np.linalg.norm(a_arr) norm_b = np.linalg.norm(b_arr) - if norm_a == 0 or norm_b == 0: + EPSILON = 1e-10 + # Use epsilon tolerance to avoid division by (near-)zero due to floating point precision. + if norm_a < EPSILON or norm_b < EPSILON: return 0.0 - return float(np.dot(a_arr, b_arr) / (norm_a * norm_b)) + denom = norm_a * norm_b + if denom < EPSILON: + return 0.0 + + return float(np.dot(a_arr, b_arr) / denom) class VectorStore: