Skip to content

Commit 76906a3

Browse files
author
jzli
committed
fix(codex): strip app-rewritten provider sub-tables so the provider never survives nameless
A Codex app config rewrite re-serializes the provider's inline env_http_headers table into a separate [model_providers.opencodex.env_http_headers] sub-table. removeOcxSection() ends its removal scope at the next table header, and the sub-table header is one, so an inject/restore removed the main table (with name/base_url) but kept the sub-table: model_providers.opencodex then exists with only env_http_headers and no name, and Codex rejects the whole config on startup ('provider name must not be empty'). Because every cleanup guard matched the exact string '[model_providers.opencodex]', the orphan never matched again, was journaled as baseline, and re-persisted on every inject/restore cycle. Recognize [model_providers.opencodex.*] sub-table headers in removeOcxSection() and in the three cleanup guards (injectCodexConfig, stripOpencodexConfigResult, hasOpencodexRouting) so the main table and its sub-tables are removed together. A user's similarly named table ([model_providers.opencodex_backup]) stays out of scope: the match requires the 'opencodex.' dot boundary.
1 parent bcc77c0 commit 76906a3

2 files changed

Lines changed: 95 additions & 10 deletions

File tree

‎src/codex/inject.ts‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ export async function injectCodexConfig(
735735
// Design B form FIRST: removeOcxSection also keys on the marker line, so a root-level
736736
// marker + openai_base_url pair must be gone before it scans or it would swallow root keys.
737737
content = stripInjectedOpenaiBaseUrl(content);
738-
if (content.includes("[model_providers.opencodex]")) {
738+
if (hasOcxProviderTable(content)) {
739739
content = removeOcxSection(content);
740740
}
741741
content = removeProfileSection(content);
@@ -1095,25 +1095,44 @@ export async function injectCodexConfig(
10951095
};
10961096
}
10971097

1098+
/**
1099+
* Sub-table headers like `[model_providers.opencodex.env_http_headers]` appear when a Codex app
1100+
* config rewrite re-serializes the provider's inline `env_http_headers` table. They define the
1101+
* same `model_providers.opencodex` provider, so cleanup must remove them too — otherwise the
1102+
* provider survives with no `name` and Codex rejects the whole config
1103+
* ("provider name must not be empty"). The dot terminator keeps a user's
1104+
* `[model_providers.opencodex_backup]`-style tables out of scope.
1105+
*/
1106+
function isOcxProviderHeaderLine(trimmedLine: string): boolean {
1107+
return (
1108+
trimmedLine === "[model_providers.opencodex]" ||
1109+
trimmedLine.startsWith("[model_providers.opencodex.")
1110+
);
1111+
}
1112+
1113+
function hasOcxProviderTable(content: string): boolean {
1114+
return content
1115+
.split("\n")
1116+
.some((line) => isOcxProviderHeaderLine(line.trim()));
1117+
}
1118+
10981119
function removeOcxSection(content: string): string {
10991120
const lines = content.split("\n");
11001121
const filtered: string[] = [];
11011122
let inOcxSection = false;
11021123
for (const line of lines) {
11031124
if (
11041125
line.includes(OCX_SECTION_MARKER) ||
1105-
line.trim() === "[model_providers.opencodex]"
1126+
isOcxProviderHeaderLine(line.trim())
11061127
) {
11071128
inOcxSection = true;
11081129
continue;
11091130
}
11101131
if (inOcxSection) {
1111-
// End the injected section at the next table header that ISN'T our own — exact match so a
1112-
// user's "[model_providers.opencodex_backup]" (or similar) is preserved, not swallowed.
1113-
if (
1114-
/^\s*\[/.test(line) &&
1115-
line.trim() !== "[model_providers.opencodex]"
1116-
) {
1132+
// End the injected section at the next table header that ISN'T our own. Exact match on the
1133+
// provider name (plus our own sub-tables) so a user's
1134+
// "[model_providers.opencodex_backup]" (or similar) is preserved, not swallowed.
1135+
if (/^\s*\[/.test(line) && !isOcxProviderHeaderLine(line.trim())) {
11171136
inOcxSection = false;
11181137
filtered.push(line);
11191138
}
@@ -1153,7 +1172,7 @@ function stripOpencodexConfigResult(
11531172
|| (journaledBaseUrl !== null && rootTomlString(out, "openai_base_url") === journaledBaseUrl);
11541173
out = stripInjectedOpenaiBaseUrl(out); // before removeOcxSection — it keys on the marker line too
11551174
out = stripJournaledOpenaiBaseUrl(out, journaledBaseUrl);
1156-
if (out.includes("[model_providers.opencodex]")) {
1175+
if (hasOcxProviderTable(out)) {
11571176
out = removeOcxSection(out);
11581177
}
11591178
out = removeProfileSection(out);
@@ -1182,7 +1201,7 @@ export function stripOpencodexConfig(content: string): string {
11821201

11831202
function hasOpencodexRouting(content: string): boolean {
11841203
return (
1185-
content.includes("[model_providers.opencodex]") ||
1204+
hasOcxProviderTable(content) ||
11861205
/^\s*model_provider\s*=\s*"opencodex"/m.test(content) ||
11871206
hasInjectedOpenaiBaseUrl(content)
11881207
);

‎tests/codex-inject.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,72 @@ describe("Design B openai_base_url injection", () => {
365365
expect(stripped).not.toContain("[model_providers.opencodex]");
366366
expect(stripped).toContain('model = "gpt-5.5"');
367367
});
368+
369+
test("app-rewritten env_http_headers sub-table strips fully: no nameless provider survives", () => {
370+
// A Codex app config rewrite re-serializes the provider's inline env_http_headers table
371+
// into a separate [model_providers.opencodex.env_http_headers] sub-table. Cleanup must
372+
// remove the provider table AND its sub-table, or the provider survives with no `name`
373+
// and Codex rejects the whole config ("provider name must not be empty").
374+
const rewritten = [
375+
'model = "gpt-5.5"',
376+
"",
377+
"[model_providers.opencodex]",
378+
'name = "OpenCodex Proxy"',
379+
'base_url = "http://127.0.0.1:10100/v1"',
380+
'wire_api = "responses"',
381+
"",
382+
"[model_providers.opencodex.env_http_headers]",
383+
'"x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN"',
384+
"",
385+
"[agents]",
386+
"max_concurrent_threads_per_session = 8",
387+
"",
388+
].join("\n");
389+
const stripped = stripOpencodexConfig(rewritten);
390+
391+
expect(stripped).not.toContain("opencodex");
392+
expect(stripped).toContain("[agents]");
393+
expect(stripped).toContain('model = "gpt-5.5"');
394+
});
395+
396+
test("an orphaned env_http_headers sub-table alone is removed (recurrence breaker)", () => {
397+
// Once the main table is gone, only the sub-table header defines the provider. The old
398+
// exact-match guards never matched that form, so the orphan was journaled as baseline and
399+
// re-persisted on every inject/restore cycle while Codex kept failing on startup.
400+
const orphan = [
401+
'model = "gpt-5.5"',
402+
"",
403+
"[agents]",
404+
"max_concurrent_threads_per_session = 8",
405+
"",
406+
"[model_providers.opencodex.env_http_headers]",
407+
'"x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN"',
408+
'"CF-Access-Client-Id" = "CF_ACCESS_CLIENT_ID"',
409+
"",
410+
].join("\n");
411+
const stripped = stripOpencodexConfig(orphan);
412+
413+
expect(stripped).not.toContain("opencodex");
414+
expect(stripped).not.toContain("CF-Access-Client-Id");
415+
expect(stripped).toContain('model = "gpt-5.5"');
416+
expect(stripped).toContain("[agents]");
417+
});
418+
419+
test("a user's similarly named provider table is preserved while opencodex sub-tables strip", () => {
420+
const content = [
421+
"[model_providers.opencodex.env_http_headers]",
422+
'"x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN"',
423+
"",
424+
"[model_providers.opencodex_backup]",
425+
'name = "user backup"',
426+
"",
427+
].join("\n");
428+
const stripped = stripOpencodexConfig(content);
429+
430+
expect(stripped).not.toContain("env_http_headers");
431+
expect(stripped).toContain("[model_providers.opencodex_backup]");
432+
expect(stripped).toContain('name = "user backup"');
433+
});
368434
});
369435

370436
describe("EOL boundary helpers (Windows CRLF configs)", () => {

0 commit comments

Comments
 (0)