Repository navigation
Description
What's wrong
Fuzzy.Contains(subject, pattern, out int score) at 862888d:
| pattern | subject | score | subject with one extra letter | score | |
|---|---|---|---|---|---|
sc |
sc_config |
8 | src_config |
12 | |
abc |
ab_bc |
13 | aXb_bc |
22 | |
db |
db_backup |
8 | dumb_backup |
11 | |
ui |
ui_input |
9 | uxi_input |
13 | |
ab |
abaB |
13 | axbaB |
17 |
Under the README's scoring table an unmatched letter costs at least 1, so the left column should never score below the right one. The test suite states the same rule: Score_InsertingJunkBeforeABoundaryCopy_DoesNotRaiseTheScore asserts "Inserting a junk character should not raise the score", but checks only one doubled-letter case (baaA).
Cause
This is the #91/#109 guard at FuzzySearch/Fuzzy.cs:282-284:
bool displacesRun = rematch && !nextMatch && bestLetterContinuesRun;
...
if (newScore >= bestLetterScore && !displacesRun)When the current best letter continues a run with the previous match, a later word-start copy of it (after a separator, or at a camelCase capital) can never replace it. Put one unmatched letter between the two, and the run is broken. The guard no longer applies, so the word-start copy takes the match with its full separator/camelCase bonus. The extra letter ends up worth +4 to +9 rather than costing 1.
Evidence the guard is the cause, from an exhaustive check over alphabet abcABC_x1, subjects of length 1-6 and 11 patterns (no adjacent duplicate letters, so #101 is not involved):
- Inserting an uppercase
Xthat is not in the pattern raised the score in 22,638 cases. - With only
displacesRunforced tofalse, that drops to 0, and the pairs above become 13/12, 23/22, 13/11 and 14/13. - The same change brings Exact match scores lower than the same text with a suffix (Score("itemMap","item") = 32 > Score("item","item") = 25) #91 back (
itemMap32 >item25), which is why the guard can't just be deleted.
Why it matters
The subject that holds the pattern letters contiguously, typically the one the user meant, ranks below one that is strictly worse. For example, typing sc lists src_config above sc_config, and typing db lists dumb_backup above db_backup.
This is not #122: its proposed fix, capping the bonus when the gap is only separators, leaves this case alone, because a gap such as Xb_ is not only separators. It is not #101 either, since no letter is doubled. #117, #131 and #142 were the guard misfiring. Here it fires exactly as designed, but the design isn't monotone.
Suggested fix / acceptance criteria
- Make the rematch decision the same whether or not the best letter continues a run. For example, allow the rematch while the pattern is still incomplete (so
ab_bcgets its better alignment), and block every rematch once the pattern is complete. Another route is to resolve the bonus weighting behind Exact match ranks below the same letters split by a separator: Score("filename","filename") = 45 < Score("file_name","filename") = 49, and "test" (25) < "t_e_s_t" (37) #122/An exact match ranks below a camelCase split with letters in between: "ui" scores UserId 16 but UI and ui only 15; "id" scores IsDb 18 vs ID 15 #126, so the guard is no longer needed. Score("itemMap","item") <= Score("item","item")(Exact match scores lower than the same text with a suffix (Score("itemMap","item") = 32 > Score("item","item") = 25) #91) must still hold.- Add a property test: inserting a letter that is not in the pattern and is not a separator never raises the score. The table above should be among its cases.
Activity
matt-edmondson commentedon Nov 12, 2026
Triage
sc→src_configoversc_config,db→dumb_backupoverdb_backup). It is a ranking-quality problem, not a crash or data loss.displacesRun) inFuzzySearch/Fuzzy.cs:282-284, which is the scoring core.Suggested next step: land the monotonicity property test first, seeded with this table, so that whichever of the two fix routes is chosen is checked against #91 and the sibling issues at once.
Generated by Claude Code