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): detect read_lifecycle stale/superseded markers in the injector (#2148)
Abhay Singhec97443e66cb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 019f3811e..372b70ebf 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -107,6 +107,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug Fixes+* **ccr:** detect `read_lifecycle` stale/superseded markers in the retrieve-tool injector so they stay redeemable. Those markers (`[Read content stale: … Retrieve original: hash=<hash>]`) store the original bytes in the CCR store under a valid hash, but none of `CCRToolInjector`'s patterns matched them — every pattern required the word "compressed" or the `<<ccr:` form. So on a frozen-prefix turn (both `read_lifecycle` and prefix freezing are on by default) the injector reported no compressed content, the `headroom_retrieve` tool was not injected, and the model was handed a marker advertising `Retrieve original: hash=X` with no tool to redeem it — silent data loss for stale reads, where retrieval is the only way to recover the original-at-read-time content (the exact case the #1006 guard exists to prevent). Added a pattern matching the load-bearing `Retrieve original: hash=` phrase, aligning the injector with the sibling `read_maturation` marker that was already (incidentally) detected.* **proxy:** strip output-only content blocks from request messages before forwarding. Anthropic's server-side refusal-fallback feature (`server-side-fallback-2026-06-01`) emits a `{"type":"fallback","from":{...},"to":{...}}` block inside the assistant response to signal that a refused request was re-served by the fallback model. That block is valid on the *response* path but rejected on the *request* path, so when a client replays the assistant turn the next request 400s (`invalid_request_error: messages.N.content.0: Input tag 'fallback' ...`) and the conversation gets permanently stuck through the proxy. `read_request_json_with_bytes` (Anthropic/OpenAI/Bedrock) and `_read_request_json` (Gemini) now drop such blocks — re-encoding the raw bytes so byte-faithful passthrough cannot leak the pre-strip body, backfilling a benign text block if a turn is emptied, and leaving requests without such blocks byte-identical (no cache churn).* **wrap/doctor:** make the Claude Remote Control gate warning accurate and stop it firing for users who never had the feature ([#1779](https://github.com/headroomlabs-ai/headroom/issues/1779)). Claude Code 2.1.196 added a client-side check that **deterministically** disables first-party Remote Control (`/remote-control` / `/rc`) whenever `ANTHROPIC_BASE_URL` points at a non-`api.anthropic.com` host — which Headroom always does. The old notice hedged ("may hide the Remote Control menu"); it now states the disable as fact, names the `/rc` command, and detects the installed Claude Code version so the wording is exact (`2.1.196` when known, `2.1.196+` when not). The gate is upstream and RC's control-plane talks to `claude.ai` (not the API host), so Headroom cannot restore it — the warning tells you to run Claude without Headroom for RC sessions. The warning is suppressed for auth modes that never had Remote Control (API-key/PAYG via `ANTHROPIC_API_KEY`/`ANTHROPIC_AUTH_TOKEN`, and Bedrock/Vertex/Foundry cloud IAM) and on Claude Code builds older than 2.1.196 where RC is unaffected by a custom base URL. Both the `headroom wrap claude` launch banner and `headroom doctor` co-report the sibling base-URL gates Headroom *does* restore — on-demand tool loading (#746, automatic) and the 1M context window (#1158, via `--1m`) — and the wrap-side co-report is session-accurate: it says "already restored via --1m" when the flag is in effect and reports tool deferral OFF (not falsely "kept on") when the user chose `--tool-search false`/`ENABLE_TOOL_SEARCH=false`; the `ENABLE_TOOL_SEARCH=...` banner line got the same accuracy fix. `is_custom_anthropic_base_url` now recognizes scheme-less values (`myproxy.local:8080`, `127.0.0.1:8787`) as custom hosts and degrades gracefully on malformed URLs instead of crashing `doctor`. `doctor` resolves the Claude Code version lazily, so runs with no custom base URL never pay the `claude --version` subprocess. No request bytes are touched (cache-safe); this is UX/notice-only.* **install:** default the docker image to `ghcr.io/headroomlabs-ai/headroom:latest` instead of the dead `ghcr.io/chopratejas/headroom:latest`. After the repo moved to the `headroomlabs-ai` org, GHCR did not redirect the old package, so `headroom install` / `headroom init` and the install scripts pulled a frozen `0.27.0` image while current releases publish to the new path ([#1867](https://github.com/headroomlabs-ai/headroom/issues/1867)).diff --git a/headroom/ccr/tool_injection.py b/headroom/ccr/tool_injection.pyindex 118cec849..db929886a 100644--- a/headroom/ccr/tool_injection.py+++ b/headroom/ccr/tool_injection.py@@ -199,6 +199,14 @@ class CCRToolInjector:# `<<ccr:HASH,KIND,SIZE>>`. HASH is 12-24 hex chars, terminated by a# space, comma, or the closing `>>`.re.compile(r"<<ccr:([a-f0-9]{12,24})\b"),+ # read_lifecycle STALE/SUPERSEDED markers:+ # `[Read content stale/superseded: ... Retrieve original: hash=xxx]`.+ # These carry a retrievable CCR hash but never contain the word+ # "compressed", so the patterns above miss them -- and the retrieve+ # tool is then not injected, leaving the model a marker it cannot+ # redeem (silent data loss, #1006). Match the load-bearing+ # "Retrieve original: hash=" phrase directly.+ re.compile(r"Retrieve original: hash=([a-f0-9]{12,24})"),])diff --git a/tests/test_ccr_tool_injection.py b/tests/test_ccr_tool_injection.pyindex 7a781f7db..63301c263 100644--- a/tests/test_ccr_tool_injection.py+++ b/tests/test_ccr_tool_injection.py@@ -66,6 +66,28 @@ class TestCCRToolInjector:assert "abc123def456abc123def456" in hashesassert injector.has_compressed_content+ def test_scan_detects_read_lifecycle_stale_marker(self):+ """A read_lifecycle STALE marker carries a retrievable CCR hash via the+ 'Retrieve original: hash=' phrase but never says 'compressed', so the+ other patterns miss it. The injector must still detect it, or the+ retrieve tool is not offered and the marker is unredeemable (#1006)."""+ ccr_hash = "a1b2c3d4e5f6a1b2c3d4e5f6" # 24 hex chars (SHA-256[:24])+ messages = [+ {+ "role": "tool",+ "content": (+ "[Read content stale: app.py was modified after this read — "+ f"re-read the file for current content. Retrieve original: hash={ccr_hash}]"+ ),+ },+ ]++ injector = CCRToolInjector()+ hashes = injector.scan_for_markers(messages)++ assert ccr_hash in hashes+ assert injector.has_compressed_content+def test_scan_for_markers_multiple_hashes(self):"""Scanner finds multiple distinct hashes."""messages = [