Skip to content

fix: sort diff matches by similarity before picking overall suggestion#26

Merged
Grivn merged 1 commit into
mnemon-dev:masterfrom
chancsc:fix/diff-match-ordering
May 17, 2026
Merged

fix: sort diff matches by similarity before picking overall suggestion#26
Grivn merged 1 commit into
mnemon-dev:masterfrom
chancsc:fix/diff-match-ordering

Conversation

@chancsc
Copy link
Copy Markdown
Contributor

@chancsc chancsc commented May 17, 2026

Follow-up to #25 based on author review feedback.

Diff picks its overall suggestion from matches[0], but matches is ordered by KeywordSearch token overlap score — not by the final Jaccard Similarity. A candidate with high keyword score (all of new's tokens present) but low Jaccard (many additional tokens, classified as ADD) would rank first, silently masking a lower-keyword-score candidate with higher Jaccard that should have been UPDATE or DUPLICATE.

Fix: sort matches by Similarity descending after all candidates are scored, so matches[0] is always the most similar candidate and drives the overall suggestion correctly.

Adds regression test TestDiff_LowerKeywordScoreUpdateNotMasked that fails on the pre-fix code and passes after.

Changes
internal/search/diff.go — sort.Slice by Similarity descending before the overall-suggestion block

internal/search/diff_test.go — TestDiff_LowerKeywordScoreUpdateNotMasked regression test (fails before fix, passes after)
CHANGELOG.md — [Unreleased] entry

KeywordSearch orders candidates by token overlap score, which can diverge
from the Jaccard-based Similarity computed in Diff. A candidate that
contains all of new's tokens (keyword score = 1.0) but has many additional
tokens (Jaccard ≈ 0.36 → ADD) would rank first, masking a later candidate
with lower keyword score but higher Jaccard (→ UPDATE or DUPLICATE).

Fix: sort matches by Similarity descending after all candidates are scored,
so matches[0] is always the most similar candidate and drives the overall
suggestion correctly.

Adds regression test TestDiff_LowerKeywordScoreUpdateNotMasked that fails
on the pre-fix code and passes after.
@Grivn
Copy link
Copy Markdown
Member

Grivn commented May 17, 2026

LGTM. This addresses the Jaccard follow-up cleanly, and the regression test covers the masking case we discussed.

Copy link
Copy Markdown
Member

@Grivn Grivn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@Grivn Grivn merged commit 439ca22 into mnemon-dev:master May 17, 2026
1 check passed
@chancsc chancsc deleted the fix/diff-match-ordering branch May 18, 2026 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants