fix: handle non‑finite magnitude in L2 normalization and remove stale test reset

This commit is contained in:
SK Akram
2026-01-31 10:52:23 +00:00
committed by Gustavo Madeira Santana
parent 61c82998cc
commit 8160d80f35
2 changed files with 1 additions and 2 deletions

View File

@@ -330,7 +330,6 @@ describe("embedding provider local fallback", () => {
describe("local embedding L2 normalization", () => {
afterEach(() => {
vi.resetAllMocks();
vi.resetModules();
vi.unstubAllGlobals();
vi.doUnmock("./node-llama.js");
});

View File

@@ -8,7 +8,7 @@ import { importNodeLlamaCpp } from "./node-llama.js";
function l2Normalize(vec: number[]): number[] {
const magnitude = Math.sqrt(vec.reduce((sum, x) => sum + x * x, 0));
if (magnitude < 1e-10) return vec;
if (!Number.isFinite(magnitude) || magnitude < 1e-10) return vec;
return vec.map((x) => x / magnitude);
}