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(mcp): report correct savings_percent in headroom_compress (#1106)

Kessy Similienf216e4305597
diff --git a/headroom/ccr/mcp_server.py b/headroom/ccr/mcp_server.py
index d4ef58f5b..d8a8c2ed3 100644
--- a/headroom/ccr/mcp_server.py
+++ b/headroom/ccr/mcp_server.py
@@ -391,9 +391,15 @@ class HeadroomMCPServer:
)
self._stats.record_compression(input_tokens, output_tokens, strategy)
- savings_pct = (
- round((1 - result.compression_ratio) * 100, 1) if result.compression_ratio < 1.0 else 0
- )
+ # Percentage of tokens removed. Derive from the same token counts used
+ # for ``tokens_saved`` so all three fields agree — this mirrors the
+ # convention in ``_Stats.record_compression`` above. The previous
+ # ``(1 - result.compression_ratio)`` inverted the value: since
+ # ``compression_ratio`` is already the *saved* fraction (see
+ # ``CompressResult`` in headroom/compress.py — "0.0 = no savings, 1.0 =
+ # 100% removed"), the old expression reported the *retained* percentage,
+ # e.g. a no-op (0% saved) was reported as 100%.
+ savings_pct = round((1 - output_tokens / input_tokens) * 100, 1) if input_tokens > 0 else 0
return {
"compressed": compressed_content,
diff --git a/tests/test_ccr_mcp_server.py b/tests/test_ccr_mcp_server.py
index 7ecb983d0..7a353b1e2 100644
--- a/tests/test_ccr_mcp_server.py
+++ b/tests/test_ccr_mcp_server.py
@@ -67,6 +67,30 @@ def test_mcp_retrieves_proxy_stored_content(fresh_store) -> None:
assert result["original_content"] == original
+def test_compress_savings_percent_tracks_token_counts(fresh_store) -> None:
+ """``savings_percent`` must be the *removed* percentage derived from the
+ token counts — never the retained percentage. Regression for the inversion
+ where ``(1 - compression_ratio)`` reported a no-op (0% saved) as 100%."""
+ pytest.importorskip("mcp", reason="MCP SDK required")
+ server = mcp_server.HeadroomMCPServer(check_proxy=False)
+
+ # Repetitive JSON array — the shape the engine actually compresses.
+ content = json.dumps([{"id": i, "status": "ok", "kind": "run"} for i in range(40)])
+ result = server._compress_content(content)
+
+ orig = result["original_tokens"]
+ comp = result["compressed_tokens"]
+ expected = round((1 - comp / orig) * 100, 1) if orig > 0 else 0
+
+ # Reported savings agrees with the token fields (and with tokens_saved).
+ assert result["savings_percent"] == expected
+ assert 0.0 <= result["savings_percent"] <= 100.0
+ if result["tokens_saved"] == 0:
+ assert result["savings_percent"] == 0.0 # not inverted to 100
+ else:
+ assert result["savings_percent"] > 0.0
+
+
def test_mcp_retrieve_with_nonmatching_query_returns_full_content(fresh_store) -> None:
"""A query that matches no item above the relevance floor must still return
the stored entry (it exists and is unexpired) rather than the "Content not