fix(codex): leave multi-line inline profile keys out of root key edits

This commit is contained in:
Abelo9996
2026-10-23 00:53:00 -04:00
parent dfe9372035
commit dc19387075
8 changed files with 187 additions and 29 deletions
+10 -7
View File
@@ -58,7 +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.config_toml import loads_codex_config, root_assignment_lines
from headroom.providers.codex.install import (
CodexAuthConfigError,
build_codex_auth_config,
@@ -92,6 +92,7 @@ _STARTUP_READY_TIMEOUT_SECONDS = 15
# report ready, and every session is permanently cold.
_HOOK_ENSURE_TIMEOUT_SECONDS = 60
_TOML_TABLE_HEADER_RE = re.compile(r"^[ \t]*(?:\[\[[^\]\r\n]+\]\]|\[[^\]\r\n]+\])[ \t]*(?:#.*)?$")
_ROOT_PROVIDER_KEY_RE = re.compile(r"[ \t]*(?:model_provider|openai_base_url)[ \t]*=")
_TOML_FEATURES_NAME_RE = r"(?:features|\"features\"|'features')"
_TOML_CODEX_HOOKS_NAME_RE = r"(?:codex_hooks|\"codex_hooks\"|'codex_hooks')"
_CODEX_FEATURES_TABLE_RE = re.compile(
@@ -471,12 +472,14 @@ def _ensure_codex_provider(path: Path, port: int) -> None:
# first table header) -- these keys also appear legitimately inside
# [profiles.*] tables as per-profile overrides, and stripping them there
# silently reroutes the user's profiles to the injected "headroom" default.
_first_table = re.search(r"(?m)^[ \t]*\[", content)
_split = _first_table.start() if _first_table else len(content)
root, rest = content[:_split], content[_split:]
root = re.sub(r"(?m)^[ \t]*model_provider[ \t]*=.*\r?\n", "", root)
root = re.sub(r"(?m)^[ \t]*openai_base_url[ \t]*=.*\r?\n", "", root)
content = root + rest
# Lines nested in a multi-line inline table (e.g. ``profiles = {``) are
# profile overrides too, so only true root assignments are dropped.
_root_lines = root_assignment_lines(content)
content = "".join(
line
for index, line in enumerate(content.splitlines(keepends=True))
if index not in _root_lines or not _ROOT_PROVIDER_KEY_RE.match(line)
)
# The provider block carries top-level keys (model_provider, openai_base_url),
# so it must land at the document root rather than after a trailing table (#260).
content = _replace_marker_block(
+16 -7
View File
@@ -113,7 +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.config_toml import loads_codex_config, root_assignment_lines
from headroom.providers.codex.install import (
CodexAuthConfigError,
build_codex_auth_config,
@@ -3348,19 +3348,28 @@ def _redirect_existing_top_level_keys(content: str, port: int) -> str:
return _replace
redirected = content
# Only root assignments: the same keys inside [profiles.*] tables or a
# multi-line inline ``profiles = {`` table are the user's per-profile routing.
lines = content.splitlines(keepends=True)
root_lines = sorted(root_assignment_lines(content))
for key in _REDIRECTABLE_KEYS:
pattern = re.compile(rf'(?m)^[ \t]*{re.escape(key)}[ \t]*=[ \t]*"(?P<value>[^"\n]*)"[^\n]*')
redirected = pattern.sub(_make_replacer(key, port), redirected, count=1)
return redirected
pattern = re.compile(rf'[ \t]*{re.escape(key)}[ \t]*=[ \t]*"(?P<value>[^"\n]*)"[^\n]*')
replace = _make_replacer(key, port)
for index in root_lines:
match = pattern.match(lines[index])
if match:
lines[index] = replace(match) + lines[index][match.end() :]
break
return "".join(lines)
def _has_redirectable_top_level_key(content: str, key: str) -> bool:
"""Return True if ``content`` declares ``key = "..."`` as a top-level key."""
import re # local import to match the module's existing convention
pattern = re.compile(rf'(?m)^[ \t]*{key}[ \t]*=[ \t]*"[^"\n]*"')
return pattern.search(content) is not None
pattern = re.compile(rf'[ \t]*{key}[ \t]*=[ \t]*"[^"\n]*"')
lines = content.splitlines(keepends=True)
return any(pattern.match(lines[index]) for index in root_assignment_lines(content))
def _codex_config_has_headroom_markers(content: str) -> bool:
+54
View File
@@ -30,3 +30,57 @@ def loads_codex_config(content: str) -> dict[str, Any]:
return tomlkit.parse(content).unwrap()
except TOMLKitError:
raise exc from None
def root_assignment_lines(content: str) -> set[int]:
"""Return indexes of the lines that start a key/value pair at the document root.
Indexes refer to ``content.splitlines(keepends=True)``. Only lines before
the first table header count, and a line inside a multi-line inline table,
array, or string is skipped, so a key nested in a multi-line inline table
(TOML 1.1, e.g. ``profiles = {\\n work = { model_provider = ... } }``) is
never mistaken for a root key.
"""
roots: set[int] = set()
depth = 0
open_string: str | None = None # delimiter of an open multi-line string
for index, line in enumerate(content.splitlines(keepends=True)):
if depth == 0 and open_string is None:
stripped = line.lstrip(" \t")
if stripped.startswith("["):
break # first table header ends the root
if stripped and stripped[0] not in "#\r\n":
roots.add(index)
pos = 0
while pos < len(line):
if open_string is not None:
if line.startswith(open_string, pos):
pos += 3
# Up to two quotes may directly precede the closing delimiter.
for _ in range(2):
if pos < len(line) and line[pos] == open_string[0]:
pos += 1
open_string = None
elif open_string == '"""' and line[pos] == "\\":
pos += 2
else:
pos += 1
continue
char = line[pos]
if char == "#":
break
if line.startswith('"""', pos) or line.startswith("'''", pos):
open_string = line[pos : pos + 3]
pos += 3
elif char in "\"'":
pos += 1
while pos < len(line) and line[pos] not in (char, "\n"):
pos += 2 if char == '"' and line[pos] == "\\" else 1
pos += 1
else:
if char in "[{":
depth += 1
elif char in "]}" and depth:
depth -= 1
pos += 1
return roots
+13 -14
View File
@@ -21,7 +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 .config_toml import loads_codex_config, root_assignment_lines
from .runtime import proxy_base_url
from .threads import retag_to_headroom, retag_to_native
@@ -370,19 +370,18 @@ def _insert_block_at_root(content: str, block: str) -> str:
def _strip_root_provider_assignments(content: str) -> str:
"""Remove root provider assignments without touching table-scoped settings."""
lines = content.splitlines(keepends=True)
kept: list[str] = []
in_root = True
for line in lines:
if in_root and _TOML_TABLE_HEADER_RE.search(line):
in_root = False
if in_root and (
_ROOT_MODEL_PROVIDER_RE.match(line) or _ROOT_OPENAI_BASE_URL_RE.match(line)
):
continue
kept.append(line)
return "".join(kept)
"""Remove root provider assignments without touching table-scoped settings.
Keys nested in a multi-line inline table (e.g. a ``profiles = {`` override)
are not root assignments and are kept.
"""
root_lines = root_assignment_lines(content)
return "".join(
line
for index, line in enumerate(content.splitlines(keepends=True))
if index not in root_lines
or not (_ROOT_MODEL_PROVIDER_RE.match(line) or _ROOT_OPENAI_BASE_URL_RE.match(line))
)
def apply_provider_scope(manifest: DeploymentManifest) -> ManagedMutation | None:
+29
View File
@@ -661,6 +661,35 @@ def test_ensure_codex_provider_preserves_profile_overrides(monkeypatch, tmp_path
assert parsed["profiles"]["work"]["openai_base_url"] == "https://azure.example/v1"
def test_ensure_codex_provider_preserves_multiline_inline_profile_overrides(
monkeypatch, tmp_path: Path
) -> None:
"""A profile in a multi-line inline table (TOML 1.1) keeps its provider."""
from headroom.providers.codex.config_toml import loads_codex_config
init_cli, _ = _load_init_module(monkeypatch)
path = tmp_path / "config.toml"
path.write_text(
'model_provider = "openai"\n'
"profiles = {\n"
" work = {\n"
' model_provider = "company",\n'
' openai_base_url = "https://company.example/v1",\n'
" },\n"
"}\n",
encoding="utf-8",
)
init_cli._ensure_codex_provider(path, 8787)
parsed = loads_codex_config(path.read_text(encoding="utf-8"))
assert parsed["model_provider"] == "headroom"
assert parsed["profiles"]["work"] == {
"model_provider": "company",
"openai_base_url": "https://company.example/v1",
}
def test_ensure_codex_provider_emits_requires_openai_auth_for_chatgpt(
monkeypatch, tmp_path: Path
) -> None:
+21
View File
@@ -846,6 +846,27 @@ class TestSubscriptionRouting:
content = (tmp_path / ".codex" / "config.toml").read_text(encoding="utf-8")
assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in content
def test_inject_preserves_multiline_inline_profile_overrides(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Keys nested in a multi-line inline profiles table are not root keys."""
from headroom.providers.codex.config_toml import loads_codex_config
_set_test_home(monkeypatch, tmp_path)
config_file = tmp_path / ".codex" / "config.toml"
config_file.parent.mkdir(parents=True)
config_file.write_text(
'profiles = {\n work = {\n model_provider = "company",\n },\n}\n',
encoding="utf-8",
)
wrap_mod._inject_codex_provider_config(8787)
parsed = loads_codex_config(config_file.read_text(encoding="utf-8"))
assert parsed["model_provider"] == "headroom"
assert parsed["openai_base_url"] == "http://127.0.0.1:8787/v1"
assert parsed["profiles"]["work"] == {"model_provider": "company"}
def test_inject_emits_requires_openai_auth_for_chatgpt(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
+23
View File
@@ -13,6 +13,7 @@ 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,
@@ -122,6 +123,28 @@ def test_api_key_auth_is_persisted_as_a_codex_command(tmp_path: Path, monkeypatc
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"}
def test_persistent_install_helper_collision_preserves_config(tmp_path: Path, monkeypatch) -> None:
config = tmp_path / "config.toml"
auth = tmp_path / "auth.json"
+21 -1
View File
@@ -6,7 +6,7 @@ import sys
import pytest
from headroom.providers.codex.config_toml import loads_codex_config
from headroom.providers.codex.config_toml import loads_codex_config, root_assignment_lines
if sys.version_info >= (3, 11):
import tomllib
@@ -33,3 +33,23 @@ def test_loads_codex_config_accepts_multiline_inline_table() -> None:
def test_loads_codex_config_raises_tomllib_error_for_invalid_toml() -> None:
with pytest.raises(tomllib.TOMLDecodeError):
loads_codex_config("env = {\n")
def test_root_assignment_lines_skip_keys_nested_in_multiline_inline_table() -> None:
content = (
'model_provider = "openai"\n' # 0: root
"profiles = {\n" # 1: root
" work = {\n"
' model_provider = "company",\n'
" },\n"
"}\n"
'notes = """\n' # 6: root
'model_provider = "in a string"\n'
'"""\n'
"# comment\n"
'openai_base_url = "https://example.test/v1" # {\n' # 10: root
"\n"
"[profiles.other]\n"
'model_provider = "other"\n'
)
assert root_assignment_lines(content) == {0, 1, 6, 10}