mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(codex): accept multi-line inline tables in Codex config.toml
This commit is contained in:
@@ -58,6 +58,7 @@ from headroom.install.state import ManifestError, load_manifest, save_manifest
|
||||
from headroom.install.supervisors import start_supervisor
|
||||
from headroom.providers.claude import TOOL_SEARCH_DEFAULT, TOOL_SEARCH_ENV
|
||||
from headroom.providers.claude.runtime import TOOL_SEARCH_FOUNDRY_DEFAULT
|
||||
from headroom.providers.codex.config_toml import loads_codex_config
|
||||
from headroom.providers.codex.install import (
|
||||
CodexAuthConfigError,
|
||||
build_codex_auth_config,
|
||||
@@ -347,7 +348,7 @@ def _codex_init_provider_snapshot(content: str) -> str | None:
|
||||
snapshot = json.loads(match.group(1))
|
||||
if not isinstance(snapshot, str):
|
||||
raise ValueError("Invalid Headroom init provider snapshot")
|
||||
tomllib.loads(snapshot)
|
||||
loads_codex_config(snapshot)
|
||||
return snapshot
|
||||
|
||||
|
||||
@@ -482,7 +483,7 @@ def _ensure_codex_provider(path: Path, port: int) -> None:
|
||||
content, _CODEX_PROVIDER_MARKER_START, _CODEX_PROVIDER_MARKER_END, block, at_root=True
|
||||
)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tomllib.loads(content)
|
||||
loads_codex_config(content)
|
||||
fsutil.write_text(path, content)
|
||||
# Codex filters its history menu by the active model_provider, so existing
|
||||
# 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:
|
||||
if not content.strip():
|
||||
return None
|
||||
try:
|
||||
parsed = tomllib.loads(content)
|
||||
parsed = loads_codex_config(content)
|
||||
except tomllib.TOMLDecodeError:
|
||||
return None
|
||||
features = parsed.get("features")
|
||||
|
||||
@@ -113,6 +113,7 @@ from headroom.providers.claude import (
|
||||
)
|
||||
from headroom.providers.claude.runtime import TOOL_SEARCH_FOUNDRY_DEFAULT
|
||||
from headroom.providers.codex import build_launch_env as _build_codex_launch_env
|
||||
from headroom.providers.codex.config_toml import loads_codex_config
|
||||
from headroom.providers.codex.install import (
|
||||
CodexAuthConfigError,
|
||||
build_codex_auth_config,
|
||||
@@ -3086,7 +3087,7 @@ def _codex_session_launch_settings(
|
||||
"""Build process-local routing while preserving the selected provider id."""
|
||||
config_file, _ = _codex_config_paths()
|
||||
try:
|
||||
config = tomllib.loads(_read_text(config_file)) if config_file.exists() else {}
|
||||
config = loads_codex_config(_read_text(config_file)) if config_file.exists() else {}
|
||||
except (OSError, tomllib.TOMLDecodeError) as exc:
|
||||
raise click.ClickException(
|
||||
f"could not read Codex config for session routing: {exc}"
|
||||
@@ -3718,10 +3719,10 @@ def _inject_codex_provider_config(port: int) -> str | None:
|
||||
# name a line the user never wrote. Only the user's part has to
|
||||
# parse: stripping a stale Headroom block may be what repairs it.
|
||||
try:
|
||||
tomllib.loads(content)
|
||||
loads_codex_config(content)
|
||||
except tomllib.TOMLDecodeError:
|
||||
try:
|
||||
tomllib.loads(original)
|
||||
loads_codex_config(original)
|
||||
reason = "it is no longer valid TOML once Headroom's block is removed"
|
||||
except tomllib.TOMLDecodeError as exc:
|
||||
reason = f"it is not valid TOML ({exc})"
|
||||
@@ -3769,7 +3770,7 @@ def _inject_codex_provider_config(port: int) -> str | None:
|
||||
)
|
||||
|
||||
# Never replace a usable config with a malformed generated candidate.
|
||||
tomllib.loads(content)
|
||||
loads_codex_config(content)
|
||||
_write_text(config_file, content)
|
||||
click.echo(f" Codex config: injected Headroom provider (WS + HTTP) into {config_file}")
|
||||
if custom_upstream_base_url:
|
||||
@@ -4066,7 +4067,7 @@ def _inject_memory_mcp_config(user_id: str) -> None:
|
||||
content = mcp_section
|
||||
|
||||
# Never replace a usable config with a malformed generated candidate.
|
||||
tomllib.loads(content)
|
||||
loads_codex_config(content)
|
||||
_write_text(config_file, content)
|
||||
click.echo(f" Memory MCP: registered in {config_file}")
|
||||
except Exception as e:
|
||||
|
||||
@@ -18,6 +18,7 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from headroom import fsutil
|
||||
from headroom.providers.codex.config_toml import loads_codex_config
|
||||
|
||||
from .base import (
|
||||
MCPRegistrar,
|
||||
@@ -121,7 +122,7 @@ def _only_server_changed(old: str, new: str, server_name: str) -> bool:
|
||||
makes this False and the rewrite is refused instead of written.
|
||||
"""
|
||||
try:
|
||||
before, after = tomllib.loads(old), tomllib.loads(new)
|
||||
before, after = loads_codex_config(old), loads_codex_config(new)
|
||||
except tomllib.TOMLDecodeError:
|
||||
return False
|
||||
return _without_server(before, server_name) == _without_server(after, server_name)
|
||||
@@ -255,7 +256,7 @@ class _TomlMarkerRegistrar(MCPRegistrar):
|
||||
# Read via fsutil (UTF-8 with locale fallback) so a config that a
|
||||
# tool wrote in the system locale (e.g. GBK) still parses instead
|
||||
# of failing tomllib's UTF-8 requirement. See #733.
|
||||
data = tomllib.loads(fsutil.read_text(self._config_file))
|
||||
data = loads_codex_config(fsutil.read_text(self._config_file))
|
||||
except (tomllib.TOMLDecodeError, OSError):
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
@@ -276,7 +277,7 @@ class _TomlMarkerRegistrar(MCPRegistrar):
|
||||
if not raw.strip():
|
||||
return None
|
||||
try:
|
||||
data = tomllib.loads(fsutil.read_text(self._config_file))
|
||||
data = loads_codex_config(fsutil.read_text(self._config_file))
|
||||
except (tomllib.TOMLDecodeError, OSError) as exc:
|
||||
return f"{self._config_file} is not valid TOML ({exc})"
|
||||
if not isinstance(data, dict):
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Parse Codex ``config.toml`` with the syntax Codex itself accepts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from typing import Any, cast
|
||||
|
||||
import tomlkit
|
||||
from tomlkit.exceptions import TOMLKitError
|
||||
|
||||
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]
|
||||
|
||||
|
||||
def loads_codex_config(content: str) -> dict[str, Any]:
|
||||
"""Parse Codex config text, falling back to ``tomlkit`` for TOML 1.1 syntax.
|
||||
|
||||
Codex accepts TOML 1.1, where an inline table may span several lines and
|
||||
end with a trailing comma (#4378). ``tomllib`` before Python 3.15 only
|
||||
implements TOML 1.0 and rejects that, so retry with ``tomlkit`` (0.15+),
|
||||
which parses it. Text neither parser accepts raises the original
|
||||
``tomllib.TOMLDecodeError``, so callers keep their existing error handling.
|
||||
"""
|
||||
try:
|
||||
return cast("dict[str, Any]", tomllib.loads(content))
|
||||
except tomllib.TOMLDecodeError as exc:
|
||||
try:
|
||||
return tomlkit.parse(content).unwrap()
|
||||
except TOMLKitError:
|
||||
raise exc from None
|
||||
@@ -21,6 +21,7 @@ from headroom._subprocess import run
|
||||
from headroom.install.models import ConfigScope, DeploymentManifest, ManagedMutation, ToolTarget
|
||||
from headroom.install.paths import codex_config_path
|
||||
|
||||
from .config_toml import loads_codex_config
|
||||
from .runtime import proxy_base_url
|
||||
from .threads import retag_to_headroom, retag_to_native
|
||||
|
||||
@@ -65,7 +66,7 @@ print(key, end="")
|
||||
|
||||
def _codex_credential_store(config_dir: Path) -> str | None:
|
||||
try:
|
||||
config = tomllib.loads((config_dir / "config.toml").read_text(encoding="utf-8"))
|
||||
config = loads_codex_config((config_dir / "config.toml").read_text(encoding="utf-8"))
|
||||
except (OSError, tomllib.TOMLDecodeError):
|
||||
return None
|
||||
store = config.get("cli_auth_credentials_store")
|
||||
@@ -261,7 +262,7 @@ def codex_auth_helper_is_referenced(content: str, helper_path: str) -> bool | No
|
||||
the helper instead of risking deletion of a pre-existing user file.
|
||||
"""
|
||||
try:
|
||||
document = tomllib.loads(content)
|
||||
document = loads_codex_config(content)
|
||||
except (tomllib.TOMLDecodeError, TypeError):
|
||||
return None
|
||||
|
||||
@@ -411,7 +412,7 @@ def apply_provider_scope(manifest: DeploymentManifest) -> ManagedMutation | None
|
||||
existing = _CODEX_PATTERN.sub("", existing)
|
||||
existing = _strip_root_provider_assignments(existing)
|
||||
merged = _insert_block_at_root(existing, section)
|
||||
tomllib.loads(merged)
|
||||
loads_codex_config(merged)
|
||||
path.write_text(merged, encoding="utf-8")
|
||||
# Pull existing native threads into the headroom-provider menu so Codex's
|
||||
# history list stays whole once it routes through Headroom. Best-effort.
|
||||
|
||||
@@ -20,6 +20,7 @@ except ModuleNotFoundError: # pragma: no cover - Python 3.10
|
||||
import tomli as tomllib
|
||||
|
||||
from headroom.memory.storage_router import ProjectResolver, RequestContext
|
||||
from headroom.providers.codex.config_toml import loads_codex_config
|
||||
from headroom.providers.codex.threads import _codex_state_db_paths
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -298,7 +299,7 @@ class CodexProjectContextResolver:
|
||||
return self._configured_sqlite_home
|
||||
codex_home = Path(os.environ.get("CODEX_HOME", "").strip() or Path.home() / ".codex")
|
||||
try:
|
||||
config = tomllib.loads((codex_home / "config.toml").read_text(encoding="utf-8"))
|
||||
config = loads_codex_config((codex_home / "config.toml").read_text(encoding="utf-8"))
|
||||
configured_sqlite_home = config.get("sqlite_home")
|
||||
if isinstance(configured_sqlite_home, str) and configured_sqlite_home.strip():
|
||||
return Path(configured_sqlite_home).expanduser()
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ dependencies = [
|
||||
"ast-grep-cli>=0.30.0,!=0.44.1",
|
||||
"pyyaml>=6.0", # omp wrap: parse/merge omp's models.yml registry
|
||||
"tomli>=2.0.0; python_version < '3.11'", # tomllib backport for helper scripts
|
||||
"tomlkit>=0.13.0,<1.0", # Loss-minimizing Codex config recovery
|
||||
"tomlkit>=0.15.0,<1.0", # Loss-minimizing Codex config recovery
|
||||
# Verify upstream TLS through the OS trust store (macOS Keychain, Windows
|
||||
# cert store), where IT installs corporate TLS-inspection roots (Zscaler,
|
||||
# Netskope). Pure Python, no deps; the same library pip uses by default.
|
||||
|
||||
@@ -1564,6 +1564,38 @@ def test_codex_session_launch_settings_keep_routing_process_local(
|
||||
assert config_file.read_text(encoding="utf-8") == original_config
|
||||
|
||||
|
||||
def test_codex_session_launch_settings_accept_multiline_inline_table(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
# #4378: Codex accepts an inline table split across lines.
|
||||
_set_test_home(monkeypatch, tmp_path)
|
||||
codex_home = tmp_path / "custom-codex-home"
|
||||
codex_home.mkdir()
|
||||
monkeypatch.setenv("CODEX_HOME", str(codex_home))
|
||||
monkeypatch.setattr(wrap_mod, "_project_name_from_cwd", lambda: None)
|
||||
config_file = codex_home / "config.toml"
|
||||
original_config = (
|
||||
'model = "gpt-5"\n\n'
|
||||
"[mcp_servers.dingtalk]\n"
|
||||
'command = "npx"\n'
|
||||
"env = {\n"
|
||||
' DINGTALK_Client_ID = "**********",\n'
|
||||
" \n"
|
||||
" }\n"
|
||||
)
|
||||
config_file.write_text(original_config, encoding="utf-8")
|
||||
|
||||
args, env, _ = wrap_mod._codex_session_launch_settings(
|
||||
port=9898,
|
||||
codex_args=("exec", "hello"),
|
||||
environ={"CODEX_HOME": str(codex_home)},
|
||||
)
|
||||
|
||||
assert args[:2] == ("--config", 'openai_base_url="http://127.0.0.1:9898/v1"')
|
||||
assert env["OPENAI_BASE_URL"] == "http://127.0.0.1:9898/v1"
|
||||
assert config_file.read_text(encoding="utf-8") == original_config
|
||||
|
||||
|
||||
def test_codex_session_launch_settings_preserve_custom_provider_identity(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
@@ -228,6 +228,27 @@ def test_register_appends_to_existing_config_preserves_other_keys(tmp_path: Path
|
||||
assert parsed["mcp_servers"]["headroom"]["command"] == _RESOLVED_COMMAND[0]
|
||||
|
||||
|
||||
def test_register_accepts_multiline_inline_table(tmp_path: Path) -> None:
|
||||
# Codex accepts an inline table split across lines (#4378); registering must
|
||||
# not refuse the file as invalid TOML, and the user's text must be kept as is.
|
||||
cfg = _config_path(tmp_path)
|
||||
cfg.parent.mkdir()
|
||||
original = (
|
||||
"[mcp_servers.dingtalk]\n"
|
||||
'command = "npx"\n'
|
||||
"env = {\n"
|
||||
' DINGTALK_Client_ID = "**********",\n'
|
||||
" \n"
|
||||
" }\n"
|
||||
)
|
||||
cfg.write_text(original)
|
||||
result = _make_registrar(tmp_path).register_server(_spec())
|
||||
assert result.status == RegisterStatus.REGISTERED, result.detail
|
||||
text = cfg.read_text()
|
||||
assert text.startswith(original)
|
||||
assert "[mcp_servers.headroom]" in text
|
||||
|
||||
|
||||
def test_register_includes_env_subtable(tmp_path: Path) -> None:
|
||||
spec = _spec(env={"HEADROOM_PROXY_URL": "http://127.0.0.1:9000"})
|
||||
_make_registrar(tmp_path).register_server(spec)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
"""Tests for parsing Codex config.toml with the syntax Codex accepts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from headroom.providers.codex.config_toml import loads_codex_config
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
import tomllib
|
||||
else: # pragma: no cover
|
||||
import tomli as tomllib
|
||||
|
||||
# Shape from #4378: an inline table split across lines with a trailing comma.
|
||||
MULTILINE_INLINE_TABLE = (
|
||||
"[mcp_servers.dingtalk]\n"
|
||||
'command = "npx"\n'
|
||||
"env = {\n"
|
||||
' DINGTALK_Client_ID = "**********",\n'
|
||||
" \n"
|
||||
" }\n"
|
||||
)
|
||||
|
||||
|
||||
def test_loads_codex_config_accepts_multiline_inline_table() -> None:
|
||||
assert loads_codex_config(MULTILINE_INLINE_TABLE) == {
|
||||
"mcp_servers": {"dingtalk": {"command": "npx", "env": {"DINGTALK_Client_ID": "**********"}}}
|
||||
}
|
||||
|
||||
|
||||
def test_loads_codex_config_raises_tomllib_error_for_invalid_toml() -> None:
|
||||
with pytest.raises(tomllib.TOMLDecodeError):
|
||||
loads_codex_config("env = {\n")
|
||||
@@ -1887,7 +1887,7 @@ requires-dist = [
|
||||
{ name = "strands-agents", marker = "extra == 'strands'", specifier = ">=0.1.0" },
|
||||
{ name = "tiktoken", specifier = ">=0.5.0" },
|
||||
{ name = "tomli", marker = "python_full_version < '3.11'", specifier = ">=2.0.0" },
|
||||
{ name = "tomlkit", specifier = ">=0.13.0,<1.0" },
|
||||
{ name = "tomlkit", specifier = ">=0.15.0,<1.0" },
|
||||
{ name = "torch", marker = "sys_platform == 'darwin' and extra == 'pytorch-mps'", specifier = ">=2.12.1" },
|
||||
{ name = "torch", marker = "(platform_machine != 'x86_64' and extra == 'ml') or (sys_platform != 'darwin' and extra == 'ml')", specifier = ">=2.12.1" },
|
||||
{ name = "torch", marker = "(platform_machine != 'x86_64' and extra == 'voice') or (sys_platform != 'darwin' and extra == 'voice')", specifier = ">=2.12.1" },
|
||||
|
||||
Reference in New Issue
Block a user