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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(ccr): lowercase a retrieved hash so an uppercase echo still hits the store (#2236)
Abhay Singh842d7e1ad186
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 11a13f1b1..6d2343ddb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -126,6 +126,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug Fixes+* **ccr:** normalise a retrieved hash to lowercase so an uppercase echo still hits the store. `parse_tool_call` validated the hash case-insensitively (`hash_key.lower()`) but returned it verbatim, while the compression store keys every entry by a lowercase hash (sha256 hexdigest, and `explicit_hash.lower()` on write) and `retrieve` / `get_entry_status` look the key up as-is. A model that echoed the marker hash in uppercase therefore passed validation but missed the store, failing an otherwise-valid `headroom_retrieve` and losing the original content. `parse_tool_call` now returns the canonical lowercase form.* **proxy:** run a cold-start fast pass before background-compression deferral so byte-identical freeze doesn't lock sessions to the uncompressed transcript. Since #1850, a session's provider-cached prefix is frozen in whatever form its cold start forwarded; deferring the WHOLE pipeline (`HEADROOM_BACKGROUND_COMPRESSION=1`, frozen=0, ≥50k tokens) therefore cached the raw transcript and forfeited the session's compression savings for its lifetime — including sub-second lossless wins like `read_lifecycle` stale-read drops, observed in the field as sessions permanently stuck at 0 savings. The deferral branch now runs the pipeline synchronously with the new `skip_kompress=True` kwarg (everything except the Kompress ML stage — the only stage that can blow the request budget per #1171) under a bounded fast-pass budget (`HEADROOM_COLD_START_FAST_PASS_TIMEOUT_SECONDS`, default 10s), forwards the pruned form, and defers only Kompress to the background job (tagged `deferred:kompress_background`). Fail-open: on fast-pass timeout/error the request forwards uncompressed exactly as before. Units routed to Kompress under `skip_kompress` take the same fallback as when the model isn't ready.* **proxy/batch:** preserve sibling tool configs on Google batch requests. When optimizing a Gemini batch item, the handler rebuilt the request's `tools` as a single `[{"functionDeclarations": ...}]` entry, discarding any other entries in the array (`googleSearch`, `codeExecution`, etc.). A batch request that combined function calling with Google Search or code execution therefore reached Google with those siblings stripped, silently disabling them. The handler now replaces only the `functionDeclarations` entry (appending one when the original had none) and keeps every sibling entry intact.* **savings:** record the pre-compression original as the ledger `before`, not the forwarded count. In `PrometheusMetrics.record_request` the durable savings ledger was written with `tokens_before=input_tokens`, but `input_tokens` there is the optimized (post-compression) count that was forwarded (`emit_request_outcome` passes `outcome.optimized_tokens`). `headroom savings` derives the reported reduction percent as saved / before, so understating `before` by `tokens_saved` inflated it — a real 40% reduction (1000 → 600) was reported as ~67% (400 / 600). The event now records `tokens_before=input_tokens + tokens_saved` (the reconstructed original) and `tokens_after=input_tokens` (the forwarded count); the `saved` and cost figures are unchanged.diff --git a/headroom/ccr/tool_injection.py b/headroom/ccr/tool_injection.pyindex db929886a..90a83c9a6 100644--- a/headroom/ccr/tool_injection.py+++ b/headroom/ccr/tool_injection.py@@ -519,4 +519,10 @@ def parse_tool_call(if not all(c in "0123456789abcdef" for c in hash_key.lower()):return None- return hash_key+ # Normalise to lowercase. The compression store always keys entries by a+ # lowercase hash (sha256 hexdigest, and `explicit_hash.lower()` on store),+ # and `retrieve` / `get_entry_status` look up the key verbatim. The hex+ # validation above is already case-insensitive, so a model that echoes the+ # marker hash uppercase passed validation but then missed the store lookup,+ # failing an otherwise-valid retrieval. Return the canonical lowercase form.+ return hash_key.lower()diff --git a/tests/test_ccr_tool_injection.py b/tests/test_ccr_tool_injection.pyindex 63301c263..d4506bc33 100644--- a/tests/test_ccr_tool_injection.py+++ b/tests/test_ccr_tool_injection.py@@ -310,6 +310,19 @@ class TestParseToolCall:assert hash_key == "def456abc123def456abc123"+ def test_parse_normalises_uppercase_hash_to_lowercase(self):+ """An uppercase hash echoed by the model must be lowercased so it+ matches the store (which keys entries by a lowercase hash)."""+ tool_call = {+ "id": "toolu_123",+ "name": CCR_TOOL_NAME,+ "input": {"hash": "ABC123DEF456ABC123DEF456"},+ }++ hash_key = parse_tool_call(tool_call, "anthropic")++ assert hash_key == "abc123def456abc123def456"+def test_parse_non_ccr_tool(self):"""Returns None for non-CCR tool calls."""tool_call = {@@ -410,7 +423,7 @@ class TestHashSecurityValidation:hash_key = parse_tool_call(tool_call, "anthropic")# Note: validation accepts uppercase since we use .lower() for hex check- assert hash_key == "ABC123DEF456ABC123DEF456"+ assert hash_key == "abc123def456abc123def456"class TestSmartCrusherCcrMarkers: