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/ccr): tolerate a malformed CCR entry row instead of crashing the store (#3156)
Abhay Singh728ff7e22b05
diff --git a/headroom/cache/backends/sqlite.py b/headroom/cache/backends/sqlite.pyindex 764b138ee..6d0ca410d 100644--- a/headroom/cache/backends/sqlite.py+++ b/headroom/cache/backends/sqlite.py@@ -221,8 +221,18 @@ class SQLiteBackend:data = json.loads(raw)except (json.JSONDecodeError, TypeError):return None+ if not isinstance(data, dict):+ return Noneknown = {f.name for f in fields(CompressionEntry)}- return CompressionEntry(**{k: v for k, v in data.items() if k in known})+ try:+ return CompressionEntry(**{k: v for k, v in data.items() if k in known})+ except (TypeError, ValueError):+ # A blob that parses as JSON but is missing a required field (schema+ # drift across an upgrade, a partially written row) must degrade to a+ # miss, not raise. Otherwise a single bad row crashes get() — and,+ # via items(), _clean_expired() runs it on every store's eviction, so+ # one poison row would break all reads, evictions, and stores.+ return Nonedef _purge_expired(self, now: float) -> int:"""Delete expired rows using metadata; caller holds ``_lock``."""diff --git a/tests/test_ccr_sqlite_backend.py b/tests/test_ccr_sqlite_backend.pyindex 7accad392..2e5c211ab 100644--- a/tests/test_ccr_sqlite_backend.py+++ b/tests/test_ccr_sqlite_backend.py@@ -252,6 +252,37 @@ class TestSQLiteBackend:assert got is not Noneassert got.original_content == "x" * 600+ def test_missing_required_field_degrades_to_miss(self, db_path):+ """A row that parses as JSON but is missing a required CompressionEntry+ field (schema drift across an upgrade, a partially written row) must+ degrade to a miss, not raise. Otherwise it crashes get() and — via+ items(), which _clean_expired runs on every store's eviction — breaks+ all reads, evictions, and stores for one poison row."""+ b = SQLiteBackend(db_path)+ b.set("good", make_entry("good"))+ # Insert a valid-JSON blob missing required fields, plus a non-dict blob.+ with b._lock:+ for hash_key, blob in (+ ("poison", '{"hash": "poison", "original_content": "x"}'),+ ("nulljson", "null"),+ ):+ b._conn.execute(+ "INSERT OR REPLACE INTO ccr_entries "+ "(hash, entry_json, created_at, ttl) VALUES (?, ?, ?, ?)",+ (hash_key, blob, time.time(), 1800),+ )+ b._conn.commit()++ assert b.get("poison") is None # no TypeError+ assert b.get("nulljson") is None+ # items() skips the bad rows instead of raising; the good row survives.+ items = dict(b.items())+ assert set(items) == {"good"}+ # A store cycle (which evicts via items()) still works with poison present.+ store = CompressionStore(backend=b, max_entries=1000)+ store.store(original="fresh original", compressed="c", original_item_count=1)+ assert b.get("good") is not None+def test_store_ttl_enforcement_via_compression_store(self, db_path):"""TTL checks stay in CompressionStore; expired entries miss."""store = CompressionStore(backend=SQLiteBackend(db_path))