Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): count written lines the way Read numbers them (#2734)
lihongyuan99b337928ff7a5
diff --git a/src/agentscope/tool/_builtin/_write.py b/src/agentscope/tool/_builtin/_write.pyindex d39d03344..c90f95c8c 100644--- a/src/agentscope/tool/_builtin/_write.py+++ b/src/agentscope/tool/_builtin/_write.py@@ -304,8 +304,8 @@ Usage:content.encode("utf-8"),)- # Count lines in content- line_count = len(content.split("\n"))+ # Count lines the way the ``Read`` tool numbers them+ line_count = len(content.splitlines())# Build the unified diff between previous and new content. When the# file is brand new, ``unified_diff`` over an empty old side naturallydiff --git a/tests/builtin_write_test.py b/tests/builtin_write_test.pyindex 29b77447c..294ae490f 100644--- a/tests/builtin_write_test.py+++ b/tests/builtin_write_test.py@@ -4,6 +4,8 @@ import osimport tempfilefrom unittest.async_case import IsolatedAsyncioTestCase+from utils import AnyString+from agentscope.tool import Writefrom agentscope.permission import (PermissionContext,@@ -126,6 +128,39 @@ class WriteToolTest(IsolatedAsyncioTestCase):content = f.read()self.assertEqual(content, "")+ async def test_written_line_count_matches_read_numbering(self) -> None:+ """The reported line count matches the ``Read`` tool numbering."""+ cases = [+ ("alpha\nbeta\n", 2),+ ("alpha\nbeta", 2),+ ("alpha\n", 1),+ ("alpha", 1),+ ("", 0),+ ]+ for index, (content, line_count) in enumerate(cases):+ file_path = os.path.join(self.temp_dir, f"count-{index}.txt")++ chunk = await self.write_tool(+ file_path=file_path,+ content=content,+ )++ self.assertListEqual(+ [block.model_dump() for block in chunk.content],+ [+ {+ "type": "text",+ "text": (+ f"The file {file_path} has been written "+ f"successfully ({line_count} lines)."+ ),+ "id": AnyString(),+ "created_at": AnyString(),+ "finished_at": None,+ },+ ],+ )+async def test_overwrite_existing_without_prior_read_errors(self) -> None:"""Overwriting an existing file via state-injected call requiresthe file to have been read first (cached in tool_context).