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(router): preserve nested JSON records through compression (#3680)

iuiuiufd067033da18
diff --git a/headroom/transforms/dense_line_elider.py b/headroom/transforms/dense_line_elider.py
index 3fd2cd6a6..7d61439c3 100644
--- a/headroom/transforms/dense_line_elider.py
+++ b/headroom/transforms/dense_line_elider.py
@@ -42,7 +42,14 @@ def is_dense_line(line: str) -> bool:
if n < MIN_LINE_CHARS or "\t" in line or (line.count(" ") / n) >= MAX_SPACE_RATIO:
return False
stripped = line.strip()
- return not (stripped[:1] in "{[" and stripped[-1:] in "}]")
+ if stripped[:1] in "{[" and stripped[-1:] in "}]":
+ return False
+ # A label can precede compact JSON. Eliding that line would silently
+ # rewrite its values even when the JSON record guard skipped Kompress.
+ from .recursive_json import scan_json_documents
+
+ spans, complete = scan_json_documents(line)
+ return complete and not spans
def elide_dense_lines(text: str) -> tuple[str, int]:
diff --git a/headroom/transforms/recursive_json.py b/headroom/transforms/recursive_json.py
index eb29b9e8c..539536b85 100644
--- a/headroom/transforms/recursive_json.py
+++ b/headroom/transforms/recursive_json.py
@@ -119,7 +119,7 @@ def _scan_from(text: str, start: int, known: dict[int, int | None]) -> tuple[int
return None, n - start
-def _scan_spans(text: str) -> tuple[list[tuple[int, int]], bool]:
+def _scan_spans(text: str, *, include_nested: bool = False) -> tuple[list[tuple[int, int]], bool]:
"""``(spans, complete)``: the top-level balanced spans ``_spans`` returns, and
whether the walk covered the whole text within its budget.
@@ -148,6 +148,14 @@ def _scan_spans(text: str) -> tuple[list[tuple[int, int]], bool]:
i = end
continue
i += 1
+ if include_nested:
+ # The walk already records every nested bracket's verdict. Reuse it
+ # in source order without scanning any suffix again.
+ out = []
+ for start in range(n):
+ end = known.get(start)
+ if end is not None:
+ out.append((start, end))
return out, True
@@ -167,19 +175,33 @@ def json_document_spans(text: str) -> list[tuple[int, int]]:
def scan_json_documents(text: str) -> tuple[list[tuple[int, int]], bool]:
"""``json_document_spans`` plus whether the scan covered all of ``text``.
- ``complete`` is only ever False past the scan budget, i.e. on input built to
- defeat the linear walk; a caller that must not miss a document treats that
- as "may contain one".
+ ``complete`` is False when the bracket walk or cumulative JSON validation
+ exhausts its work budget, or the decoder reaches its recursion limit.
+ A caller that must not miss a document treats any incomplete result as
+ "may contain one" and declines lossy compression.
"""
- spans, complete = _scan_spans(text)
+ spans, complete = _scan_spans(text, include_nested=True)
out: list[tuple[int, int]] = []
+ covered_until = 0
+ parsed_chars = 0
+ parse_budget = _SCAN_BUDGET_PER_CHAR * len(text) + _SCAN_BUDGET_FLOOR
for a, b in spans:
+ if a < covered_until:
+ continue
+ # Invalid wrappers may contain valid JSON. Checking each nested
+ # candidate must not turn deeply nested malformed input quadratic.
+ if parsed_chars + b - a > parse_budget:
+ return out, False
+ parsed_chars += b - a
try:
parsed = json.loads(text[a:b])
- except (ValueError, TypeError, RecursionError):
+ except RecursionError:
+ return out, False
+ except (ValueError, TypeError):
continue
if isinstance(parsed, dict | list):
out.append((a, b))
+ covered_until = b
return out, complete
diff --git a/tests/test_json_protection_boundaries.py b/tests/test_json_protection_boundaries.py
new file mode 100644
index 000000000..459077a78
--- /dev/null
+++ b/tests/test_json_protection_boundaries.py
@@ -0,0 +1,52 @@
+from types import SimpleNamespace
+
+import pytest
+
+from headroom.transforms.content_router import ContentRouter, ContentRouterConfig
+from headroom.transforms.dense_line_elider import elide_dense_lines
+from headroom.transforms.recursive_json import json_document_spans, scan_json_documents
+
+
[email protected]("wrapper", ["[response: {}]", "{{response: {}}}"])
+def test_nested_record_document_survives_non_json_wrapper(monkeypatch, wrapper):
+ document = '{"items":[{"id":1},{"id":2}]}'
+ payload = wrapper.format(document)
+ calls = []
+
+ class Kompress:
+ def is_ready(self):
+ return True
+
+ def compress(self, content, **kwargs):
+ calls.append(content)
+ return SimpleNamespace(compressed="records deleted", compressed_tokens=2)
+
+ router = ContentRouter(ContentRouterConfig())
+ monkeypatch.setattr(router, "_get_kompress", lambda: Kompress())
+ out, _ = router._try_ml_compressor(payload, context="")
+ assert out == payload
+ assert calls == []
+ assert [payload[a:b] for a, b in json_document_spans(payload)] == [document]
+
+
+def test_dense_prefixed_json_retains_its_value():
+ payload = 'Tool result: {"token":"' + "x" * 3500 + '"}'
+ assert elide_dense_lines(payload) == (payload, 0)
+
+
+def test_invalid_wrapper_parsing_has_a_linear_character_budget(monkeypatch):
+ import headroom.transforms.recursive_json as recursive_json
+
+ text = "[" * 500 + "response: " + '{"items":[{"id":1},{"id":2}]}' + "]" * 500
+ parsed_chars = 0
+ real_loads = recursive_json.json.loads
+
+ def count_loads(value, *args, **kwargs):
+ nonlocal parsed_chars
+ parsed_chars += len(value)
+ return real_loads(value, *args, **kwargs)
+
+ monkeypatch.setattr(recursive_json.json, "loads", count_loads)
+ _spans, complete = scan_json_documents(text)
+ assert not complete
+ assert parsed_chars <= 4 * len(text) + 4096