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): inject openai_base_url in init and persistent-install paths
JerrettDavisbf1e31b27c64
diff --git a/headroom/cli/init.py b/headroom/cli/init.pyindex 2d9724070..10ae1afc9 100644--- a/headroom/cli/init.py+++ b/headroom/cli/init.py@@ -207,11 +207,49 @@ def _replace_marker_block(content: str, marker_start: str, marker_end: str, blocreturn (content.rstrip() + "\n\n" + block.strip() + "\n").lstrip()+def _strip_codex_init_block(content: str) -> str:+ """Remove all Headroom init-managed blocks and orphan keys from a Codex config.toml string."""+ import re++ # Remove any provider marker → end marker span, possibly repeated.+ while _CODEX_PROVIDER_MARKER_START in content and _CODEX_PROVIDER_MARKER_END in content:+ start = content.index(_CODEX_PROVIDER_MARKER_START)+ end_idx = content.index(_CODEX_PROVIDER_MARKER_END, start)+ if end_idx < start:+ break+ end = end_idx + len(_CODEX_PROVIDER_MARKER_END)+ content = content[:start].rstrip("\n") + "\n" + content[end:].lstrip("\n")++ # Remove stale unpaired markers.+ content = content.replace(_CODEX_PROVIDER_MARKER_START + "\n", "")+ content = content.replace(_CODEX_PROVIDER_MARKER_END + "\n", "")++ # Strip any orphan top-level keys that a crashed or partial write may have+ # left outside the marker block.+ content = re.sub(r'(?m)^[ \t]*model_provider[ \t]*=[ \t]*"headroom"[ \t]*\r?\n', "", content)+ content = re.sub(+ r'(?m)^[ \t]*openai_base_url[ \t]*=[ \t]*"http://127\.0\.0\.1:\d+/v1"[ \t]*\r?\n',+ "",+ content,+ )++ # Strip any orphaned [model_providers.headroom] table that is recognisably ours.+ orphan_headroom_table = re.compile(+ r"(?ms)^\[model_providers\.headroom\][^\[]*?"+ r'base_url[ \t]*=[ \t]*"http://127\.0\.0\.1:\d+/v1"[^\[]*?'+ r"(?=^\[|\Z)"+ )+ content = orphan_headroom_table.sub("", content)++ return content.lstrip("\n").rstrip() + "\n" if content.strip() else ""++def _ensure_codex_provider(path: Path, port: int) -> None:logger.debug("ensure codex provider block: %s (port=%s)", path, port)block = (f"{_CODEX_PROVIDER_MARKER_START}\n"- 'model_provider = "headroom"\n\n'+ 'model_provider = "headroom"\n'+ f'openai_base_url = "http://127.0.0.1:{port}/v1"\n\n'"[model_providers.headroom]\n"'name = "Headroom init proxy"\n'f'base_url = "http://127.0.0.1:{port}/v1"\n'diff --git a/headroom/providers/codex/install.py b/headroom/providers/codex/install.pyindex 3ac7f8b86..12ec1ecc9 100644--- a/headroom/providers/codex/install.py+++ b/headroom/providers/codex/install.py@@ -17,6 +17,20 @@ _CODEX_PATTERN = re.compile(re.DOTALL,)+# Orphan-key patterns: strip any top-level keys that a crashed or partial write+# may have left outside the marker block.+_ORPHAN_MODEL_PROVIDER = re.compile(+ r'(?m)^[ \t]*model_provider[ \t]*=[ \t]*"headroom"[ \t]*\r?\n'+)+_ORPHAN_OPENAI_BASE_URL = re.compile(+ r'(?m)^[ \t]*openai_base_url[ \t]*=[ \t]*"http://127\.0\.0\.1:\d+/v1"[ \t]*\r?\n'+)+_ORPHAN_HEADROOM_TABLE = re.compile(+ r"(?ms)^\[model_providers\.headroom\][^\[]*?"+ r'base_url[ \t]*=[ \t]*"http://127\.0\.0\.1:\d+/v1"[^\[]*?'+ r"(?=^\[|\Z)"+)+def build_install_env(*, port: int, backend: str) -> dict[str, str]:"""Build the persistent install environment for Codex."""@@ -33,7 +47,8 @@ def apply_provider_scope(manifest: DeploymentManifest) -> ManagedMutation | Nonepath.parent.mkdir(parents=True, exist_ok=True)section = (f"{_CODEX_MARKER_START}\n"- 'model_provider = "headroom"\n\n'+ 'model_provider = "headroom"\n'+ f'openai_base_url = "{proxy_base_url(manifest.port)}"\n\n'"[model_providers.headroom]\n"'name = "Headroom persistent proxy"\n'f'base_url = "{proxy_base_url(manifest.port)}"\n'@@ -62,6 +77,12 @@ def revert_provider_scope(mutation: ManagedMutation, manifest: DeploymentManifesif not path.exists():returncontent = path.read_text()- if _CODEX_MARKER_START not in content:- return- path.write_text(_CODEX_PATTERN.sub("", content).strip() + "\n")+ # Remove the managed marker block.+ if _CODEX_MARKER_START in content:+ content = _CODEX_PATTERN.sub("", content)+ # Strip any orphan top-level keys that a crashed or partial write may have+ # left outside the marker block (mirrors wrap.py _strip_codex_headroom_blocks).+ content = _ORPHAN_MODEL_PROVIDER.sub("", content)+ content = _ORPHAN_OPENAI_BASE_URL.sub("", content)+ content = _ORPHAN_HEADROOM_TABLE.sub("", content)+ path.write_text(content.strip() + "\n")diff --git a/tests/test_cli/test_init_cli.py b/tests/test_cli/test_init_cli.pyindex 6ec6b5e8d..b5feaa94b 100644--- a/tests/test_cli/test_init_cli.py+++ b/tests/test_cli/test_init_cli.py@@ -925,3 +925,70 @@ def test_init_hook_ensure_uses_explicit_profile(monkeypatch) -> None:assert result.exit_code == 0, result.outputassert ensured == ["init-explicit"]+++# ---------------------------------------------------------------------------+# Bug 3 (#406): _ensure_codex_provider must inject openai_base_url+# ---------------------------------------------------------------------------+++def test_init_codex_writes_openai_base_url(monkeypatch, tmp_path: Path) -> None:+ """_ensure_codex_provider must write openai_base_url at the top level so that+ subscription (ChatGPT plan) users are routed through headroom even when the+ init entry point is used instead of wrap."""+ init_cli, _ = _load_init_module(monkeypatch)+ path = tmp_path / ".codex" / "config.toml"+ path.parent.mkdir(parents=True, exist_ok=True)++ init_cli._ensure_codex_provider(path, 8787)++ content = path.read_text(encoding="utf-8")+ assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in content, (+ f"openai_base_url missing from init codex config:\n{content}"+ )+ # Must NOT appear inside a [section] block.+ lines = content.splitlines()+ in_section = False+ for line in lines:+ stripped = line.strip()+ if stripped.startswith("["):+ in_section = True+ if in_section and stripped.startswith("openai_base_url"):+ raise AssertionError(+ f"openai_base_url appeared inside a section block in init output:\n{content}"+ )+ # Bug 3 regression guard.+ assert "requires_openai_auth" not in content, (+ f"requires_openai_auth must not appear in init codex config:\n{content}"+ )+++def test_init_codex_strip_removes_openai_base_url(monkeypatch, tmp_path: Path) -> None:+ """_strip_codex_init_block must remove both the managed block and any orphaned+ openai_base_url lines left by a crashed or partial init."""+ init_cli, _ = _load_init_module(monkeypatch)++ # Normal install-then-strip cycle.+ path = tmp_path / "config.toml"+ path.write_text('model = "gpt-4o"\n', encoding="utf-8")+ init_cli._ensure_codex_provider(path, 8787)+ assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in path.read_text(encoding="utf-8")++ stripped = init_cli._strip_codex_init_block(path.read_text(encoding="utf-8"))+ assert "openai_base_url" not in stripped, (+ f"_strip_codex_init_block must remove openai_base_url after install:\n{stripped}"+ )+ assert "requires_openai_auth" not in stripped+ assert 'model = "gpt-4o"' in stripped++ # Orphan-cleanup path: openai_base_url left outside marker block.+ orphan_content = (+ 'model = "gpt-4o"\n'+ 'openai_base_url = "http://127.0.0.1:8787/v1"\n'+ 'model_provider = "headroom"\n'+ )+ orphan_stripped = init_cli._strip_codex_init_block(orphan_content)+ assert "openai_base_url" not in orphan_stripped, (+ f"_strip_codex_init_block must remove orphaned openai_base_url:\n{orphan_stripped}"+ )+ assert 'model = "gpt-4o"' in orphan_strippeddiff --git a/tests/test_install/test_providers.py b/tests/test_install/test_providers.pyindex 2aae48d0f..848c0a167 100644--- a/tests/test_install/test_providers.py+++ b/tests/test_install/test_providers.py@@ -606,3 +606,84 @@ def test_unwrap_removes_top_level_openai_base_url(f"got:\n{stripped}")assert 'model = "gpt-4o"' in stripped+++# ---------------------------------------------------------------------------+# Bug 3 (#406): openai_base_url must appear in persistent-install and init paths+# ---------------------------------------------------------------------------+++def test_apply_provider_scope_writes_openai_base_url(monkeypatch, tmp_path: Path) -> None:+ """apply_provider_scope must write openai_base_url at the top level (not inside+ a [model_providers.*] block) so subscription (ChatGPT plan) users are routed+ through headroom regardless of which entry point they used."""+ config_path = tmp_path / "config.toml"+ config_path.write_text('model = "gpt-4o"\n')+ monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config_path)+ manifest = _manifest(tmp_path)+ manifest.port = 8787++ apply_codex_provider_scope(manifest)++ content = config_path.read_text()+ # Must be present as a top-level key.+ assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in content, (+ f"openai_base_url missing from persistent-install config:\n{content}"+ )+ # Must NOT be inside any [model_providers.*] block — the key must appear+ # before the first [section] header that follows it.+ lines = content.splitlines()+ in_provider_section = False+ for line in lines:+ stripped = line.strip()+ if stripped.startswith("["):+ in_provider_section = True+ if in_provider_section and stripped.startswith("openai_base_url"):+ raise AssertionError(+ f"openai_base_url appeared inside a section block:\n{content}"+ )+ # Bug 3 regression: requires_openai_auth must NOT appear.+ assert "requires_openai_auth" not in content, (+ f"requires_openai_auth must not be present in config:\n{content}"+ )+++def test_persistent_install_strip_removes_openai_base_url(monkeypatch, tmp_path: Path) -> None:+ """revert_provider_scope must remove openai_base_url during uninstall, and+ must also clean up orphaned openai_base_url lines left by a crashed install."""+ config_path = tmp_path / "config.toml"+ config_path.write_text('model = "gpt-4o"\n')+ monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config_path)+ manifest = _manifest(tmp_path)+ manifest.port = 8787++ mutation = apply_codex_provider_scope(manifest)+ assert mutation is not None+ assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in config_path.read_text()++ revert_codex_provider_scope(mutation, manifest)++ reverted = config_path.read_text()+ assert "openai_base_url" not in reverted, (+ f"openai_base_url must be removed after revert:\n{reverted}"+ )+ assert "requires_openai_auth" not in reverted+ # User's original content must survive.+ assert 'model = "gpt-4o"' in reverted++ # Also verify orphan-cleanup path: revert on a config where openai_base_url+ # was left outside the marker block (crash-recovery scenario).+ config_path.write_text(+ 'model = "gpt-4o"\n'+ 'openai_base_url = "http://127.0.0.1:8787/v1"\n'+ 'model_provider = "headroom"\n'+ )+ revert_codex_provider_scope(+ ManagedMutation(target="codex", kind="toml-block", path=str(config_path)),+ manifest,+ )+ orphan_reverted = config_path.read_text()+ assert "openai_base_url" not in orphan_reverted, (+ f"orphaned openai_base_url must be removed by revert:\n{orphan_reverted}"+ )+ assert 'model = "gpt-4o"' in orphan_reverted