Skip to content

Commit 01de197

Browse files
authored
Python: Fix Purview cache size accounting when replacing entries (#8988) (#10257)
1 parent 22b5e75 commit 01de197

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎python/packages/purview/agent_framework_purview/_cache.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ async def set(self, key: str, value: Any, ttl_seconds: int | None = None) -> Non
140140
size = self._estimate_size(value)
141141

142142
# Remove old entry if exists
143-
if key in self._cache:
144-
old_size = self._cache[key][2]
145-
self._current_size_bytes -= old_size
143+
old_entry = self._cache.pop(key, None)
144+
if old_entry is not None:
145+
self._current_size_bytes -= old_entry[2]
146146

147147
# Evict if needed
148148
self._evict_if_needed(size)

‎python/packages/purview/tests/purview/test_cache.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Tests for Purview cache provider."""
44

55
import asyncio
6+
from unittest.mock import patch
67

78
from agent_framework_purview._cache import (
89
InMemoryCacheProvider,
@@ -162,6 +163,30 @@ async def test_eviction_with_stale_heap_entries(self) -> None:
162163
result = await cache.get("key1")
163164
assert result is not None
164165

166+
async def test_growing_entry_at_capacity_maintains_size_and_evicts_other_entry(self) -> None:
167+
"""Test that updating an entry to a larger size at capacity correctly tracks size and evicts."""
168+
cache = InMemoryCacheProvider(max_size_bytes=20)
169+
170+
with patch("agent_framework_purview._cache.time.time", return_value=1000.0):
171+
# JSON string quotes add 2 bytes to string length.
172+
await cache.set("older", "a" * 6, ttl_seconds=10) # 8 bytes
173+
await cache.set("newer", "b" * 6, ttl_seconds=20) # 8 bytes
174+
assert cache._current_size_bytes == 16
175+
176+
# Replace 'older' with 16-byte value. Total needed = 16.
177+
# 'older' was 8 bytes. If 'older' was not removed from cache before eviction,
178+
# eviction pops 'older' from heap and subtracts its size again.
179+
await cache.set("older", "c" * 14, ttl_seconds=30) # 16 bytes
180+
181+
assert await cache.get("newer") is None
182+
assert await cache.get("older") == "c" * 14
183+
assert cache._current_size_bytes == 16
184+
assert sum(entry[2] for entry in cache._cache.values()) == 16
185+
186+
await cache.remove("older")
187+
assert cache._current_size_bytes == 0
188+
189+
165190

166191
class TestCreateProtectionScopesCacheKey:
167192
"""Test cache key generation for ProtectionScopesRequest."""

0 commit comments

Comments
 (0)