Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(cache): reject nonpositive semantic cache capacities (#4301)

Romin Parekh911bb7407465
diff --git a/headroom/cache/semantic.py b/headroom/cache/semantic.py
index a8b8dce07..43e77ab4a 100644
--- a/headroom/cache/semantic.py
+++ b/headroom/cache/semantic.py
@@ -87,7 +87,7 @@ class SemanticCacheConfig:
# Similarity threshold for cache hit (0.0 - 1.0)
similarity_threshold: float = 0.95
- # Maximum entries in cache
+ # Maximum entries in cache (must be positive)
max_entries: int = 1000
# TTL in seconds (0 = no expiry)
@@ -212,7 +212,13 @@ class SemanticCache:
Returns:
Cache key for the entry
+
+ Raises:
+ ValueError: If max_entries is not positive.
"""
+ if self.config.max_entries <= 0:
+ raise ValueError("max_entries must be positive")
+
self._cleanup_expired()
# Create cache key. Prefer the full-context hash: two requests that share
diff --git a/tests/test_cache/test_semantic.py b/tests/test_cache/test_semantic.py
index 90ec45004..536211338 100644
--- a/tests/test_cache/test_semantic.py
+++ b/tests/test_cache/test_semantic.py
@@ -53,6 +53,43 @@ class TestSemanticCache:
entry = cache.get("Unknown query", messages_hash="unknown")
assert entry is None
+ @pytest.mark.parametrize("max_entries", [0, -1])
+ def test_put_rejects_nonpositive_capacity(self, max_entries, monkeypatch):
+ cache = SemanticCache(SemanticCacheConfig(max_entries=max_entries))
+ # Fail promptly instead of hanging if the unguarded eviction loop returns.
+ monkeypatch.setattr(
+ cache, "_evict_oldest", lambda: pytest.fail("Invalid capacity must not evict entries")
+ )
+
+ with pytest.raises(ValueError, match="max_entries must be positive"):
+ cache.put("query", "response", messages_hash="request")
+
+ assert cache.get_stats()["entries"] == 0
+
+ @pytest.mark.parametrize("max_entries", [0, -1])
+ @pytest.mark.parametrize("messages_hash", ["existing", "new"])
+ def test_invalid_capacity_change_preserves_entries(
+ self, max_entries, messages_hash, monkeypatch
+ ):
+ cache = SemanticCache(SemanticCacheConfig(max_entries=1))
+ cache.put("query", "original", messages_hash="existing")
+ cache.config.max_entries = max_entries
+ stats = cache.get_stats()
+ monkeypatch.setattr(
+ cache, "_evict_oldest", lambda: pytest.fail("Invalid capacity must not evict entries")
+ )
+
+ with pytest.raises(ValueError, match="max_entries must be positive"):
+ cache.put("query", "replacement", messages_hash=messages_hash)
+
+ assert cache.get_stats() == stats
+ assert cache.get("query", messages_hash="existing").response == "original"
+ assert cache.get("query", messages_hash="new") is None
+
+ cache.config.max_entries = 1
+ cache.put("query", "updated", messages_hash="existing")
+ assert cache.get("query", messages_hash="existing").response == "updated"
+
def test_same_query_different_context_does_not_collide(self, cache):
"""Two requests that share a trailing user message but differ in earlier
context (distinct messages_hash) must not overwrite each other. Before the