From 18cc536f6574eddf909cdf8c4a88fd3dc76b8a8f Mon Sep 17 00:00:00 2001 From: catlog22 Date: Sun, 28 Dec 2025 21:40:46 +0800 Subject: [PATCH] refactor(vector-store): use consistent EPSILON constant Define module-level EPSILON constant and use it in both _cosine_similarity and _refresh_cache for consistent floating point precision handling. Solution-ID: SOL-20251228113619 Issue-ID: ISS-1766921318981-11 Task-ID: T3 --- codex-lens/src/codexlens/semantic/vector_store.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/codex-lens/src/codexlens/semantic/vector_store.py b/codex-lens/src/codexlens/semantic/vector_store.py index 4e6b001b..11bb9a3b 100644 --- a/codex-lens/src/codexlens/semantic/vector_store.py +++ b/codex-lens/src/codexlens/semantic/vector_store.py @@ -36,6 +36,9 @@ except ImportError: logger = logging.getLogger(__name__) +# Epsilon used to guard against floating point precision edge cases (e.g., near-zero norms). +EPSILON = 1e-10 + def _cosine_similarity(a: List[float], b: List[float]) -> float: """Compute cosine similarity between two vectors.""" @@ -48,7 +51,6 @@ def _cosine_similarity(a: List[float], b: List[float]) -> float: norm_a = np.linalg.norm(a_arr) norm_b = np.linalg.norm(b_arr) - 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 @@ -313,7 +315,7 @@ class VectorStore: ) # Avoid division by zero self._embedding_norms = np.where( - self._embedding_norms == 0, 1e-10, self._embedding_norms + self._embedding_norms == 0, EPSILON, self._embedding_norms ) return True