Skip to content

chore(agent-core-v2): remove same-file Edit restriction and re-read requirement - #4708

Merged
RealKai42 merged 1 commit into
mainfrom
chore/edit-same-file-reread
Nov 5, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
chore/edit-same-file-reread

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

模型要对同一个文件改好几处时,Edit 工具描述要求它每改一次之前都重新 Read 一遍这个文件。轨迹就变成"读、改、读、改"串行进行,整份文件内容一次次进入上下文(Read 默认最多返回 100k 字符),后面每一轮都要带着,轮数也跟着变多。本 PR 删掉这条限制,让模型可以读一次、改多次。无关联 issue。

Bug Reproduction Steps

N/A

Root Cause

这两句来自 #540。那个 PR 讨论的是 Edit 和 Write 怎么选,顺手把 Edit 描述最初的写法(同一文件的多个 Edit 可以放在一个响应里,写锁按顺序执行)改成了"禁止同一文件连续 Edit、每次 Edit 前重读",没有附数据。后来 "Read the target file before every Edit" 和 "Edit is mandatory" 先后被删,这两句一直留着。

它还和另外两处文本矛盾:Read 描述写着 "After a successful Edit/Write, do not re-read solely to prove the write landed",Edit 成功后的返回也写着 "there is no need to Read the file back to check it"。模型会按更具体的 Edit 描述来,结果就是每次改之前都重读。删掉规则是根本修复。

Code Changes

 Edit description
   Drop the line-number prefix and tab; match only file content.
   `old_string` must be unique unless `replace_all` is set.
   If `old_string` is ambiguous, add surrounding context.
-  Multiple Edit calls may run in one response only when they do not target the same file.
-  DO NOT issue consecutive Edit calls on the same file. ... Read the file again before the next Edit.

只删两行工具描述,不改工具参数、执行逻辑或权限检查。旧规则想防的是"前一个 Edit 让后一个 Edit 的 old_string 失效",这件事运行时已经兜住了:

  • 同一文件上的多个 Edit 按响应里的顺序排队执行,不会并发(toolScheduler.ts 对读写同一路径的调用排队)。
  • 每个 Edit 执行时都从磁盘读最新内容再替换(fileEditService.ts)。
  • old_string 匹配不到时只报错、不写文件,报错文本会提示用 Read 重新加载(editService.ts)。

所以删掉之后,最坏情况是一次可恢复的报错,换来的是每次编辑不再整读一遍文件。Codex 的做法相同:apply_patch 一次调用就能改同一文件的多处,它的基础指令还写着 "Do not waste tokens by re-reading files after calling apply_patch on them. The tool call will fail if it didn't work."

决策 选择 没选 理由
删掉之后要不要补一句"同一文件可以多次 Edit" 只删不加 新增一句允许同一文件多次 Edit、改完不重读 Codex 的工具描述里也没有这类句子;"改完不重读"已经写在 Read 描述和 Edit 成功的返回里;"匹配不到时重读"已经写在报错文本里。补一句只是推测模型需要明说才会改过来,没有数据支撑。

Behavior Changes and Affected Users

受影响的是所有拿到内置 Edit 工具的会话,包括主 agent 和子 agent,覆盖 TUI、print 模式、desktop/web、VS Code、ACP 和 SDK。

Behavior Before After Who relies on the old behavior Escape hatch
Multiple Edit calls may run in one response only when they do not target the same file. 一个响应里不能对同一个文件发多个 Edit。 不再限制;同一文件的多个 Edit 由运行时按顺序执行。 依赖模型把同一文件的编辑拆到不同轮次、以减少 old_string not found 的用户。 无;需维护者确认。
DO NOT issue consecutive Edit calls on the same file. A previous Edit can invalidate a later Edit's old_string, causing old_string not found. Read the file again before the next Edit. 同一文件的每次 Edit 之前都要重读整个文件。 不再要求重读;匹配失败时由报错文本提示重新 Read。 同上。 无;需维护者确认。

改完后可能出现的情况:

  • 后一个 Edit 的 old_string 引用了前一个 Edit 已经改掉的文本,会报 old_string not found,多花一轮,文件不受影响。
  • 少见情况:前一个 Edit 的 new_string 恰好包含后一个 Edit 的 old_string,而原来那处已经被替换掉,后一个 Edit 会改到新插入的文本上,并且不报错。这需要两次编辑的文本互相重叠。

没有测试锁定 Edit 描述的原文,本 PR 不新增测试。模型行为上的效果(每次 Edit 前的 Read 次数、old_string not found 比例、input token、轮数、通过率)还没有用 A/B 验证。

验证详情
pnpm exec vitest run \
  test/app/edit/tools/edit.test.ts \
  test/tool/tool.test.ts \
  -t '^(?!.*returns the spawned agent id when a foreground subagent times out).*$'

在 packages/agent-core-v2 下运行,结果:2 个测试文件通过,182 个测试通过,1 个跳过。returns the spawned agent id when a foreground subagent times out 在本分支和未修改的 main 上都以同样的 30 秒超时失败,所以排除。未运行全量测试。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

…equirement

The Edit description forbade several Edit calls on one file and required a
full Read before each next Edit. Same-file Edits already run in response
order, each Edit reads the file from disk, and a stale old_string fails
safely with a reload hint, so the rule only added a full-file Read per edit.
It also contradicted the Read description and the Edit result note, which
both say not to re-read after a successful edit.
Copilot AI balanced review requested due to automatic review settings November 5, 2026 09:56
@changeset-bot

changeset-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0d6afd9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T09:58:52.299337Z 0d6afd9 PR opened
🔒 Security Review ✅ Completed 2026-11-05T09:59:38.467329Z 0d6afd9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@0d6afd9
npx https://pkg.pr.new/@moonshot-ai/kimi-code@0d6afd9

commit: 0d6afd9

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d6afd9dce

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -3,5 +3,3 @@ Perform exact replacements in existing files.
- Drop the line-number prefix and tab; match only file content.
- `old_string` must be unique unless `replace_all` is set.
- If `old_string` is ambiguous, add surrounding context.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add an escape hatch or obtain approval before removing the policy

For every session exposing the built-in Edit tool, this unconditionally removes both the same-file batching restriction and the reread requirement: EditTool.description loads this template directly, without a flag preserving the old policy. The commit description explicitly lists no escape hatch and says maintainer confirmation is still required. Although the runtime protections justify considering this change, they do not satisfy the removal requirement in AGENTS.md. Provide a config/env/flag restoring the old policy with a guard test, or obtain and record explicit maintainer sign-off before shipping.

AGENTS.md reference: AGENTS.md:L116-L116

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Maintainer sign-off is needed because removing Edit safeguards affects all built-in sessions without an escape hatch, and the impact on edit correctness and reread frequency remains unvalidated.

No open issues 🎉

What changed in this PR

本 PR 精简内置 Edit 工具的提示词,允许模型读取一次文件后连续编辑多处,旨在减少重复读取带来的上下文开销和交互轮次;不修改工具参数、执行逻辑或权限检查。

Changes:

  • 删除同一响应内不能对同一文件多次调用 Edit 的限制,以及连续编辑前必须重读文件的要求。
  • 新增 CLI patch changeset,说明移除的两项行为限制。
File Description
packages/​agent-core-v2/​src/​agent/​tools/​edit/​edit.md 删除同文件多次编辑限制和重读要求,保留精确匹配指导。
.changeset/​edit-same-file-without-reread.md 记录面向用户的 Edit 行为变化。

Review effort: Balanced

@RealKai42
RealKai42 merged commit d0cd33c into main Nov 5, 2026
17 of 18 checks passed
@RealKai42
RealKai42 deleted the chore/edit-same-file-reread branch November 5, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants