Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): reject empty edit replacement targets (#2995)
Sicheng Han149e968b1bf1
diff --git a/src/agentscope/tool/_builtin/_edit.py b/src/agentscope/tool/_builtin/_edit.pyindex ce0029255..9e87720a2 100644--- a/src/agentscope/tool/_builtin/_edit.py+++ b/src/agentscope/tool/_builtin/_edit.py@@ -56,8 +56,9 @@ Usage:},"old_string": {"type": "string",+ "minLength": 1,"description": (- "The exact string to replace. Must match exactly "+ "The nonempty string to replace. Must match exactly ""including whitespace and indentation."),},@@ -259,6 +260,18 @@ Usage:_agent_state: AgentState | None = None,) -> ToolChunk:"""Execute the edit and return the result."""+ if not old_string:+ return ToolChunk(+ content=[+ TextBlock(+ text="Error: old_string must not be empty. "+ "Use the Write tool to populate an empty file.",+ ),+ ],+ state=ToolResultState.ERROR,+ is_last=True,+ )+# Validate file_path is absoluteif not self._backend.isabs(file_path):return ToolChunk(diff --git a/tests/builtin_edit_test.py b/tests/builtin_edit_test.pyindex 9d4bbec22..25c166cfe 100644--- a/tests/builtin_edit_test.py+++ b/tests/builtin_edit_test.py@@ -37,6 +37,10 @@ class EditToolTest(IsolatedAsyncioTestCase):self.assertEqual(self.edit_tool.name, "Edit")self.assertIsInstance(self.edit_tool.description, str)self.assertIsInstance(self.edit_tool.input_schema, dict)+ old_string_schema = self.edit_tool.input_schema["properties"][+ "old_string"+ ]+ self.assertEqual(old_string_schema["minLength"], 1)self.assertFalse(self.edit_tool.is_mcp)self.assertFalse(self.edit_tool.is_read_only)self.assertFalse(self.edit_tool.is_concurrency_safe)@@ -81,6 +85,60 @@ class EditToolTest(IsolatedAsyncioTestCase):self.assertEqual(chunk.state, "error")self.assertIn("not found", chunk.content[0].text)+ async def test_empty_target_preserves_file(self) -> None:+ """An empty target must not expand between every character."""+ for original in (b"abc\r\ndef\r\n", b""):+ for replace_all in (False, True):+ with self.subTest(original=original, replace_all=replace_all):+ with open(self.temp_file.name, "wb") as stream:+ stream.write(original)+ chunk = await self.edit_tool(+ file_path=self.temp_file.name,+ old_string="",+ new_string="X",+ replace_all=replace_all,+ )+ with open(self.temp_file.name, "rb") as stream:+ self.assertEqual(stream.read(), original)+ self.assertEqual(+ chunk.model_dump(+ exclude={+ "id": True,+ "content": {"__all__": {"id", "created_at"}},+ },+ ),+ {+ "content": [+ {+ "type": "text",+ "text": (+ "Error: old_string must not be empty. "+ "Use the Write tool to populate "+ "an empty file."+ ),+ "finished_at": None,+ },+ ],+ "state": "error",+ "is_last": True,+ "metadata": {},+ },+ )++ async def test_whitespace_target_can_be_deleted(self) -> None:+ """A nonempty whitespace target and an empty replacement are valid."""+ with open(self.temp_file.name, "w", encoding="utf-8") as stream:+ stream.write("a b c")+ chunk = await self.edit_tool(+ file_path=self.temp_file.name,+ old_string=" ",+ new_string="",+ replace_all=True,+ )+ self.assertEqual(chunk.state, "running")+ with open(self.temp_file.name, "r", encoding="utf-8") as stream:+ self.assertEqual(stream.read(), "abc")+async def test_edit_multiple_occurrences(self) -> None:"""Test editing with multiple occurrences."""# Write file with duplicate content