Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): report Write failures as error chunks (#3627)
dengzi7f21db7221eb
diff --git a/src/agentscope/tool/_builtin/_write.py b/src/agentscope/tool/_builtin/_write.pyindex d0e650565..8ffb7c0d2 100644--- a/src/agentscope/tool/_builtin/_write.py+++ b/src/agentscope/tool/_builtin/_write.py@@ -296,10 +296,17 @@ Usage:previous_content = ""# Write content to file (backend handles parent dir creation)- await self._backend.write_file(- file_path,- content.encode("utf-8"),- )+ try:+ await self._backend.write_file(+ file_path,+ content.encode("utf-8"),+ )+ except Exception as e:+ return ToolChunk(+ content=[TextBlock(text=f"Error writing file: {str(e)}")],+ state=ToolResultState.ERROR,+ is_last=True,+ )# Refresh the read cache so a later Edit doesn't require a re-readif _agent_state is not None:diff --git a/tests/builtin_write_test.py b/tests/builtin_write_test.pyindex e815aab19..7d5f7d21c 100644--- a/tests/builtin_write_test.py+++ b/tests/builtin_write_test.py@@ -183,6 +183,41 @@ class WriteToolTest(IsolatedAsyncioTestCase):with open(file_path, "r", encoding="utf-8") as f:self.assertEqual(f.read(), "original")+ async def test_write_failure_returns_error_chunk(self) -> None:+ """A failed write is reported as an error chunk, not raised.++ A regular file can never act as a parent directory, so the backend+ write fails identically for every user and platform (root included).+ """+ blocker = os.path.join(self.temp_dir, "blocker.txt")+ with open(blocker, "w", encoding="utf-8") as f:+ f.write("not a directory\n")++ chunk = await self.write_tool(+ file_path=os.path.join(blocker, "report.csv"),+ content="a,b\n",+ )++ self.assertEqual(+ chunk.model_dump(),+ {+ "content": [+ {+ "type": "text",+ "text": AnyString(),+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ "state": ToolResultState.ERROR,+ "is_last": True,+ "metadata": {},+ "id": AnyString(),+ },+ )+ self.assertIn("Error writing file:", chunk.content[0].text)+async def test_match_rule_glob_pattern(self) -> None:"""Test match_rule with glob patterns."""# Test exact match