Skip to content

fuzzy_match: stop silently overwriting stale or blanked interior lines - #292

Merged
RecursiveIntell merged 1 commit into
mainfrom
rsi/patch-block-anchor-floor-20261130
Dec 1, 2026
Merged

RecursiveIntell merged 1 commit into
mainfrom
rsi/patch-block-anchor-floor-20261130

Conversation

@RecursiveIntell

Copy link
Copy Markdown
Owner

What this does

Stops the reproduced silent-overwrite cases in the patch tool'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 on main): 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's old_string had the right line count but one interior line it remembered wrongly, and it kept that line unchanged in new_string, the edit applied with no error and wrote the stale line over the real one.
  • context_aware (related gap, first seen only after the block_anchor floor was in place): a blank pattern line matched any file line, so a blank could be written over a real line. On main the blanked-line case was reached through block_anchor; the context_aware route 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 bar context_aware already 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_string did not correctly describe.

Tests

  • New TestNoSilentOverwriteOfStaleInteriorLine:
    • refused, file unchanged: a stale kept line with the same line count, an absent block whose first and last lines match, one blanked line, two blanked lines;
    • still applied once: a one-character typo, and an interior line 0.889 similar to the file's (this one is also checked directly against block_anchor);
    • context_aware's blank-line rule checked directly.
  • Three existing tests changed after reading their intent. Each passed only because the pre-fix block_anchor accepted 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. Their old_string interior 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 2 vs beta 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 of main at 3d3fa9d):

  • Patch and file tests (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).
  • Synthetic replace-mode probe, 17 generated families × 300 cases, compared with main:
    • unsafe applies 1,198 → 0. The four families that changed hold 1,200 cases (300 each): one_stale_interior_line, absent_block_with_decoy, near_dup_blocks_same_anchors, and two_interior_lines_stale, of which 298 applied unsafely on main and 2 were already refused. All 1,200 are refused with the fix.
    • cases in recoverable families applied exactly: 2,400 → 2,400; the remaining cases were refused on both.
  • Synthetic v4a probe, 16 families × 300 cases, compared with main: stale context line and stale removed line in a hunk 600 unsafe applies → 600 refusals. No other family changed.
  • Eight targeted mutants of the new checks (floor not applied, floor 0.5, floor 0.95, helper always true, only the first interior line checked, blank lines exempted, helper comparing the pattern to itself, context_aware blank-line rule removed): 8 of 8 caught, each by a named test. For example, floor 0.95 is caught by test_floor_constant_matches_context_aware_bar and test_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) on tools/fuzzy_match.py and tests/tools/test_fuzzy_match.py: All checks passed.

Scope and limits

  • The floor bounds the damage; it does not detect staleness. A stale interior line that is 0.80 or more similar to the file's line (for example 0.889) is still accepted.
  • The synthetic probes cover generated cases only. How often the overwrite happened in real use is unknown: as implemented today, the tool does not record file contents or which strategy matched.
  • An edited line whose old text the model got less than 0.80 similar is no longer recovered by block_anchor. On the synthetic probe that loses no case in any recoverable family. It is a deliberate safety trade.
  • Single-host local runs. This repository runs hosted CI on pull requests; the PR's checks page is where hosted results are confirmed.

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.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-12-01T02:26:21.099680Z
We couldn’t complete this review. Comment @codex review to try again.
e965ae6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

૮ >ﻌ< ა ci review

ran on e965ae6 — fuzzy_match: stop silently overwriting stale or blanked inte

⚠️ Warnings

OSV vulnerability scan · View job

320 known vulnerabilities found in pinned dependencies.

How to fix:

Review the findings in the Security tab. Update the affected dependencies if a patched version is available.

@RecursiveIntell
RecursiveIntell merged commit 97847f0 into main Dec 1, 2026
50 checks passed
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.

1 participant