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.

Extract query log policy (#1984)

JD Davisd45748d14333
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index d050dacca..ce3c93a8b 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -26,6 +26,7 @@ from headroom import paths as _paths
from headroom._subprocess import run
from headroom.proxy import (
diagnostic_decode_policy,
+ query_log_policy,
request_limit_policy,
sse_byte_buffer_policy,
wire_debug_format_policy,
@@ -257,8 +258,7 @@ def hash_query_for_log(query: str) -> str:
Uses BLAKE2b truncated to 16 hex chars. Never logs the raw query content.
"""
- h = hashlib.blake2b(query.encode("utf-8", errors="replace"), digest_size=8)
- return h.hexdigest()
+ return query_log_policy.hash_query_for_log(query)
def extract_tags(headers: Any) -> dict[str, str]:
diff --git a/headroom/proxy/query_log_policy.py b/headroom/proxy/query_log_policy.py
new file mode 100644
index 000000000..0d36041f2
--- /dev/null
+++ b/headroom/proxy/query_log_policy.py
@@ -0,0 +1,13 @@
+"""Privacy-preserving query identifiers for proxy logs."""
+
+from __future__ import annotations
+
+import hashlib
+
+QUERY_LOG_HASH_BYTES = 8
+
+
+def hash_query_for_log(query: str) -> str:
+ """Stable short hash of a memory-context query, safe to log."""
+ h = hashlib.blake2b(query.encode("utf-8", errors="replace"), digest_size=QUERY_LOG_HASH_BYTES)
+ return h.hexdigest()
diff --git a/tests/test_query_log_policy.py b/tests/test_query_log_policy.py
new file mode 100644
index 000000000..83478bb9b
--- /dev/null
+++ b/tests/test_query_log_policy.py
@@ -0,0 +1,26 @@
+from __future__ import annotations
+
+from headroom.proxy.helpers import hash_query_for_log as helper_hash_query_for_log
+from headroom.proxy.query_log_policy import QUERY_LOG_HASH_BYTES, hash_query_for_log
+
+
+def test_hash_query_for_log_is_stable_short_hex() -> None:
+ query_hash = hash_query_for_log("find user memory about project x")
+
+ assert query_hash == hash_query_for_log("find user memory about project x")
+ assert len(query_hash) == QUERY_LOG_HASH_BYTES * 2
+ assert all(char in "0123456789abcdef" for char in query_hash)
+
+
+def test_hash_query_for_log_changes_with_query_content() -> None:
+ assert hash_query_for_log("alpha") != hash_query_for_log("beta")
+
+
+def test_hash_query_for_log_handles_unpaired_surrogates() -> None:
+ query_hash = hash_query_for_log("bad-surrogate-\ud800")
+
+ assert len(query_hash) == QUERY_LOG_HASH_BYTES * 2
+
+
+def test_helpers_hash_query_for_log_delegates_to_policy() -> None:
+ assert helper_hash_query_for_log("same") == hash_query_for_log("same")