Skip to content

Commit dfe9372

Browse files
committed
fix(codex): accept multi-line inline tables in Codex config.toml
1 parent e995eb9 commit dfe9372

11 files changed

Lines changed: 142 additions & 17 deletions

File tree

‎headroom/cli/init.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from headroom.install.supervisors import start_supervisor
5959
from headroom.providers.claude import TOOL_SEARCH_DEFAULT, TOOL_SEARCH_ENV
6060
from headroom.providers.claude.runtime import TOOL_SEARCH_FOUNDRY_DEFAULT
61+
from headroom.providers.codex.config_toml import loads_codex_config
6162
from headroom.providers.codex.install import (
6263
CodexAuthConfigError,
6364
build_codex_auth_config,
@@ -347,7 +348,7 @@ def _codex_init_provider_snapshot(content: str) -> str | None:
347348
snapshot = json.loads(match.group(1))
348349
if not isinstance(snapshot, str):
349350
raise ValueError("Invalid Headroom init provider snapshot")
350-
tomllib.loads(snapshot)
351+
loads_codex_config(snapshot)
351352
return snapshot
352353

353354

@@ -482,7 +483,7 @@ def _ensure_codex_provider(path: Path, port: int) -> None:
482483
content, _CODEX_PROVIDER_MARKER_START, _CODEX_PROVIDER_MARKER_END, block, at_root=True
483484
)
484485
path.parent.mkdir(parents=True, exist_ok=True)
485-
tomllib.loads(content)
486+
loads_codex_config(content)
486487
fsutil.write_text(path, content)
487488
# Codex filters its history menu by the active model_provider, so existing
488489
# native threads vanish once we switch to "headroom". Retag them to match the
@@ -511,7 +512,7 @@ def _codex_features(content: str) -> dict[str, Any] | None:
511512
if not content.strip():
512513
return None
513514
try:
514-
parsed = tomllib.loads(content)
515+
parsed = loads_codex_config(content)
515516
except tomllib.TOMLDecodeError:
516517
return None
517518
features = parsed.get("features")

‎headroom/cli/wrap.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
)
114114
from headroom.providers.claude.runtime import TOOL_SEARCH_FOUNDRY_DEFAULT
115115
from headroom.providers.codex import build_launch_env as _build_codex_launch_env
116+
from headroom.providers.codex.config_toml import loads_codex_config
116117
from headroom.providers.codex.install import (
117118
CodexAuthConfigError,
118119
build_codex_auth_config,
@@ -3086,7 +3087,7 @@ def _codex_session_launch_settings(
30863087
"""Build process-local routing while preserving the selected provider id."""
30873088
config_file, _ = _codex_config_paths()
30883089
try:
3089-
config = tomllib.loads(_read_text(config_file)) if config_file.exists() else {}
3090+
config = loads_codex_config(_read_text(config_file)) if config_file.exists() else {}
30903091
except (OSError, tomllib.TOMLDecodeError) as exc:
30913092
raise click.ClickException(
30923093
f"could not read Codex config for session routing: {exc}"
@@ -3718,10 +3719,10 @@ def _build_top_level_block(user_content: str) -> str:
37183719
# name a line the user never wrote. Only the user's part has to
37193720
# parse: stripping a stale Headroom block may be what repairs it.
37203721
try:
3721-
tomllib.loads(content)
3722+
loads_codex_config(content)
37223723
except tomllib.TOMLDecodeError:
37233724
try:
3724-
tomllib.loads(original)
3725+
loads_codex_config(original)
37253726
reason = "it is no longer valid TOML once Headroom's block is removed"
37263727
except tomllib.TOMLDecodeError as exc:
37273728
reason = f"it is not valid TOML ({exc})"
@@ -3769,7 +3770,7 @@ def _build_top_level_block(user_content: str) -> str:
37693770
)
37703771

37713772
# Never replace a usable config with a malformed generated candidate.
3772-
tomllib.loads(content)
3773+
loads_codex_config(content)
37733774
_write_text(config_file, content)
37743775
click.echo(f" Codex config: injected Headroom provider (WS + HTTP) into {config_file}")
37753776
if custom_upstream_base_url:
@@ -4066,7 +4067,7 @@ def _inject_memory_mcp_config(user_id: str) -> None:
40664067
content = mcp_section
40674068

40684069
# Never replace a usable config with a malformed generated candidate.
4069-
tomllib.loads(content)
4070+
loads_codex_config(content)
40704071
_write_text(config_file, content)
40714072
click.echo(f" Memory MCP: registered in {config_file}")
40724073
except Exception as e:

‎headroom/mcp_registry/codex.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Any
1919

2020
from headroom import fsutil
21+
from headroom.providers.codex.config_toml import loads_codex_config
2122

2223
from .base import (
2324
MCPRegistrar,
@@ -121,7 +122,7 @@ def _only_server_changed(old: str, new: str, server_name: str) -> bool:
121122
makes this False and the rewrite is refused instead of written.
122123
"""
123124
try:
124-
before, after = tomllib.loads(old), tomllib.loads(new)
125+
before, after = loads_codex_config(old), loads_codex_config(new)
125126
except tomllib.TOMLDecodeError:
126127
return False
127128
return _without_server(before, server_name) == _without_server(after, server_name)
@@ -255,7 +256,7 @@ def _load_toml(self) -> dict[str, Any]:
255256
# Read via fsutil (UTF-8 with locale fallback) so a config that a
256257
# tool wrote in the system locale (e.g. GBK) still parses instead
257258
# of failing tomllib's UTF-8 requirement. See #733.
258-
data = tomllib.loads(fsutil.read_text(self._config_file))
259+
data = loads_codex_config(fsutil.read_text(self._config_file))
259260
except (tomllib.TOMLDecodeError, OSError):
260261
return {}
261262
return data if isinstance(data, dict) else {}
@@ -276,7 +277,7 @@ def _unmergeable_reason(self, name: str) -> str | None:
276277
if not raw.strip():
277278
return None
278279
try:
279-
data = tomllib.loads(fsutil.read_text(self._config_file))
280+
data = loads_codex_config(fsutil.read_text(self._config_file))
280281
except (tomllib.TOMLDecodeError, OSError) as exc:
281282
return f"{self._config_file} is not valid TOML ({exc})"
282283
if not isinstance(data, dict):
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Parse Codex ``config.toml`` with the syntax Codex itself accepts."""
2+
3+
from __future__ import annotations
4+
5+
import sys
6+
from typing import Any, cast
7+
8+
import tomlkit
9+
from tomlkit.exceptions import TOMLKitError
10+
11+
if sys.version_info >= (3, 11):
12+
import tomllib
13+
else: # pragma: no cover - exercised only on Python 3.10
14+
import tomli as tomllib # type: ignore[no-redef]
15+
16+
17+
def loads_codex_config(content: str) -> dict[str, Any]:
18+
"""Parse Codex config text, falling back to ``tomlkit`` for TOML 1.1 syntax.
19+
20+
Codex accepts TOML 1.1, where an inline table may span several lines and
21+
end with a trailing comma (#4378). ``tomllib`` before Python 3.15 only
22+
implements TOML 1.0 and rejects that, so retry with ``tomlkit`` (0.15+),
23+
which parses it. Text neither parser accepts raises the original
24+
``tomllib.TOMLDecodeError``, so callers keep their existing error handling.
25+
"""
26+
try:
27+
return cast("dict[str, Any]", tomllib.loads(content))
28+
except tomllib.TOMLDecodeError as exc:
29+
try:
30+
return tomlkit.parse(content).unwrap()
31+
except TOMLKitError:
32+
raise exc from None

‎headroom/providers/codex/install.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from headroom.install.models import ConfigScope, DeploymentManifest, ManagedMutation, ToolTarget
2222
from headroom.install.paths import codex_config_path
2323

24+
from .config_toml import loads_codex_config
2425
from .runtime import proxy_base_url
2526
from .threads import retag_to_headroom, retag_to_native
2627

@@ -65,7 +66,7 @@
6566

6667
def _codex_credential_store(config_dir: Path) -> str | None:
6768
try:
68-
config = tomllib.loads((config_dir / "config.toml").read_text(encoding="utf-8"))
69+
config = loads_codex_config((config_dir / "config.toml").read_text(encoding="utf-8"))
6970
except (OSError, tomllib.TOMLDecodeError):
7071
return None
7172
store = config.get("cli_auth_credentials_store")
@@ -261,7 +262,7 @@ def codex_auth_helper_is_referenced(content: str, helper_path: str) -> bool | No
261262
the helper instead of risking deletion of a pre-existing user file.
262263
"""
263264
try:
264-
document = tomllib.loads(content)
265+
document = loads_codex_config(content)
265266
except (tomllib.TOMLDecodeError, TypeError):
266267
return None
267268

@@ -411,7 +412,7 @@ def apply_provider_scope(manifest: DeploymentManifest) -> ManagedMutation | None
411412
existing = _CODEX_PATTERN.sub("", existing)
412413
existing = _strip_root_provider_assignments(existing)
413414
merged = _insert_block_at_root(existing, section)
414-
tomllib.loads(merged)
415+
loads_codex_config(merged)
415416
path.write_text(merged, encoding="utf-8")
416417
# Pull existing native threads into the headroom-provider menu so Codex's
417418
# history list stays whole once it routes through Headroom. Best-effort.

‎headroom/providers/codex/project_context.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import tomli as tomllib
2121

2222
from headroom.memory.storage_router import ProjectResolver, RequestContext
23+
from headroom.providers.codex.config_toml import loads_codex_config
2324
from headroom.providers.codex.threads import _codex_state_db_paths
2425

2526
logger = logging.getLogger(__name__)
@@ -298,7 +299,7 @@ def _state_root(self) -> Path:
298299
return self._configured_sqlite_home
299300
codex_home = Path(os.environ.get("CODEX_HOME", "").strip() or Path.home() / ".codex")
300301
try:
301-
config = tomllib.loads((codex_home / "config.toml").read_text(encoding="utf-8"))
302+
config = loads_codex_config((codex_home / "config.toml").read_text(encoding="utf-8"))
302303
configured_sqlite_home = config.get("sqlite_home")
303304
if isinstance(configured_sqlite_home, str) and configured_sqlite_home.strip():
304305
return Path(configured_sqlite_home).expanduser()

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ dependencies = [
6464
"ast-grep-cli>=0.30.0,!=0.44.1",
6565
"pyyaml>=6.0", # omp wrap: parse/merge omp's models.yml registry
6666
"tomli>=2.0.0; python_version < '3.11'", # tomllib backport for helper scripts
67-
"tomlkit>=0.13.0,<1.0", # Loss-minimizing Codex config recovery
67+
"tomlkit>=0.15.0,<1.0", # Loss-minimizing Codex config recovery
6868
# Verify upstream TLS through the OS trust store (macOS Keychain, Windows
6969
# cert store), where IT installs corporate TLS-inspection roots (Zscaler,
7070
# Netskope). Pure Python, no deps; the same library pip uses by default.

‎tests/test_cli/test_wrap_codex.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,38 @@ def test_codex_session_launch_settings_keep_routing_process_local(
15641564
assert config_file.read_text(encoding="utf-8") == original_config
15651565

15661566

1567+
def test_codex_session_launch_settings_accept_multiline_inline_table(
1568+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
1569+
) -> None:
1570+
# #4378: Codex accepts an inline table split across lines.
1571+
_set_test_home(monkeypatch, tmp_path)
1572+
codex_home = tmp_path / "custom-codex-home"
1573+
codex_home.mkdir()
1574+
monkeypatch.setenv("CODEX_HOME", str(codex_home))
1575+
monkeypatch.setattr(wrap_mod, "_project_name_from_cwd", lambda: None)
1576+
config_file = codex_home / "config.toml"
1577+
original_config = (
1578+
'model = "gpt-5"\n\n'
1579+
"[mcp_servers.dingtalk]\n"
1580+
'command = "npx"\n'
1581+
"env = {\n"
1582+
' DINGTALK_Client_ID = "**********",\n'
1583+
" \n"
1584+
" }\n"
1585+
)
1586+
config_file.write_text(original_config, encoding="utf-8")
1587+
1588+
args, env, _ = wrap_mod._codex_session_launch_settings(
1589+
port=9898,
1590+
codex_args=("exec", "hello"),
1591+
environ={"CODEX_HOME": str(codex_home)},
1592+
)
1593+
1594+
assert args[:2] == ("--config", 'openai_base_url="http://127.0.0.1:9898/v1"')
1595+
assert env["OPENAI_BASE_URL"] == "http://127.0.0.1:9898/v1"
1596+
assert config_file.read_text(encoding="utf-8") == original_config
1597+
1598+
15671599
def test_codex_session_launch_settings_preserve_custom_provider_identity(
15681600
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
15691601
) -> None:

‎tests/test_mcp_registry/test_codex_registrar.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,27 @@ def test_register_appends_to_existing_config_preserves_other_keys(tmp_path: Path
228228
assert parsed["mcp_servers"]["headroom"]["command"] == _RESOLVED_COMMAND[0]
229229

230230

231+
def test_register_accepts_multiline_inline_table(tmp_path: Path) -> None:
232+
# Codex accepts an inline table split across lines (#4378); registering must
233+
# not refuse the file as invalid TOML, and the user's text must be kept as is.
234+
cfg = _config_path(tmp_path)
235+
cfg.parent.mkdir()
236+
original = (
237+
"[mcp_servers.dingtalk]\n"
238+
'command = "npx"\n'
239+
"env = {\n"
240+
' DINGTALK_Client_ID = "**********",\n'
241+
" \n"
242+
" }\n"
243+
)
244+
cfg.write_text(original)
245+
result = _make_registrar(tmp_path).register_server(_spec())
246+
assert result.status == RegisterStatus.REGISTERED, result.detail
247+
text = cfg.read_text()
248+
assert text.startswith(original)
249+
assert "[mcp_servers.headroom]" in text
250+
251+
231252
def test_register_includes_env_subtable(tmp_path: Path) -> None:
232253
spec = _spec(env={"HEADROOM_PROXY_URL": "http://127.0.0.1:9000"})
233254
_make_registrar(tmp_path).register_server(spec)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Tests for parsing Codex config.toml with the syntax Codex accepts."""
2+
3+
from __future__ import annotations
4+
5+
import sys
6+
7+
import pytest
8+
9+
from headroom.providers.codex.config_toml import loads_codex_config
10+
11+
if sys.version_info >= (3, 11):
12+
import tomllib
13+
else: # pragma: no cover
14+
import tomli as tomllib
15+
16+
# Shape from #4378: an inline table split across lines with a trailing comma.
17+
MULTILINE_INLINE_TABLE = (
18+
"[mcp_servers.dingtalk]\n"
19+
'command = "npx"\n'
20+
"env = {\n"
21+
' DINGTALK_Client_ID = "**********",\n'
22+
" \n"
23+
" }\n"
24+
)
25+
26+
27+
def test_loads_codex_config_accepts_multiline_inline_table() -> None:
28+
assert loads_codex_config(MULTILINE_INLINE_TABLE) == {
29+
"mcp_servers": {"dingtalk": {"command": "npx", "env": {"DINGTALK_Client_ID": "**********"}}}
30+
}
31+
32+
33+
def test_loads_codex_config_raises_tomllib_error_for_invalid_toml() -> None:
34+
with pytest.raises(tomllib.TOMLDecodeError):
35+
loads_codex_config("env = {\n")

0 commit comments

Comments
 (0)