fix: replenish() hardcodes CosineDistance instead of graph distance#21
Merged
Conversation
added 2 commits
April 11, 2026 12:26
In a binary min-heap, the last array element is not necessarily the maximum. Max() must scan the leaf nodes (indices n/2..n-1) to find the true maximum. PopLast() used Remove(Len()-1) which removed an arbitrary element instead of the worst. This caused incorrect evictions during neighbor selection and search result trimming, degrading graph quality.
…ance replenish() is called after neighbor eviction and node deletion to restore connectivity. It unconditionally used CosineDistance, ignoring the graph's configured distance function. This corrupted the graph topology for any non-Cosine metric. Thread DistanceFunc through replenish() and isolate() so the correct distance function is always used.
2 tasks
3 tasks
Contributor
Author
ammario
approved these changes
Jun 22, 2026
ammario
left a comment
Member
There was a problem hiding this comment.
Correct fix: threads the graph's DistanceFunc through replenish()/isolate() instead of hardcoding CosineDistance. Tests pass.
ammario
pushed a commit
that referenced
this pull request
Jun 22, 2026
Rewrites layerNode.search() to follow HNSW Algorithm 5 (SEARCH-LAYER): the result set is bounded by efSearch during exploration, termination uses the worst-of-results condition, and only the k closest are returned (with deterministic key tiebreaking). This fixes severely degraded recall: on a 2000-point, 16-dim Euclidean benchmark, recall@1 improves from 0.20 to 1.00 and recall@10 from 0.175 to 0.998. Also clamps efSearch to max(k, efSearch) so Search returns up to k results even when k exceeds the configured EfSearch. Builds on #20 (heap Max/PopLast fix) and #21 (replenish distance fix).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
replenish()unconditionally calledaddNeighbor(..., CosineDistance)regardless of the graph's configured distance functionDistanceFuncthroughreplenish()andisolate()so the correct function is always usedDepends on #20.
Test plan
TestGraph_DeleteReplenishUsesGraphDistance— builds a Euclidean graph, deletes a node to trigger replenish, verifies search still returns the correct nearest neighborgo test ./...andgo vet ./...pass