Repository navigation
fix(filesystem): refuse an edit_file oldText that matches more than once - #5078
vishalhabib99 wants to merge 3 commits into
Conversation
edit_file only checked that oldText occurred at least once, then replaced the first occurrence. An oldText that occurred more than once silently rewrote the first match and still reported success, so the model could not tell whether it edited the site it meant. Count matches instead. Zero matches still fail with "old text not found"; more than one now fails with the count and a hint to add context, and the file is left untouched. Matches are counted against the content as it stands after earlier edits in the same call, and the CRLF retry counts the converted text. The ACP edit_file handler gets the same check. Fixes docker#3929 Signed-off-by: Vishal Habib <vishalhabib99@gmail.com>
|
👋 Thanks for the update. This PR has merge conflicts with the base branch. Please rebase or merge the latest base branch and resolve them. It has been moved to draft and marked |
aheritier
left a comment
There was a problem hiding this comment.
One blocking issue: overlapping matches can still silently edit the wrong occurrence.
[should-fix] Update the validation section in docs/tools/filesystem/index.md:51 to say that oldText must match exactly once in the content produced by any earlier edits in the same call, and that an ambiguous match leaves the file unchanged.
| } | ||
| oldText, newText := edit.OldText, edit.NewText | ||
| if !strings.Contains(content, oldText) { | ||
| n := strings.Count(content, oldText) |
There was a problem hiding this comment.
[blocking] strings.Count skips overlapping matches. In header\nsame\nsame\nfooter\n, oldText: "\nsame\n" can match either same line because the two matches share the middle newline. The count is 1, so the edit rewrites the first line and reports success—the wrong-site edit #3929 is meant to prevent. Reject matches at distinct start offsets even when they overlap, including in the CRLF retry at line 37 and in the ACP handler at pkg/acp/filesystem.go:457. Add regression cases that assert an error and no write.
There was a problem hiding this comment.
Thanks, good catch. Fixed in b010bd4:
- Added
types.CountMatches, which counts every start offset (advancing by one byte, so overlapping matches count), and used it at all three sites: the direct match and the CRLF retry inApplyEdits, and the ACPedit_filehandler. - Regression cases for
header\nsame\nsame\nfooter\nwitholdText: "\nsame\n"(LF, CRLF retry, and ACP), each asserting the error and that nothing is written. They fail on the previous commit and pass now. - Docs: the validation section now says
oldTextmust match exactly once in the content produced by earlier edits in the same call, and that an ambiguous match leaves the file unchanged.
|
Please move this PR back to ready for review once the requested changes are addressed. |
strings.Count skips overlapping matches, so an oldText such as "\nsame\n" in "header\nsame\nsame\nfooter\n" counted once although it matches at two start offsets, and edit_file rewrote the first one and reported success. Add types.CountMatches, which counts every start offset including overlaps, and use it for the direct match, the CRLF retry, and the ACP edit_file handler. Add regression cases that assert an error and no write, and note in the docs that oldText must match exactly once in the content produced by earlier edits in the same call, and that an ambiguous match leaves the file unchanged. Signed-off-by: Vishal Habib <vishalhabib99@gmail.com>
aheritier
left a comment
There was a problem hiding this comment.
One blocking performance regression: rejecting an ambiguous edit can require millions of redundant comparisons. The overlapping-match and documentation feedback is addressed.
| func CountMatches(s, substr string) int { | ||
| n := 0 | ||
| for { | ||
| i := strings.Index(s, substr) |
There was a problem hiding this comment.
[blocking] Both the built-in and ACP edit_file paths keep counting after the second match has already made the edit invalid. Advancing by one byte makes strings.Index recompare the entire oldText at each overlapping match. For a 16 MiB file of repeated abc\n lines and a 32 KiB oldText from that block, this means about 4.2 million full comparisons—roughly 128 GiB of cumulative comparison work before returning an error. That's a source-derived estimate, not a wall-clock measurement. Stop at the second match, starting the second search one byte after the first match's start so overlaps are still rejected, and report that the text matches more than once. Keep the no-write regression tests and add a benchmark for densely overlapping matches.
There was a problem hiding this comment.
Fixed in 70eb3c6. Both edit_file paths now call CountMatchesUpTo(content, oldText, 2), which stops at the second match. It still starts the next search one byte after the previous match start, so overlapping matches are still rejected. The error now says the old text "matches more than once" instead of giving a count.
I added a benchmark (BenchmarkApplyEditsDenselyOverlapping) and a regression test that use your case: a 32 KiB oldText in a 16 MiB file of repeated abc\n lines. On my machine, rejecting it took ~1.52 s before this change and ~18 µs after. The no-write regression tests are unchanged apart from the new message, and go test ./pkg/tools/builtin/filesystem/... ./pkg/acp/... passes.
Both edit_file paths only need to know whether oldText matches zero, one, or more than one time. Counting every overlapping match made a rejected edit scan the whole file, recomparing oldText at each offset: a 32 KiB oldText in a 16 MiB file of repeated lines took ~1.5 s to reject. Stop at the second match (still advancing one byte, so overlapping matches are rejected) and report that the text matches more than once. Adds a regression test and a benchmark for densely overlapping matches. Signed-off-by: Vishal Habib <vishalhabib99@gmail.com>
Fixes #3929
This picks up #3929 after #3930 closed as stale on merge conflicts. #3930's approach was reviewed and approved there. The edit logic has since moved into
types.ApplyEditsand gained CRLF retry, so this is a new change against currentmain. Credit to @dwin-gharibi for the report and the original design.Problem
edit_filechecks thatoldTextoccurs at least once, then callsstrings.Replace(content, oldText, newText, 1). WhenoldTextoccurs more than once, the first occurrence is rewritten and the tool still reportsFile edited successfully. The model can't tell whether it edited the place it meant. If it meant a later occurrence, the wrong code changes and nothing prompts the model to read the file again.On
main(51f04e7):The ACP transport (
pkg/acp/filesystem.go) serves the sameedit_filetool and has the same check.Fix
types.ApplyEditscounts matches withstrings.Countand doesn't usestrings.Contains. Zero matches still returnold text not found. More than one match now returnsEdit N failed: old text appears K times; include more surrounding context so it matches exactly once, and nothing is written.oldTextunique.oldTextin a CRLF file is refused too.edit_filehandler gets the same check, with the same message.Tool descriptions are unchanged, so the e2e cassettes don't change.
Three existing tests used a repeated
oldTextonly as fixture data. I gave them unique matches and kept what they check:TestApplyEditsandTestEditFileRecordsAggregateDiffkeep the same before/after content (same\nsame\ntosame\n), andTestEditDiffCapturesWholeFilestill chains a second edit onto the first one's output.Tests
New:
TestFilesystemTool_EditFileRejectsAmbiguousOldText(built-in toolset, 5 cases) andTestEditRefusesAmbiguousOldText(ACP).TestApplyEditsalso asserts the new error.Before the fix, the three refusal cases fail:
After the fix, all of them pass.
Commands run:
go test ./...with.env.test(same astask test): passesgolangci-lint run(v2.14.0) onpkg/acp/...andpkg/tools/builtin/filesystem/...: 0 issuesgolangci-lint fmt --diff: cleango run ./lint .: no offensesgo mod tidy --diff: cleanAI disclosure
I used Claude Code to find the issue, write the regression tests and draft the change and this description. I reproduced the bug on
main, reviewed every line and ran the checks above myself.