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): compress class member containers (#1334)
Vinay Guptac35af858eaa5
diff --git a/headroom/transforms/code_compressor.py b/headroom/transforms/code_compressor.pyindex d762cd669..fe05e86b7 100644--- a/headroom/transforms/code_compressor.py+++ b/headroom/transforms/code_compressor.py@@ -257,6 +257,8 @@ class LangConfig:# Quick pre-filter hints for language detection (substrings to check)detection_hints: tuple[str, ...] = ()+ # Optional override for node types that contain class/impl members.+ class_body_node_types: frozenset[str] | None = None_LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {@@ -283,6 +285,7 @@ _LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {comment_prefix="//",uses_colon_after_signature=False,detection_hints=("function ", "const ", "let ", "var ", "export ", "require("),+ class_body_node_types=frozenset({"class_body"}),),CodeLanguage.TYPESCRIPT: LangConfig(import_nodes=frozenset({"import_statement", "import_declaration"}),@@ -294,6 +297,7 @@ _LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {comment_prefix="//",uses_colon_after_signature=False,detection_hints=("interface ", "type ", ": string", ": number", ": boolean"),+ class_body_node_types=frozenset({"class_body"}),),CodeLanguage.GO: LangConfig(import_nodes=frozenset({"import_declaration"}),@@ -317,6 +321,7 @@ _LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {comment_prefix="//",uses_colon_after_signature=False,detection_hints=("fn ", "struct ", "impl ", "mod ", "use "),+ class_body_node_types=frozenset({"declaration_list"}),),CodeLanguage.JAVA: LangConfig(import_nodes=frozenset({"import_declaration"}),@@ -329,6 +334,7 @@ _LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {uses_colon_after_signature=False,package_node="package_declaration",detection_hints=("public ", "private ", "protected ", "class ", "interface "),+ class_body_node_types=frozenset({"class_body"}),),CodeLanguage.C: LangConfig(import_nodes=frozenset({"preproc_include"}),@@ -351,6 +357,7 @@ _LANG_CONFIGS: dict[CodeLanguage, LangConfig] = {comment_prefix="//",uses_colon_after_signature=False,detection_hints=("#include", "namespace ", "class ", "::"),+ class_body_node_types=frozenset({"field_declaration_list"}),),CodeLanguage.PERL: LangConfig(import_nodes=frozenset({"use_statement", "use_version_statement"}),@@ -1315,6 +1322,11 @@ class CodeAwareCompressor(Transform):)structure.class_definitions.append(compressed)captured_byte_ranges.append((node.start_byte, node.end_byte))+ trailing_semicolon = _get_same_line_trailing_semicolon(node)+ if trailing_semicolon is not None:+ captured_byte_ranges.append(+ (trailing_semicolon.start_byte, trailing_semicolon.end_byte)+ )return# Type definitions@@ -1623,10 +1635,12 @@ class CodeAwareCompressor(Transform):node_lines = code_lines[start_row : end_row + 1]node_text = "\n".join(node_lines)- # Find the body node+ # Find the class/member container. For some languages this is not the+ # same node type as a function body's executable block.+ class_body_node_types = lang_config.class_body_node_types or lang_config.body_node_typesbody_node = Nonefor child in node.children:- if child.type in lang_config.body_node_types:+ if child.type in class_body_node_types:body_node = childbreak@@ -1644,6 +1658,9 @@ class CodeAwareCompressor(Transform):processed_ranges: list[tuple[int, int]] = []for child in body_node.children:+ if not child.is_named:+ continue+# Use line-based extraction for children toochild_start = child.start_point[0]child_end = child.end_point[0]@@ -1695,17 +1712,23 @@ class CodeAwareCompressor(Transform):for part in body_parts:result_parts.append(part)- # Handle closing brace for brace-delimited languages+ # Handle closing brace for brace-delimited languages. The class body+ # node ends at the brace, while C++ class_specifier excludes the+ # trailing semicolon; keeping only the body node span prevents a second+ # semicolon from being rendered later as top-level code.body_end_line = body_node.end_point[0]body_end_rel = body_end_line - node_start_line + 1after_lines = node_lines[body_end_rel:]- if after_lines:+ if not lang_config.uses_colon_after_signature:+ if body_end_line != start_row:+ closing_line = code_lines[body_end_line]+ closing_text = closing_line[: body_node.end_point[1]]+ if _get_same_line_trailing_semicolon(node) is not None:+ closing_text += ";"+ if closing_text.strip():+ result_parts.append(closing_text)+ elif after_lines:result_parts.extend(after_lines)- elif not lang_config.uses_colon_after_signature:- # Ensure closing brace- last_body_line = node_lines[-1] if node_lines else ""- if last_body_line.strip() == "}":- result_parts.append(last_body_line)return "\n".join(result_parts)@@ -1992,6 +2015,18 @@ def _get_node_text(node: Any, code: str) -> str:return code[node.start_byte : node.end_byte]+def _get_same_line_trailing_semicolon(node: Any) -> Any | None:+ """Return a trailing semicolon sibling that belongs to this declaration."""+ next_sibling = getattr(node, "next_sibling", None)+ if (+ next_sibling is not None+ and next_sibling.type == ";"+ and next_sibling.start_point[0] == node.end_point[0]+ ):+ return next_sibling+ return None++def _get_definition_name(node: Any) -> str | None:"""Extract the name identifier from a definition AST node."""for child in node.children:diff --git a/tests/test_transforms/test_code_compressor.py b/tests/test_transforms/test_code_compressor.pyindex a94a4a235..76de4e3ae 100644--- a/tests/test_transforms/test_code_compressor.py+++ b/tests/test_transforms/test_code_compressor.py@@ -770,6 +770,137 @@ class TestTreeSitterIntegration:assert result.language == CodeLanguage.GOassert result.compressed # Some output is produced+ @pytest.mark.parametrize(+ (+ "language",+ "code",+ "expected_signature",+ "expected_omitted_lines",+ "expected_removed_line",+ "expected_closing",+ ),+ [+ (+ "javascript",+ (+ "class Calc {\n"+ " compute(x) {\n"+ " let a = x + 1;\n"+ " let b = a * 2;\n"+ " let c = b - 3;\n"+ " return c;\n"+ " }\n"+ "}\n"+ ),+ "compute(x) {",+ 3,+ "return c;",+ "}\n}",+ ),+ (+ "typescript",+ (+ "class Calc {\n"+ " compute(x: number): number {\n"+ " let a = x + 1;\n"+ " let b = a * 2;\n"+ " let c = b - 3;\n"+ " return c;\n"+ " }\n"+ "}\n"+ ),+ "compute(x: number): number {",+ 3,+ "return c;",+ "}\n}",+ ),+ (+ "java",+ (+ "public class Calc {\n"+ " public int compute(int x) {\n"+ " int a = x + 1;\n"+ " int b = a * 2;\n"+ " int c = b - 3;\n"+ " int d = c / 4;\n"+ " int e = d + 5;\n"+ " return e;\n"+ " }\n"+ "}\n"+ ),+ "public int compute(int x) {",+ 5,+ "return e;",+ "}\n}",+ ),+ (+ "cpp",+ (+ "class Calc {\n"+ "public:\n"+ " int compute(int x) {\n"+ " int a = x + 1;\n"+ " int b = a * 2;\n"+ " int c = b - 3;\n"+ " int d = c / 4;\n"+ " int e = d + 5;\n"+ " return e;\n"+ " }\n"+ "};\n"+ ),+ "int compute(int x) {",+ 5,+ "return e;",+ "};",+ ),+ (+ "rust",+ (+ "impl Calc {\n"+ " pub fn compute(&self, x: i32) -> i32 {\n"+ " let a = x + 1;\n"+ " let b = a * 2;\n"+ " let c = b - 3;\n"+ " let d = c / 4;\n"+ " let e = d + 5;\n"+ " e\n"+ " }\n"+ "}\n"+ ),+ "pub fn compute(&self, x: i32) -> i32 {",+ 5,+ " e\n",+ "}\n}",+ ),+ ],+ )+ def test_compresses_methods_inside_class_member_containers(+ self,+ language,+ code,+ expected_signature,+ expected_omitted_lines,+ expected_removed_line,+ expected_closing,+ ):+ """Class/impl member containers are distinct from executable method bodies."""+ config = CodeCompressorConfig(+ min_tokens_for_compression=1,+ max_body_lines=1,+ enable_ccr=False,+ )+ compressor = CodeAwareCompressor(config)++ result = compressor.compress(code, language=language)++ assert result.language == CodeLanguage(language)+ assert result.syntax_valid is True+ assert result.compression_ratio < 1.0+ assert expected_signature in result.compressed+ assert f"// [{expected_omitted_lines} lines omitted]" in result.compressed+ assert expected_removed_line not in result.compressed+ assert result.compressed.endswith(expected_closing)+def test_imports_preserved(self):"""Imports are preserved in compressed output."""config = CodeCompressorConfig(