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(router): keep tagged blocks out of the lossy size-gate fallback (#4543)
HetCreepf333edf20f0d
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex 87e06c4cc..e5905f0fb 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -5193,6 +5193,13 @@ class ContentRouter(Transform):self._kompress_max_tokens,self._kompress_gate_fires,)+ if protected:+ # The fallback compressors take no ``ccr_original`` and would+ # store the ``{{HEADROOM_TAG_N}}`` placeholder text in CCR; any+ # placeholder line they drop is also discarded by+ # ``restore_tags``, losing the tagged block outright. Pass the+ # original through unchanged instead (as the budget branch above).+ return content, _estimate_tokens(content)out = text_to_compresscrusher = self._get_text_crusher()if crusher is not None:@@ -5213,8 +5220,6 @@ class ContentRouter(Transform):"Kompress size-gate -> LogCompressor failed (%s); passing through", e)out = text_to_compress- if protected:- out = restore_tags(out, protected)return out, _estimate_tokens(out)# Reached only when the gate is enabled and this eligible block isdiff --git a/tests/test_transforms/test_kompress_size_gate.py b/tests/test_transforms/test_kompress_size_gate.pyindex 3c718401d..eb2344678 100644--- a/tests/test_transforms/test_kompress_size_gate.py+++ b/tests/test_transforms/test_kompress_size_gate.py@@ -74,3 +74,28 @@ def test_gate_fires_through_strategy_dispatch(monkeypatch, strategy):router._apply_strategy_to_content(big, strategy, "")assert router._kompress_gate_fires == 1+++def test_gate_keeps_tagged_blocks_out_of_lossy_fallback(monkeypatch):+ # Over the gate, with custom tags protected: the TextCrusher/LogCompressor+ # fallback must not see the {{HEADROOM_TAG_N}} placeholder text. It would+ # store the placeholders in CCR and ``restore_tags`` discards any wrap whose+ # placeholder line it dropped, so the tagged content would vanish from both+ # the output and the retrieval path.+ router = ContentRouter()+ router._kompress_max_tokens = 50 # tiny ceiling (x4 = 200 chars)++ def _boom():+ raise AssertionError("kompress must not be invoked for oversized (gated) input")++ monkeypatch.setattr(router, "_get_kompress", _boom)++ lines = [f"<item>row {i} value</item> note text here for item number {i}" for i in range(200)]+ text = "Results from the fetch tool:\n\n" + "\n".join(lines)++ out, _ = router._try_ml_compressor(text, "")++ assert router._kompress_gate_fires == 1+ assert out == text+ assert out.count("<item>") == 200+ assert "{{HEADROOM_TAG" not in out