Skip to content

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.

fix(code): keep multi-line class headers' opening brace (#4623)

X0d0cda22b407
diff --git a/headroom/transforms/code_compressor.py b/headroom/transforms/code_compressor.py
index 71bdb0b05..2483e6680 100644
--- a/headroom/transforms/code_compressor.py
+++ b/headroom/transforms/code_compressor.py
@@ -2142,7 +2142,17 @@ class CodeAwareCompressor(Transform):
if sig_end > 0:
header_lines = node_lines[:sig_end]
brace_line = node_lines[sig_end]
- if brace_line.strip().startswith("{"):
+ # Brace languages put the opening brace on the member container's
+ # start line. A multi-line signature can share that line with its
+ # last fragment (`impl<T> Foo<T> where T: Debug {`,
+ # `class Foo implements Bar {`), so keep the line whenever the
+ # container starts with the brace, not only when it opens the line.
+ brace_starts_line = brace_line.strip().startswith("{")
+ body_starts_with_brace = (
+ not lang_config.uses_colon_after_signature
+ and _slice_code_bytes(code, body_node.start_byte, body_node.start_byte + 1) == "{"
+ )
+ if brace_starts_line or body_starts_with_brace:
header_lines = [*header_lines, brace_line]
else:
header_lines = [node_lines[0]]
diff --git a/tests/test_transforms/test_code_compressor.py b/tests/test_transforms/test_code_compressor.py
index 4ac106c9c..131d68a97 100644
--- a/tests/test_transforms/test_code_compressor.py
+++ b/tests/test_transforms/test_code_compressor.py
@@ -1848,6 +1848,72 @@ class TestRealASTRuns:
assert "pub fn add(a: i64, b: i64) -> i64" in result.compressed
[email protected](not TREE_SITTER_INSTALLED, reason="tree-sitter grammar pack not installed")
+class TestMultiLineClassHeaders:
+ """Keep the opening brace when a multi-line signature shares its last
+ line with it (``impl<T> Foo<T> where T: Debug {``, ``class Foo
+ implements Bar {``). Dropping that line leaves the rebuilt header
+ without a brace, so the compressed output no longer parses and the
+ compressor falls back to the original.
+ """
+
+ def _compressor(self):
+ return CodeAwareCompressor(
+ CodeCompressorConfig(
+ min_tokens_for_compression=1,
+ max_body_lines=2,
+ enable_ccr=False,
+ semantic_analysis=False,
+ )
+ )
+
+ @pytest.mark.parametrize(
+ ("language", "code", "expected_header"),
+ [
+ (
+ "rust",
+ (
+ "impl<T> Wrapper<T>\n"
+ "where T: Debug + Clone {\n"
+ " pub fn total(&self) -> u64 {\n"
+ " let mut acc = 0;\n"
+ " for item in &self.items {\n"
+ " acc += item.id;\n"
+ " }\n"
+ " acc\n"
+ " }\n"
+ "}\n"
+ ),
+ "where T: Debug + Clone {",
+ ),
+ (
+ "java",
+ (
+ "public class Calc\n"
+ " extends Base\n"
+ " implements Runnable {\n"
+ " public int compute(int x) {\n"
+ " int a = x + 1;\n"
+ " int b = a * 2;\n"
+ " int c = b - 3;\n"
+ " return c;\n"
+ " }\n"
+ "}\n"
+ ),
+ "implements Runnable {",
+ ),
+ ],
+ )
+ def test_opening_brace_line_survives_compression(self, language, code, expected_header):
+ result = self._compressor().compress(code, language=language)
+
+ assert result.language == CodeLanguage(language)
+ # A ratio of 1.0 means the syntax check rejected the output and the
+ # compressor returned the original untouched.
+ assert result.compression_ratio < 1.0
+ assert expected_header in result.compressed
+
+
@pytest.mark.skipif(not TREE_SITTER_INSTALLED, reason="tree-sitter grammar pack not installed")
class TestCSharpSupport:
"""C# (``csharp`` grammar) parity with Java/C++/Rust: signatures preserved