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/kompress-remote): keep compress fail-open on malformed 200 (#2320)

Abhay Singhb75999017fc0
diff --git a/headroom/transforms/kompress_remote.py b/headroom/transforms/kompress_remote.py
index 6c335e5d3..8615e613a 100644
--- a/headroom/transforms/kompress_remote.py
+++ b/headroom/transforms/kompress_remote.py
@@ -102,19 +102,23 @@ class RemoteKompressCompressor:
compressed = data["compressed"]
if not isinstance(compressed, str):
raise TypeError("remote Kompress response field 'compressed' must be a string")
+ # Coerce the numeric/string metadata fields inside the fail-open guard.
+ # A 200 response with a malformed field (e.g. a non-numeric string, or
+ # an explicit JSON null: data.get returns None for a present key, and
+ # float(None)/int(None) raise) would otherwise escape uncaught and break
+ # the proxy request, defeating the fail-open contract this class promises.
+ result = KompressResult(
+ compressed=compressed,
+ original=content,
+ original_tokens=int(data.get("original_tokens", n_words)),
+ compressed_tokens=int(data.get("compressed_tokens", len(compressed.split()))),
+ compression_ratio=float(data.get("compression_ratio", 1.0)),
+ model_used=str(data.get("model_used", self.config.model_id)),
+ )
except Exception as e: # fail OPEN — never break the proxy on a bad endpoint
logger.warning("Remote Kompress failed (%s); passing through", e)
return self._passthrough(content, n_words)
- result = KompressResult(
- compressed=compressed,
- original=content,
- original_tokens=int(data.get("original_tokens", n_words)),
- compressed_tokens=int(data.get("compressed_tokens", len(compressed.split()))),
- compression_ratio=float(data.get("compression_ratio", 1.0)),
- model_used=str(data.get("model_used", self.config.model_id)),
- )
-
# CCR stays PROXY-LOCAL: endpoint is stateless (enable_ccr=False), so we
# store the mapping + append the retrieval marker here — same policy and
# marker format as KompressCompressor.compress.
diff --git a/tests/test_transforms/test_kompress_remote.py b/tests/test_transforms/test_kompress_remote.py
index f7d81cc4b..234a19962 100644
--- a/tests/test_transforms/test_kompress_remote.py
+++ b/tests/test_transforms/test_kompress_remote.py
@@ -96,6 +96,49 @@ def test_remote_kompress_malformed_success_fails_open() -> None:
assert result.compression_ratio == 1.0
+def test_remote_kompress_null_numeric_field_fails_open() -> None:
+ # A 200 response with a valid 'compressed' but a malformed numeric field
+ # (here an explicit JSON null) must still fail open, not raise. data.get
+ # returns None for a present key, so float(None) would blow up if the
+ # coercions were outside the fail-open guard.
+ content = _long_text()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={"compressed": "short result", "compression_ratio": None},
+ )
+
+ compressor = _compressor(httpx.MockTransport(handler))
+ try:
+ result = compressor.compress(content)
+ finally:
+ compressor.close()
+
+ assert result.compressed == content
+ assert result.compression_ratio == 1.0
+
+
+def test_remote_kompress_non_numeric_field_fails_open() -> None:
+ # A non-numeric string in a numeric field is also a malformed response.
+ content = _long_text()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={"compressed": "short result", "original_tokens": "lots"},
+ )
+
+ compressor = _compressor(httpx.MockTransport(handler))
+ try:
+ result = compressor.compress(content)
+ finally:
+ compressor.close()
+
+ assert result.compressed == content
+ assert result.compression_ratio == 1.0
+
+
def test_content_router_selects_remote_kompress_from_env(monkeypatch) -> None:
monkeypatch.setenv("HEADROOM_KOMPRESS_ENDPOINT", "https://kompress.example")
monkeypatch.setenv("HEADROOM_KOMPRESS_ENDPOINT_TOKEN", "secret")