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): use shared compression store (#875)
Devanshi Vyas249af6cc7b37
diff --git a/headroom/ccr/mcp_server.py b/headroom/ccr/mcp_server.pyindex a9fe25dfd..948e8bf28 100644--- a/headroom/ccr/mcp_server.py+++ b/headroom/ccr/mcp_server.py@@ -342,14 +342,17 @@ class HeadroomMCPServer:self._setup_handlers()def _get_local_store(self) -> Any:- """Get or create the local compression store (lazy init)."""+ """Get the shared compression store singleton (lazy init).++ Returns the same instance the proxy and response_handler use so+ retrieval can see content either side compressed in-process.+ Called with no args to keep one shared config; the compress path+ passes its own per-entry ``ttl`` at store time.+ """if self._local_store is None:- from headroom.cache.compression_store import CompressionStore+ from headroom.cache.compression_store import get_compression_store- self._local_store = CompressionStore(- max_entries=500,- default_ttl=MCP_SESSION_TTL,- )+ self._local_store = get_compression_store()return self._local_storedef _compress_content(self, content: str) -> dict[str, Any]:diff --git a/tests/test_ccr_mcp_server.py b/tests/test_ccr_mcp_server.pyindex 303ada944..d217593e2 100644--- a/tests/test_ccr_mcp_server.py+++ b/tests/test_ccr_mcp_server.py@@ -1,7 +1,14 @@from __future__ import annotations+import asyncioimport json+import pytest++from headroom.cache.compression_store import (+ get_compression_store,+ reset_compression_store,+)from headroom.ccr import mcp_server@@ -22,3 +29,39 @@ def test_shared_stats_work_without_fcntl(monkeypatch, tmp_path) -> None:events = mcp_server._read_shared_events(window_seconds=60)assert events == [{"type": "compress", "timestamp": 1000.0, "pid": 4242}]+++# --- Shared compression store wiring ---------------------------------------+# MCP's _get_local_store() must return the get_compression_store() singleton —+# the same instance the proxy and response_handler use — so content compressed+# on either side is retrievable in-process. These pin that wiring so a private+# store can't creep back.+++def fresh_store():+ reset_compression_store()+ yield+ reset_compression_store()+++def test_mcp_uses_shared_singleton_store(fresh_store) -> None:+ """MCP's store is the global singleton, not a private instance."""+ pytest.importorskip("mcp", reason="MCP SDK required")+ server = mcp_server.HeadroomMCPServer(check_proxy=False)+ assert server._get_local_store() is get_compression_store()+++def test_mcp_retrieves_proxy_stored_content(fresh_store) -> None:+ """Content stored via the singleton (as the proxy does) is retrievable+ through MCP's local-store path. The HTTP fallback is disabled so this+ passes only via the shared store."""+ pytest.importorskip("mcp", reason="MCP SDK required")+ original = '{"some": "original proxy-compressed content"}'+ hash_key = get_compression_store().store(original, '{"compressed": true}')++ server = mcp_server.HeadroomMCPServer(check_proxy=False)+ result = asyncio.run(server._retrieve_content(hash_key, query=None))++ assert result.get("source") == "local"+ assert result["original_content"] == original