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(read-lifecycle): persist STALE Read originals in the CCR store (#1488)
Kiryu Tsukimiya915717301860
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex 53c03be75..a0e45ecee 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -2419,9 +2419,20 @@ class ContentRouter(Transform):if self.config.read_lifecycle.enabled:from .read_lifecycle import ReadLifecycleManager+ # is None (not truthiness) so falsy test doubles are honored;+ # guarded import keeps read_lifecycle running in stripped builds.+ injected_store = kwargs.get("compression_store")+ if injected_store is None:+ try:+ from ..cache.compression_store import get_compression_store++ injected_store = get_compression_store()+ except ImportError:+ pass+lifecycle_mgr = ReadLifecycleManager(self.config.read_lifecycle,- compression_store=kwargs.get("compression_store"),+ compression_store=injected_store,)lifecycle_result = lifecycle_mgr.apply(messages,diff --git a/headroom/transforms/read_lifecycle.py b/headroom/transforms/read_lifecycle.pyindex c5ea9296b..967b5b696 100644--- a/headroom/transforms/read_lifecycle.py+++ b/headroom/transforms/read_lifecycle.py@@ -474,21 +474,25 @@ class ReadLifecycleManager:if content_bytes < self.config.min_size_bytes:return False, content, None- # Store original in CCR if available- ccr_hash = None+ # Best-effort CCR persistence (mirrors read_maturation.py): a store+ # failure must not break compress().+ ccr_hash = hashlib.sha256(content.encode()).hexdigest()[:24]if self.store is not None:- ccr_hash = self.store.store(- original=content,- compressed="",- tool_name="Read",- tool_call_id=classification.tool_call_id,- compression_strategy=f"read_lifecycle:{classification.state.value}",- )-- # Generate marker- if ccr_hash is None:- # No CCR store — generate a content hash for reference- ccr_hash = hashlib.sha256(content.encode()).hexdigest()[:24]+ try:+ ccr_hash = self.store.store(+ original=content,+ compressed="",+ tool_name="Read",+ tool_call_id=classification.tool_call_id,+ compression_strategy=f"read_lifecycle:{classification.state.value}",+ explicit_hash=ccr_hash,+ )+ except Exception as e: # noqa: BLE001 - storage failure must not break the request+ logger.warning(+ "read_lifecycle: CCR store failed for %s: %s",+ classification.tool_call_id,+ e,+ )file_display = classification.file_path or "unknown"diff --git a/tests/test_transforms/test_read_lifecycle.py b/tests/test_transforms/test_read_lifecycle.pyindex 8cde22239..be27c5f7c 100644--- a/tests/test_transforms/test_read_lifecycle.py+++ b/tests/test_transforms/test_read_lifecycle.py@@ -610,3 +610,92 @@ class TestNoFilePathHandling:# Can't match file_path, so Read is not classified at allassert result.reads_total == 0assert result.messages[1]["content"] == LARGE_CONTENT+++class TestContentRouterIntegration:+ """Regression: ContentRouter.transform must wire a real CCR store into+ ReadLifecycleManager so STALE Read markers resolve via headroom_retrieve."""++ def test_stale_read_marker_retrievable_via_compress(self, monkeypatch):+ import re++ # Force an in-memory backend so the test is hermetic.+ monkeypatch.setenv("HEADROOM_CCR_BACKEND", "memory")++ from headroom import compress+ from headroom.cache.compression_store import (+ get_compression_store,+ reset_compression_store,+ )++ reset_compression_store()+ try:+ large_content = "source line\n" * 500 # above read_lifecycle min_size_bytes+ messages = [+ {+ "role": "assistant",+ "content": [+ {+ "type": "tool_use",+ "id": "t1",+ "name": "Read",+ "input": {"file_path": "/tmp/foo.txt"},+ }+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t1",+ "content": large_content,+ }+ ],+ },+ # Edit the same file -> the Read above becomes STALE.+ {+ "role": "assistant",+ "content": [+ {+ "type": "tool_use",+ "id": "t2",+ "name": "Edit",+ "input": {"file_path": "/tmp/foo.txt"},+ }+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": "t2",+ "content": "edited",+ }+ ],+ },+ ]++ result = compress(messages, model="claude-sonnet-4-5-20250929")++ hashes: list[str] = []+ for m in result.messages:+ content = m.get("content")+ if isinstance(content, list):+ for b in content:+ if isinstance(b, dict) and b.get("type") == "tool_result":+ s = b.get("content", "")+ if isinstance(s, str):+ hashes.extend(re.findall(r"hash=([a-f0-9]+)", s))+ assert hashes, "Expected a STALE Read marker with a hash"++ store = get_compression_store()+ entry = store.retrieve(hashes[0])+ assert entry is not None, "STALE Read marker hash not in CCR store"+ assert entry.tool_name == "Read"+ assert entry.compression_strategy == "read_lifecycle:stale"+ finally:+ # Drop the memory-backend singleton so later tests in the suite+ # see the env-driven default again.+ reset_compression_store()