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(transforms): keep the fences of a code block passed through for a protected tag (#4292)

Rudy Mizrahi Celekli02958c6ffe41
diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.py
index d8c4c6cd8..d4ec4da6f 100644
--- a/headroom/transforms/content_router.py
+++ b/headroom/transforms/content_router.py
@@ -3453,6 +3453,7 @@ class ContentRouter(Transform):
# Any section carrying a placeholder is passed through verbatim
# instead of ever entering a compressor.
placeholders = [placeholder for placeholder, _ in protected]
+ source_lines = sections_source.split("\n")
compressed_sections: list[str] = []
routing_log: list[RoutingDecision] = []
@@ -3460,7 +3461,13 @@ class ContentRouter(Transform):
for i, section in enumerate(sections):
if placeholders and any(ph in section.content for ph in placeholders):
section_tokens = _estimate_tokens(section.content)
- compressed_sections.append(section.content)
+ passthrough = section.content
+ # A fenced section's content is only the body. Emit its source
+ # lines instead, so the fence lines come back exactly as they
+ # were (and a fence left open stays open).
+ if section.is_code_fence:
+ passthrough = "\n".join(source_lines[section.start_line : section.end_line + 1])
+ compressed_sections.append(passthrough)
routing_log.append(
RoutingDecision(
content_type=section.content_type,
diff --git a/tests/test_content_router_mixed_fence_placeholder.py b/tests/test_content_router_mixed_fence_placeholder.py
new file mode 100644
index 000000000..0c7476351
--- /dev/null
+++ b/tests/test_content_router_mixed_fence_placeholder.py
@@ -0,0 +1,53 @@
+"""A fenced block passed through verbatim (it holds a protected tag) keeps its fences."""
+
+from __future__ import annotations
+
+from headroom.transforms.content_router import ContentRouter
+
+CONTENT = """Here is the Template file that we wrote for the project today.
+```xml
+<config-block>
+ <name>demo</name>
+</config-block>
+```
+Please Review this carefully and tell me what you think about it.
+The Second paragraph explains the Design choice that we made here.
+{"name": "demo", "version": 1, "debug": true}
+Then the Search results came back from the Second query run today.
+src/main.c:10: int main() {
+src/util.c:22: void helper() {
+"""
+
+
+def test_mixed_passthrough_of_protected_fenced_block_keeps_fence_lines():
+ result = ContentRouter()._compress_mixed(CONTENT, context="")
+
+ lines = result.compressed.splitlines()
+ assert "```xml" in lines
+ opening = lines.index("```xml")
+ assert lines[opening + 1 : opening + 5] == [
+ "<config-block>",
+ " <name>demo</name>",
+ "</config-block>",
+ "```",
+ ]
+
+
+UNCLOSED_BARE = """Here is the Template file that we wrote for the project today.
+Please Review this carefully and tell me what you think about it.
+{"name": "demo", "version": 1, "debug": true}
+Then the Search results came back from the Second query run today.
+src/main.c:10: int main() {
+```
+<config-block>
+ <name>demo</name>
+</config-block>"""
+
+
+def test_mixed_passthrough_keeps_an_unclosed_bare_fence_as_written():
+ """Passthrough is verbatim: no closing fence is invented and a bare fence stays bare."""
+ result = ContentRouter()._compress_mixed(UNCLOSED_BARE, context="")
+
+ lines = result.compressed.splitlines()
+ assert lines[-4:] == ["```", "<config-block>", " <name>demo</name>", "</config-block>"]
+ assert "```unknown" not in lines