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(cache): keep indented lines intact in OpenAI whitespace normalization (#4343)

Abel Yagubyan248010091578
diff --git a/headroom/cache/openai.py b/headroom/cache/openai.py
index 22c3cd7a2..a10f83acc 100644
--- a/headroom/cache/openai.py
+++ b/headroom/cache/openai.py
@@ -421,9 +421,7 @@ class OpenAICacheOptimizer(BaseCacheOptimizer):
leading = len(line) - len(line.lstrip())
# Collapse multiple spaces in content (not indentation)
content_part = " ".join(stripped.split())
- normalized_lines.append(
- " " * leading + content_part[leading:] if leading else content_part
- )
+ normalized_lines.append(line[:leading] + content_part)
else:
normalized_lines.append("")
@@ -434,7 +432,9 @@ class OpenAICacheOptimizer(BaseCacheOptimizer):
while "\n\n\n" in result:
result = result.replace("\n\n\n", "\n\n")
- return result.strip()
+ # Lines are already right-stripped and blank lines are empty, so only
+ # surrounding newlines remain; keep the first line's indentation.
+ return result.strip("\n")
def _format_dynamic_section(
self,
diff --git a/tests/test_cache/test_openai.py b/tests/test_cache/test_openai.py
index 24559d502..42a6e223d 100644
--- a/tests/test_cache/test_openai.py
+++ b/tests/test_cache/test_openai.py
@@ -74,6 +74,37 @@ class TestOpenAICacheOptimizer:
system_content = result.messages[0]["content"]
assert " " not in system_content # Multiple spaces collapsed
+ def test_whitespace_normalization_keeps_indented_lines_intact(self, optimizer, context):
+ """Indented lines keep their indentation and their first characters (#4336)."""
+ code = "Example:\n def f():\n return x"
+ assert optimizer._normalize_whitespace(code) == code
+
+ tabbed = "Example:\n\tif ok:\n\t\treturn x"
+ assert optimizer._normalize_whitespace(tabbed) == "Example:\n\tif ok:\n\t\treturn x"
+
+ messages = [
+ {"role": "system", "content": code},
+ {"role": "user", "content": "Hello!"},
+ ]
+ result = optimizer.optimize(messages, context)
+ assert result.messages[0]["content"] == code
+
+ def test_whitespace_normalization_keeps_first_line_indentation(self, optimizer, context):
+ """A prompt that starts with an indented line keeps that indentation."""
+ code = " if ready:\n run()"
+ assert optimizer._normalize_whitespace(code) == code
+
+ # Surrounding blank lines and trailing whitespace are still trimmed.
+ padded = "\n \n if ready:\n run() \n\n"
+ assert optimizer._normalize_whitespace(padded) == code
+
+ messages = [
+ {"role": "system", "content": code},
+ {"role": "user", "content": "Hello!"},
+ ]
+ result = optimizer.optimize(messages, context)
+ assert result.messages[0]["content"] == code
+
def test_optimize_disabled(self, context):
"""Test optimization when disabled."""
config = CacheConfig(enabled=False)