Skip to content

fix(agent-core): refuse multi-line empty Edit deletions - #2511

Open
mangeshraut712 wants to merge 26 commits into
MoonshotAI:mainfrom
mangeshraut712:cursor/fix-edit-refuse-large-delete-7813
Open

mangeshraut712 wants to merge 26 commits into
MoonshotAI:mainfrom
mangeshraut712:cursor/fix-edit-refuse-large-delete-7813

Conversation

@mangeshraut712

@mangeshraut712 mangeshraut712 commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Requirement or Bug

Fixes #2427.

Bug Reproduction Steps

  1. Read a file containing a unique three-line section, then call Edit with that section as old_string and an empty or whitespace-only new_string.
  2. On the base behavior, the call succeeds and removes the section. The same occurs when replace_all: true matches multiple multi-line sections whose total is at least three lines.
  3. If a later Edit has stale old_string, its error only asks the model to reload content, which permits a short-read retry loop.

Root Cause

EditService treated an empty replacement like any other exact replacement, so it had no size-based refusal before writing the result. Its stale-content error did not tell the model how much context to reread. This fixes both at the shared service boundary and in the Edit tool guidance rather than relying only on prompt behavior.

Code Changes

Edit input
  -> EditTool
  -> FileEditService
  -> EditService
       count matching old_string occurrences
       refuse empty or whitespace-only deletion of 3+ lines
       allow deliberate deletion only with allow_large_delete=true
  • Add the optional boolean allow_large_delete to the Edit input schema and forward it to the service.
  • Reject empty or whitespace-only replacements that delete at least three lines, including cumulative multi-line replace_all matches, before writing the file.
  • Preserve ordinary single-line and two-line deletions, plus deliberate large deletions that set the flag.
  • Give stale-match failures and the Edit tool description explicit reread guidance.
  • Document the behavior in both reference locales and retain the existing changeset.

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Empty or whitespace-only replacement of 3+ lines, including cumulative multi-line replace_all matches The file was edited immediately. Edit refuses before writing. Any agent-core-v2 session intentionally deleting a large file section through Edit. Set allow_large_delete: true for an intentional deletion.
Ordinary single-line and two-line empty deletions; repeated one-line replace_all deletions Accepted. Unchanged. All Edit tool callers using small deletions. N/A.
old_string not found error guidance Requested a reload without a required scope. Asks the model to reread the whole file or the affected region before retrying and discourages short-read loops. Every agent-core-v2 session exposed to the Edit tool. None; this is guidance only and does not block retries.
New edit.md deletion-guidance sentence No explicit large-deletion instruction. Directs models to use allow_large_delete=true only for intentional 3+ line empty or whitespace-only deletions. Every agent-core-v2 session exposed to the Edit tool. The explicit tool parameter above.
Empty old_string at the file-edit service boundary The public tool schema rejected it, but the service had no explicit guard. The service returns an explicit error without editing. No supported public Edit caller; this protects internal service callers. None.

Affected modules and coverage:

  • packages/agent-core-v2/src/app/edit/ implements the refusal before file writes.
  • packages/agent-core-v2/src/agent/tools/edit/ exposes and explains the opt-in flag.
  • packages/agent-core-v2/test/app/edit/tools/edit.test.ts covers direct refusal, the explicit override, cumulative replace_all, whitespace-only input, LF/CRLF/lone-CR handling, allowed small deletions, stale/non-unique precedence, no-write behavior, and the public schema.
  • docs/en/reference/tools.md and docs/zh/reference/tools.md describe the same threshold and opt-in.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). #2427 is linked; a maintainer approval is still required.
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and the existing changeset names the removed default behavior and its allow_large_delete escape hatch.
  • Ran gen-changesets review; the existing user-facing changeset covers the fix, and no additional entry is needed for the test and wording hardening.
  • Ran gen-docs review; the English and Chinese reference pages are mirrored and the docs build passes. The skill's expected docs/scripts/sync-changelog.mjs prerequisite is absent, so no changelog-sync step was run.

Prompt change

Population: every agent session that exposes the built-in Edit tool (CLI and other hosts that load the default tool set).

Before: after old_string not found, the model guidance only said to reread; a short 30–50 line window often satisfied that and the retry loop continued.

After: Edit tool guidance plus the notFoundMessage tell the model to reread the whole file or a large enough region before retrying, and not to loop Edit → short Read → Edit. This is prompt/guidance only; the oversized empty-delete refusal is enforced in code via allow_large_delete.

@changeset-bot

changeset-bot Bot commented Aug 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 55e1940

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 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: 097b1774d0

ℹ️ 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".

Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated
Comment thread packages/agent-core-v2/src/app/edit/editService.ts
@pkg-pr-new

pkg-pr-new Bot commented Aug 1, 2026 •

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

commit: 55e1940

@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from 097b177 to eb224a2 Compare August 1, 2026 18:20
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main and refreshed the agent-core-v2 inline tools_snapshot hashes for the new Edit allow_large_delete field (fixes the shard-2 snapshot mismatches).

CI is currently stuck in action_required (fork workflow re-approval after the force-push). A maintainer click on "Approve and run workflows" should unblock it — local edit + snapshot tests pass.

@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: 380b0df96d

ℹ️ 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".

Comment thread .changeset/edit-refuse-large-delete.md Outdated

@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: a17e3b9f8c

ℹ️ 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".

Comment thread packages/agent-core-v2/src/agent/tools/edit/edit.ts
@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from 2f08cd7 to 51cc962 Compare August 30, 2026 13:10
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main — merge conflicts are gone. CI is waiting on Approve and run workflows from a maintainer (fork PR).

@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from c6652fd to 1a45ffa Compare September 2, 2026 05:34
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main. Mergeability is MERGEABLE / UNSTABLE — GitHub still needs a maintainer to approve fork-PR workflows so CI can run.

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

rebase it

@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch 2 times, most recently from 744fcd0 to 1ee01a3 Compare September 6, 2026 14:30
@mangeshraut712
mangeshraut712 force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from 1ee01a3 to 4aeab95 Compare September 6, 2026 14:34

@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: 403a94d376

ℹ️ 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".

Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated
@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch 3 times, most recently from 8cc1aee to bf501f5 Compare September 16, 2026 05:07

@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: bf501f5b79

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated
Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Following up on the earlier rebases. This is mergeable and still needs a maintainer review: multi-line empty Edit deletions are refused. I will leave the branch as it is unless you want a change.

@mangeshraut712
mangeshraut712 force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from b1c9995 to b2db743 Compare September 29, 2026 11:08
Guard Edit against accidental +0/−N wipeouts when new_string is empty
across 3+ lines unless allow_large_delete is set. Strengthen the
old_string-not-found recovery guidance so the model rereads a large
enough region instead of looping short Reads (fixes MoonshotAI#2427).
Count matched occurrences when guarding empty replace_all edits so a
short old_string cannot delete 3+ lines without allow_large_delete.
Match and uniqueness checks run before refusing an empty deletion.
replace_all still accumulates multi-line matches, but a one-line token
is not treated as a multi-line span.
Re-record loop and tool inline snapshots so they include the post-rebase
tool list and allow_large_delete hashes.
@cursor
cursor Bot force-pushed the cursor/fix-edit-refuse-large-delete-7813 branch from b2db743 to 2ba98cb Compare September 30, 2026 08:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 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 ⚠️ Failed 2026-10-26T08:03:25.134612Z
We couldn’t complete this review. Comment @codex review to try again.
55e1940 New commits
ℹ️ 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.

@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: 87672fd030

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/app/edit/editService.ts
Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated
Comment thread packages/agent-core-v2/src/agent/tools/edit/edit.ts

@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: e1cf039374

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/edit-refuse-large-delete.md Outdated
Comment thread docs/en/reference/tools.md Outdated
Name the 3-or-more-line cutoff in the changelog entry, and split the
English and Chinese Edit references so the large-delete rules are a
separate paragraph.

@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: ac19e02b9f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/app/edit/editService.ts Outdated

@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: d314b22030

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/edit-refuse-large-delete.md Outdated
Comment thread packages/agent-core-v2/src/app/edit/editService.ts
…use-large-delete-7813

# Conflicts:
#	packages/agent-core-v2/test/agent/loop/loop.test.ts
#	packages/agent-core-v2/test/tool/tool.test.ts

@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: 3d2282dc2f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/edit-refuse-large-delete.md Outdated
mangeshraut712 and others added 2 commits October 14, 2026 05:01
…use-large-delete-7813

# Conflicts:
#	packages/agent-core-v2/test/agent/loop/loop.test.ts
#	packages/agent-core-v2/test/tool/tool.test.ts

@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: f75505a976

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread .changeset/edit-refuse-large-delete.md Outdated
…use-large-delete-7813

# Conflicts:
#	packages/agent-core-v2/test/agent/loop/loop.test.ts
#	packages/agent-core-v2/test/tool/tool.test.ts

@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: 3fe661cef7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/tools/edit/edit.md
…rge-delete-7813

# Conflicts:
#	packages/agent-core-v2/test/agent/loop/loop.test.ts
#	packages/agent-core-v2/test/tool/tool.test.ts
Signed-off-by: Mangesh Raut <mbr63@drexel.edu>
Signed-off-by: Mangesh Raut <mbr63@drexel.edu>
…use-large-delete-7813

Signed-off-by: Mangesh Raut <mbr63@drexel.edu>
Signed-off-by: Mangesh Raut <mbr63@drexel.edu>
Signed-off-by: Mangesh Raut <mbr63@drexel.edu>
Keep the 3-line threshold and multi-line replace_all qualifier without the extra not-found retry clause.
Inline snapshots pick up allow_large_delete in the Edit schema and
updated tools_snapshot hashes from main.
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

CI is green on this. It refuses multi-line empty Edit deletions in agent-core. Happy to take any feedback — if it looks good, could you please review and merge when you get a chance? Thanks.

This branch has not been deployed

No deployments
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.

Edit 失败后陷入死循环:反复"Edit 失败 → 重读 → 再失败",并逐步删空了被编辑文件的多个章节

2 participants