diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 04e08c03f..1da8f23a2 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -651,8 +651,9 @@ def _leading_ws(line: str) -> str: def _reindent_like_match(old_text: str, actual_text: str, new_text: str) -> str: """Preserve the outer indentation from the actual matched block.""" - old_lines = old_text.split("\n") - actual_lines = actual_text.split("\n") + # A terminal newline does not add a logical line, even at an unterminated EOF. + old_lines = old_text.removesuffix("\n").split("\n") + actual_lines = actual_text.removesuffix("\n").split("\n") if len(old_lines) != len(actual_lines): return new_text @@ -756,7 +757,11 @@ def _find_trim_matches(content: str, old_text: str, *, normalize_quotes: bool = start = offsets[i] end = offsets[i + window_size] - if content_lines_keepends[i + window_size - 1].endswith("\n"): + # Include the line terminator only when the requested match includes it. + if ( + not old_text.endswith("\n") + and content_lines_keepends[i + window_size - 1].endswith("\n") + ): end -= 1 matches.append( _MatchSpan( diff --git a/tests/tools/test_edit_advanced.py b/tests/tools/test_edit_advanced.py index 51b98723c..8759998de 100644 --- a/tests/tools/test_edit_advanced.py +++ b/tests/tools/test_edit_advanced.py @@ -159,6 +159,29 @@ class TestIndentationPreservation: " return 1\n" ) + @pytest.mark.asyncio + @pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["lf", "crlf"]) + @pytest.mark.parametrize("ending", ["", "\n"], ids=["no-request-newline", "request-newline"]) + @pytest.mark.parametrize( + "tail", ["\nnext_step()\n", "\n", ""], + ids=["followed-by-code", "terminated-eof", "unterminated-eof"], + ) + async def test_trim_fallback_preserves_indentation_and_line_endings( + self, tool, tmp_path, newline, ending, tail, + ): + f = tmp_path / "indent.py" + content = "if ready:\n x = 1\n y = 2" + tail + f.write_bytes(content.replace("\n", newline).encode("utf-8")) + result = await tool.execute( + path=str(f), + old_text=("x = 1\ny = 2" + ending).replace("\n", newline), + new_text=("x = 3\ny = 4" + ending).replace("\n", newline), + ) + assert "Patch applied:" in result + expected = "if ready:\n x = 3\n y = 4" + (tail or ending) + assert f.read_bytes() == expected.replace("\n", newline).encode("utf-8") + compile(f.read_text(encoding="utf-8"), str(f), "exec") + # --------------------------------------------------------------------------- # Failure diagnostics @@ -212,7 +235,8 @@ class TestAdvancedReplaceAll: return EditFileTool(workspace=tmp_path) @pytest.mark.asyncio - async def test_replace_all_preserves_each_match_indentation(self, tool, tmp_path): + @pytest.mark.parametrize("ending", ["", "\n"], ids=["no-request-newline", "request-newline"]) + async def test_replace_all_preserves_each_match_indentation(self, tool, tmp_path, ending): f = tmp_path / "indent_multi.py" f.write_text( "if a:\n" @@ -225,8 +249,8 @@ class TestAdvancedReplaceAll: ) result = await tool.execute( path=str(f), - old_text="def foo():\n pass", - new_text="def bar():\n return 1", + old_text="def foo():\n pass" + ending, + new_text="def bar():\n return 1" + ending, replace_all=True, ) assert "Patch applied:" in result @@ -240,13 +264,14 @@ class TestAdvancedReplaceAll: ) @pytest.mark.asyncio - async def test_trim_and_quote_fallback_match_succeeds(self, tool, tmp_path): + @pytest.mark.parametrize("ending", ["", "\n"], ids=["no-request-newline", "request-newline"]) + async def test_trim_and_quote_fallback_match_succeeds(self, tool, tmp_path, ending): f = tmp_path / "quote_indent.py" f.write_text(" message = “hello”\n", encoding="utf-8") result = await tool.execute( path=str(f), - old_text='message = "hello"', - new_text='message = "goodbye"', + old_text='message = "hello"' + ending, + new_text='message = "goodbye"' + ending, ) assert "Patch applied:" in result assert f.read_text(encoding="utf-8") == " message = “goodbye”\n"