Skip to content

Build and run agents you can see, understand and trust.

fix(embedding): reject negative file cache budgets (#3582)

Aman8fb6d77f6026
diff --git a/src/agentscope/embedding/_file_cache.py b/src/agentscope/embedding/_file_cache.py
index 94e1626c6..e5e538a68 100644
--- a/src/agentscope/embedding/_file_cache.py
+++ b/src/agentscope/embedding/_file_cache.py
@@ -35,13 +35,24 @@ class FileEmbeddingCache(EmbeddingCacheBase):
The maximum number of files to keep in the cache directory. If
exceeded, the oldest files will be removed. `None` leaves the
file count unlimited; `0` retains no embedding files after a
- write.
+ write. Negative values are not allowed.
max_cache_size (`int | None`, defaults to `None`):
The maximum size of the cache directory in MB. If exceeded,
the oldest files will be removed until the size is within the
limit. A vector set that exceeds the limit on its own is not
- cached, so that storing it cannot empty the cache.
+ cached, so that storing it cannot empty the cache. `None`
+ leaves the size unlimited; `0` skips caching new entries.
+ Negative values are not allowed.
+
+ Raises:
+ ValueError:
+ If `max_file_number` or `max_cache_size` is negative.
"""
+ if max_file_number is not None and max_file_number < 0:
+ raise ValueError("max_file_number must be non-negative or None.")
+ if max_cache_size is not None and max_cache_size < 0:
+ raise ValueError("max_cache_size must be non-negative or None.")
+
self._cache_dir = os.path.abspath(cache_dir)
self.max_file_number = max_file_number
self.max_cache_size = max_cache_size
diff --git a/tests/embedding_file_cache_test.py b/tests/embedding_file_cache_test.py
index 830cc0ae9..d7dc6850a 100644
--- a/tests/embedding_file_cache_test.py
+++ b/tests/embedding_file_cache_test.py
@@ -27,6 +27,49 @@ def _vectors(count: int) -> list:
class FileEmbeddingCacheEvictionTest(IsolatedAsyncioTestCase):
"""The size limit must never cost more entries than it stores."""
+ def test_negative_limits_are_rejected(self) -> None:
+ """Invalid budgets fail before the cache directory is accessed."""
+ cases = [
+ (-1, None, "max_file_number"),
+ (-10, None, "max_file_number"),
+ (None, -1, "max_cache_size"),
+ (None, -10, "max_cache_size"),
+ (-1, -1, "max_file_number"),
+ ]
+ with tempfile.TemporaryDirectory() as directory:
+ cache_dir = os.path.join(directory, "cache")
+ for max_file_number, max_cache_size, parameter in cases:
+ with self.subTest(
+ max_file_number=max_file_number,
+ max_cache_size=max_cache_size,
+ ):
+ with self.assertRaisesRegex(
+ ValueError,
+ f"{parameter} must be non-negative",
+ ):
+ FileEmbeddingCache(
+ cache_dir=cache_dir,
+ max_file_number=max_file_number,
+ max_cache_size=max_cache_size,
+ )
+ self.assertFalse(os.path.exists(cache_dir))
+
+ async def test_cache_size_limits(self) -> None:
+ """None, zero, and positive size budgets keep their behavior."""
+ embeddings = [[1.0, 2.0]]
+ for limit in (None, 0, 1):
+ with self.subTest(limit=limit):
+ with tempfile.TemporaryDirectory() as cache_dir:
+ cache = FileEmbeddingCache(
+ cache_dir=cache_dir,
+ max_cache_size=limit,
+ )
+ await cache.store(embeddings, "entry")
+ self.assertEqual(
+ await cache.retrieve("entry"),
+ None if limit == 0 else embeddings,
+ )
+
async def test_file_count_limits(self) -> None:
"""Only None is unlimited; zero and positive limits are enforced."""
for limit in (None, 0, 1, 2):