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(init/codex): don't delete per-profile provider settings (#2146)
Abhay Singh8da4384bfcb2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bd1beac72..85c35f733 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -116,6 +116,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0* **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.* **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.+* **init/codex:** stop `headroom init codex` from deleting per-profile provider settings. `_ensure_codex_provider` removed the root-level `model_provider`/`openai_base_url` (which init owns) with a multiline regex that matched those keys in **every** table, so a user's `[profiles.*]` overrides (e.g. `[profiles.work] model_provider = "azure"`) were silently stripped and those profiles fell through to the injected `"headroom"` default — config corruption. The strip is now scoped to the document root (everything before the first table header), so per-profile overrides are preserved while init still replaces a root-level assignment.* **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/cli/init.py b/headroom/cli/init.pyindex 85cf7882c..b37ef7ced 100644--- a/headroom/cli/init.py+++ b/headroom/cli/init.py@@ -322,11 +322,18 @@ def _ensure_codex_provider(path: Path, port: int) -> None:f"{_CODEX_PROVIDER_MARKER_END}")content = path.read_text(encoding="utf-8") if path.exists() else ""- # init owns model_provider/openai_base_url: drop any prior assignment (any- # value, including one an older version mis-scoped under a table) so we- # replace it instead of emitting a duplicate top-level key (#260).- content = re.sub(r"(?m)^[ \t]*model_provider[ \t]*=.*\r?\n", "", content)- content = re.sub(r"(?m)^[ \t]*openai_base_url[ \t]*=.*\r?\n", "", content)+ # init owns the ROOT-level model_provider/openai_base_url: drop any prior+ # root assignment so we replace it instead of emitting a duplicate top-level+ # key (#260). Scope the strip to the document root (everything before the+ # first table header) -- these keys also appear legitimately inside+ # [profiles.*] tables as per-profile overrides, and stripping them there+ # silently reroutes the user's profiles to the injected "headroom" default.+ _first_table = re.search(r"(?m)^[ \t]*\[", content)+ _split = _first_table.start() if _first_table else len(content)+ root, rest = content[:_split], content[_split:]+ root = re.sub(r"(?m)^[ \t]*model_provider[ \t]*=.*\r?\n", "", root)+ root = re.sub(r"(?m)^[ \t]*openai_base_url[ \t]*=.*\r?\n", "", root)+ content = root + rest# The provider block carries top-level keys (model_provider, openai_base_url),# so it must land at the document root rather than after a trailing table (#260).content = _replace_marker_block(diff --git a/tests/test_cli/test_init_cli.py b/tests/test_cli/test_init_cli.pyindex 72cc7210f..f91469f27 100644--- a/tests/test_cli/test_init_cli.py+++ b/tests/test_cli/test_init_cli.py@@ -533,6 +533,33 @@ def test_ensure_codex_provider_replaces_existing_model_provider(assert parsed["features"]["hooks"] is True+def test_ensure_codex_provider_preserves_profile_overrides(monkeypatch, tmp_path: Path) -> None:+ """Per-profile model_provider/openai_base_url overrides must survive init.++ init owns the ROOT-level keys, but the same keys inside [profiles.*] are the+ user's per-profile routing; a broad strip silently reroutes those profiles+ to the injected "headroom" default (config corruption).+ """+ init_cli, _ = _load_init_module(monkeypatch)+ path = tmp_path / "config.toml"+ path.write_text(+ 'model_provider = "openai"\n\n'+ "[profiles.work]\n"+ 'model_provider = "azure"\n'+ 'openai_base_url = "https://azure.example/v1"\n',+ encoding="utf-8",+ )++ init_cli._ensure_codex_provider(path, 8787)++ parsed = tomllib.loads(path.read_text(encoding="utf-8"))+ # Root is replaced by headroom (no duplicate top-level key).+ assert parsed["model_provider"] == "headroom"+ # The user's per-profile overrides are untouched.+ assert parsed["profiles"]["work"]["model_provider"] == "azure"+ assert parsed["profiles"]["work"]["openai_base_url"] == "https://azure.example/v1"++def test_ensure_codex_provider_emits_requires_openai_auth_for_chatgpt(monkeypatch, tmp_path: Path) -> None: