Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Validate to_replace in edit_file_by_replace AgentSkill (#3073)

Boxuan Li445f290beb39
diff --git a/opendevin/runtime/plugins/agent_skills/agentskills.py b/opendevin/runtime/plugins/agent_skills/agentskills.py
index 1d8f74f9ce..d6fdea9084 100644
--- a/opendevin/runtime/plugins/agent_skills/agentskills.py
+++ b/opendevin/runtime/plugins/agent_skills/agentskills.py
@@ -605,7 +605,6 @@ def edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> N
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
@@ -650,12 +649,20 @@ def edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> N
if to_replace.strip() == '':
raise ValueError('`to_replace` must not be empty.')
+ if to_replace == new_content:
+ raise ValueError('`to_replace` and `new_content` must be different.')
+
# search for `to_replace` in the file
# if found, replace it with `new_content`
# if not found, perform a fuzzy search to find the closest match and replace it with `new_content`
with open(file_name, 'r') as file:
file_content = file.read()
+ if file_content.count(to_replace) > 1:
+ raise ValueError(
+ '`to_replace` appears more than once, please include enough lines to make code in `to_replace` unique.'
+ )
+
start = file_content.find(to_replace)
if start != -1:
# Convert start from index to line number
diff --git a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log
index cfc52c8988..0fdee5cca1 100644
--- a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log
+++ b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_001.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log
index e1bebb5b97..645ac5d1ef 100644
--- a/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log
+++ b/tests/integration/mock/CodeActAgent/test_browse_internet/prompt_005.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log
index e0ca75ebb8..83437bb6c1 100644
--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log
+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_001.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log
index f5702f695d..14ca3b1ee8 100644
--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log
+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_002.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log
index aec5f2bd51..c154368638 100644
--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log
+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_003.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log
index 2d75772fc0..5234b65b2a 100644
--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log
+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_004.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log b/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log
index 5f56b53ad2..9d3c125ec7 100644
--- a/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log
+++ b/tests/integration/mock/CodeActAgent/test_edits/prompt_005.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log
index 627445039b..c0b99ce0e6 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_001.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log
index cc47904d48..544403208a 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_002.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log b/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log
index f54ff09d66..1eb6cc6274 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython/prompt_003.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log
index 358eba2130..ed28f3b2ad 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_001.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log
index a4d046d0d0..5b67d25233 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_002.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log
index e1660e5949..a5e6549bf3 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_003.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log
index 5318198df2..c4c3175080 100644
--- a/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log
+++ b/tests/integration/mock/CodeActAgent/test_ipython_module/prompt_004.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log
index f5d5ceef99..312e366130 100644
--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log
+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_001.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log
index a52d2ff80f..ca4f9fe02e 100644
--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log
+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_002.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log
index 5c24215971..08061310e5 100644
--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log
+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_003.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log
index 06d9d6568f..af83dd9d13 100644
--- a/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log
+++ b/tests/integration/mock/CodeActAgent/test_write_simple_script/prompt_004.log
@@ -58,7 +58,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log
index 33fdddb6c9..0d198ce1f0 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_001.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log
index e224832d31..4f046938ce 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_002.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log
index fd22253ba1..0ddb465d3a 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_003.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log
index 760989f05b..831b0fe98d 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_004.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log
index c4102e3882..ec72733678 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_edits/prompt_005.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
@@ -602,13 +601,13 @@ open_file("bad.txt")
----------
OBSERVATION:
-[File: /workspace/bad.txt (4 lines total)]
-(this is the beginning of the file)
-1|This is a stupid typoo.
-2|Really?
-3|No mor typos!
-4|Enjoy!
-(this is the end of the file)
+[File: /workspace/bad.txt (4 lines total)]
+(this is the beginning of the file)
+1|This is a stupid typoo.
+2|Really?
+3|No mor typos!
+4|Enjoy!
+(this is the end of the file)
----------
@@ -630,12 +629,12 @@ edit_file_by_replace(
----------
OBSERVATION:
-[No exact match found in bad.txt for
-```
-This is a stupid typoo.
-No mor typos!
-```
-]
+[No exact match found in bad.txt for
+```
+This is a stupid typoo.
+No mor typos!
+```
+]
----------
@@ -657,14 +656,14 @@ edit_file_by_replace(
----------
OBSERVATION:
-[File: /workspace/bad.txt (4 lines total after edit)]
-(this is the beginning of the file)
-1|This is a stupid typo.
-2|Really?
-3|No mor typos!
-4|Enjoy!
-(this is the end of the file)
-[File updated (edited at line 1). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
+[File: /workspace/bad.txt (4 lines total after edit)]
+(this is the beginning of the file)
+1|This is a stupid typo.
+2|Really?
+3|No mor typos!
+4|Enjoy!
+(this is the end of the file)
+[File updated (edited at line 1). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
----------
@@ -681,14 +680,14 @@ edit_file_by_replace(
----------
OBSERVATION:
-[File: /workspace/bad.txt (4 lines total after edit)]
-(this is the beginning of the file)
-1|This is a stupid typo.
-2|Really?
-3|No more typos!
-4|Enjoy!
-(this is the end of the file)
-[File updated (edited at line 3). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
+[File: /workspace/bad.txt (4 lines total after edit)]
+(this is the beginning of the file)
+1|This is a stupid typo.
+2|Really?
+3|No more typos!
+4|Enjoy!
+(this is the end of the file)
+[File updated (edited at line 3). Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary.]
ENVIRONMENT REMINDER: You have 10 turns left to complete the task.
diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log
index b63f68e535..69a60d8dbb 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_001.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log
index d757bc821d..c64727d5e1 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython/prompt_002.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log
index 90e2e17752..65bbb95fc3 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_001.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log
index 000fe927c5..22fe91d275 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_002.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log
index eec21d8302..1542f5f3e8 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_ipython_module/prompt_003.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log
index 0185d48559..1fcc8aae38 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_001.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log
index a85a965aa4..e06f9bd513 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_002.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log
index 1dbf786a6e..0c54161524 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_003.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log
index e4e4a1a988..d8a0cfb83a 100644
--- a/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log
+++ b/tests/integration/mock/CodeActSWEAgent/test_write_simple_script/prompt_004.log
@@ -46,7 +46,6 @@ edit_file_by_replace(file_name: str, to_replace: str, new_content: str) -> None:
Edit a file. This will search for `to_replace` in the given file and replace it with `new_content`.
Every *to_replace* must *EXACTLY MATCH* the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in `to_replace` unique. `to_replace` should NOT be empty.
- `edit_file_by_replace` will only replace the *first* matching occurrences.
For example, given a file "/workspace/example.txt" with the following content:
```
line 1
diff --git a/tests/unit/test_agent_skill.py b/tests/unit/test_agent_skill.py
index ea6a4bd30a..46d205e6f4 100644
--- a/tests/unit/test_agent_skill.py
+++ b/tests/unit/test_agent_skill.py
@@ -693,41 +693,58 @@ def test_edit_file_by_replace_multiline(tmp_path):
with io.StringIO() as buf:
with contextlib.redirect_stdout(buf):
- edit_file_by_replace(
- file_name=str(temp_file_path),
- to_replace='Line 2',
- new_content='REPLACE TEXT',
- )
- result = buf.getvalue()
- expected = (
- f'[File: {temp_file_path} (5 lines total after edit)]\n'
- '(this is the beginning of the file)\n'
- '1|Line 1\n'
- '2|REPLACE TEXT\n'
- '3|Line 2\n'
- '4|Line 4\n'
- '5|Line 5\n'
- '(this is the end of the file)\n'
- + MSG_FILE_UPDATED.format(line_number=2)
- + '\n'
- )
- assert result.split('\n') == expected.split('\n')
+ with pytest.raises(
+ ValueError,
+ match='`to_replace` appears more than once, please include enough lines to make code in `to_replace` unique',
+ ):
+ edit_file_by_replace(
+ file_name=str(temp_file_path),
+ to_replace='Line 2',
+ new_content='REPLACE TEXT',
+ )
- with open(temp_file_path, 'r') as file:
- lines = file.readlines()
- assert len(lines) == 5
- assert lines[0].rstrip() == 'Line 1'
- assert lines[1].rstrip() == 'REPLACE TEXT'
- assert lines[2].rstrip() == 'Line 2'
- assert lines[3].rstrip() == 'Line 4'
- assert lines[4].rstrip() == 'Line 5'
+def test_edit_file_by_replace_no_diff(tmp_path):
+ temp_file_path = tmp_path / 'a.txt'
+ content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
+ temp_file_path.write_text(content)
-def test_edit_file_by_replace_toreplace_empty():
- with pytest.raises(ValueError):
+ open_file(str(temp_file_path))
+
+ with io.StringIO() as buf:
+ with contextlib.redirect_stdout(buf):
+ with pytest.raises(
+ ValueError, match='`to_replace` and `new_content` must be different'
+ ):
+ edit_file_by_replace(
+ file_name=str(temp_file_path),
+ to_replace='Line 1',
+ new_content='Line 1',
+ )
+
+
+def test_edit_file_by_replace_toreplace_empty(tmp_path):
+ temp_file_path = tmp_path / 'a.txt'
+ content = 'Line 1\nLine 2\nLine 2\nLine 4\nLine 5'
+ temp_file_path.write_text(content)
+
+ open_file(str(temp_file_path))
+
+ with io.StringIO() as buf:
+ with contextlib.redirect_stdout(buf):
+ with pytest.raises(ValueError, match='`to_replace` must not be empty.'):
+ edit_file_by_replace(
+ file_name=str(temp_file_path),
+ to_replace=' ',
+ new_content='Line 1',
+ )
+
+
+def test_edit_file_by_replace_unknown_file():
+ with pytest.raises(FileNotFoundError):
edit_file_by_replace(
str('unknown file'),
- '',
+ 'ORIGINAL TEXT',
'REPLACE TEXT',
)