fix: bm25RankToScore returns 1.0 for all negative BM25 values#5214
Closed
papago2355 wants to merge 11 commits intoopenclaw:mainfrom
Closed
fix: bm25RankToScore returns 1.0 for all negative BM25 values#5214papago2355 wants to merge 11 commits intoopenclaw:mainfrom
papago2355 wants to merge 11 commits intoopenclaw:mainfrom
Conversation
…lawdbot into fix/bm25RankToScore
This was referenced Jan 31, 2026
2 tasks
Member
|
Closing as duplicate of #14005. If this is incorrect, comment and we can reopen. |
Contributor
Author
|
This is a same issue. Since I worked on the same fix earlier , would you mind adding an attribution (e.g., referencing my PR in the merged PR/commit or release notes)? @sebslight |
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.
What was the issue?
The existing
bm25RankToScoreimplementation incorrectly normalized SQLite FTS5 BM25 scores.SQLite FTS5’s
bm25()function returns negative values, where more negative values indicate better matches. However, the previous implementation clamped all negative ranks to0usingMath.max(0, rank). As a result:1.0)This caused the text component of hybrid search to ignore actual BM25 ranking differences.
Fix
The incorrect normalization logic was replaced with a proper BM25 normalization function that:
[0, 1)New implementation:
This function converts BM25 scores into a monotonic, saturating relevance score suitable for weighted hybrid fusion.
Further recommendations
hybrid-test.tsfileGreptile Overview
Greptile Summary
Fixed critical bug in BM25 scoring normalization that was causing hybrid search to lose all keyword ranking information.
The previous implementation incorrectly clamped SQLite FTS5's negative BM25 scores to zero using
Math.max(0, rank), which caused all valid results to normalize to the same score (1.0). This completely eliminated the ranking signal from keyword search in hybrid results.The new implementation properly handles FTS5's "more negative = better match" scoring by:
-bm25Score)1 - 1/(1 + k * positive)[0, 1)that preserve relative rankingThe fix is mathematically sound and properly tested. The sigmoid function with
k=0.5provides monotonic transformation where better matches (more negative) produce higher normalized scores suitable for weighted fusion with cosine similarity scores.Confidence Score: 5/5