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(telemetry): give empty-item compressions a stable tool signature (#4396)

HetCreepa79def4dd1b7
diff --git a/headroom/telemetry/models.py b/headroom/telemetry/models.py
index 86b25f44b..9e04b344b 100644
--- a/headroom/telemetry/models.py
+++ b/headroom/telemetry/models.py
@@ -214,13 +214,11 @@ class ToolSignature:
def from_items(cls, items: list[dict[str, Any]]) -> ToolSignature:
"""Create signature from sample items."""
if not items:
- # HIGH FIX: Generate unique hash for empty outputs to prevent
- # different tools' empty responses from colliding into one pattern.
- # Use a random component to ensure uniqueness across tool types.
- import uuid
-
- # MEDIUM FIX #15: Use 24 chars (96 bits) instead of 16 (64 bits) to reduce collision risk
- empty_hash = hashlib.sha256(f"empty:{uuid.uuid4()}".encode()).hexdigest()[:24]
+ # Fixed hash (not random): a per-call uuid made every empty result a new
+ # signature, so collector._tool_stats grew without bound. Empty outputs carry
+ # no structure to distinguish, so they share one bucket. Not the literal
+ # "empty", to stay distinct from any caller-supplied string key.
+ empty_hash = hashlib.sha256(b"empty").hexdigest()[:24]
return cls(
structure_hash=empty_hash,
field_count=0,
diff --git a/tests/test_telemetry.py b/tests/test_telemetry.py
index d0420ccda..19a6bb898 100644
--- a/tests/test_telemetry.py
+++ b/tests/test_telemetry.py
@@ -502,6 +502,30 @@ class TestTelemetryCollector:
assert len(collector._events) <= 5
+class TestEmptyItemsSignature:
+ """Empty-item compressions must not create a new tool signature each time."""
+
+ def test_empty_signature_is_stable(self):
+ assert (
+ ToolSignature.from_items([]).structure_hash
+ == ToolSignature.from_items([]).structure_hash
+ )
+
+ def test_repeated_empty_compressions_share_one_tool_stat(self):
+ collector = TelemetryCollector()
+ for _ in range(50):
+ collector.record_compression(
+ items=[],
+ original_count=0,
+ compressed_count=0,
+ original_tokens=10,
+ compressed_tokens=10,
+ strategy="none",
+ )
+
+ assert len(collector.get_all_tool_stats()) == 1
+
+
class TestTelemetryPersistence:
"""Test telemetry persistence to disk."""