diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index 5c6b62a..bdaa307 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -3,7 +3,7 @@ "type": "function", "function": { "name": "edit_file", - "description": "This is a tool for applying edits to an existing file.\n\nBefore using this tool, use the `read_file` tool to understand the file's contents and context.\nTo create a new file or overwrite an existing one with completely new contents, use the `write_file` tool instead.\n\nWhen copying text from `read_file` output into `old_text` or `new_text`, strip the line-number prefix and the tab after it, and keep the original indentation exactly.\n\nMatching rules:\n- `old_text` is first matched exactly. If there is no exact match, a fuzzy match tolerating minor whitespace and formatting differences is attempted.\n- If `old_text` matches more than one location (overlapping matches count as separate locations), the edit fails. Include enough surrounding context to make it unique; avoid very short or repetitive `old_text`.\n- If no match is found at all, the edit fails. For very large files the fuzzy stage degrades to per-line whitespace-insensitive exact matching; the error message says so — do not retry the same text, locate with `read_file` by line numbers or use `terminal` with sed.\n\nTo delete a whole line, set `old_text` to that line WITHOUT its trailing newline and `new_text` to an empty string. Otherwise a blank line will be left behind.\n\nEdits are applied in order. If one edit fails, the edits before it are already written to disk and the file has changed: the error message tells you how many were applied. You must call `read_file` again before retrying; do not reuse the previous `old_text` values.", + "description": "This is a tool for applying edits to an existing file.\n\nBefore using this tool, use the `read_file` tool to understand the file's contents and context.\nTo create a new file or overwrite an existing one with completely new contents, use the `write_file` tool instead.\n\nWhen copying text from `read_file` output into `old_text` or `new_text`, strip the line-number prefix and the tab after it, and keep the original indentation exactly.\n\nMatching rules:\n- `old_text` is first matched exactly. If there is no exact match, a fuzzy match tolerating minor whitespace and formatting differences is attempted.\n- If `old_text` matches more than one location (overlapping matches count as separate locations), the edit fails. Include enough surrounding context to make it unique; avoid very short or repetitive `old_text`.\n- If no match is found at all, the edit fails. For very large files the fuzzy stage degrades to per-line whitespace-insensitive exact matching; the error message says so — do not retry the same text, locate with `read_file` by line numbers or use `terminal` with sed.\n\nTo delete whole lines, put the exact line(s) in `old_text` and set `new_text` to an empty string — matched whole lines are removed cleanly together with their line endings (a trailing newline in `old_text` makes no difference).\n\nEdits are applied in order. If one edit fails, the edits before it are already written to disk and the file has changed: the error message tells you how many were applied. You must call `read_file` again before retrying; do not reuse the previous `old_text` values.", "parameters": { "type": "object", "required": ["path", "edits"], @@ -21,7 +21,7 @@ "properties": { "old_text": { "type": "string", - "description": "The text to find in the file. Matched exactly first, then fuzzily (whitespace / minor formatting differences tolerated). Provide enough context to make the match unique; short or repeated snippets are reported as ambiguous. A trailing newline, if present, is stripped before matching; for whole-line deletion omit it." + "description": "The text to find in the file. Matched exactly first, then fuzzily (whitespace / minor formatting differences tolerated). Provide enough context to make the match unique; short or repeated snippets are reported as ambiguous. A trailing newline, if present, is stripped before matching. For whole-line deletion, put the exact line(s) in old_text and pass an empty new_text." }, "new_text": { "type": "string", diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index 59f7d5e..4a2c20a 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -279,8 +279,15 @@ function fuzzyLocate(curLines, oldLinesTrim) { else { c--; } } if (!pairs.length) continue; - // 回溯是从下往上收集 pairs(降序),用 min/max 取区间,不依赖收集顺序。 - const startRow = Math.min(...pairs), endRow = Math.max(...pairs); + // 升序排列并要求连续:不连续区间说明 DP 在中间跳过了无关的 buffer 行(left/13 代价 diag 路径), + // 整段 splice 会静默删掉这些行——这类候选直接丢弃(上层报找不到,模型补上下文重试)。 + pairs.sort((a, b) => a - b); + let contiguous = true; + for (let k = 1; k < pairs.length; k++) { + if (pairs[k] !== pairs[k - 1] + 1) { contiguous = false; break; } + } + if (!contiguous) continue; + const startRow = pairs[0], endRow = pairs[pairs.length - 1]; const span = endRow - startRow + 1; const ratio = pairs.length / Math.max(span, m); if (ratio < FUZZY_THRESHOLD) continue; @@ -348,6 +355,12 @@ function applyEdit(text, edit, i) { } // CRLF 文件:新内容里的行尾统一成 \r\n。 if (text.includes('\r\n')) repl = repl.replace(/\r?\n/g, '\r\n'); + // 空 new_text 的整行删除:区间恰好行对齐(前邻行首/换行、后随换行)时把尾随行尾一并吃掉, + // 对齐 Zed 空 new_text=删零行(否则 JS 语义会留下一个空行)。 + if (repl === '' && (after === '\n' || afterCRLF) && + (idx === 0 || text.charAt(idx - 1) === '\n' || text.charAt(idx - 1) === '\r')) { + return text.slice(0, idx) + text.slice(idx + pat.length + (afterCRLF ? 2 : 1)); + } return text.slice(0, idx) + repl + text.slice(idx + pat.length); } // 2) 模糊匹配(逐行 trim) @@ -379,7 +392,9 @@ function applyEdit(text, edit, i) { ' 行附近)。请在 old_text 中包含更多上下文以唯一定位。'); } const { startRow, endRow } = loc; - let newLines = newLinesRaw; + // 空 new_text=整行删除:对齐 Zed "".lines() 的零行语义(JS ''.split('\n') 是 1 个空行,会留白); + // new_text='\n' 仍是「替换为一个空行」,语义不合并。 + let newLines = new_text === '' ? [] : newLinesRaw; // 匹配区间后紧跟换行时,吃掉 new_text 的尾换行(防止吞掉行尾)。 const hasNewlineAfter = endRow < curLines.length - 1 || text.endsWith('\n'); if (hasNewlineAfter && newLines.length > 1 && newLines[newLines.length - 1] === '') { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 3ef66d6..28c7171 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -286,7 +286,7 @@ 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 找最相似行;`old_text`/`new_text` 协议见 `definitions.json` | +| `write_file` / `edit_file` | `edit_file` 用模糊定位(Zed 风格):先精确匹配,失败后用 Levenshtein DP 找最相似行;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` 转;engine 不截工具结果,返回值末尾另有 `truncate` 1MB 字符护栏 |