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(transforms): honour Subscription toin_read_only when the router calls SmartCrusher (#4493)
Ryrenz7c11d462e6d8
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex c0c5c9304..276e6ad9a 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -360,9 +360,18 @@ def _invoke_smart_crusher(router: ContentRouter, inp: CompressInput) -> str | Nocrusher = router._get_smart_crusher()if crusher is None:return None+ # F2.2: the router never calls ``crusher.apply()``, so the crusher's own+ # policy field stays None. Forward the request's TOIN read-only gate per+ # call instead (the crusher is shared across concurrent requests). The+ # kwarg is only sent when gating, so crushers exposing the plain+ # ``crush(content, query, bias)`` signature keep working.+ policy = router._runtime_compression_policy+ gate: dict[str, bool] = {}+ if policy is not None and policy.toin_read_only:+ gate["record_toin"] = False# ``_get_*`` getters are typed ``Any``; pin the result to the contract type.compressed: str = crusher.crush(- inp.content, query=inp.query, bias=_adapter_bias(inp)+ inp.content, query=inp.query, bias=_adapter_bias(inp), **gate).compressedreturn compresseddiff --git a/headroom/transforms/smart_crusher.py b/headroom/transforms/smart_crusher.pyindex 8d57ba85b..2cd3db299 100644--- a/headroom/transforms/smart_crusher.py+++ b/headroom/transforms/smart_crusher.py@@ -497,6 +497,7 @@ class SmartCrusher(Transform):query: str = "",bias: float = 1.0,lossless_only: bool | None = None,+ record_toin: bool = True,) -> CrushResult:"""Crush a single JSON content string.@@ -510,6 +511,12 @@ class SmartCrusher(Transform):but any path that would need a CCR marker (row-drop oropaque-blob offload) leaves the content uncompacted instead.`None` (default) uses the instance's configured value.++ `record_toin=False` skips the TOIN write for this call only. The+ ContentRouter passes it when the request's ``CompressionPolicy``+ has ``toin_read_only=True`` (Subscription, F2.2): the crusher is+ shared across concurrent requests, so the gate has to travel with+ the call rather than through ``_runtime_compression_policy``."""# Web search tools often return space-separated JSON objects# (``{...} {...} {...}``) rather than a real array. The Rust crusher@@ -548,7 +555,7 @@ class SmartCrusher(Transform):# JSON re-canonicalization (whitespace normalization) without# actually compressing — the strategy stays `"passthrough"` in# that case, and there's no learning value in recording it.- if r.was_modified and r.strategy != "passthrough":+ if record_toin and r.was_modified and r.strategy != "passthrough":self._record_to_toin(original=content,compressed=r.compressed,diff --git a/tests/test_compression_policy_toin_gate.py b/tests/test_compression_policy_toin_gate.pyindex 6724fdc32..950a6421a 100644--- a/tests/test_compression_policy_toin_gate.py+++ b/tests/test_compression_policy_toin_gate.py@@ -287,3 +287,55 @@ def test_content_router_payg_records_to_toin(fresh_toin):assert post >= pre, ("PAYG must not be blocked by the F2.2 gate — write should happen or fall through naturally")+++# ─── ContentRouter → SmartCrusher.crush() (#4474) ───────────────────────+++@_skip_no_core+ ("mode", "expect_write"),+ [(AuthMode.SUBSCRIPTION, False), (AuthMode.PAYG, True)],+)+def test_content_router_smart_crusher_path_honours_toin_read_only(fresh_toin, mode, expect_write):+ """The router compresses JSON arrays via the shared crusher's+ ``crush()`` and never calls ``crusher.apply()``, so the crusher's+ own ``_runtime_compression_policy`` stays None. The router must+ forward the request's ``toin_read_only`` gate to that call.+ """+ from headroom.transforms.content_router import ContentRouter++ router = ContentRouter()+ messages = _wrap_in_tool_message(_bigger_array(60))+ pre = sum(p.total_compressions for p in fresh_toin._patterns.values())++ result = router.apply(messages, _tokenizer(), compression_policy=policy_for_mode(mode))++ assert any("smart_crusher" in t for t in result.transforms_applied), (+ "payload must route through SmartCrusher for this test to mean anything"+ )+ # The shared crusher's own field is untouched — the gate travels per call.+ assert router._get_smart_crusher()._runtime_compression_policy is None+ post = sum(p.total_compressions for p in fresh_toin._patterns.values())+ if expect_write:+ assert post > pre, "write-enabled policy must still feed TOIN via the router"+ else:+ assert post == pre, "Subscription MUST NOT write to TOIN via router → crush()"+++@_skip_no_core+def test_smart_crusher_crush_record_toin_false_skips_write(fresh_toin):+ """``crush(record_toin=False)`` compresses as usual but skips TOIN;+ the default keeps the legacy write-enabled behaviour."""+ from headroom.transforms.smart_crusher import SmartCrusher, SmartCrusherConfig++ crusher = SmartCrusher(SmartCrusherConfig())+ payload = _bigger_array(60)++ gated = crusher.crush(payload, record_toin=False)+ if not gated.was_modified or gated.strategy == "passthrough":+ pytest.skip("payload didn't trigger compression — bump the size")+ assert sum(p.total_compressions for p in fresh_toin._patterns.values()) == 0++ crusher.crush(payload)+ assert sum(p.total_compressions for p in fresh_toin._patterns.values()) > 0