Skip to content

fix(core): port fuzzy edit matching to the V2 edit tool - #51707

Closed
mohitjoer wants to merge 2 commits into
anomalyco:devfrom
mohitjoer:v2-edit-fuzzy-match
Closed

mohitjoer wants to merge 2 commits into
anomalyco:devfrom
mohitjoer:v2-edit-fuzzy-match

Conversation

@mohitjoer

Copy link
Copy Markdown

Issue for this PR

Closes #51706

Fixes #41872
Fixes #45199

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The V2 edit tool matched oldString with one literal indexOf scan, so a model that reproduced it with trailing whitespace, an indentation slip, or an extra blank line got a hard failure instead of an edit. This ports V1's lenient strategies into packages/core/src/tool/edit-match.ts as pure synchronous helpers, consulted only after exact matching returns zero.

It also fixes two open bugs in that logic rather than importing them:

Unchanged: the tool schema, permission ordering, and writeIfUnchanged, so stale-read protection is untouched. Exact matching is still the fast path and still wins.

I filed this as a bug fix rather than a feature because most of it is parity with shipped V1 behavior plus two verified bug fixes. If you'd rather treat the port itself as a design-review item, happy to split or hold it.

How did you verify your code works?

  • bun test in packages/core: 1109 pass, 0 fail.
  • The two bug-fix tests were each run with the fix reverted, and both fail without it — so they pin the fixes, not the surrounding logic. Worth noting my first attempt at the edit: blank lines inside a block make BlockAnchorReplacer reject near-identical matches #45199 test passed with the fix reverted, because contextAware was matching the input instead of block-anchor; the test now uses an input where only block-anchor can match.
  • The 9 pre-existing edit tests passed unchanged before I added anything, which is the evidence that the exact path did not regress.
  • bunx oxlint on the four touched files: 0 warnings, 0 errors. Prettier clean.

Not done: no runtime smoke test against a live model, and isDisproportionateMatch is carried over as-is. I could not construct an input that trips it, since every strategy is structurally bounded below both its thresholds — but #31785 reported it as a real single-line bypass, so I did not want to assert it is dead without someone who knows the history confirming it.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The V2 edit leaf matched oldString with a single literal indexOf scan and
hard-failed on anything inexact. V1's replace() already handled the common
near-miss cases with nine ordered strategies; this ports the lenient ones
as pure synchronous helpers and consults them only after exact matching
returns zero.

Adds line-trimmed, block-anchor, whitespace-normalized, indentation-flexible,
escape-normalized, trimmed-boundary and context-aware matching, each bounded
to the region oldString describes. An ambiguous candidate is skipped rather
than guessed, so it still surfaces a multiple-matches error. The tool schema
is unchanged, permission ordering is unchanged, and writes still go through
writeIfUnchanged so stale-read protection is preserved.

Clears the deferred fuzzy-parity TODOs in tool/edit.ts and tool/builtins.ts.
Fixes anomalyco#41872: replace() committed the first byte-unique candidate from a
strategy, so two candidates that normalize to the same oldString but differ
in bytes bypassed the multiple-match safeguard. Resolve a whole strategy
before committing and dedupe by span rather than matched bytes.

Fixes anomalyco#45199: block-anchor scored two blank lines as 0 instead of 1, so any
block with enough blank lines could never clear the similarity threshold no
matter how similar the rest was. Blank lines are an exact match.

Both tests assert the old behavior fails, so they pin the fixes rather than
the surrounding logic.
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant