Repository navigation
Conversation
|
Tested this against the 1,000-line fixture from #1869 — the LCS output is a clean single-line diff. Nice work. One edge case I noticed while testing: the I don't think it triggers with the default filters on a simple insertion, but if any filter expands the diff output beyond the concatenated file size, the guard silently swaps it for both full files — no indication that the diff was discarded. Probably not in scope for this PR, just flagging it in case it's worth a follow-up. |
compute_diff() compared lines by index position, causing cascading false positives when lines were inserted or deleted. A single insertion in a 20-line file would report 16 removed + 17 added lines instead of 1 added. Replace with standard LCS (Longest Common Subsequence) dynamic programming. Adjacent remove+add pairs are still merged into "modified" when lines have >50% similarity (Jaccard). No new dependencies — the DP table is computed inline. Fixes rtk-ai#1869
c95964c to
b75875a
Compare
|
Closing as a duplicate of #3268, which replaces the same naive positional comparison in The bug you're fixing is real and worse in production than the issue describes: on a 40-line file with one line inserted, the positional compare marks everything after the insertion as modified, the rendered output grows past the raw baseline, and #3268 gets the nod because it already carries a cell cap on the DP table. This PR allocates One thing worth flagging, because it applies to #3268 too and it's the reason neither is merged yet: matched lines emit no op, so a deletion and an insertion that are far apart in the file end up adjacent in the ops vector and get paired by the similarity check. Sorting a three-line import block is enough to trigger it. The fix is an explicit "keep" op (or tracking the last matched index) so runs can't merge across matched regions. Your #2652 is unaffected by any of this and is still on the list. |
Summary
rtk diff file1 file2used a naive index-based line comparison that caused cascading false positives on any insertion/deletionRoot Cause
compute_diff()comparedlines1[i]withlines2[i]by index position. Any insertion shifted all subsequent indices, causing every line after the insertion point to be reported as a remove+add pair. The LCS approach finds the longest set of common lines between the two files, then correctly identifies only the actual differences.Reproduction (from #1869)
Test plan
test_compute_diff_insertion_no_cascade— verifies single insertion produces exactly 1 added changetest_compute_diff_deletion_no_cascade— verifies single deletion produces exactly 1 removed changecargo fmt && cargo clippy --all-targets && cargo test --allpassesFixes #1869