Repository navigation
Description
What's wrong
CalculateScoreCore (FuzzySearch/Fuzzy.cs, around L198–290) walks the subject left to right and takes the first occurrence of each pattern letter. A letter's position is final as soon as the next pattern letter matches (around L213–221: advanced → score += bestLetterScore; bestLetterIdx = null). The only later correction is rematch, which can move the most recent best letter but cannot undo letters already committed.
So when the first two or more pattern letters appear early in the subject, the matcher commits to that fragment. It then charges every character between the fragment and the real match as an unmatched letter: −1 each, with no cap. It also loses the separator, camelCase and adjacency bonuses the real match would have earned. A subject that contains the whole pattern as a word therefore scores far below an otherwise identical subject without the early fragment.
Repro
Compiled from main at 77fe8c4 on net10.0. "optimal" is a brute-force best alignment scored with the library's own scoring rules:
| pattern | subject | Score |
optimal |
|---|---|---|---|
parse |
src/parser/test_parse |
4 | 25 |
parse |
src/lexer/test_parse |
25 | 25 |
push |
Git: Pull, then Push |
9 | 20 |
push |
Git: Fetch, then Push |
20 | 20 |
ab |
axb ab |
6 | 11 |
ab |
xxx ab |
11 | 11 |
Trace for axb ab:
a@0 is the best letter and earns the start bonus: +10.b@2 matches the next pattern letter, which commitsa@0.- The real word
ab@4–5 can only rematchb, which gets no adjacency bonus becausea@4 was never matched. - Total: 10 − 1 − 1 − 1 − 1 + 0 = 6.
Why it matters
Ranking inverts in the cases fuzzy finders exist for: file pickers and command palettes. Searching parse ranks src/lexer/test_parse far above src/parser/test_parse, which contains the word twice. Searching push ranks "Fetch, then Push" above "Pull, then Push", because Pu of Pull was taken first. Any path whose directory names share a prefix with the file being searched for is pushed down the list.
This is not a duplicate of the existing scoring issues:
- maxPrefixPenalty cap is bypassed when the first pattern letter also appears earlier: "a"+N×"x"+"_ab" scores −87 at N=100 vs 10 without the leading "a" #94 covers a rematch of the first letter only. Its fix re-runs the prefix penalty and doesn't apply once a second letter has committed the first.
- Exact match scores lower than the same text with a suffix (Score("itemMap","item") = 32 > Score("item","item") = 25) #91 and Repeating a pattern letter raises the score: Score("xaab","ab") = 8 > Score("xab","ab") = 4, because a rematch keeps +5 adjacency for the copy it replaced #101 are about scores above the optimum. This issue is about scores below it.
- README's "Ranking a Collection" example ranks the prefix match "Storage Provider" last, not first: actual scores are Data Store 19, AppDataStorage 17, Storage Provider 15 #99 is about trailing text.
Suggested fix / acceptance criteria
- Score the best alignment rather than the leftmost one. Either option works:
- an O(n·m) dynamic programme over (pattern index, subject index) that tracks the previous matched index for adjacency, as fzf v2 does;
- the recursive best-match search that fts_fuzzy_match v0.2 adopted when it replaced the greedy v0.1 algorithm this code is adapted from (see the attribution at
Fuzzy.cs:10).
- Add these tests:
Score("src/parser/test_parse","parse") >= Score("src/lexer/test_parse","parse")Score("Git: Pull, then Push","push") >= Score("Git: Fetch, then Push","push")Score("axb ab","ab") == Score("xxx ab","ab")- an exhaustive small-alphabet test asserting
Scoreequals a brute-force optimal alignment under the documented scoring table. This pairs with Repeating a pattern letter raises the score: Score("xaab","ab") = 8 > Score("xab","ab") = 4, because a rematch keeps +5 adjacency for the copy it replaced #101's "never above the optimum".
- The same change would likely also resolve maxPrefixPenalty cap is bypassed when the first pattern letter also appears earlier: "a"+N×"x"+"_ab" scores −87 at N=100 vs 10 without the leading "a" #94.
Activity
matt-edmondson commentedon Oct 6, 2026
Triage
CalculateScoreCoreinFuzzy.cs).Generated by Claude Code