Skip to content

Commit 4a8de68

Browse files
amihosclaude
andcommitted
fix: Clamp waypoint weights to [0, 1] range to prevent score corruption
Database may contain waypoint records with corrupted weights (e.g., 1.4 trillion instead of expected 0-1 range). These corrupted weights cause irrelevant memories to dominate search results due to massive score inflation in the hybrid scoring formula. This fix adds defensive clamping in two locations: 1. expand_via_waypoints() - when calculating expansion weights from neighbors 2. hsg_query() scoring loop - when using waypoint weights in final score calculation The clamping ensures that even if database contains invalid values, they won't corrupt query results. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8862e3c commit 4a8de68

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

backend/src/memory/hsg.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ export async function expand_via_waypoints(
520520
const neighs = await q.get_neighbors.all(cur.id);
521521
for (const neigh of neighs) {
522522
if (vis.has(neigh.dst_id)) continue;
523-
const exp_wt = cur.weight * neigh.weight * 0.8;
523+
// Clamp neighbor weight to valid range - protect against corrupted data
524+
const neigh_wt = Math.min(1.0, Math.max(0, neigh.weight || 0));
525+
const exp_wt = cur.weight * neigh_wt * 0.8;
524526
if (exp_wt < 0.1) continue;
525527
const exp_item = {
526528
id: neigh.dst_id,
@@ -772,7 +774,8 @@ export async function hsg_query(
772774
}
773775
}
774776
const em = exp.find((e: { id: string }) => e.id === mid);
775-
const ww = em?.weight || 0;
777+
// Clamp waypoint weight to valid range [0, 1] - protect against corrupted data
778+
const ww = Math.min(1.0, Math.max(0, em?.weight || 0));
776779
const ds = (Date.now() - m.last_seen_at) / 86400000;
777780
const sal = calc_decay(m.primary_sector, m.salience, ds);
778781
const mtk = canonical_token_set(m.content);

0 commit comments

Comments
 (0)