fix(tools): make oversized line truncation explicit

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-24 16:24:06 +08:00
1 parent f57eba2b07
commit cab3a360ea
2 files changed
+54 -18

No files matched your search

+10 -5
View File
@@ -396,23 +396,28 @@ class ReadFileTool(_FsTool):
end = min(start + (limit or self._DEFAULT_LIMIT), total)
numbered = [f"{start + i + 1}| {line}" for i, line in enumerate(all_lines[start:end])]
result = "\n".join(numbered)
line_truncated = False
if len(result) > self._MAX_CHARS:
trimmed: list[str] = []
chars = 0
for line in numbered:
chars += len(line) + 1
if chars > self._MAX_CHARS:
extra = len(line) + (1 if trimmed else 0)
if chars + extra > 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])
line_truncated = True
break
trimmed.append(line)
chars += extra
end = start + len(trimmed)
result = "\n".join(trimmed)
if line_truncated:
result += (
f"\n\n(Line {offset} truncated; its remaining characters are not shown. "
"Use exec with a targeted command to inspect the omitted content.)"
)
if end < total:
result += f"\n\n(Showing lines {offset}-{end} of {total}. Use offset={end + 1} to continue.)"
else: