Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
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
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
53 changes: 53 additions & 0 deletions tests/tools/test_str_replace_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,56 @@ async def test_replace_empty_strings(
assert not result.is_error
assert "successfully edited" in result.message
assert await file_path.read_text() == "Hello !"


async def test_replace_rejects_empty_old_string(
str_replace_file_tool: StrReplaceFile, temp_work_dir: KaosPath
):
"""An empty `old` would silently insert `new` via str.replace() rather than
match nothing, so it must be rejected instead of corrupting the file."""
file_path = temp_work_dir / "test.txt"
original_content = "Hello world!"
await file_path.write_text(original_content)

result = await str_replace_file_tool(
Params(path=str(file_path), edit=Edit(old="", new="X"))
)

assert result.is_error
assert "old" in result.message and "empty" in result.message
assert await file_path.read_text() == original_content # Content unchanged


async def test_replace_rejects_empty_old_string_replace_all(
str_replace_file_tool: StrReplaceFile, temp_work_dir: KaosPath
):
"""Same guard applies when replace_all is set."""
file_path = temp_work_dir / "test.txt"
original_content = "Hello world!"
await file_path.write_text(original_content)

result = await str_replace_file_tool(
Params(path=str(file_path), edit=Edit(old="", new="X", replace_all=True))
)

assert result.is_error
assert await file_path.read_text() == original_content # Content unchanged


async def test_replace_rejects_empty_old_string_in_list(
str_replace_file_tool: StrReplaceFile, temp_work_dir: KaosPath
):
"""An empty `old` anywhere in a list of edits aborts the whole call."""
file_path = temp_work_dir / "test.txt"
original_content = "Hello world!"
await file_path.write_text(original_content)

result = await str_replace_file_tool(
Params(
path=str(file_path),
edit=[Edit(old="Hello", new="Hi"), Edit(old="", new="X")],
)
)

assert result.is_error
assert await file_path.read_text() == original_content # Content unchanged
Loading