diff --git a/python/packages/purview/agent_framework_purview/_cache.py b/python/packages/purview/agent_framework_purview/_cache.py index df5d03b97ee..8637c04fade 100644 --- a/python/packages/purview/agent_framework_purview/_cache.py +++ b/python/packages/purview/agent_framework_purview/_cache.py @@ -140,9 +140,9 @@ async def set(self, key: str, value: Any, ttl_seconds: int | None = None) -> Non size = self._estimate_size(value) # Remove old entry if exists - if key in self._cache: - old_size = self._cache[key][2] - self._current_size_bytes -= old_size + old_entry = self._cache.pop(key, None) + if old_entry is not None: + self._current_size_bytes -= old_entry[2] # Evict if needed self._evict_if_needed(size) diff --git a/python/packages/purview/tests/purview/test_cache.py b/python/packages/purview/tests/purview/test_cache.py index 1892842d424..82c1d4ecdd7 100644 --- a/python/packages/purview/tests/purview/test_cache.py +++ b/python/packages/purview/tests/purview/test_cache.py @@ -3,6 +3,7 @@ """Tests for Purview cache provider.""" import asyncio +from unittest.mock import patch from agent_framework_purview._cache import ( InMemoryCacheProvider, @@ -162,6 +163,30 @@ async def test_eviction_with_stale_heap_entries(self) -> None: result = await cache.get("key1") assert result is not None + async def test_growing_entry_at_capacity_maintains_size_and_evicts_other_entry(self) -> None: + """Test that updating an entry to a larger size at capacity correctly tracks size and evicts.""" + cache = InMemoryCacheProvider(max_size_bytes=20) + + with patch("agent_framework_purview._cache.time.time", return_value=1000.0): + # JSON string quotes add 2 bytes to string length. + await cache.set("older", "a" * 6, ttl_seconds=10) # 8 bytes + await cache.set("newer", "b" * 6, ttl_seconds=20) # 8 bytes + assert cache._current_size_bytes == 16 + + # Replace 'older' with 16-byte value. Total needed = 16. + # 'older' was 8 bytes. If 'older' was not removed from cache before eviction, + # eviction pops 'older' from heap and subtracts its size again. + await cache.set("older", "c" * 14, ttl_seconds=30) # 16 bytes + + assert await cache.get("newer") is None + assert await cache.get("older") == "c" * 14 + assert cache._current_size_bytes == 16 + assert sum(entry[2] for entry in cache._cache.values()) == 16 + + await cache.remove("older") + assert cache._current_size_bytes == 0 + + class TestCreateProtectionScopesCacheKey: """Test cache key generation for ProtectionScopesRequest."""