Files
headroom/tests/test_install/test_codex_install.py
4a851ed254 fix(codex): accept multi-line inline tables in Codex config.toml (#4436)
## Description

Codex accepts an inline table split across lines, with a trailing comma,
in `~/.codex/config.toml`:

```toml
env = {
  DINGTALK_Client_ID = "**********",
}
```

That is TOML 1.1 syntax. Headroom reads the Codex config with the
standard library `tomllib`, which only implements TOML 1.0 before Python
3.15, so it rejects the file with `Invalid initial character for a key
part`. The result is what the issue shows: `headroom wrap codex` and
`headroom mcp install` refuse to register MCP servers ("is not valid
TOML ... refusing to overwrite") and session routing fails with "could
not read Codex config for session routing", even though Codex itself is
happy with the file.

`tomlkit` is already a runtime dependency (used for Codex config
recovery) and parses this syntax from 0.15.0. 0.13.3 and 0.14.0 both
fail on it (`Empty key at line 1 col 7`).

The fix adds one helper, `loads_codex_config()`, that parses with
`tomllib` first and only on `TOMLDecodeError` retries with `tomlkit`. If
`tomlkit` rejects the text too, it re-raises the original
`tomllib.TOMLDecodeError`, so every caller keeps its existing `except
tomllib.TOMLDecodeError` handling and its error messages unchanged. All
places that parse the user's Codex config now go through it.

The follow-up commit (`dc193870`) fixes a problem the first commit
exposed: once a multi-line inline table parses, a `profiles = {` table
can carry per-profile `model_provider` / `openai_base_url` keys on their
own indented lines, and the line-based "root key" edits in init, the
provider-scope install and persistent wrap mistook those nested lines
for root assignments. Init and install deleted the profile's provider,
and wrap rewrote it to `headroom` and then skipped adding a real root
`model_provider`. Those edits now only touch lines that are actual root
assignments.

Fixes #4378

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `headroom/providers/codex/config_toml.py` (new):
`loads_codex_config(content)`, `tomllib` first, `tomlkit` fallback,
original `tomllib` error if both fail.
- Routed the Codex config parse sites through it (15 call sites, 16
parse calls):
- `headroom/cli/wrap.py`: session routing read, the user-content check
and its fallback in `_inject_codex_provider_config`, and the two "never
write a malformed candidate" checks.
- `headroom/cli/init.py`: provider snapshot check, the candidate check
in `_ensure_codex_provider`, and `_codex_features`.
- `headroom/providers/codex/install.py`: `_codex_credential_store`,
`codex_auth_helper_is_referenced`, and the merged-config check before
writing.
- `headroom/providers/codex/project_context.py`: the `sqlite_home`
lookup.
- `headroom/mcp_registry/codex.py`: `_load_toml`, `_unmergeable_reason`,
and both sides of `_only_server_changed`. This registrar class is shared
with Grok, so a Grok `config.toml` with the same syntax is accepted too.
- Left on `tomllib` on purpose: `_table_path` (parses a single `[table]`
header line) and the auth snippet check in `build_codex_auth_config`
(validates text Headroom generates itself).
- `pyproject.toml` / `uv.lock`: raise the `tomlkit` minimum from
`>=0.13.0` to `>=0.15.0`, the first release that parses multi-line
inline tables. The lock already resolved 0.15.0, so `uv lock` only
changed the specifier line.
- Write paths: the user's text is never re-serialized by this change.
The registrar and the wrap/init injectors still append or splice marker
blocks as text, and the existing `tomlkit` round-trip in wrap keeps the
multi-line table byte for byte (checked below).
- Tests:
- `tests/test_provider_codex_config_toml.py`: the helper on the exact
shape from the issue (multi-line, trailing comma, whitespace-only line,
indented `}`), and that invalid TOML still raises
`tomllib.TOMLDecodeError`.
-
`tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table`:
registering into that config succeeds and the user's text is kept as a
byte-for-byte prefix.
-
`tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table`:
session routing reads that config and leaves the file untouched.
- Follow-up commit `dc193870` (multi-line inline profile overrides):
- `headroom/providers/codex/config_toml.py`:
`root_assignment_lines(content)` returns the indexes of lines that start
a key/value pair at the document root. It stops at the first table
header and skips lines inside a multi-line inline table, array or
string, with a small scanner that tracks quotes, comments and bracket
depth.
- `headroom/cli/init.py` (`_ensure_codex_provider`) and
`headroom/providers/codex/install.py`
(`_strip_root_provider_assignments`) only drop `model_provider` /
`openai_base_url` lines that are root assignments.
- `headroom/cli/wrap.py`: `_redirect_existing_top_level_keys` and
`_has_redirectable_top_level_key` only look at root assignments. Before,
they matched the key on any line, so a `[profiles.work]` section
override was also rewritten to `headroom` and counted as a root key;
that now stays the user's, matching how init, install and
`_strip_codex_headroom_blocks` already treat `[profiles.*]`.
- Tests:
`test_root_assignment_lines_skip_keys_nested_in_multiline_inline_table`,
`test_ensure_codex_provider_preserves_multiline_inline_profile_overrides`
(init),
`test_provider_scope_preserves_multiline_inline_profile_overrides`
(install) and `test_inject_preserves_multiline_inline_profile_overrides`
(wrap).

## Testing

- [x] Unit tests pass (`pytest`) - Codex, MCP registry, init and Grok
suites: 774 passed, 2 failed (the 2 failures are in
`tests/test_codex_responses_passthrough_bytes.py` and fail the same way
on `main` without this change)
- [x] Linting passes (`ruff check .`) - `ruff check` and `ruff format
--check` on the 9 changed Python files
- [x] Type checking passes (`mypy headroom`) - `mypy` on the 6 changed
source files, no issues
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

New end-to-end tests on `main` (fix stashed):

```text
$ pytest tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table -q
E   AssertionError: .../.codex/config.toml is not valid TOML (Invalid initial character for a key part (at line 3, column 8)); refusing to overwrite. Fix or remove the file, then re-run.
E   click.exceptions.ClickException: could not read Codex config for session routing: Invalid initial character for a key part (at line 5, column 8)
============================== 2 failed in 0.38s ===============================
```

(`tests/test_provider_codex_config_toml.py` cannot import on `main`
because the helper does not exist yet.)

With the fix:

```text
$ pytest tests/test_provider_codex_config_toml.py tests/test_mcp_registry/test_codex_registrar.py::test_register_accepts_multiline_inline_table tests/test_cli/test_wrap_codex.py::test_codex_session_launch_settings_accept_multiline_inline_table -q
============================== 4 passed in 0.56s ===============================

$ pytest tests/test_provider_codex_config_toml.py tests/test_mcp_registry tests/test_cli/test_wrap_codex.py tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_provider_codex*.py tests/test_provider_grok*.py tests/test_codex*.py -q
FAILED tests/test_codex_responses_passthrough_bytes.py::test_unmutated_zstd_post_forwards_decoded_bytes_and_strips_content_encoding - concurrent.futures._base.CancelledError
FAILED tests/test_codex_responses_passthrough_bytes.py::test_unmutated_plain_post_passes_original_bytes_through - concurrent.futures._base.CancelledError
================== 2 failed, 774 passed, 8 warnings in 50.43s ==================

$ pytest tests/test_codex_responses_passthrough_bytes.py -q   # on main, without this change
========================= 2 failed, 1 warning in 4.09s =========================

$ mypy headroom/cli/init.py headroom/cli/wrap.py headroom/mcp_registry/codex.py headroom/providers/codex/install.py headroom/providers/codex/project_context.py headroom/providers/codex/config_toml.py
Success: no issues found in 6 source files
```

Follow-up commit, the three new write-path tests on the first commit
(`dfe93720`, source changes reverted):

```text
$ pytest tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_cli/test_wrap_codex.py -k multiline_inline_profile -q
FAILED tests/test_cli/test_init_cli.py::test_ensure_codex_provider_preserves_multiline_inline_profile_overrides - AssertionError: assert {} == {'model_provi...y.example/v1'}
FAILED tests/test_install/test_codex_install.py::test_provider_scope_preserves_multiline_inline_profile_overrides - AssertionError: assert {} == {'model_provider': 'company'}
FAILED tests/test_cli/test_wrap_codex.py::TestSubscriptionRouting::test_inject_preserves_multiline_inline_profile_overrides - KeyError: 'model_provider'
====================== 3 failed, 220 deselected in 0.56s =======================
```

With `dc193870`:

```text
$ pytest tests/test_provider_codex_config_toml.py tests/test_cli/test_init_cli.py tests/test_install/test_codex_install.py tests/test_cli/test_wrap_codex.py -k "multiline_inline_profile or root_assignment" -q
======================= 4 passed, 222 deselected in 0.27s =======================

$ pytest tests/test_provider_codex_config_toml.py tests/test_cli/test_init_cli.py tests/test_install/ -q
================== 346 passed, 13 skipped, 1 warning in 3.94s ==================
$ pytest tests/test_cli/test_wrap_codex.py tests/test_cli/test_port_alignment.py -q
============================= 167 passed in 6.01s ==============================
$ pytest tests/test_mcp_registry/ tests/test_provider_codex_install.py tests/test_cli/test_install_cli.py tests/test_cli/test_recover_codex.py -q
============================= 364 passed in 4.71s ==============================
$ pytest tests/test_cli/test_wrap_persistent.py tests/test_codex_project_context.py tests/test_cli/test_wrap_stale_marker.py tests/test_cli/test_wrap_dead_marker_selfheal.py tests/test_provider_codex_runtime.py tests/test_install_tool_env_reconcile.py -q
======================== 4 failed, 261 passed in 9.01s =========================
```

The 4 failures are in `tests/test_cli/test_wrap_persistent.py`
(`AttributeError: '_Manifest' object has no attribute 'runtime_kind'`)
and fail the same way with this PR's changes stashed. `ruff check`,
`ruff format --check` (8 files) and `mypy` on the 4 changed source files
are clean.

tomlkit versions on the issue's snippet:

```text
0.13.3 FAIL Empty key at line 1 col 7
0.14.0 FAIL Empty key at line 1 col 7
0.15.0 {'env': {'A': 'x'}}
```

## Real Behavior Proof

- Environment: macOS 26.2 (Apple Silicon), Python 3.14.7, repo venv with
the `mcp` package installed, a throwaway `HOME`/`CODEX_HOME` whose
`config.toml` holds the issue's config:

  ```toml
  [mcp_servers.dingtalk]
  command = "npx"
  env = {
    DINGTALK_Client_ID = "**********",
    
      }
  ```

- Exact command / steps: `HOME=<tmp>/home CODEX_HOME=<tmp>/home/.codex
python -m headroom.cli mcp install --agent codex`, then compare the
start of the resulting file with the original bytes and count
`[mcp_servers.headroom]` tables. Also called
`wrap._inject_codex_provider_config(8787)` and
`init._ensure_codex_provider(path, 8787)` on the same config to check
the write paths.
- Observed result: before (`main`):

  ```text
codex: install failed (failed): <tmp>/home/.codex/config.toml is not
valid TOML (Invalid initial character for a key part (at line 3, column
8)); refusing to overwrite. Fix or remove the file, then re-run.
  ```

  After:

  ```text
  codex: registered (restart the agent if it was already running)
  ```

The user's original bytes are an exact prefix of the new file and one
`[mcp_servers.headroom]` table was appended. The wrap injector printed
`Codex config: injected Headroom provider (WS + HTTP)`, and in both the
wrap and init outputs the user's multi-line table is still present
verbatim, the file parses, `env` is `{'DINGTALK_Client_ID':
'**********'}` and `model_provider` is `headroom`.
- Follow-up commit: with a throwaway `HOME`/`CODEX_HOME` holding

  ```toml
  model_provider = "openai"
  profiles = {
    work = {
      model_provider = "company",
    },
  }
  ```

I ran `init._ensure_codex_provider`, `install.apply_provider_scope`
(provider scope) and `wrap._inject_codex_provider_config` (same config
without the root key) and parsed the result:

  ```text
  == before (dfe93720)
  init:   root = headroom  work = {}
  install: root = headroom  work = {}
  wrap:   root = None  work = {'model_provider': 'headroom'}
  == after (dc193870)
  init:   root = headroom  work = {'model_provider': 'company'}
  install: root = headroom  work = {'model_provider': 'company'}
  wrap:   root = headroom  work = {'model_provider': 'company'}
  ```

- Not tested: a full `headroom wrap codex` run with a live proxy and the
real Codex CLI, and Python 3.10 (where `tomli` is the first parser; the
`tomlkit` fallback is the same there).

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: only for Codex configs `tomllib`
rejects but `tomlkit` 0.15 accepts (TOML 1.1 multi-line inline tables).
Valid TOML 1.0 still goes through `tomllib` exactly as before, and text
both parsers reject fails with the same error as before.
- Kill switch / disable path: none needed; the fallback only runs after
`tomllib` has already failed.
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert the two commits

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`, it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

N/A

## Additional Notes

- Documentation: no user-facing docs describe which TOML syntax the
Codex config may use, so nothing to update.
- Python 3.15's `tomllib` implements TOML 1.1 and parses this config
directly (as noted in the issue thread), so on 3.15 the fallback simply
never runs.
- `tomlkit` is a bit more permissive than `tomllib` only where TOML 1.1
is; it still rejects duplicate keys and malformed tables, so the
existing "refuse to overwrite an unparseable file" guards still trigger
for genuinely broken configs (covered by the existing registrar tests).
- Dependency review for the `tomlkit` minimum bump (`>=0.13.0,<1.0` to
`>=0.15.0,<1.0`):
- Why this package: it is already a direct runtime dependency (Codex
config recovery and the memory MCP block edit in `wrap.py`), so no new
package enters the tree. Writing a TOML 1.1 parser here is not worth it,
and `tomllib` only gains TOML 1.1 in Python 3.15.
- Who maintains it: the Poetry project (`python-poetry/tomlkit`, MIT),
with Frost Ming and Sébastien Eustace as the main maintainers. It is
actively released: 0.13.3 (2025-06), 0.14.0 (2026-01), 0.15.0 (2026-05),
0.15.1 (2026-07), 0.15.2 (2026-10). No GitHub security advisories are
published for the repo.
- Install surface: unchanged. Pure Python, a single `py3-none-any` wheel
(about 41 KB for 0.15.0), no runtime dependencies, no native code, no
install-time or runtime network access. `uv.lock` already resolved
0.15.0 before this PR, so the locked version and hashes do not change;
only the specifier line moves.
- Why this version: required new functionality. 0.15.0 is the first
release with TOML 1.1 support ("feat(spec): update to toml spec v1.1",
python-poetry/tomlkit#456), which is what parses multi-line inline
tables with trailing commas. 0.13.3 and 0.14.0 both reject the issue's
snippet (`Empty key at line 1 col 7`, output above). 0.15.0 also
includes an upstream parser hang fix (python-poetry/tomlkit#470).

---------

Co-authored-by: Abelo9996 <Abelo9996@users.noreply.github.com>
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
2026-10-28 23:31:18 -05:00

274 lines
10 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
import json
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
if sys.version_info >= (3, 11):
import tomllib
else: # pragma: no cover - exercised only on Python 3.10
import tomli as tomllib # type: ignore[no-redef]
from headroom.install.models import DeploymentManifest
from headroom.providers.codex.config_toml import loads_codex_config
from headroom.providers.codex.install import (
CodexAuthConfigError,
_codex_login_status,
apply_provider_scope,
build_codex_auth_config,
build_provider_section,
codex_auth_helper_path,
codex_uses_chatgpt_auth,
revert_provider_scope,
)
def _manifest(tmp_path: Path) -> DeploymentManifest:
return DeploymentManifest(
profile="test",
preset="persistent-service",
runtime_kind="python",
supervisor_kind="service",
scope="provider",
provider_mode="manual",
targets=["codex"],
port=8787,
host="127.0.0.1",
backend="anthropic",
memory_db_path=str(tmp_path / "memory.db"),
tool_envs={},
)
def _login_status(
stdout: str = "",
*,
stderr: str = "",
returncode: int = 0,
) -> SimpleNamespace:
return SimpleNamespace(returncode=returncode, stdout=stdout, stderr=stderr)
def test_keyring_chatgpt_auth_emits_provider_flag(monkeypatch, tmp_path: Path) -> None:
config = tmp_path / "config.toml"
config.write_text('cli_auth_credentials_store = "keyring"\n', encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
monkeypatch.setattr(
"headroom.providers.codex.install.run",
lambda *args, **kwargs: _login_status(stderr="Logged in using ChatGPT\n"),
)
apply_provider_scope(_manifest(tmp_path))
assert "requires_openai_auth = true" in config.read_text(encoding="utf-8")
def test_keyring_non_chatgpt_auth_keeps_provider_flag_off(monkeypatch, tmp_path: Path) -> None:
config = tmp_path / "config.toml"
config.write_text('cli_auth_credentials_store = "keyring"\n', encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
monkeypatch.setattr(
"headroom.providers.codex.install.run",
lambda *args, **kwargs: _login_status(stderr="Logged in using API key\n"),
)
apply_provider_scope(_manifest(tmp_path))
assert "requires_openai_auth" not in config.read_text(encoding="utf-8")
def test_auto_store_chatgpt_auth_is_detected(monkeypatch, tmp_path: Path) -> None:
auth = tmp_path / "auth.json"
(tmp_path / "config.toml").write_text('cli_auth_credentials_store = "auto"\n', encoding="utf-8")
monkeypatch.setattr(
"headroom.providers.codex.install.run",
lambda *args, **kwargs: _login_status(stderr="Logged in using ChatGPT\n"),
)
assert codex_uses_chatgpt_auth(auth) is True
def test_file_backed_auth_preserves_existing_modes(tmp_path: Path) -> None:
auth = tmp_path / "auth.json"
auth.write_text('{"auth_mode": "CHATGPT"}', encoding="utf-8")
assert codex_uses_chatgpt_auth(auth) is True
auth.write_text('{"auth_mode": "apikey", "tokens": {"account_id": "acct"}}', encoding="utf-8")
assert codex_uses_chatgpt_auth(auth) is False
def test_api_key_auth_is_persisted_as_a_codex_command(tmp_path: Path, monkeypatch) -> None:
config = tmp_path / "config.toml"
auth = tmp_path / "auth.json"
config.write_text('model = "gpt-5"\n', encoding="utf-8")
auth.write_text('{"OPENAI_API_KEY": "sk-test-only"}', encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
mutation = apply_provider_scope(_manifest(tmp_path))
assert mutation is not None
content = config.read_text(encoding="utf-8")
helper = codex_auth_helper_path(auth, config_path=config)
assert "auth = { command =" in content
assert tomllib.loads(content)["model_providers"]["headroom"]["auth"]["args"] == [
str(helper.resolve())
]
assert "sk-test-only" not in content
assert helper.exists()
revert_provider_scope(mutation, _manifest(tmp_path))
assert not helper.exists()
def test_provider_scope_preserves_multiline_inline_profile_overrides(
tmp_path: Path, monkeypatch
) -> None:
config = tmp_path / "config.toml"
config.write_text(
'model_provider = "openai"\n'
"profiles = {\n"
" work = {\n"
' model_provider = "company",\n'
" },\n"
"}\n",
encoding="utf-8",
)
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
apply_provider_scope(_manifest(tmp_path))
parsed = loads_codex_config(config.read_text(encoding="utf-8"))
assert parsed["model_provider"] == "headroom"
assert parsed["profiles"]["work"] == {"model_provider": "company"}
@pytest.mark.parametrize("sep", ["
", "\x85"])
def test_provider_scope_keeps_single_line_string_with_unicode_separator(
tmp_path: Path, monkeypatch, sep: str
) -> None:
config = tmp_path / "config.toml"
notes = f'hello{sep}model_provider = "company"{sep}world'
config.write_text(f"notes = '{notes}'\n\n[features]\nhooks = true\n", encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
apply_provider_scope(_manifest(tmp_path))
parsed = loads_codex_config(config.read_text(encoding="utf-8"))
assert parsed["notes"] == notes
assert parsed["model_provider"] == "headroom"
def test_persistent_install_helper_collision_preserves_config(tmp_path: Path, monkeypatch) -> None:
config = tmp_path / "config.toml"
auth = tmp_path / "auth.json"
auth.write_text('{"OPENAI_API_KEY": "sk-test-only"}', encoding="utf-8")
original = 'model_provider = "openai"\n'
config.write_text(original, encoding="utf-8")
helper = codex_auth_helper_path(auth, config_path=config)
helper.write_text("user content", encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
with pytest.raises(CodexAuthConfigError, match="Codex provider configuration was not updated"):
apply_provider_scope(_manifest(tmp_path))
assert config.read_text(encoding="utf-8") == original
assert helper.read_text(encoding="utf-8") == "user content"
def test_persistent_uninstall_preserves_project_authentication(tmp_path: Path, monkeypatch) -> None:
from headroom.cli.init import _ensure_codex_provider
monkeypatch.setenv("CODEX_HOME", str(tmp_path))
config = tmp_path / "config.toml"
auth = tmp_path / "auth.json"
auth.write_text('{"OPENAI_API_KEY": "sk-test-only"}', encoding="utf-8")
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
project_config = tmp_path / "project" / ".codex" / "config.toml"
_ensure_codex_provider(project_config, 8787)
project_helper = codex_auth_helper_path(auth, config_path=project_config)
mutation = apply_provider_scope(_manifest(tmp_path))
assert mutation is not None
revert_provider_scope(mutation, _manifest(tmp_path))
assert project_helper.exists()
assert tomllib.loads(project_config.read_text(encoding="utf-8"))["model_providers"]["headroom"][
"auth"
]["args"] == [str(project_helper.resolve())]
assert not codex_auth_helper_path(auth, config_path=config).exists()
def test_uninstall_retains_legacy_shared_helper(tmp_path: Path) -> None:
from headroom.install.models import ManagedMutation
auth = tmp_path / "auth.json"
auth.write_text('{"OPENAI_API_KEY": "sk-test-only"}', encoding="utf-8")
build_codex_auth_config(auth)
legacy_helper = tmp_path / ".headroom-codex-auth.py"
legacy_helper.write_text(codex_auth_helper_path(auth).read_text(encoding="utf-8"))
config = tmp_path / "config.toml"
config.write_text(
"# --- Headroom persistent provider ---\n"
"[model_providers.headroom]\n"
f'auth = {{ command = "python", args = [{json.dumps(str(legacy_helper), ensure_ascii=False)}] }}\n'
"# --- end Headroom persistent provider ---\n",
encoding="utf-8",
)
revert_provider_scope(
ManagedMutation(target="codex", kind="toml-block", path=str(config)), _manifest(tmp_path)
)
assert legacy_helper.exists()
def test_legacy_file_backed_account_id_stays_supported(tmp_path: Path) -> None:
auth = tmp_path / "auth.json"
auth.write_text('{"tokens": {"account_id": "acct"}}', encoding="utf-8")
assert codex_uses_chatgpt_auth(auth) is True
def test_missing_or_failed_login_status_fails_closed(monkeypatch, tmp_path: Path) -> None:
(tmp_path / "config.toml").write_text(
'cli_auth_credentials_store = "keyring"\n', encoding="utf-8"
)
monkeypatch.setattr(
"headroom.providers.codex.install.run",
lambda *args, **kwargs: (_ for _ in ()).throw(OSError()),
)
assert codex_uses_chatgpt_auth(tmp_path / "auth.json") is False
def test_login_status_probe_uses_codex_contract(monkeypatch, tmp_path: Path) -> None:
calls: list[tuple[list[str], dict]] = []
def probe(command: list[str], **kwargs):
calls.append((command, kwargs))
return _login_status(stderr="Logged in using ChatGPT\n")
monkeypatch.setattr("headroom.providers.codex.install.run", probe)
assert _codex_login_status(tmp_path) is True
assert calls[0][0] == ["codex", "login", "status"]
assert calls[0][1]["timeout"] == 3
assert calls[0][1]["env"]["CODEX_HOME"] == str(tmp_path)
assert calls[0][1]["capture_output"] is True
def test_login_status_probe_accepts_stdout_or_stderr(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setattr(
"headroom.providers.codex.install.run",
lambda *args, **kwargs: _login_status(stdout="Logged in using ChatGPT\n"),
)
assert _codex_login_status(tmp_path) is True
def test_provider_section_still_emits_flag_when_requested() -> None:
assert "requires_openai_auth = true" in build_provider_section(
port=8787, name="Headroom", requires_openai_auth=True
)