Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(file): reject empty old string in StrReplaceFile
An empty `old` makes str.replace() insert `new` at the start or between
every character instead of matching nothing, so a bad edit silently mangles
the file. Validate the edit before applying so the tool errors out instead.
  • Loading branch information
rootkiller6788 committed Aug 30, 2026
commit fbb18cbd45dcc0f3766ea148b941cf04fe94a59b
12 changes: 12 additions & 0 deletions src/kimi_cli/tools/file/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ async def __call__(self, params: Params) -> ToolReturnValue:
original_content = content
edits = [params.edit] if isinstance(params.edit, Edit) else params.edit

# An empty `old` makes str.replace() insert `new` at every position
# instead of matching nothing, silently mangling the file. Reject it.
for edit in edits:
if not edit.old:
return ToolError(
message=(
"The `old` string cannot be empty. "
"Give the exact text to replace, or use WriteFile to insert text."
),
brief="Empty old string",
)

# Apply all edits
for edit in edits:
content = self._apply_edit(content, edit)
Expand Down