Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): refresh the read cache after Edit and Write (#2865)
ydflow9b356024bfe1
diff --git a/src/agentscope/tool/_builtin/_edit.py b/src/agentscope/tool/_builtin/_edit.pyindex 4753a84bd..ce0029255 100644--- a/src/agentscope/tool/_builtin/_edit.py+++ b/src/agentscope/tool/_builtin/_edit.py@@ -392,6 +392,16 @@ Usage:is_last=True,)+ # Refresh the read cache so consecutive edits don't require a re-read+ if _agent_state is not None:+ await _agent_state.tool_context.cache_file(+ file_path=file_path,+ lines=_normalize_newlines(updated_content).splitlines(+ keepends=True,+ ),+ mtime=await self._backend.stat_mtime(file_path),+ )+# Return success messagereplacement_msg = (f"all {occurrences} occurrences" if replace_all else "1 occurrence"diff --git a/src/agentscope/tool/_builtin/_write.py b/src/agentscope/tool/_builtin/_write.pyindex c90f95c8c..4b016c761 100644--- a/src/agentscope/tool/_builtin/_write.py+++ b/src/agentscope/tool/_builtin/_write.py@@ -20,7 +20,7 @@ from ...permission import (from .._response import ToolChunkfrom ...message import TextBlock, ToolResultStatefrom ...state import AgentState-from ._backend import BackendBase+from ._backend import BackendBase, _normalize_newlinesclass Write(ToolBase):@@ -304,6 +304,14 @@ Usage:content.encode("utf-8"),)+ # Refresh the read cache so a later Edit doesn't require a re-read+ if _agent_state is not None:+ await _agent_state.tool_context.cache_file(+ file_path=file_path,+ lines=_normalize_newlines(content).splitlines(keepends=True),+ mtime=await self._backend.stat_mtime(file_path),+ )+# Count lines the way the ``Read`` tool numbers themline_count = len(content.splitlines())diff --git a/tests/builtin_file_cache_test.py b/tests/builtin_file_cache_test.pyindex 6aedb28f1..523ed26bf 100644--- a/tests/builtin_file_cache_test.py+++ b/tests/builtin_file_cache_test.py@@ -11,7 +11,9 @@ from agentscope.state._state import ReadCacheEntryfrom agentscope.tool import Read, Write, Edit-class FileCacheTest(IsolatedAsyncioTestCase):+class FileCacheTest( # pylint: disable=too-many-public-methods+ IsolatedAsyncioTestCase,+):"""Test file cache functionality for Read/Write/Edit tools."""async def asyncSetUp(self) -> None:@@ -500,8 +502,42 @@ class FileCacheTest(IsolatedAsyncioTestCase):content = f.read()self.assertEqual(content, "Hello Python\nThis is a test\n")- async def test_write_invalidates_cache(self) -> None:- """Test that Write updates the cache so Edit can use it afterwards."""+ async def test_edit_then_edit_without_reread(self) -> None:+ """Test consecutive Edits succeed after a single Read."""+ with open(self.test_file, "w", encoding="utf-8") as f:+ f.write("alpha\nbeta\ngamma\n")++ # Read to populate the cache+ read_chunk = await self.read_tool(+ file_path=self.test_file,+ _agent_state=self.state,+ )+ self.assertEqual(read_chunk.state, "running")++ # First edit succeeds and refreshes the cache+ first_edit = await self.edit_tool(+ file_path=self.test_file,+ old_string="alpha",+ new_string="ALPHA",+ _agent_state=self.state,+ )+ self.assertEqual(first_edit.state, "running")++ # Second edit passes the staleness gate without a fresh Read+ second_edit = await self.edit_tool(+ file_path=self.test_file,+ old_string="beta",+ new_string="BETA",+ _agent_state=self.state,+ )+ self.assertEqual(second_edit.state, "running")++ with open(self.test_file, "r", encoding="utf-8") as f:+ content = f.read()+ self.assertEqual(content, "ALPHA\nBETA\ngamma\n")++ async def test_write_then_edit_without_reread(self) -> None:+ """Test Write updates the cache so Edit can use it afterwards."""with open(self.test_file, "w", encoding="utf-8") as f:f.write("original content\n")@@ -511,7 +547,7 @@ class FileCacheTest(IsolatedAsyncioTestCase):_agent_state=self.state,)- # Overwrite with Write (mtime changes, old cache becomes stale)+ # Overwrite with Write (mtime changes, cache is refreshed)write_chunk = await self.write_tool(file_path=self.test_file,content="new content\n",@@ -519,15 +555,18 @@ class FileCacheTest(IsolatedAsyncioTestCase):)self.assertEqual(write_chunk.state, "running")- # The old cache entry is now stale; Edit should require a new Read+ # Edit should succeed against the written contentedit_chunk = await self.edit_tool(file_path=self.test_file,old_string="new content",new_string="updated content",_agent_state=self.state,)- self.assertEqual(edit_chunk.state, "error")- self.assertIn("must first read", edit_chunk.content[0].text)+ self.assertEqual(edit_chunk.state, "running")++ with open(self.test_file, "r", encoding="utf-8") as f:+ content = f.read()+ self.assertEqual(content, "updated content\n")async def test_write_cache_stale_then_reread(self) -> None:"""Test workflow: Read -> Write -> Read -> Edit works correctly."""