fix(tools): keep read_file progressing when a single line exceeds the output budget

ReadFileTool stops at the first numbered line that does not fit the
128 KiB budget. When that line is the first one, no line is appended and
the tool returns an empty page that reports lines 2-1 and repeats the
same offset, so the caller can never reach the rest of the file. A long
line further down has the same effect for everything after it.

Emit a bounded prefix of that line and advance the continuation offset
past it, so every read returns content and makes progress.
This commit is contained in:
FanouZeng-TT authored and chengyongru committed 2026-09-24 16:24:06 +08:00
1 parent a724ad4fcd
commit f57eba2b07
2 files changed
+37

No files matched your search

+5
View File
@@ -403,6 +403,11 @@ class ReadFileTool(_FsTool):
for line in numbered:
chars += len(line) + 1
if chars > self._MAX_CHARS:
if not trimmed:
# Do not leave the reader at the same offset when a
# single numbered line exceeds the entire budget.
# Return a bounded prefix and advance past the line.
trimmed.append(line[: self._MAX_CHARS])
break
trimmed.append(line)
end = start + len(trimmed)
+32
View File
@@ -98,6 +98,38 @@ class TestReadFileTool:
assert len(result) <= ReadFileTool._MAX_CHARS + 500 # small margin for footer
assert "Use offset=" in result
@pytest.mark.asyncio
async def test_first_line_longer_than_budget_returns_prefix(self, tool, tmp_path):
"""A single line over the whole budget must still yield content."""
f = tmp_path / "minified.txt"
f.write_text("y" * (ReadFileTool._MAX_CHARS * 2) + "\nsecond line\n", encoding="utf-8")
result = await tool.execute(path=str(f))
assert "1| " in result
assert "second line" not in result
assert len(result) <= ReadFileTool._MAX_CHARS + 500
assert "Use offset=2 to continue" in result
@pytest.mark.asyncio
async def test_long_middle_line_advances_to_following_content(self, tool, tmp_path):
f = tmp_path / "bundle.txt"
f.write_text(
"first\n" + "z" * (ReadFileTool._MAX_CHARS * 2) + "\nlast\n",
encoding="utf-8",
)
first = await tool.execute(path=str(f))
assert "Use offset=2 to continue" in first
second = await tool.execute(path=str(f), offset=2)
assert "2| " in second
assert len(second) <= ReadFileTool._MAX_CHARS + 500
assert "Use offset=3 to continue" in second
third = await tool.execute(path=str(f), offset=3)
assert "3| last" in third
@pytest.mark.asyncio
async def test_oversized_file_is_rejected_before_read(self, tool, tmp_path, monkeypatch):
f = tmp_path / "huge.txt"