Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(state): refresh read file cache recency on access (#1811)
Muhammad Faisal Ishfaq4e9516f44ceb
diff --git a/src/agentscope/state/_state.py b/src/agentscope/state/_state.pyindex 009666c19..90eee3bb1 100644--- a/src/agentscope/state/_state.py+++ b/src/agentscope/state/_state.py@@ -54,12 +54,14 @@ class ToolContext(BaseModel):"""# Find the cache entry- for entry in self.read_file_cache:+ for idx, entry in enumerate(self.read_file_cache):if entry.file_path == file_path:# Check if cache is still validtry:updated_at = await aiofiles.os.path.getmtime(file_path)if updated_at == entry.updated_at:+ self.read_file_cache.pop(idx)+ self.read_file_cache.append(entry)return entryelse:# Cache is outdated, remove itdiff --git a/tests/builtin_file_cache_test.py b/tests/builtin_file_cache_test.pyindex 9e77019f1..b26960fd4 100644--- a/tests/builtin_file_cache_test.py+++ b/tests/builtin_file_cache_test.py@@ -245,6 +245,41 @@ class FileCacheTest(IsolatedAsyncioTestCase):self.assertIn(files[2], cached_paths)self.assertIn(files[3], cached_paths)+ async def test_cache_hit_refreshes_lru_recency(self) -> None:+ """Test cache hits keep recently used files from being evicted."""+ self.state.tool_context.max_cache_files = 3++ files = []+ for i in range(4):+ file_path = os.path.join(self.temp_dir, f"file{i}.txt")+ with open(file_path, "w", encoding="utf-8") as f:+ f.write(f"Content {i}\n")+ files.append(file_path)++ for file_path in files[:3]:+ await self.read_tool(+ file_path=file_path,+ _agent_state=self.state,+ )++ cache = await self.state.tool_context.get_cache(files[0])+ self.assertIsNotNone(cache)++ await self.read_tool(+ file_path=files[3],+ _agent_state=self.state,+ )++ cached_paths = [+ entry.file_path+ for entry in self.state.tool_context.read_file_cache+ ]++ self.assertIn(files[0], cached_paths)+ self.assertNotIn(files[1], cached_paths)+ self.assertIn(files[2], cached_paths)+ self.assertIn(files[3], cached_paths)+async def test_cache_without_state(self) -> None:"""Test tools work without state (fallback mode)."""# Create a file