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(code): preserve syntax in CCR retrieval comments (#4583)
Endidac8423965d97
diff --git a/headroom/transforms/code_compressor.py b/headroom/transforms/code_compressor.pyindex 734c2ccc6..71bdb0b05 100644--- a/headroom/transforms/code_compressor.py+++ b/headroom/transforms/code_compressor.py@@ -1510,8 +1510,9 @@ class CodeAwareCompressor(Transform):# Use the actual config attribute (not the wrong name)ttl_min = max(1, self.config.ccr_ttl // 60)+ comment_prefix = _LANG_CONFIGS[detected_lang].comment_prefixcompressed += (- f"\n# [{original_tokens - compressed_tokens} tokens compressed."+ f"\n{comment_prefix} [{original_tokens - compressed_tokens} tokens compressed."f"{summary_str}"f" Retrieve more: hash={cache_key}."f" Expires in {ttl_min}m.]"diff --git a/tests/test_transforms/test_code_compressor.py b/tests/test_transforms/test_code_compressor.pyindex c689f4976..4ac106c9c 100644--- a/tests/test_transforms/test_code_compressor.py+++ b/tests/test_transforms/test_code_compressor.py@@ -15,6 +15,11 @@ from unittest.mock import patchimport pytestimport headroom.transforms.code_compressor as cc+from headroom.cache.compression_store import (+ CompressionStore,+ clear_request_compression_store,+ set_request_compression_store,+)from headroom.transforms.code_compressor import (CodeAwareCompressor,CodeCompressionResult,@@ -210,6 +215,46 @@ def generate_go_code(n_functions: int = 3) -> str:return "\n".join(lines)[email protected](not TREE_SITTER_INSTALLED, reason="tree-sitter not installed")+ ("language", "code", "comment_prefix"),+ [+ pytest.param("python", generate_python_code(), "#", id="python"),+ pytest.param("javascript", generate_javascript_code(), "//", id="javascript"),+ pytest.param("typescript", generate_javascript_code(), "//", id="typescript"),+ pytest.param("go", generate_go_code(), "//", id="go"),+ ],+)+def test_ccr_marker_preserves_code_syntax(language, code, comment_prefix):+ """CCR metadata must be a comment in the final output, not just before storage."""+ store = CompressionStore()+ set_request_compression_store(store)+ try:+ compressor = CodeAwareCompressor(+ CodeCompressorConfig(+ min_tokens_for_compression=10,+ enable_ccr=True,+ fallback_to_kompress=False,+ )+ )+ result = compressor.compress(code, language=language)++ assert result.cache_key is not None+ assert result.syntax_valid+ # Parse the returned source, including the marker appended after the+ # compressor's internal syntax check (#4478, item 4).+ assert compressor._verify_syntax(result.compressed, CodeLanguage(language))+ marker = result.compressed.splitlines()[-1]+ assert marker.startswith(f"{comment_prefix} [")+ assert f"Retrieve more: hash={result.cache_key}." in marker+ assert "Expires in 5m.]" in marker+ entry = store.retrieve(result.cache_key)+ assert entry is not None+ assert entry.original_content == code+ finally:+ clear_request_compression_store()++# =============================================================================# TestCodeCompressorConfig# =============================================================================