Skip to content

The agent that grows with you

fix(tools): count final unterminated line in read_file total_lines

nikkoxgonzales71063b1dbeb5
diff --git a/tests/tools/test_file_operations_edge_cases.py b/tests/tools/test_file_operations_edge_cases.py
index 075508c9b87..566be82799d 100644
--- a/tests/tools/test_file_operations_edge_cases.py
+++ b/tests/tools/test_file_operations_edge_cases.py
@@ -314,3 +314,74 @@ class TestSearchContextParsing:
assert result.matches[0].path == "dir/file-12-name.py"
assert result.matches[0].line_number == 8
assert result.matches[0].content == "context here"
+
+
+# =========================================================================
+# total_lines for files without a trailing newline (#3907)
+# =========================================================================
+
+
+class TestNoTrailingNewlineTotalLines:
+ """``wc -l`` counts newlines, not lines: a final unterminated line must
+ still count. Covers the live read paths plus the assembler contract."""
+
+ @pytest.fixture()
+ def ops(self):
+ from tools.environments.local import LocalEnvironment
+
+ return ShellFileOperations(LocalEnvironment())
+
+ def test_total_lines_counts_final_unterminated_line(self, tmp_path, ops):
+ target = tmp_path / "no_trailing.txt"
+ target.write_bytes(b"line1\nline2\nline3") # no trailing newline
+
+ result = ops.read_file(str(target))
+
+ assert result.error is None
+ assert result.total_lines == 3
+ assert result.content.split("\n") == ["1|line1", "2|line2", "3|line3"]
+
+ def test_pagination_admits_final_unterminated_line(self, tmp_path, ops):
+ target = tmp_path / "no_trailing.txt"
+ target.write_bytes(b"line1\nline2\nline3")
+
+ first = ops.read_file(str(target), offset=1, limit=2)
+ assert first.truncated is True
+ assert "of 3 lines" in (first.hint or "")
+
+ last = ops.read_file(str(target), offset=3)
+ assert last.error is None
+ assert last.content == "3|line3"
+ assert last.total_lines == 3
+
+ def test_terminated_and_empty_files_unchanged(self, tmp_path, ops):
+ terminated = tmp_path / "terminated.txt"
+ terminated.write_bytes(b"a\nb\nc\n")
+ assert ops.read_file(str(terminated)).total_lines == 3
+
+ empty = tmp_path / "empty.txt"
+ empty.write_bytes(b"")
+ result = ops.read_file(str(empty))
+ assert result.total_lines == 0
+
+ def test_native_path_counts_final_unterminated_line(self, tmp_path, ops):
+ target = tmp_path / "no_trailing.txt"
+ target.write_bytes(b"line1\nline2\nline3")
+
+ result = ops._read_file_native(str(target), 1, 2000)
+
+ assert result.error is None
+ assert result.total_lines == 3
+
+ def test_assembler_bumps_count_only_on_proven_missing_newline(self):
+ ops = ShellFileOperations.__new__(ShellFileOperations)
+
+ proved = ops._assemble_read_result(
+ "a\nb\nc\n", offset=1, end_line=2000, total_lines=2,
+ file_size=5, file_ends_with_newline=False)
+ assert proved.total_lines == 3
+
+ unknown = ops._assemble_read_result(
+ "a\nb\nc\n", offset=1, end_line=2000, total_lines=2,
+ file_size=5, file_ends_with_newline=None)
+ assert unknown.total_lines == 2
diff --git a/tools/file_operations.py b/tools/file_operations.py
index 22baf83a944..c7194d3c02b 100644
--- a/tools/file_operations.py
+++ b/tools/file_operations.py
@@ -869,6 +869,12 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):
read path so the BOM strip, pagination hint, ``cut`` newline-artifact fix and
the ambiguous-silence guards never drift apart. ``file_ends_with_newline`` is
None when the caller could not tell (artifact left alone, as before)."""
+ # ``wc -l`` counts newlines, not lines: a nonempty file whose last byte
+ # is not a newline holds one more line than the count (#3907). Adjust
+ # here — the single choke point — so total_lines, truncation, and the
+ # past-EOF guard agree on every read path (compound, sequential, native).
+ if file_size > 0 and file_ends_with_newline is False:
+ total_lines += 1
if offset == 1: # only the first chunk can carry a BOM (byte 0)
read_output, _ = _strip_bom(read_output)
truncated = total_lines > end_line