mateclaw icon indicating copy to clipboard operation
mateclaw copied to clipboard

[Bug] read_file 工具对超过 30KB 的单行文件无法读取(content 空 + 续读死循环)

Open mateaix opened this issue 6 months ago • 1 comments

出了什么问题?(必填,附截图)

read_file 工具对单行超长文件(典型场景:单行 JSON 数组 / 单行 CSV 数据流 / 工具输出落盘的紧凑 JSON)完全无法读取,并且会陷入"建议你重读、重读后仍然为空"的死循环。

现象

对一份 ~32KB 的单行 JSON 数组调用 read_file({filePath: "/tmp/assets.json"}),返回:

{
  "filePath": "/tmp/assets.json",
  "totalLines": 1,
  "startLine": 1,
  "endLine": 0,
  "readLines": 0,
  "content": "",
  "truncated": true,
  "message": "输出已截断(最多 1000 行 / 30KB)。使用 startLine=1 继续读取。"
}

注意三点:

  1. content 为空字符串 —— 一字节业务数据都没拿到
  2. message 让 agent "用 startLine=1 继续读取" —— 但下一次仍然返回 content="",死循环
  3. endLine=0、readLines=0,但 truncated=true —— 状态字段彼此矛盾

根因

ReadFileTool.java:139-150 的截断循环逐行判断 sb.length() + numberedLine.length() > MAX_OUTPUT_BYTES:

for (int i = 0; i < selectedLines.size(); i++) {
    String numberedLine = String.format("%6d\t%s\n", lineNum, line);
    if (sb.length() + numberedLine.length() > MAX_OUTPUT_BYTES || linesRead >= DEFAULT_MAX_LINES) {
        truncated = true;
        break;
    }
    sb.append(numberedLine);
    linesRead++;
}

当第一行本身就 >30KB 时:

  • i=0,sb.length()=0,numberedLine.length()=32KB > MAX_OUTPUT_BYTES(30KB) → 立即 break
  • linesRead 保持为 0,sb 为空 → content=""
  • nextStart = start + linesRead = 1 + 0 = 1 → 建议 agent 用 startLine=1 重读,但行号变不了 → 同样的空返回

startLine / endLine 是行维度参数,对单行文件根本没有可调空间。

业务影响

在端到端验证 #187 的 fix 时碰到(详见 #187 评审历程)。真实业务里"超大单行紧凑 JSON"是常见数据形态:

  • execute_shell_command 落盘的 spill 文件(命令 stdout 被打包进 {"stdout":"..."} 的单行 JSON)
  • MCP 工具返回 / 缓存的紧凑 JSON
  • API 抓取下来未格式化的 JSON 数组
  • 单行 NDJSON 拼接的中间产物

对这些文件,agent 只能改用 execute_shell_command + python3 -c "json.load(...)" 绕道——但**read_file 作为系统级、每个 agent 都默认绑定的"安全读取入口",它自身应当能完成读取**。

服务端日志证据

2026-05-21 15:08:59.064 [pool-3-thread-3] INFO  v.m.a.g.e.ToolExecutionExecutor
  - [ToolExecutor] Tool read_file returned 17161 chars (now 8000 after spill/truncate)

read_file 自身只返回了一个 222 字节的 "content=空 / truncated=true" 信封,agent 完全拿不到 JSON 主体。

怎么复现?(必填,编号步骤)

# 1. 造一份 32KB 单行 JSON
python3 -c '
import json
rows = [{"SN": f"A{i:04d}", "name": f"设备{i}", **{f"col{j}":f"v{i}_{j}" for j in range(1,29)}} for i in range(1,61)]
open("/tmp/assets.json","w").write(json.dumps(rows, ensure_ascii=False))
'
wc -c /tmp/assets.json   # → 约 32KB,单行
  1. 让任意 agent(system-level tools 默认绑定 read_file)执行:

"请用 read_file 读取 /tmp/assets.json 的内容并告诉我 A0030 的 name 字段值。"

  1. 观察:tool result 的 content 为空字符串,truncated=true,message 让用 startLine=1 继续读
  2. 如果让 agent 按 message 重试,会重复返回同样的空 content(死循环)
  3. agent 只能放弃 read_file,改走 execute_shell_command + python3 绕道

影响模块(选填,多选)

  • 后端 / Backend
  • Tool / 工具
  • Agent / StateGraph 运行时

环境(必填,一行)

dev branch @ dd1e62c2 / admin / curl + admin agent 1000000001 / macOS


修复建议

read_file 需要一条"单行也能分段读"的兜底路径。两个非互斥的方向:

  1. 字节维度参数:新增 byteOffset / byteLength 可选参数。当传入时按字节窗口读取(按 UTF-8 边界向后对齐避免半字符),与 startLine 互斥。message 在 totalLines==1 && truncated 时改成提示用户用 byteOffset=N 续读,而不是 startLine=1。
  2. 单行兜底逻辑:当 selectedLines.size()==1 && line.length() > MAX_OUTPUT_BYTES 时,自动切到"按 ~28KB UTF-8 安全字节窗口截取首段 + 在 result 里返回 nextByteOffset 字段",避免空 content。

实现量级很小(一个工具单文件),但能消除一类"agent 无路可走"的真实卡点。也跟 #187 的 StructuredTruncator 形成呼应:那边管的是"工具返回值落地前的结构感知裁切",这边管的是"超大单元素文件的可读性兜底"。

愿意接,可以分配给我。

mateaix avatar May 21 '26 07:05 mateaix

已在 e61b05bb 修复(私库 bb10c34a)。

核心改动:当第一行就超出 30KB 预算时,不再立即 break 留下 content="" + 无效 startLine 续读提示,而是就地裁剪这一行到剩余预算并计入 readLines,让截断循环至少推进一格。

行为对比:

字段 Before After
content ""(空) 裁剪后的内容(带行号)
readLines 0 1(或实际读到的行数)
truncated true true
lineTruncated — 新增 true:明确标识有行被裁掉了尾巴
message 建议 startLine=1(死循环源头) 不再 advertise startLine —— 行范围参数本来就救不了被裁掉尾部的单行
nextStart 1(不变) 正确推进或不返回

附带打磨:

  • safeTruncate 避免在 UTF-16 surrogate pair 中间切断(中文 emoji 等多字节字符不会切坏)
  • 截断提示文案统一迁移到 messages.properties / messages_en.properties(中英双语 i18n)
  • 新增回归测试 ReadFileToolLargeLineTest,覆盖单行 32KB JSON / 单行 NDJSON / {"stdout":"..."} 这几个 issue 里列举的真实业务形态

影响 surface:

  • execute_shell_command 落盘的 spill JSON(命令 stdout 被打包进 {"stdout":"..."})
  • MCP 工具返回的紧凑 JSON 缓存
  • 外部抓取的未格式化 JSON 数组
  • 单行 NDJSON 拼接中间产物

全部从"agent 读到空内容 + 死循环重试"变成"agent 读到截断 JSON head + 明确知道这一行还有尾巴没拿到",剩下交给 #187 的 StructuredTruncator 在边界对齐 + 数据保真 prompt 处理。两条 fix 是配套的:#190 保证 read_file 至少能吐出字节,#187 保证模型读到截断字节后不会脑补。

Closing.

mateaix avatar May 21 '26 09:05 mateaix