Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(state): preserve LRU cache under concurrent hits (#2457)
老福特d74b9af14286
diff --git a/src/agentscope/state/_state.py b/src/agentscope/state/_state.pyindex 90eee3bb1..54c34b2c0 100644--- a/src/agentscope/state/_state.py+++ b/src/agentscope/state/_state.py@@ -54,23 +54,27 @@ class ToolContext(BaseModel):"""# Find the cache entry- for idx, entry in enumerate(self.read_file_cache):+ for entry in 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 entry- else:- # Cache is outdated, remove it- self.read_file_cache.remove(entry)- return Noneexcept Exception:- # File might not exist anymore- self.read_file_cache.remove(entry)+ updated_at = None++ # Concurrent calls may have reordered or dropped the entry+ # while awaiting, so locate it again by the object itself.+ if entry not in self.read_file_cache:+ return None++ self.read_file_cache.remove(entry)+ if updated_at != entry.updated_at:+ # Cache is outdated, or the file no longer existsreturn None++ # Move the entry to the most recent position+ self.read_file_cache.append(entry)+ return entryreturn Noneasync def cache_file(self, file_path: str, lines: list[str]) -> None:diff --git a/tests/builtin_file_cache_test.py b/tests/builtin_file_cache_test.pyindex b26960fd4..519c74bfd 100644--- a/tests/builtin_file_cache_test.py+++ b/tests/builtin_file_cache_test.py@@ -1,10 +1,13 @@# -*- coding: utf-8 -*-"""File cache test case for Read/Write/Edit tools."""+import asyncioimport osimport tempfilefrom unittest.async_case import IsolatedAsyncioTestCase+from unittest.mock import patch-from agentscope.state import AgentState+from agentscope.state import AgentState, ToolContext+from agentscope.state._state import ReadCacheEntryfrom agentscope.tool import Read, Write, Edit@@ -280,6 +283,81 @@ class FileCacheTest(IsolatedAsyncioTestCase):self.assertIn(files[2], cached_paths)self.assertIn(files[3], cached_paths)+ async def test_concurrent_cache_hits_preserve_lru_entries(self) -> None:+ """Concurrent hits must not duplicate or evict cache entries."""+ context = ToolContext(+ read_file_cache=[+ {+ "lines": ["a"],+ "updated_at": 1.0,+ "bytes": 1.0,+ "file_path": "a",+ },+ {+ "lines": ["b"],+ "updated_at": 1.0,+ "bytes": 1.0,+ "file_path": "b",+ },+ ],+ )+ both_started = asyncio.Event()+ calls = 0++ async def synchronized_getmtime(_: str) -> float:+ nonlocal calls+ calls += 1+ if calls == 2:+ both_started.set()+ await both_started.wait()+ return 1.0++ with patch(+ "agentscope.state._state.aiofiles.os.path.getmtime",+ side_effect=synchronized_getmtime,+ ):+ results = list(+ await asyncio.gather(+ context.get_cache("a"),+ context.get_cache("a"),+ ),+ )++ self.assertListEqual(+ results,+ [+ ReadCacheEntry(+ lines=["a"],+ updated_at=1.0,+ bytes=1.0,+ file_path="a",+ ),+ ReadCacheEntry(+ lines=["a"],+ updated_at=1.0,+ bytes=1.0,+ file_path="a",+ ),+ ],+ )+ self.assertListEqual(+ context.read_file_cache,+ [+ ReadCacheEntry(+ lines=["b"],+ updated_at=1.0,+ bytes=1.0,+ file_path="b",+ ),+ ReadCacheEntry(+ lines=["a"],+ updated_at=1.0,+ bytes=1.0,+ file_path="a",+ ),+ ],+ )+async def test_cache_without_state(self) -> None:"""Test tools work without state (fallback mode)."""# Create a file