Skip to content

Commit dc19387

Browse files
committed
fix(codex): leave multi-line inline profile keys out of root key edits
1 parent dfe9372 commit dc19387

8 files changed

Lines changed: 187 additions & 29 deletions

File tree

‎headroom/cli/init.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +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
61+
from headroom.providers.codex.config_toml import loads_codex_config, root_assignment_lines
6262
from headroom.providers.codex.install import (
6363
CodexAuthConfigError,
6464
build_codex_auth_config,
@@ -92,6 +92,7 @@
9292
# report ready, and every session is permanently cold.
9393
_HOOK_ENSURE_TIMEOUT_SECONDS = 60
9494
_TOML_TABLE_HEADER_RE = re.compile(r"^[ \t]*(?:\[\[[^\]\r\n]+\]\]|\[[^\]\r\n]+\])[ \t]*(?:#.*)?$")
95+
_ROOT_PROVIDER_KEY_RE = re.compile(r"[ \t]*(?:model_provider|openai_base_url)[ \t]*=")
9596
_TOML_FEATURES_NAME_RE = r"(?:features|\"features\"|'features')"
9697
_TOML_CODEX_HOOKS_NAME_RE = r"(?:codex_hooks|\"codex_hooks\"|'codex_hooks')"
9798
_CODEX_FEATURES_TABLE_RE = re.compile(
@@ -471,12 +472,14 @@ def _ensure_codex_provider(path: Path, port: int) -> None:
471472
# first table header) -- these keys also appear legitimately inside
472473
# [profiles.*] tables as per-profile overrides, and stripping them there
473474
# silently reroutes the user's profiles to the injected "headroom" default.
474-
_first_table = re.search(r"(?m)^[ \t]*\[", content)
475-
_split = _first_table.start() if _first_table else len(content)
476-
root, rest = content[:_split], content[_split:]
477-
root = re.sub(r"(?m)^[ \t]*model_provider[ \t]*=.*\r?\n", "", root)
478-
root = re.sub(r"(?m)^[ \t]*openai_base_url[ \t]*=.*\r?\n", "", root)
479-
content = root + rest
475+
# Lines nested in a multi-line inline table (e.g. ``profiles = {``) are
476+
# profile overrides too, so only true root assignments are dropped.
477+
_root_lines = root_assignment_lines(content)
478+
content = "".join(
479+
line
480+
for index, line in enumerate(content.splitlines(keepends=True))
481+
if index not in _root_lines or not _ROOT_PROVIDER_KEY_RE.match(line)
482+
)
480483
# The provider block carries top-level keys (model_provider, openai_base_url),
481484
# so it must land at the document root rather than after a trailing table (#260).
482485
content = _replace_marker_block(

‎headroom/cli/wrap.py‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +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
116+
from headroom.providers.codex.config_toml import loads_codex_config, root_assignment_lines
117117
from headroom.providers.codex.install import (
118118
CodexAuthConfigError,
119119
build_codex_auth_config,
@@ -3348,19 +3348,28 @@ def _replace(match: re.Match[str]) -> str:
33483348

33493349
return _replace
33503350

3351-
redirected = content
3351+
# Only root assignments: the same keys inside [profiles.*] tables or a
3352+
# multi-line inline ``profiles = {`` table are the user's per-profile routing.
3353+
lines = content.splitlines(keepends=True)
3354+
root_lines = sorted(root_assignment_lines(content))
33523355
for key in _REDIRECTABLE_KEYS:
3353-
pattern = re.compile(rf'(?m)^[ \t]*{re.escape(key)}[ \t]*=[ \t]*"(?P<value>[^"\n]*)"[^\n]*')
3354-
redirected = pattern.sub(_make_replacer(key, port), redirected, count=1)
3355-
return redirected
3356+
pattern = re.compile(rf'[ \t]*{re.escape(key)}[ \t]*=[ \t]*"(?P<value>[^"\n]*)"[^\n]*')
3357+
replace = _make_replacer(key, port)
3358+
for index in root_lines:
3359+
match = pattern.match(lines[index])
3360+
if match:
3361+
lines[index] = replace(match) + lines[index][match.end() :]
3362+
break
3363+
return "".join(lines)
33563364

33573365

33583366
def _has_redirectable_top_level_key(content: str, key: str) -> bool:
33593367
"""Return True if ``content`` declares ``key = "..."`` as a top-level key."""
33603368
import re # local import to match the module's existing convention
33613369

3362-
pattern = re.compile(rf'(?m)^[ \t]*{key}[ \t]*=[ \t]*"[^"\n]*"')
3363-
return pattern.search(content) is not None
3370+
pattern = re.compile(rf'[ \t]*{key}[ \t]*=[ \t]*"[^"\n]*"')
3371+
lines = content.splitlines(keepends=True)
3372+
return any(pattern.match(lines[index]) for index in root_assignment_lines(content))
33643373

33653374

33663375
def _codex_config_has_headroom_markers(content: str) -> bool:

‎headroom/providers/codex/config_toml.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,57 @@ def loads_codex_config(content: str) -> dict[str, Any]:
3030
return tomlkit.parse(content).unwrap()
3131
except TOMLKitError:
3232
raise exc from None
33+
34+
35+
def root_assignment_lines(content: str) -> set[int]:
36+
"""Return indexes of the lines that start a key/value pair at the document root.
37+
38+
Indexes refer to ``content.splitlines(keepends=True)``. Only lines before
39+
the first table header count, and a line inside a multi-line inline table,
40+
array, or string is skipped, so a key nested in a multi-line inline table
41+
(TOML 1.1, e.g. ``profiles = {\\n work = { model_provider = ... } }``) is
42+
never mistaken for a root key.
43+
"""
44+
roots: set[int] = set()
45+
depth = 0
46+
open_string: str | None = None # delimiter of an open multi-line string
47+
for index, line in enumerate(content.splitlines(keepends=True)):
48+
if depth == 0 and open_string is None:
49+
stripped = line.lstrip(" \t")
50+
if stripped.startswith("["):
51+
break # first table header ends the root
52+
if stripped and stripped[0] not in "#\r\n":
53+
roots.add(index)
54+
pos = 0
55+
while pos < len(line):
56+
if open_string is not None:
57+
if line.startswith(open_string, pos):
58+
pos += 3
59+
# Up to two quotes may directly precede the closing delimiter.
60+
for _ in range(2):
61+
if pos < len(line) and line[pos] == open_string[0]:
62+
pos += 1
63+
open_string = None
64+
elif open_string == '"""' and line[pos] == "\\":
65+
pos += 2
66+
else:
67+
pos += 1
68+
continue
69+
char = line[pos]
70+
if char == "#":
71+
break
72+
if line.startswith('"""', pos) or line.startswith("'''", pos):
73+
open_string = line[pos : pos + 3]
74+
pos += 3
75+
elif char in "\"'":
76+
pos += 1
77+
while pos < len(line) and line[pos] not in (char, "\n"):
78+
pos += 2 if char == '"' and line[pos] == "\\" else 1
79+
pos += 1
80+
else:
81+
if char in "[{":
82+
depth += 1
83+
elif char in "]}" and depth:
84+
depth -= 1
85+
pos += 1
86+
return roots

‎headroom/providers/codex/install.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +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
24+
from .config_toml import loads_codex_config, root_assignment_lines
2525
from .runtime import proxy_base_url
2626
from .threads import retag_to_headroom, retag_to_native
2727

@@ -370,19 +370,18 @@ def _insert_block_at_root(content: str, block: str) -> str:
370370

371371

372372
def _strip_root_provider_assignments(content: str) -> str:
373-
"""Remove root provider assignments without touching table-scoped settings."""
374-
lines = content.splitlines(keepends=True)
375-
kept: list[str] = []
376-
in_root = True
377-
for line in lines:
378-
if in_root and _TOML_TABLE_HEADER_RE.search(line):
379-
in_root = False
380-
if in_root and (
381-
_ROOT_MODEL_PROVIDER_RE.match(line) or _ROOT_OPENAI_BASE_URL_RE.match(line)
382-
):
383-
continue
384-
kept.append(line)
385-
return "".join(kept)
373+
"""Remove root provider assignments without touching table-scoped settings.
374+
375+
Keys nested in a multi-line inline table (e.g. a ``profiles = {`` override)
376+
are not root assignments and are kept.
377+
"""
378+
root_lines = root_assignment_lines(content)
379+
return "".join(
380+
line
381+
for index, line in enumerate(content.splitlines(keepends=True))
382+
if index not in root_lines
383+
or not (_ROOT_MODEL_PROVIDER_RE.match(line) or _ROOT_OPENAI_BASE_URL_RE.match(line))
384+
)
386385

387386

388387
def apply_provider_scope(manifest: DeploymentManifest) -> ManagedMutation | None:

‎tests/test_cli/test_init_cli.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,35 @@ def test_ensure_codex_provider_preserves_profile_overrides(monkeypatch, tmp_path
661661
assert parsed["profiles"]["work"]["openai_base_url"] == "https://azure.example/v1"
662662

663663

664+
def test_ensure_codex_provider_preserves_multiline_inline_profile_overrides(
665+
monkeypatch, tmp_path: Path
666+
) -> None:
667+
"""A profile in a multi-line inline table (TOML 1.1) keeps its provider."""
668+
from headroom.providers.codex.config_toml import loads_codex_config
669+
670+
init_cli, _ = _load_init_module(monkeypatch)
671+
path = tmp_path / "config.toml"
672+
path.write_text(
673+
'model_provider = "openai"\n'
674+
"profiles = {\n"
675+
" work = {\n"
676+
' model_provider = "company",\n'
677+
' openai_base_url = "https://company.example/v1",\n'
678+
" },\n"
679+
"}\n",
680+
encoding="utf-8",
681+
)
682+
683+
init_cli._ensure_codex_provider(path, 8787)
684+
685+
parsed = loads_codex_config(path.read_text(encoding="utf-8"))
686+
assert parsed["model_provider"] == "headroom"
687+
assert parsed["profiles"]["work"] == {
688+
"model_provider": "company",
689+
"openai_base_url": "https://company.example/v1",
690+
}
691+
692+
664693
def test_ensure_codex_provider_emits_requires_openai_auth_for_chatgpt(
665694
monkeypatch, tmp_path: Path
666695
) -> None:

‎tests/test_cli/test_wrap_codex.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,27 @@ def test_inject_writes_openai_base_url(
846846
content = (tmp_path / ".codex" / "config.toml").read_text(encoding="utf-8")
847847
assert 'openai_base_url = "http://127.0.0.1:8787/v1"' in content
848848

849+
def test_inject_preserves_multiline_inline_profile_overrides(
850+
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
851+
) -> None:
852+
"""Keys nested in a multi-line inline profiles table are not root keys."""
853+
from headroom.providers.codex.config_toml import loads_codex_config
854+
855+
_set_test_home(monkeypatch, tmp_path)
856+
config_file = tmp_path / ".codex" / "config.toml"
857+
config_file.parent.mkdir(parents=True)
858+
config_file.write_text(
859+
'profiles = {\n work = {\n model_provider = "company",\n },\n}\n',
860+
encoding="utf-8",
861+
)
862+
863+
wrap_mod._inject_codex_provider_config(8787)
864+
865+
parsed = loads_codex_config(config_file.read_text(encoding="utf-8"))
866+
assert parsed["model_provider"] == "headroom"
867+
assert parsed["openai_base_url"] == "http://127.0.0.1:8787/v1"
868+
assert parsed["profiles"]["work"] == {"model_provider": "company"}
869+
849870
def test_inject_emits_requires_openai_auth_for_chatgpt(
850871
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
851872
) -> None:

‎tests/test_install/test_codex_install.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import tomli as tomllib # type: ignore[no-redef]
1414

1515
from headroom.install.models import DeploymentManifest
16+
from headroom.providers.codex.config_toml import loads_codex_config
1617
from headroom.providers.codex.install import (
1718
CodexAuthConfigError,
1819
_codex_login_status,
@@ -122,6 +123,28 @@ def test_api_key_auth_is_persisted_as_a_codex_command(tmp_path: Path, monkeypatc
122123
assert not helper.exists()
123124

124125

126+
def test_provider_scope_preserves_multiline_inline_profile_overrides(
127+
tmp_path: Path, monkeypatch
128+
) -> None:
129+
config = tmp_path / "config.toml"
130+
config.write_text(
131+
'model_provider = "openai"\n'
132+
"profiles = {\n"
133+
" work = {\n"
134+
' model_provider = "company",\n'
135+
" },\n"
136+
"}\n",
137+
encoding="utf-8",
138+
)
139+
monkeypatch.setattr("headroom.providers.codex.install.codex_config_path", lambda: config)
140+
141+
apply_provider_scope(_manifest(tmp_path))
142+
143+
parsed = loads_codex_config(config.read_text(encoding="utf-8"))
144+
assert parsed["model_provider"] == "headroom"
145+
assert parsed["profiles"]["work"] == {"model_provider": "company"}
146+
147+
125148
def test_persistent_install_helper_collision_preserves_config(tmp_path: Path, monkeypatch) -> None:
126149
config = tmp_path / "config.toml"
127150
auth = tmp_path / "auth.json"

‎tests/test_provider_codex_config_toml.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from headroom.providers.codex.config_toml import loads_codex_config
9+
from headroom.providers.codex.config_toml import loads_codex_config, root_assignment_lines
1010

1111
if sys.version_info >= (3, 11):
1212
import tomllib
@@ -33,3 +33,23 @@ def test_loads_codex_config_accepts_multiline_inline_table() -> None:
3333
def test_loads_codex_config_raises_tomllib_error_for_invalid_toml() -> None:
3434
with pytest.raises(tomllib.TOMLDecodeError):
3535
loads_codex_config("env = {\n")
36+
37+
38+
def test_root_assignment_lines_skip_keys_nested_in_multiline_inline_table() -> None:
39+
content = (
40+
'model_provider = "openai"\n' # 0: root
41+
"profiles = {\n" # 1: root
42+
" work = {\n"
43+
' model_provider = "company",\n'
44+
" },\n"
45+
"}\n"
46+
'notes = """\n' # 6: root
47+
'model_provider = "in a string"\n'
48+
'"""\n'
49+
"# comment\n"
50+
'openai_base_url = "https://example.test/v1" # {\n' # 10: root
51+
"\n"
52+
"[profiles.other]\n"
53+
'model_provider = "other"\n'
54+
)
55+
assert root_assignment_lines(content) == {0, 1, 6, 10}

0 commit comments

Comments
 (0)