Skip to content

fix(tools): refuse str_replace when old_str matches at overlapping offsets - #6429

Merged
VascoSch92 merged 2 commits into
OpenHands:mainfrom
yftx293:fix/file-editor-overlapping-occurrences
Nov 7, 2026
Merged

VascoSch92 merged 2 commits into
OpenHands:mainfrom
yftx293:fix/file-editor-overlapping-occurrences

Conversation

@yftx293

@yftx293 yftx293 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

Fixed the overlapping-match check from #6348. I reviewed the diff and the test; an ambiguous old_str must be refused, not silently applied to the first match.


AGENT:

Why

str_replace counted occurrences with re.finditer, which only reports non-overlapping matches. In aaa, old_str="aa" matches at offsets 0 and 1, but the scan saw one occurrence and replaced the first one — the uniqueness check passed on an ambiguous string, contradicting its own contract.

Summary

Issue Number

Part of #6348 (menu sub-item: "str_replace uniqueness check misses overlapping occurrences")

How to Test

uv run pytest tests/tools/file_editor/test_basic_operations.py -k overlapping

Before this change the replacement goes through; after, it raises ToolError mentioning multiple occurrences and aaa\n is untouched. The full tests/tools/file_editor/ suite is 170 passed, including the empty-old_str and multi-occurrence cases, whose messages are unchanged. ruff check/format on the touched files are clean.

Type

  • Bug fix

Notes

The line numbers in the error come from the same lookahead matches, so they stay accurate for ordinary (non-overlapping) inputs.

@VascoSch92 VascoSch92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the overlap check itself is right and the new test pins it down nicely.

One thing I ran into while testing: wrapping the pattern in a lookahead (here and here) stops Python's regex engine from using its fast literal search, so it tries a full match at every offset. On a 9 MB file that is mostly one repeated character, with an old_str of about 1000 characters, str_replace goes from 0.013 s on main to roughly 14 s on this branch. Ordinary code files are less extreme but still slower; an old_str that isn't in a 9 MB file went from about 0.05 s to 0.2 s.

A plain str.find loop gives exactly the same overlapping positions and is actually faster than main (0.007 s on the same input):

starts = []
i = file_content.find(old_str)
while i != -1:
    starts.append(i)
    i = file_content.find(old_str, i + 1)

Could you switch both scans to that? Everything else in the PR looks good to me.

@yftx293

yftx293 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on the lookahead — switched both scans to the str.find loop you suggested (in a small _occurrence_starts helper), and dropped the now-unused re import.

One data point from re-running your pathological input (9 MB of one repeated character, old_str of 1000 chars): the find loop and the lookahead produce byte-identical result lists, and the find loop is somewhat faster (≈25 s vs ≈30 s here), but both are dominated by collecting ~9M start offsets before the "multiple occurrences" error is raised. Sparse inputs, which is what real files look like, benefit from removing the regex entirely.

tests/tools/file_editor/ is 170 passed / 1 skipped. If you'd rather the scan stop as soon as a second occurrence is found — the only thing the uniqueness check needs — I can add that as a separate change; I kept this one to the literal swap you asked for.

@VascoSch92
VascoSch92 enabled auto-merge (squash) November 7, 2026 22:04
@VascoSch92
VascoSch92 self-requested a review November 7, 2026 22:04

@VascoSch92 VascoSch92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@VascoSch92
VascoSch92 merged commit 9f9bd44 into OpenHands:main Nov 7, 2026
37 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.

2 participants