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(ccr): make retrieval TTL configurable (#715)
Hc2533f7703ee2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 46cf18156..3117999be 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0* **proxy:** per-project savings breakdown on the dashboard for all wrapped agents — Claude Code, Codex, aider, Copilot, and Cursor ([#802](https://github.com/chopratejas/headroom/issues/802)). `headroom wrap claude`/`codex` tag requests with an `X-Headroom-Project` header (launch-directory name); `wrap aider`/`copilot`/`cursor` — whose clients cannot send custom headers — use a `/p/<name>` base-URL prefix the proxy strips. Savings are aggregated per project (persisted, schema v3 with transparent v2 migration), exposed as `savings.per_project` in `/stats` and `projects` in `/stats-history`, and shown in a Per-Project Savings dashboard table.+### Bug Fixes++* **ccr:** make retrieval store TTL configurable with `HEADROOM_CCR_TTL_SECONDS`, expose the effective TTL in `/v1/retrieve/stats`, and distinguish expired retrievals from missing hashes.+## [0.24.0](https://github.com/chopratejas/headroom/compare/v0.23.0...v0.24.0) (2026-06-08)diff --git a/README.md b/README.mdindex 06ac86443..07932dc22 100644--- a/README.md+++ b/README.md@@ -52,7 +52,7 @@ Headroom compresses everything your AI agent reads — tool outputs, logs, RAG c- **MCP server** — `headroom_compress`, `headroom_retrieve`, `headroom_stats` for any MCP client- **Cross-agent memory** — shared store across Claude, Codex, Gemini, auto-dedup- **`headroom learn`** — mines failed sessions, writes corrections to `CLAUDE.md` / `AGENTS.md`-- **Reversible (CCR)** — originals never deleted; LLM retrieves on demand+- **Reversible (CCR)** — originals are cached for retrieval on demand## How it works (30 seconds)@@ -159,7 +159,7 @@ Platform support note: macOS auth reuse via Copilot CLI Keychain storage has bee**Great fit if you…**- run AI coding agents daily and want savings without changing your code- work across multiple agents and want shared memory-- need reversible compression — originals always retrievable via CCR+- need reversible compression — originals are retrievable via CCR within the configured TTL**Skip it if you…**- only use a single provider's native compaction and don't need cross-agent memory@@ -287,4 +287,4 @@ Devcontainers in `.devcontainer/` (default + `memory-stack` with Qdrant & Neo4j)## License-Apache 2.0 — see [LICENSE](LICENSE).\ No newline at end of file+Apache 2.0 — see [LICENSE](LICENSE).diff --git a/docs/content/docs/ccr.mdx b/docs/content/docs/ccr.mdxindex 3aed20814..7bb5e4c78 100644--- a/docs/content/docs/ccr.mdx+++ b/docs/content/docs/ccr.mdx@@ -162,6 +162,18 @@ response = client.chat.completions.create(</Tab></Tabs>+## Retention++Proxy CCR originals are kept for 300 seconds by default. For longer autonomous+agent runs, set `HEADROOM_CCR_TTL_SECONDS` before starting the proxy:++```bash+HEADROOM_CCR_TTL_SECONDS=7200 headroom proxy+```++Check the effective setting at `/v1/retrieve/stats` under+`store.default_ttl_seconds`.+## Message-level CCR> **Retired:** The "Message-level CCR via IntelligentContext" feature (where `IntelligentContext` would store dropped messages in CCR with a retrieval marker) was part of the `IntelligentContextConfig` API that was removed in 0.9.x. Context management is now handled automatically by the pipeline without a separate configurable IntelligentContext stage. Tool-output CCR via SmartCrusher and ContentRouter remains fully supported.diff --git a/headroom/cache/compression_store.py b/headroom/cache/compression_store.pyindex 30c65fd79..b6eb94660 100644--- a/headroom/cache/compression_store.py+++ b/headroom/cache/compression_store.py@@ -53,6 +53,9 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)+DEFAULT_CCR_TTL_SECONDS = 300+CCR_TTL_SECONDS_ENV = "HEADROOM_CCR_TTL_SECONDS"+_RETRIEVAL_LOG_PREVIEW_CHARS = 4096_SECRET_KEY_VALUE_RE = re.compile(r"(?i)\b([A-Z0-9_-]*(?:API[_-]?KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL|AUTH)[A-Z0-9_-]*)"@@ -62,6 +65,48 @@ _AUTH_VALUE_RE = re.compile(r"(?i)\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{12,}")_API_KEY_VALUE_RE = re.compile(r"\bsk-[A-Za-z0-9_-]{12,}\b")+def _get_env_default_ttl_seconds() -> int:+ raw_value = os.environ.get(CCR_TTL_SECONDS_ENV)+ if raw_value is None or not raw_value.strip():+ return DEFAULT_CCR_TTL_SECONDS++ try:+ ttl_seconds = int(raw_value)+ except ValueError:+ logger.warning(+ "%s must be a positive integer number of seconds, got %r; using %s",+ CCR_TTL_SECONDS_ENV,+ raw_value,+ DEFAULT_CCR_TTL_SECONDS,+ )+ return DEFAULT_CCR_TTL_SECONDS++ if ttl_seconds <= 0:+ logger.warning(+ "%s must be greater than 0, got %s; using %s",+ CCR_TTL_SECONDS_ENV,+ ttl_seconds,+ DEFAULT_CCR_TTL_SECONDS,+ )+ return DEFAULT_CCR_TTL_SECONDS++ return ttl_seconds+++def format_retrieval_miss_detail(status: dict[str, Any]) -> str:+ """Return an operator-facing miss reason for CCR retrieval failures."""+ default_ttl = status.get("default_ttl_seconds", DEFAULT_CCR_TTL_SECONDS)+ ttl_seconds = status.get("ttl_seconds", default_ttl)++ if status.get("status") == "expired":+ age_seconds = status.get("age_seconds")+ if isinstance(age_seconds, (int, float)):+ return f"Entry expired (CCR TTL: {ttl_seconds} seconds; age: {age_seconds:.0f} seconds)"+ return f"Entry expired (CCR TTL: {ttl_seconds} seconds)"++ return f"Entry not found (CCR TTL: {default_ttl} seconds)"++def _redact_retrieval_log_payload(payload: str) -> str:redacted = _SECRET_KEY_VALUE_RE.sub(r"\1\2\3[REDACTED]", payload)redacted = _AUTH_VALUE_RE.sub(r"\1 [REDACTED]", redacted)@@ -95,7 +140,7 @@ class CompressionEntry:tool_call_id: str | Nonequery_context: str | Nonecreated_at: float- ttl: int = 300 # 5 minutes default+ ttl: int = DEFAULT_CCR_TTL_SECONDS# TOIN integration: Store the tool signature hash for retrieval correlation# This MUST match the hash used by SmartCrusher when recording compression@@ -146,7 +191,7 @@ class CompressionStore:Design principles:- Zero external dependencies (pure Python)- Thread-safe for concurrent access- - TTL-based expiration (default 5 minutes)+ - TTL-based expiration (default 300 seconds, env-configurable)- LRU-style eviction when capacity is reached- Built-in BM25 search for filtering"""@@ -154,7 +199,7 @@ class CompressionStore:def __init__(self,max_entries: int = 1000,- default_ttl: int = 300,+ default_ttl: int = DEFAULT_CCR_TTL_SECONDS,enable_feedback: bool = True,backend: CompressionStoreBackend | None = None,):@@ -162,7 +207,7 @@ class CompressionStore:Args:max_entries: Maximum number of entries to store.- default_ttl: Default TTL in seconds (5 minutes).+ default_ttl: Default TTL in seconds.enable_feedback: Whether to track retrieval events.backend: Storage backend to use. Defaults to InMemoryBackend.Custom backends can be passed for persistence (MongoDB, Redis).@@ -192,6 +237,11 @@ class CompressionStore:# BM25 scorer for searchself._scorer = BM25Scorer()+ @property+ def default_ttl_seconds(self) -> int:+ """Default TTL applied to new entries when callers do not override it."""+ return self._default_ttl+def store(self,original: str,@@ -730,6 +780,42 @@ class CompressionStore:return Falsereturn True+ def get_entry_status(+ self,+ hash_key: str,+ *,+ clean_expired: bool = False,+ ) -> dict[str, Any]:+ """Return availability and TTL metadata for a stored entry."""+ now = time.time()+ with self._lock:+ entry = self._backend.get(hash_key)+ if entry is None:+ return {+ "hash": hash_key,+ "status": "missing",+ "default_ttl_seconds": self._default_ttl,+ }++ age_seconds = now - entry.created_at+ expires_at = entry.created_at + entry.ttl+ expired = age_seconds > entry.ttl+ status = {+ "hash": hash_key,+ "status": "expired" if expired else "available",+ "ttl_seconds": entry.ttl,+ "default_ttl_seconds": self._default_ttl,+ "created_at": entry.created_at,+ "expires_at": expires_at,+ "age_seconds": age_seconds,+ }++ if expired and clean_expired:+ self._backend.delete(hash_key)+ self._stale_heap_entries += 1++ return status+def get_stats(self) -> dict[str, Any]:"""Get store statistics for monitoring."""with self._lock:@@ -748,6 +834,7 @@ class CompressionStore:return {"entry_count": self._backend.count(),"max_entries": self._max_entries,+ "default_ttl_seconds": self._default_ttl,"total_original_tokens": total_original_tokens,"total_compressed_tokens": total_compressed_tokens,"total_retrievals": total_retrievals,@@ -1134,7 +1221,7 @@ def _create_default_ccr_backend() -> CompressionStoreBackend | None:def get_compression_store(max_entries: int = 1000,- default_ttl: int = 300,+ default_ttl: int | None = None,backend: CompressionStoreBackend | None = None,) -> CompressionStore:"""Get the compression store instance.@@ -1146,6 +1233,7 @@ def get_compression_store(Args:max_entries: Maximum entries (only used on first call for global store).default_ttl: Default TTL (only used on first call for global store).+ When omitted, HEADROOM_CCR_TTL_SECONDS overrides the 300-second default.backend: Custom storage backend (only used on first call for global store).Defaults to InMemoryBackend if not provided; env backend used if backend is None.@@ -1162,9 +1250,12 @@ def get_compression_store(if _compression_store is None:if backend is None:backend = _create_default_ccr_backend()+ effective_default_ttl = (+ default_ttl if default_ttl is not None else _get_env_default_ttl_seconds()+ )_compression_store = CompressionStore(max_entries=max_entries,- default_ttl=default_ttl,+ default_ttl=effective_default_ttl,backend=backend,)return _compression_storediff --git a/headroom/ccr/mcp_server.py b/headroom/ccr/mcp_server.pyindex 806846eb8..a9fe25dfd 100644--- a/headroom/ccr/mcp_server.py+++ b/headroom/ccr/mcp_server.py@@ -449,7 +449,7 @@ class HeadroomMCPServer:"error": "Content not found. It may have expired or the hash may be incorrect.","hash": hash_key,"hint": "Content compressed via headroom_compress is stored for the session. "- "Content compressed by the proxy has a shorter TTL (5 minutes).",+ "Content compressed by the proxy uses the configured CCR TTL.",}async def _retrieve_via_proxy(diff --git a/headroom/ccr/response_handler.py b/headroom/ccr/response_handler.pyindex 00e33ce16..9aabebf6e 100644--- a/headroom/ccr/response_handler.py+++ b/headroom/ccr/response_handler.py@@ -19,7 +19,7 @@ from collections.abc import Awaitable, Callablefrom dataclasses import dataclass, fieldfrom typing import Any-from ..cache.compression_store import get_compression_store+from ..cache.compression_store import format_retrieval_miss_detail, get_compression_storefrom .tool_injection import CCR_TOOL_NAME, parse_tool_calllogger = logging.getLogger(__name__)@@ -202,6 +202,28 @@ class CCRResponseHandler:store = get_compression_store()try:+ get_status = getattr(store, "get_entry_status", None)+ entry_status = (+ get_status(ccr_call.hash_key, clean_expired=True) if callable(get_status) else None+ )+ if entry_status is not None and entry_status["status"] != "available":+ content = json.dumps(+ {+ "error": format_retrieval_miss_detail(entry_status),+ "hash": ccr_call.hash_key,+ "status": entry_status["status"],+ "ttl_seconds": entry_status.get(+ "ttl_seconds", entry_status["default_ttl_seconds"]+ ),+ },+ indent=2,+ )+ return CCRToolResult(+ tool_call_id=ccr_call.tool_call_id,+ content=content,+ success=False,+ )+if ccr_call.query:# Search within compressed contentresults = store.search(ccr_call.hash_key, ccr_call.query)@@ -241,10 +263,17 @@ class CCRResponseHandler:was_search=False,)else:+ miss_status = (+ get_status(ccr_call.hash_key, clean_expired=True)+ if callable(get_status)+ else {"hash": ccr_call.hash_key, "status": "missing"}+ )content = json.dumps({- "error": "Entry not found or expired (TTL: 5 minutes)",+ "error": format_retrieval_miss_detail(miss_status),"hash": ccr_call.hash_key,+ "status": miss_status["status"],+ "ttl_seconds": miss_status.get("ttl_seconds"),},indent=2,)diff --git a/headroom/config.py b/headroom/config.pyindex 7079b0ebd..daa3615ad 100644--- a/headroom/config.py+++ b/headroom/config.py@@ -407,14 +407,14 @@ class CCRConfig:- Network effect: retrieval patterns improve compression for all usersGOTCHAS:- - Cache has TTL (default 5 min) - retrieval fails after expiration+ - Cache has TTL (default 300 seconds) - retrieval fails after expiration- Memory usage: ~1KB per cached entry- Only works with array compression (not string truncation)"""enabled: bool = True # Enable CCR (cache + retrieval markers)store_max_entries: int = 1000 # Max entries in compression store- store_ttl_seconds: int = 300 # Cache TTL (5 minutes)+ store_ttl_seconds: int = 300 # Cache TTL in secondsinject_retrieval_marker: bool = True # Add retrieval hint to compressed outputfeedback_enabled: bool = True # Track retrieval events for learningmin_items_to_cache: int = 20 # Only cache if original had >= N itemsdiff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex 414828212..f7b954474 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -62,7 +62,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent))from headroom._version import __version__from headroom.cache.compression_feedback import get_compression_feedback-from headroom.cache.compression_store import get_compression_store+from headroom.cache.compression_store import format_retrieval_miss_detail, get_compression_storefrom headroom.ccr import (CCR_TOOL_NAME,# Batch processing@@ -2506,11 +2506,14 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:store = get_compression_store()+ entry_status = store.get_entry_status(hash_key, clean_expired=True)+ if entry_status["status"] != "available":+ raise HTTPException(+ status_code=404,+ detail=format_retrieval_miss_detail(entry_status),+ )+if query:- if not store.exists(hash_key, clean_expired=True):- raise HTTPException(- status_code=404, detail="Entry not found or expired (TTL: 5 minutes)"- )# Search within cached contentresults = store.search(hash_key, query)return {@@ -2533,7 +2536,10 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:"retrieval_count": entry.retrieval_count,}raise HTTPException(- status_code=404, detail="Entry not found or expired (TTL: 5 minutes)"+ status_code=404,+ detail=format_retrieval_miss_detail(+ store.get_entry_status(hash_key, clean_expired=True)+ ),)@app.get("/v1/retrieve/stats")@@ -2819,10 +2825,15 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:async def ccr_retrieve_get(hash_key: str, query: str | None = None):"""GET version of CCR retrieve for easier testing."""store = get_compression_store()+ entry_status = store.get_entry_status(hash_key, clean_expired=True)++ if entry_status["status"] != "available":+ raise HTTPException(+ status_code=404,+ detail=format_retrieval_miss_detail(entry_status),+ )if query:- if not store.exists(hash_key, clean_expired=True):- raise HTTPException(status_code=404, detail="Entry not found or expired")results = store.search(hash_key, query)return {"hash": hash_key,@@ -2842,7 +2853,12 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:"tool_name": entry.tool_name,"retrieval_count": entry.retrieval_count,}- raise HTTPException(status_code=404, detail="Entry not found or expired")+ raise HTTPException(+ status_code=404,+ detail=format_retrieval_miss_detail(+ store.get_entry_status(hash_key, clean_expired=True)+ ),+ )# CCR Tool Call Handler - for agent frameworks to call when LLM uses headroom_retrieve@app.post("/v1/retrieve/tool_call")@@ -2896,8 +2912,16 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:# Perform retrievalstore = get_compression_store()+ entry_status = store.get_entry_status(hash_key, clean_expired=True)- if query:+ if entry_status["status"] != "available":+ retrieval_data = {+ "error": format_retrieval_miss_detail(entry_status),+ "hash": hash_key,+ "status": entry_status["status"],+ "ttl_seconds": entry_status.get("ttl_seconds", entry_status["default_ttl_seconds"]),+ }+ elif query:results = store.search(hash_key, query)retrieval_data = {"hash": hash_key,@@ -2915,9 +2939,14 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:"compressed_item_count": entry.compressed_item_count,}else:+ miss_status = store.get_entry_status(hash_key, clean_expired=True)retrieval_data = {- "error": "Entry not found or expired (TTL: 5 minutes)",+ "error": format_retrieval_miss_detail(miss_status),"hash": hash_key,+ "status": miss_status["status"],+ "ttl_seconds": miss_status.get(+ "ttl_seconds", miss_status["default_ttl_seconds"]+ ),}# Format tool result for providerdiff --git a/tests/test_compression_store.py b/tests/test_compression_store.pyindex 38b95cb60..699b052ff 100644--- a/tests/test_compression_store.py+++ b/tests/test_compression_store.py@@ -27,6 +27,8 @@ from unittest.mock import MagicMock, patchimport pytestfrom headroom.cache.compression_store import (+ CCR_TTL_SECONDS_ENV,+ DEFAULT_CCR_TTL_SECONDS,CompressionEntry,CompressionStore,RetrievalEvent,@@ -128,6 +130,58 @@ def test_search_logs_retrieved_payload_preview():assert events[0]["payload_truncated"] is False+def test_global_store_uses_env_default_ttl(monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setenv(CCR_TTL_SECONDS_ENV, "7200")++ store = get_compression_store()+ hash_key = store.store(original="long-running agent payload", compressed="payload")+ entry = store.retrieve(hash_key)++ assert entry is not None+ assert entry.ttl == 7200+ assert store.get_stats()["default_ttl_seconds"] == 7200+++def test_global_store_invalid_env_ttl_falls_back(monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setenv(CCR_TTL_SECONDS_ENV, "0")++ store = get_compression_store()+ hash_key = store.store(original="payload", compressed="payload")+ entry = store.retrieve(hash_key)++ assert entry is not None+ assert entry.ttl == DEFAULT_CCR_TTL_SECONDS+ assert store.get_stats()["default_ttl_seconds"] == DEFAULT_CCR_TTL_SECONDS+++def test_explicit_global_store_ttl_overrides_env(monkeypatch: pytest.MonkeyPatch):+ monkeypatch.setenv(CCR_TTL_SECONDS_ENV, "7200")++ store = get_compression_store(default_ttl=60)+ hash_key = store.store(original="payload", compressed="payload")+ entry = store.retrieve(hash_key)++ assert entry is not None+ assert entry.ttl == 60+ assert store.get_stats()["default_ttl_seconds"] == 60+++def test_entry_status_reports_expiration_metadata():+ store = CompressionStore(default_ttl=1)++ with patch("headroom.cache.compression_store.time.time", return_value=1000.0):+ hash_key = store.store(original="payload", compressed="payload")++ with patch("headroom.cache.compression_store.time.time", return_value=1002.0):+ status = store.get_entry_status(hash_key, clean_expired=True)++ assert status["status"] == "expired"+ assert status["ttl_seconds"] == 1+ assert status["age_seconds"] == 2+ assert status["expires_at"] == 1001.0+ assert store.exists(hash_key) is False++# =============================================================================# Fixtures# =============================================================================diff --git a/tests/test_proxy_ccr.py b/tests/test_proxy_ccr.pyindex 2daf6d71b..dc86fafa9 100644--- a/tests/test_proxy_ccr.py+++ b/tests/test_proxy_ccr.py@@ -4,6 +4,7 @@ These tests verify the /v1/retrieve endpoints work correctly."""import json+from unittest.mock import patchimport pytest@@ -65,7 +66,23 @@ class TestCCRRetrieveEndpoint:"""Request with nonexistent hash should return 404."""response = client.post("/v1/retrieve", json={"hash": "nonexistent123"})assert response.status_code == 404- assert "not found or expired" in response.json()["detail"]+ assert "Entry not found" in response.json()["detail"]+ assert "CCR TTL: 300 seconds" in response.json()["detail"]++ def test_retrieve_expired_hash_reports_expiration_detail(self, client):+ """Expired entries report expiration separately from missing hashes."""+ store = get_compression_store(default_ttl=1)+ with patch("headroom.cache.compression_store.time.time", return_value=1000.0):+ hash_key = store.store(original="payload", compressed="payload")++ with patch("headroom.cache.compression_store.time.time", return_value=1002.0):+ response = client.post("/v1/retrieve", json={"hash": hash_key})++ assert response.status_code == 404+ detail = response.json()["detail"]+ assert "Entry expired" in detail+ assert "CCR TTL: 1 seconds" in detail+ assert "age: 2 seconds" in detaildef test_retrieve_full_content(self, client):"""Full retrieval returns original content."""@@ -246,8 +263,19 @@ class TestCCRStatsEndpoint:data = response.json()assert "store" in dataassert data["store"]["entry_count"] == 0+ assert data["store"]["default_ttl_seconds"] == 300assert "recent_retrievals" in data+ def test_stats_exposes_env_configured_ttl(self, client, monkeypatch):+ """Stats expose the effective CCR TTL configured through env."""+ reset_compression_store()+ monkeypatch.setenv("HEADROOM_CCR_TTL_SECONDS", "7200")++ response = client.get("/v1/retrieve/stats")++ assert response.status_code == 200+ assert response.json()["store"]["default_ttl_seconds"] == 7200+def test_stats_with_entries(self, client):"""Stats reflect store contents."""store = get_compression_store()