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(proxy): report read maturation as inert in cache mode (#4198)
M Barryba824539d6f8
diff --git a/headroom/proxy/modes.py b/headroom/proxy/modes.pyindex 1b0e61bbd..5421bc7d4 100644--- a/headroom/proxy/modes.py+++ b/headroom/proxy/modes.py@@ -36,3 +36,17 @@ def is_token_mode(mode: str | None) -> bool:def is_cache_mode(mode: str | None) -> bool:"""Return True when mode resolves to cache mode."""return normalize_proxy_mode(mode) == PROXY_MODE_CACHE+++def read_maturation_inert_reason(+ mode: str | None, *, quiesce_turns: int, max_hold_turns: int+) -> str | None:+ """Why read maturation can never fire under ``mode``, else None.++ Cache mode freezes every turn but the latest, and maturation skips frozen+ messages. A Read in the latest turn has had no quiet turns yet, so it is+ held unless a threshold is 0; by the time it could quiesce it is frozen.+ """+ if is_cache_mode(mode) and quiesce_turns > 0 and max_hold_turns > 0:+ return "cache mode freezes a Read before it can quiesce"+ return Nonediff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex 51cb6dd73..fa111f9f9 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -163,6 +163,7 @@ from headroom.proxy.modes import (PROXY_MODE_TOKEN,is_token_mode,normalize_proxy_mode,+ read_maturation_inert_reason,)from headroom.proxy.orphan_watchdog import (orphan_grace_seconds,@@ -2092,6 +2093,25 @@ class HeadroomProxy(if self.config.mode == PROXY_MODE_CACHE:logger.info(" Prefix freeze: strict (all prior turns immutable)")logger.info(" Mutations: latest turn only")+ if self.config.read_maturation:+ _inert = read_maturation_inert_reason(+ self.config.mode,+ quiesce_turns=self.config.read_maturation_quiesce_turns,+ max_hold_turns=self.config.read_maturation_max_hold_turns,+ )+ if _inert:+ logger.warning(+ "Read maturation: INERT (%s); use --mode token or disable read_maturation",+ _inert,+ )+ else:+ logger.info(+ "Read maturation: ENABLED (quiesce_turns=%d max_hold_turns=%d "+ "min_size_bytes=%d)",+ self.config.read_maturation_quiesce_turns,+ self.config.read_maturation_max_hold_turns,+ self.config.read_maturation_min_size_bytes,+ )# Effective compression posture, resolved (not merely requested). The# savings profile reaches the router through two paths — the config# object and the seeded process env — and `setdefault` semantics mean a@@ -5387,6 +5407,25 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:# Effective state from the running process. This is the supported# black-box provenance surface for benchmark/qualification tools."rollout": proxy.config.rollout.to_dict() if proxy.config.rollout else None,+ "read_maturation": _read_maturation_status(proxy.config),+ }++ def _read_maturation_status(cfg: ProxyConfig) -> dict[str, Any]:+ # The rollout snapshot says whether the feature is enabled, not whether+ # it can act; cache mode leaves it enabled but unable to fire.+ inert = (+ read_maturation_inert_reason(+ cfg.mode,+ quiesce_turns=cfg.read_maturation_quiesce_turns,+ max_hold_turns=cfg.read_maturation_max_hold_turns,+ )+ if cfg.read_maturation+ else None+ )+ return {+ "enabled": cfg.read_maturation,+ "effective": cfg.read_maturation and inert is None,+ "inert_reason": inert,}def _dashboard_config_payload() -> dict[str, Any]:diff --git a/tests/test_proxy_stats_recent_requests.py b/tests/test_proxy_stats_recent_requests.pyindex fcc20fe59..00057d4ec 100644--- a/tests/test_proxy_stats_recent_requests.py+++ b/tests/test_proxy_stats_recent_requests.py@@ -277,3 +277,50 @@ def test_stats_preserves_default_smart_crusher_compaction_state() -> None:assert response.status_code == 200assert response.json()["config"]["smart_crusher_with_compaction"] is None+++def _read_maturation_app(mode: str, quiesce_turns: int = 5):+ return create_app(+ ProxyConfig(+ mode=mode,+ read_maturation=True,+ read_maturation_quiesce_turns=quiesce_turns,+ optimize=False,+ cache_enabled=False,+ rate_limit_enabled=False,+ cost_tracking_enabled=False,+ log_requests=False,+ ccr_inject_tool=False,+ ccr_handle_responses=False,+ ccr_context_tracking=False,+ http2=False,+ )+ )+++ ("mode", "quiesce_turns", "effective", "startup_level"),+ [+ ("cache", 5, False, "WARNING"),+ ("token", 5, True, "INFO"),+ # A zero threshold matures a Read in the still-mutable latest turn.+ ("cache", 0, True, "INFO"),+ ],+)+def test_read_maturation_reports_whether_it_can_fire(+ mode, quiesce_turns, effective, startup_level, caplog+):+ """Cache mode freezes a Read before it can quiesce, so enabled maturation+ never fires there; startup and /stats must say so instead of reporting a+ silently inert feature as on."""+ app = _read_maturation_app(mode, quiesce_turns)++ with caplog.at_level("INFO", logger="headroom.proxy"):+ with TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 12345)) as client:+ payload = client.get("/stats").json()["read_maturation"]++ assert payload["enabled"] is True+ assert payload["effective"] is effective+ assert (payload["inert_reason"] is None) is effective+ banner = [r for r in caplog.records if r.getMessage().startswith("Read maturation:")]+ assert [r.levelname for r in banner] == [startup_level]