fix: stop nameMatchBonus awarding a spurious +10 on a whitespace-only query#984
Open
JOhnsonKC201 wants to merge 1 commit into
Open
fix: stop nameMatchBonus awarding a spurious +10 on a whitespace-only query#984JOhnsonKC201 wants to merge 1 commit into
JOhnsonKC201 wants to merge 1 commit into
Conversation
… query
nameMatchBonus collapsed the query to a single token (queryLower) and then
hit `nameLower.startsWith(queryLower)`. For an empty/whitespace-only query,
queryLower is '' and `String.startsWith('')` is always true, so every node
got a flat +10 'name match' bonus despite nothing matching its name.
This is reachable end-to-end: searchNodes(' ') yields text='' and a
truthy raw query, so the rescoring guard `text || query` lets the
whitespace-only query through to nameMatchBonus.
Guard with an early `if (!queryLower) return 0;`. Adds regression tests
covering empty and whitespace-only queries (fail before, pass after) plus an
exact-match case to confirm real queries are unaffected.
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
nameMatchBonus(search ranking,src/search/query-utils.ts) awards a spurious flat +10 "name match" bonus to every node when the query is empty or whitespace-only.The bug
The function collapses the query into a single comparable token and then checks
startsWith:For an empty / whitespace-only query,
queryLoweris'', and in JSanyString.startsWith('')is always true. So the branch fires for every node, withratio = 0, returningMath.round(10 + 0) = 10— a uniform, undeserved name-match bonus for a query that matches no name at all.Reachability
This is reachable end-to-end through the public
searchNodesAPI:searchNodes(' ')→parseQuery(' ')yieldstext = '', while the rawqueryis' '(truthy).src/db/queries.tsisif (results.length > 0 && (text || query)), so a whitespace-only query passes the guard ('' || ' '→ truthy).scoringQuery = text || query = ' 'is then handed tonameMatchBonus(name, ' ')for every candidate.scorePathRelevancealready handles this case correctly (it returns 0 for a whitespace-only query);nameMatchBonusdid not.The fix
A one-line early guard, before the always-true
startsWith('')check:Repro / tests
Added a
nameMatchBonus empty/whitespace queryblock to__tests__/context-ranking.test.ts:10(test fails).0, and a real exact-match query is unaffected.Verification
npx vitest run __tests__/context-ranking.test.ts __tests__/search-query-parser.test.ts→ 39 passed.main(stash the source change →expected 10 to be 0) and pass with it.tsc --noEmitclean.Diff is two files, +31/-4. No behavior change for any non-empty query.