The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): stop read_file rendering a phantom empty line for newline-terminated files
Max Freedom Pollard10c34dd7e244
diff --git a/tests/tools/test_file_operations.py b/tests/tools/test_file_operations.pyindex 94bb981d222..472d2076208 100644--- a/tests/tools/test_file_operations.py+++ b/tests/tools/test_file_operations.py@@ -418,6 +418,34 @@ class TestShellFileOpsHelpers:assert result.error is Noneassert result.content == "alpha\n"+ def test_newline_terminated_content_has_no_phantom_line(self, file_ops):+ # A file ending in a newline (the normal, well-formed case) has its+ # last line terminated, NOT followed by an empty line. The gutter must+ # match `cat -n`: three lines in, three numbered lines out.+ result = file_ops._add_line_numbers("line1\nline2\nline3\n")+ assert result == "1|line1\n2|line2\n3|line3"+ assert "4|" not in result+ assert len(result.split("\n")) == 3++ def test_non_terminated_content_still_numbered_correctly(self, file_ops):+ # Content with no trailing newline was already correct; guard it.+ result = file_ops._add_line_numbers("line1\nline2\nline3")+ assert result == "1|line1\n2|line2\n3|line3"++ def test_trailing_blank_line_is_kept(self, file_ops):+ # "a" then a genuine blank line, then the terminating newline: that is+ # two lines (a, blank), so only the single terminator is dropped.+ result = file_ops._add_line_numbers("a\n\n")+ assert result == "1|a\n2|"+ assert "3|" not in result++ def test_newline_terminated_with_offset_has_no_phantom_line(self, file_ops):+ # A truncated page (offset>1) that ends on a newline must not append a+ # phantom numbered line at the page boundary.+ result = file_ops._add_line_numbers("def f():\n return 1\n", start_line=10)+ assert result == "10|def f():\n11| return 1"+ assert "12|" not in result+class TestSearchPathValidation:"""Test that search() returns an error for non-existent paths."""diff --git a/tools/file_operations.py b/tools/file_operations.pyindex c7194d3c02b..6884dad5da8 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -312,6 +312,13 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):A/B, while dropping numbers regressed line-referencing."""from tools.tool_output_limits import get_max_line_lengthmax_line_length = get_max_line_length()+ # A trailing newline terminates the final line — it does not start a new,+ # empty one. Splitting without dropping it rendered a phantom "<N+1>|"+ # gutter line on every newline-terminated file (`cat -n` semantics).+ # Exactly ONE terminator is dropped, so a genuinely selected trailing+ # blank line in a page keeps its own number.+ if content.endswith('\n'):+ content = content[:-1]return '\n'.join(f"{i}|{line if len(line) <= max_line_length else line[:max_line_length] + '... [truncated]'}"for i, line in enumerate(content.split('\n'), start=start_line))