fix(doctor): read Codex's default provider from the TOML root only

Codex picks the default session's provider from the root `model_provider`;
one under `[profiles.*]` applies only when that profile is selected, and with
none set Codex uses its built-in `openai` provider, which a root
`openai_base_url` re-points. The doctor searched every table for
`model_provider` and preferred a `[model_providers.headroom]` table over the
root URL, so a profile override or a leftover table hid the real route.

Live-proxy discovery also read only the user-level configs, while the routing
rows include the project's `.claude/settings*.json` and `.codex/config.toml`,
so a port only a project config used was never probed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-28 11:43:05 +01:00
co-authored by Claude Opus 5.5
parent 85d32990f9
commit fd5e6fd5a1
3 changed files with 95 additions and 26 deletions
+31 -26
View File
@@ -574,15 +574,21 @@ def check_wrap_marker_staleness(settings_path: Path) -> CheckResult:
def codex_active_base_url(text: str) -> tuple[str, str | None]:
"""Return ``(provider_id, base_url)`` for the active Codex provider in ``text``."""
active_match = _CODEX_MODEL_PROVIDER_RE.search(text)
provider_id = active_match.group(1) if active_match else "headroom"
base_url = _codex_provider_base_url(text, provider_id)
if base_url is None and (active_match is None or provider_id == "openai"):
first_table = re.search(r"(?m)^[ \t]*\[", text)
root = text[: first_table.start()] if first_table is not None else text
# Only a root `model_provider` picks the default session's provider; one
# under `[profiles.*]` applies only when that profile is selected.
active_match = _CODEX_MODEL_PROVIDER_RE.search(root)
if active_match is None or active_match.group(1) == "openai":
# A root `openai_base_url` re-points Codex's built-in `openai` provider
# (the default when `model_provider` is unset). Headroom Desktop routes
# this way because the ChatGPT app hides features for any other provider.
base_url = _codex_root_openai_base_url(text)
return provider_id, base_url
# (the default when `model_provider` is unset), so it beats an inactive
# `[model_providers.headroom]` table. Headroom Desktop routes this way
# because the ChatGPT app hides features for any other provider.
root_url = _CODEX_OPENAI_BASE_URL_RE.search(root)
if root_url is not None:
return "openai", root_url.group(1)
provider_id = active_match.group(1) if active_match else "headroom"
return provider_id, _codex_provider_base_url(text, provider_id)
def check_codex_routing(
@@ -668,25 +674,21 @@ def _codex_provider_base_url(text: str, provider_id: str) -> str | None:
return base_url_match.group(1) if base_url_match else None
def _codex_root_openai_base_url(text: str) -> str | None:
first_table = re.search(r"(?m)^[ \t]*\[", text)
root = text[: first_table.start()] if first_table is not None else text
match = _CODEX_OPENAI_BASE_URL_RE.search(root)
return match.group(1) if match else None
def _configured_client_ports() -> list[int]:
"""Loopback ports the user's Claude Code and Codex configs send traffic to.
def _configured_client_ports(
claude_paths: Sequence[Path], codex_paths: Sequence[Path]
) -> list[int]:
"""Loopback ports the Claude Code and Codex configs (project and user) send traffic to.
Headroom Desktop (port 6767) and ``wrap --port N`` leave no deployment
manifest, so without these a bare ``doctor`` only ever finds 8787.
"""
urls = [_claude_base_url_in(claude_settings_path())[0]]
try:
codex_text = codex_config_path().read_text(encoding="utf-8", errors="replace")
except OSError:
codex_text = ""
urls.append(codex_active_base_url(codex_text)[1] or "")
urls = [_claude_base_url_in(path)[0] for path in claude_paths]
for path in codex_paths:
try:
text = path.read_text(encoding="utf-8", errors="replace")
except OSError:
continue
urls.append(codex_active_base_url(text)[1] or "")
return [port for port in map(loopback_port, urls) if port is not None]
@@ -1240,6 +1242,8 @@ def doctor(
health = probe_json(f"{base_url}/health", timeout=5.0) if livez else None
stats = probe_json(f"{base_url}/stats", timeout=5.0) if livez else None
installed = get_version()
project_claude_settings = Path.cwd() / ".claude" / "settings.json"
project_local_claude_settings = Path.cwd() / ".claude" / "settings.local.json"
live_elsewhere = None
if livez is None:
# Cheap, bounded: only ports Headroom recorded (HEADROOM_PORT, 8787,
@@ -1248,14 +1252,15 @@ def doctor(
live_elsewhere = find_live_proxy_elsewhere(
port,
manifests=manifests,
extra=_configured_client_ports(),
extra=_configured_client_ports(
[project_local_claude_settings, project_claude_settings, claude_settings_path()],
[codex_project_config_path(), codex_config_path()],
),
probe=lambda p: is_headroom_livez(
probe_json(f"http://127.0.0.1:{p}/livez", timeout=0.5)
),
)
project_claude_settings = Path.cwd() / ".claude" / "settings.json"
project_local_claude_settings = Path.cwd() / ".claude" / "settings.local.json"
checks = [
note_live_proxy_elsewhere(check_proxy_liveness(livez, base_url), live_elsewhere),
check_proxy_readiness(livez, readyz, base_url),
+15
View File
@@ -434,6 +434,21 @@ class TestHookAlignment:
message = init_mod.alignment_warning("codex", "init-user", probe=lambda p: False)
assert message is not None and "--port 8787 codex" in message
def test_codex_root_openai_base_url_beats_an_inactive_headroom_table(
self, home: Path, tmp_path: Path, monkeypatch
) -> None:
custom = tmp_path / "custom-codex"
custom.mkdir()
(custom / "config.toml").write_text(
'openai_base_url = "http://127.0.0.1:9200/v1"\n[model_providers.headroom]\n'
'base_url = "http://127.0.0.1:8787/v1"\n',
encoding="utf-8",
)
monkeypatch.setenv("CODEX_HOME", str(custom))
monkeypatch.setattr(init_mod, "load_manifest", lambda profile: _manifest(profile, 8787))
message = init_mod.alignment_warning("codex", "init-user", probe=lambda p: False)
assert message is not None and "127.0.0.1:9200" in message
def test_session_start_emits_hook_json(self, claude_settings: Path) -> None:
self._route_claude(claude_settings, 9200)
result = CliRunner().invoke(
+49
View File
@@ -616,6 +616,28 @@ class TestCodexRouting:
)
assert check_codex_routing(path, 8787).status == WARN
def test_root_openai_base_url_beats_an_inactive_headroom_table(self, tmp_path):
"""No root `model_provider`: Codex uses its built-in `openai`, not the table."""
path = tmp_path / "config.toml"
path.write_text(
'openai_base_url = "http://127.0.0.1:6767/v1"\n'
"[model_providers.headroom]\n"
'base_url = "http://127.0.0.1:8787/v1"\n',
encoding="utf-8",
)
assert check_codex_routing(path, 6767).status == PASS
assert check_codex_routing(path, 8787).status == WARN
def test_profile_model_provider_does_not_hide_the_default_route(self, tmp_path):
path = tmp_path / "config.toml"
path.write_text(
'openai_base_url = "http://127.0.0.1:6767/v1"\n'
"[profiles.work]\n"
'model_provider = "azure"\n',
encoding="utf-8",
)
assert check_codex_routing(path, 6767).status == PASS
def test_no_marker_warns(self, tmp_path):
path = tmp_path / "config.toml"
path.write_text('model = "gpt-5"\n', encoding="utf-8")
@@ -1031,6 +1053,33 @@ class TestDoctorCommand:
proxy = next(c for c in json.loads(result.output)["checks"] if c["name"] == "proxy")
assert "a Headroom proxy IS running on port 6767" in proxy["summary"]
@pytest.mark.parametrize(
("relative", "content"),
[
("project-codex.toml", 'openai_base_url = "http://127.0.0.1:6767/v1"\n'),
(
".claude/settings.local.json",
json.dumps({"env": {"ANTHROPIC_BASE_URL": "http://127.0.0.1:6767"}}),
),
],
)
def test_names_the_live_port_only_a_project_config_routes_to(
self, runner, isolated, monkeypatch, relative, content
):
monkeypatch.setenv("HEADROOM_PORT_DISCOVERY", "1")
monkeypatch.chdir(isolated)
path = isolated / relative
path.parent.mkdir(exist_ok=True)
path.write_text(content, encoding="utf-8")
def desktop_probe(url, timeout=2.0):
return LIVEZ_OK if url == "http://127.0.0.1:6767/livez" else None
monkeypatch.setattr(doctor_mod, "probe_json", desktop_probe)
result = runner.invoke(main, ["doctor", "--json"])
proxy = next(c for c in json.loads(result.output)["checks"] if c["name"] == "proxy")
assert "a Headroom proxy IS running on port 6767" in proxy["summary"]
class TestCostTrackerBudgetKeys:
def test_stats_exposes_budget_config(self):