Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Update edit_replace to reject non-unique search strings when replace_all is false

carlosejimenezcf5209a3f110
diff --git a/tools/defaults/lib/default_utils.py b/tools/defaults/lib/default_utils.py
index 08cb5b48..4caef645 100644
--- a/tools/defaults/lib/default_utils.py
+++ b/tools/defaults/lib/default_utils.py
@@ -120,7 +120,7 @@ class WindowedFile:
exit(1)
raise IsADirectoryError(msg)
if not self.path.exists():
- msg = f"File {self.path} not found"
+ msg = f"Error: File {self.path} not found"
if self._exit_on_exception:
print(msg)
exit(1)
@@ -237,12 +237,12 @@ class WindowedFile:
index = window_text.find(search)
if index == -1:
if self._exit_on_exception:
- print(f"Text not found: {search}")
+ print(f"Error: Text not found: {search}")
exit(1)
raise TextNotFound
window_start_line, _ = self.line_range
replace_start_line = window_start_line + len(window_text[:index].splitlines())
- new_window_text = window_text.replace(search, replace, 1)
+ new_window_text = window_text.replace(search, replace)
self.set_window_text(new_window_text)
if reset_first_line == "keep":
pass
@@ -259,7 +259,7 @@ class WindowedFile:
indices = list(_find_all(self.text, search))
if not indices:
if self._exit_on_exception:
- print(f"Text not found: {search}")
+ print(f"Error: Text not found: {search}")
exit(1)
raise TextNotFound
replace_start_line = len(self.text[: indices[0]].splitlines())
diff --git a/tools/edit_replace/bin/edit b/tools/edit_replace/bin/edit
index 6a4920f2..294d4ab7 100644
--- a/tools/edit_replace/bin/edit
+++ b/tools/edit_replace/bin/edit
@@ -76,6 +76,13 @@ def main(search: str, replace: str, replace_all: bool):
try:
if not replace_all:
+ window_text = wf.get_window_text()
+ if window_text.count(search) > 1:
+ print(
+ f"Error: Found more than one occurrence of {search} in the window while replace_all=False. "
+ "Aborting edit."
+ )
+ exit(4)
replacement_info = wf.replace_in_window(search, replace)
# todo: Should warn if more than one occurrence was found?
else: