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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(cache/google): tolerate timezone-aware cache expiry timestamps (#3210)
Abhay Singh7ce2580ff95c
diff --git a/headroom/cache/google.py b/headroom/cache/google.pyindex 360aacc68..1a34eb1cc 100644--- a/headroom/cache/google.py+++ b/headroom/cache/google.py@@ -60,6 +60,23 @@ from .base import (logger = logging.getLogger(__name__)+def _now_matching(reference: datetime) -> datetime:+ """Return the current time with tzinfo matching ``reference``.++ A ``CachedContentInfo`` timestamp may be naive (the local-time default this+ module's own examples produce with ``datetime.now() + timedelta(...)``) or+ timezone-aware (Google's ``genai.caching.CachedContent.expire_time`` is an+ aware RFC3339 value, and the documented integration registers it directly).+ Comparing or subtracting a naive ``datetime.now()`` against an aware+ ``expires_at`` raises ``TypeError``, which crashed ``register_cache`` and+ every ``is_expired`` consumer. Anchoring "now" to the same awareness as the+ value it is measured against is correct for both: ``datetime.now(None)`` is+ the naive local time used before, and ``datetime.now(tz)`` is the aware+ instant for a tz-aware timestamp.+ """+ return datetime.now(reference.tzinfo)++# Google-specific constantsGOOGLE_MIN_CACHE_TOKENS = 32_768 # 32K tokens minimumGOOGLE_CACHE_DISCOUNT = 0.75 # 75% discount on cached tokens@@ -100,18 +117,18 @@ class CachedContentInfo:@propertydef is_expired(self) -> bool:"""Check if cache has expired."""- return datetime.now() >= self.expires_at+ return _now_matching(self.expires_at) >= self.expires_at@propertydef ttl_remaining_seconds(self) -> int:"""Seconds remaining until expiry."""- remaining = (self.expires_at - datetime.now()).total_seconds()+ remaining = (self.expires_at - _now_matching(self.expires_at)).total_seconds()return max(0, int(remaining))@propertydef age_seconds(self) -> int:"""Age of the cache in seconds."""- return int((datetime.now() - self.created_at).total_seconds())+ return int((_now_matching(self.created_at) - self.created_at).total_seconds())def to_dict(self) -> dict[str, Any]:"""Serialize to dictionary."""diff --git a/tests/test_cache/test_google.py b/tests/test_cache/test_google.pyindex 2114c92db..8af7cec7a 100644--- a/tests/test_cache/test_google.py+++ b/tests/test_cache/test_google.py@@ -1,6 +1,6 @@"""Tests for GoogleCacheOptimizer."""-from datetime import datetime, timedelta+from datetime import datetime, timedelta, timezoneimport pytest@@ -123,6 +123,56 @@ class TestGoogleCacheOptimizer:found = optimizer.get_reusable_cache("expired123")assert found is None+ def test_cache_registration_accepts_aware_expiry(self, optimizer):+ """A timezone-aware ``expires_at`` must not crash lifecycle checks.++ Google's ``genai.caching.CachedContent.expire_time`` is an aware+ RFC3339 value, and the documented integration registers it directly.+ Comparing it against a naive ``datetime.now()`` raised+ ``TypeError: can't compare offset-naive and offset-aware datetimes``,+ which crashed ``register_cache`` itself (it logs ``ttl_remaining_seconds``)+ and every ``is_expired`` consumer.+ """+ aware_future = datetime.now(timezone.utc) + timedelta(hours=1)+ cache_info = optimizer.register_cache(+ cache_id="aware-cache",+ content_hash="aware123",+ token_count=50000,+ expires_at=aware_future,+ )+ assert not cache_info.is_expired+ assert cache_info.ttl_remaining_seconds > 0+ assert cache_info.age_seconds >= 0++ # Lookup and cleanup iterate is_expired over the registry.+ assert optimizer.get_reusable_cache("aware123") is not None+ assert optimizer.cleanup_expired_caches() == []++ def test_cache_lookup_expired_aware(self, optimizer):+ """An already-expired aware cache is recognized as expired, not crashed."""+ aware_past = datetime.now(timezone.utc) - timedelta(hours=1)+ cache_info = optimizer.register_cache(+ cache_id="aware-expired",+ content_hash="awareexp123",+ token_count=50000,+ expires_at=aware_past,+ )+ assert cache_info.is_expired+ assert optimizer.get_reusable_cache("awareexp123") is None++ def test_from_dict_roundtrip_preserves_aware_expiry(self):+ """``to_dict``/``from_dict`` of an aware timestamp stays comparable."""+ info = CachedContentInfo(+ cache_id="rt",+ content_hash="rt123",+ created_at=datetime.now(timezone.utc),+ expires_at=datetime.now(timezone.utc) + timedelta(hours=1),+ token_count=50000,+ )+ restored = CachedContentInfo.from_dict(info.to_dict())+ assert restored.is_expired is False+ assert restored.ttl_remaining_seconds > 0+def test_cache_lookup_insufficient_ttl(self, optimizer):"""Test that caches with insufficient TTL are not returned."""optimizer.register_cache(