From 8d73f4cc55cb1b68a833b3c2f92088b17ade266d Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 22 Sep 2026 22:26:44 +0800 Subject: [PATCH] =?UTF-8?q?read=5Ffile/edit=5Ffile:=E6=96=B0=E5=A2=9E32MB?= =?UTF-8?q?=E5=85=A8=E6=96=87=E8=AF=BB=E5=8F=96=E4=B8=8A=E9=99=90=E9=98=B2?= =?UTF-8?q?OOM(=E8=B6=85=E9=99=90=E5=BC=95=E5=AF=BCterminal);=E7=A9=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6/end= start_line. Supplying either bound disables outline mode.", "default": null, "minimum": 1 } diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index f39b08f..09b9005 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -55,6 +55,8 @@ function truncate(text, max) { // 对齐 Zed:整读时文件字节数超过该阈值则返回大纲(outline),而非截断内容。 const AUTO_OUTLINE_SIZE = 16384; +// 全文读/编辑的硬上限:超过即拒绝整读进内存(OOM 防护,st.size 在 read 前就有),引导走 terminal。 +const TEXT_FULL_READ_CAP = 32 * 1024 * 1024; const IMAGE_EXTS = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff', '.tif']); // read_file 读图(仅当会话绑定的模型 vision: true):sharp 压缩后落盘到任务目录 .imgcache/, @@ -166,12 +168,21 @@ async function toolReadFile(ctx, args) { return await readImageAsBlocks(ctx, abs, rel, st.size); } if (isProbablyBinary(abs)) return '[二进制文件,跳过文本读取] ' + relOf(ctx.taskDir, abs); + if (st.size > TEXT_FULL_READ_CAP) { + return '(文件过大:' + Math.round(st.size / 1048576) + ' MB,超过 read_file 全文读取上限 ' + + (TEXT_FULL_READ_CAP / 1048576) + ' MB,拒绝整读;请用 terminal 的 grep -n / head / sed -n 定位阅读)'; + } const text = await fsp.readFile(abs, 'utf8'); + if (!text) return '(空文件)'; let lines = text.split('\n'); + // CRLF 文件显示层剥去行尾 \r:模型拿到的行更干净;edit_file 精确/模糊匹配与 CRLF 归一不受影响。 + if (text.includes('\r\n')) lines = lines.map(l => (l.endsWith('\r') ? l.slice(0, -1) : l)); const total = lines.length; const startLine = Math.max(1, args.start_line || 1); let endLine = args.end_line || total; if (startLine > total) return '(文件共 ' + total + ' 行,start_line 超出范围)'; + // 显式拒绝而非 slice 出空串静默返回:空串会让模型误判文件为空/工具坏了而换路重试。 + if (endLine < startLine) throw new Error('end_line 必须 >= start_line(got start_line=' + startLine + ', end_line=' + args.end_line + ')'); // 对齐 Zed:未指定范围且文件超过 16KB 时返回大纲;提取不到大纲则回退回首 1KB。 if (!args.start_line && !args.end_line && Buffer.byteLength(text, 'utf8') > AUTO_OUTLINE_SIZE) { const outline = buildOutline(abs, text); @@ -419,6 +430,8 @@ async function toolEditFile(ctx, args) { try { st = await fsp.stat(abs); } catch (_) { throw new Error('文件不存在:' + args.path); } if (st.isDirectory()) throw new Error('无法编辑:路径是目录 ' + args.path); + // 同 read_file 的 OOM 防护:edit_file 也要全量读入,超限拒操作。 + if (st.size > TEXT_FULL_READ_CAP) throw new Error('文件超过 ' + (TEXT_FULL_READ_CAP / 1048576) + 'MB 全文读取上限,edit_file 拒绝操作;请用 terminal 的 sed 等工具编辑'); const edits = args.edits || []; let text = await fsp.readFile(abs, 'utf8'); const original = text; diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e4d0974..fabc1e3 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -285,8 +285,8 @@ stateDiagram-v2 | 工具 | 实现要点 | |---|---| -| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) | -| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;DP 候选区间不连续(中间跳过无关 buffer 行)即弃用,防静默吞行;空 `new_text` = 整行干净删除(精确/模糊两层统一,对齐 `"".lines()` 零行语义);`old_text`/`new_text` 协议见 `definitions.json` | +| `read_file` | 走 `resolveInTask` 防路径穿越;图像(PNG/JPEG/WebP/GIF/BMP/TIFF)按 vision 能力走 `sharp` 压缩后落盘 `.imgcache/` 并回传 `image_rel`(长边 ≤ 2000px,JPEG q=80;文件名带源文件 mtime+size 指纹,重读复用不增殖;重编码无收益时不落盘、直指原文件),发送前由 `llm.buildMessages` 现签成公网外链(engine 侧 mint 前 realpath 复核);大文本/二进制给 outline,超 32MB 拒读并引导 terminal;行号前缀格式 "`<6位右对齐行号>\\t<原文>`"(`edit_file` 用) | +| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;超 32MB 拒绝整文件读写(OOM 防护);DP 候选区间不连续(中间跳过无关 buffer 行)即弃用,防静默吞行;空 `new_text` = 整行干净删除(精确/模糊两层统一,对齐 `"".lines()` 零行语义);`old_text`/`new_text` 协议见 `definitions.json` | | 已移除的工具 | `list_directory` / `create_directory` / `delete_path` / `move_path` / `copy_path` / `find_path` / `grep`:环境固定,文件操作与检索统一走 `terminal`(ls / mkdir / rm / mv / cp / find / rg),内置工具 17 → 10 | | `terminal` | **唯一一个走 executor 的内置工具**(当 `rexec.isRemote(cfg)`);本地分支用 `spawn` + `detached` + 进程组 kill;远端分支 `rexec.run` SSE 收流,结果套上超时/截断/取消注解后 `truncate(text, cfg.terminal.max_output_chars)` 返回 | | `fetch` | `AbortController` 覆盖全生命周期(响应头之后的 body 读流同样被超时/取消打断,取消用与 terminal 同款的 200ms 轮询);content-type 先行,非文本只回状态/类型/尺寸不读 body;流式读到 `cfg.fetch.max_bytes`(默认 1MB)即断开并标注截断;按响应 charset 解码(full-icu 支持 gbk/gb18030 等,未知编码退回 UTF-8);HTML 用 `htmlToMarkdown` 转;HTML 实体单遍解码(常见命名实体+数字实体通吃,修 `&` 双重解码);网络错误解包 `err.cause` 直述 ENOTFOUND/TLS 等真因;engine 不截工具结果,返回值末尾另有 `truncate` 1MB 字符护栏 |