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(codex): avoid duplicate headroom provider config (#1431)
Rudimar Ronsoniddd4adf911ee
diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.pyindex e3a7cef07..c6f8f8d0c 100644--- a/headroom/cli/wrap.py+++ b/headroom/cli/wrap.py@@ -1350,6 +1350,20 @@ def _strip_codex_headroom_blocks(_REDIRECTABLE_KEYS: tuple[str, ...] = ("model_provider", "openai_base_url")+def _strip_existing_codex_headroom_provider_table(content: str) -> str:+ """Remove a pre-existing ``[model_providers.headroom]`` table before wrap."""+ if "[model_providers.headroom]" not in content:+ return content++ import re # local import to match surrounding helper convention++ provider_table = re.compile(+ r"(?ms)^[ \t]*\[model_providers\.headroom\][^\n]*\n.*?(?=^[ \t]*\[|\Z)"+ )+ content = provider_table.sub("", content)+ return content.lstrip("\n").rstrip() + "\n" if content.strip() else ""++def _redirect_existing_top_level_keys(content: str, port: int) -> str:"""Rewrite user-defined top-level keys so wrap does not create duplicates.@@ -1614,6 +1628,7 @@ def _inject_codex_provider_config(port: int) -> None:# Remove any prior Headroom-managed blocks before re-injecting so# the operation is idempotent and supports port changes.content = _strip_codex_headroom_blocks(content)+ content = _strip_existing_codex_headroom_provider_table(content)# Bare top-level keys must precede any [section] in TOML, and# TOML rejects duplicate top-level keys. Rewrite any existingdiff --git a/tests/test_cli/test_wrap_codex.py b/tests/test_cli/test_wrap_codex.pyindex 44cbe3c74..7af6597f4 100644--- a/tests/test_cli/test_wrap_codex.py+++ b/tests/test_cli/test_wrap_codex.py@@ -735,6 +735,61 @@ class TestInjectAvoidsDuplicateTopLevelKeys:assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in contentassert "[model_providers.headroom]" in content+ def test_inject_replaces_existing_headroom_provider_table(+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path+ ) -> None:+ """Existing headroom provider table must not create duplicate TOML keys."""+ import tomllib # Python 3.11+ stdlib++ _set_test_home(monkeypatch, tmp_path)+ config_dir = tmp_path / ".codex"+ config_dir.mkdir()+ config_file = config_dir / "config.toml"+ config_file.write_text(+ "[model_providers.headroom]\n"+ 'name = "Existing custom headroom"\n'+ 'base_url = "http://example.invalid/v1"\n'+ "supports_websockets = true\n"+ 'env_http_headers = { "X-Headroom-Project" = "HEADROOM_PROJECT" }\n'+ "\n"+ "[profiles.default]\n"+ 'model = "gpt-5"\n'+ )++ wrap_mod._inject_codex_provider_config(8787)+ content = config_file.read_text()++ tomllib.loads(content)+ assert content.count("[model_providers.headroom]") == 1+ assert content.count("env_http_headers") == 1+ assert 'base_url = "http://127.0.0.1:8787/v1"' in content+ assert 'env_http_headers = { "X-Headroom-Project" = "HEADROOM_PROJECT" }' in content+ assert "[profiles.default]" in content+ assert 'model = "gpt-5"' in content++ def test_unwrap_restores_prior_headroom_provider_table(+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path+ ) -> None:+ """Pre-wrap headroom provider table is restored from snapshot."""+ _set_test_home(monkeypatch, tmp_path)+ config_dir = tmp_path / ".codex"+ config_dir.mkdir()+ config_file = config_dir / "config.toml"+ original = (+ "[model_providers.headroom]\n"+ 'name = "Existing custom headroom"\n'+ 'base_url = "http://example.invalid/v1"\n'+ "supports_websockets = true\n"+ 'env_http_headers = { "X-Headroom-Project" = "HEADROOM_PROJECT" }\n'+ )+ config_file.write_text(original)++ wrap_mod._inject_codex_provider_config(8787)+ status, _ = wrap_mod._restore_codex_provider_config()++ assert status == "restored"+ assert config_file.read_text() == original+def test_unwrap_restores_prior_model_provider_after_rewrite(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: