Repository navigation
fuzzy_match: stop silently overwriting stale or blanked interior lines - #292
Conversation
block_anchor matched a block when its first and last lines equalled the file's and the whole middle was at least 0.50 similar. When the model's old_string had the right line count but a stale interior line, and kept that line unchanged in new_string, the edit applied without an error and the real line was overwritten. context_aware had a related gap: a blank pattern line matched any file line, so a blank sent in place of a real line was written over it. - block_anchor: every interior line must also be at least 0.80 similar to the aligned file line (BLOCK_ANCHOR_LINE_FLOOR, the bar context_aware already uses per line). A blank pattern line counts as an interior line. - context_aware: a blank pattern line matches only a blank or whitespace-only file line. Tests: - New TestNoSilentOverwriteOfStaleInteriorLine: stale kept line, absent block with matching anchors, one and two blanked lines are refused and leave the file unchanged; a one-character typo and an 0.889-similar interior line still apply (the latter pinned at block_anchor itself); blank-line behaviour of context_aware pinned directly. - Three existing tests depended on block_anchor's 0.50 middle ratio and were rewritten after reading their intent, not to make them pass: the two escape-drift tests now use the realistic drift shape (the file has real quotes, the model sent backslash-escaped ones), and a new test pins that the old contrived shape (0.57-similar interior line) is now refused as a no-match; test_all_lines_matching_still_replaces now uses a line that is actually close (0.875), and a new test pins that a 0.71-similar line is refused. Synthetic probes (17 replace-mode families x 300, seed 7): unsafe applies 1198 -> 0; recoverable families applied exactly 2400 -> 2400. v4a families (16 x 300, seed 11): stale-context and stale-removed-line hunks 600 unsafe -> 600 refused; no other family changed.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
૮ >ﻌ< ა ci reviewran on e965ae6 — fuzzy_match: stop silently overwriting stale or blanked inte
|
What this does
Stops the reproduced silent-overwrite cases in the
patchtool's fuzzy matcher: an interior line below a 0.80 similarity floor, or a blank sent in place of a real line, can no longer be written over the file's content.The defect.
block_anchor(reproduced onmain): it matched a block when its first and last lines equal the file's after whitespace stripping, and the middle section was at least 0.50 similar (0.70 when several blocks matched). When the model'sold_stringhad the right line count but one interior line it remembered wrongly, and it kept that line unchanged innew_string, the edit applied with no error and wrote the stale line over the real one.context_aware(related gap, first seen only after theblock_anchorfloor was in place): a blank pattern line matched any file line, so a blank could be written over a real line. Onmainthe blanked-line case was reached throughblock_anchor; thecontext_awareroute appeared on an intermediate build with only the floor, which is why this rule changed too.The fix.
block_anchor: every interior line must also be at least 0.80 similar to the aligned file line (BLOCK_ANCHOR_LINE_FLOOR = 0.80, the same per-line barcontext_awarealready uses). A blank pattern line counts as an interior line.context_aware: a blank pattern line matches only a blank or whitespace-only file line.In this text, "refused" means no strategy applied and the file is unchanged; an "unsafe apply" writes a region that the model's
old_stringdid not correctly describe.Tests
TestNoSilentOverwriteOfStaleInteriorLine:block_anchor);context_aware's blank-line rule checked directly.block_anchoraccepted the middle at its 0.50 bar (0.70 when several blocks matched):test_drift_blocked_apostrophe/test_drift_blocked_double_quote: they test the escape-drift guard, which runs only after a non-exact strategy has matched. Theirold_stringinterior line (x = \'a\') is 0.57 similar to the file line (x = 1), so it is now refused before the guard runs. They now use the realistic drift shape (the file has real quotes, the model's strings have backslash-escaped ones), which still reaches the guard. A new test checks that the old shape ends as a plain no-match with the file untouched.test_all_lines_matching_still_replaces: its "close" line (beta 2vsbeta two) is 0.71 similar. It now uses a line that is actually close (0.875), and a new test checks that the 0.71 line is refused.Evidence
Run locally on head
e965ae6(one commit on top ofmainat3d3fa9d):test_fuzzy_match,test_patch_parser,test_patch_multimatch_locations,test_patch_already_applied,test_patch_ws_diagnosis,test_file_operations,acp/test_edit_approval): 197 passed, 2 skipped (the two skips are Windows-only tests; the host is Linux).main:one_stale_interior_line,absent_block_with_decoy,near_dup_blocks_same_anchors, andtwo_interior_lines_stale, of which 298 applied unsafely onmainand 2 were already refused. All 1,200 are refused with the fix.main: stale context line and stale removed line in a hunk 600 unsafe applies → 600 refusals. No other family changed.context_awareblank-line rule removed): 8 of 8 caught, each by a named test. For example, floor 0.95 is caught bytest_floor_constant_matches_context_aware_barandtest_interior_line_between_floor_and_0_95_still_matches. After each run the source was restored, and its sha256 matched the original. This is a targeted check of the new guards, not a coverage measure.ruff check(0.15.10) ontools/fuzzy_match.pyandtests/tools/test_fuzzy_match.py: All checks passed.Scope and limits
block_anchor. On the synthetic probe that loses no case in any recoverable family. It is a deliberate safety trade.