Skip to content

Commit 2b56001

Browse files
author
T
committed
fix(codex): keep profile keys OpenCodex does not manage
Reinjection rebuilt ~/.codex/opencodex.config.toml from the template, so an operator's own Codex root keys (stream_idle_timeout_ms, stream_max_retries, ...) vanished on the next start without a message. Carry root keys this build does not own into the rebuilt profile, ahead of the first table. Values that span several lines are copied whole, keys owned by either profile shape (model_provider, model_catalog_json, openai_base_url) are always rewritten, and a profile with nothing to carry keeps the generated bytes byte for byte.
1 parent 1783523 commit 2b56001

4 files changed

Lines changed: 176 additions & 7 deletions

File tree

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,11 @@ reference/fallback config. On loopback it contains the root keys you can merge m
412412
injection was removed; on non-loopback it contains the dedicated provider form. External-provider
413413
mode leaves this profile untouched.
414414

415+
Rebuilding that file keeps root keys OpenCodex does not manage, so a setting you add such as
416+
`stream_idle_timeout_ms` or `stream_max_retries` survives the next injection. Keys inside tables are
417+
not preserved, and root keys this build owns (`model_provider`, `model_catalog_json`,
418+
`openai_base_url`) are always rewritten to match the routing mode in use.
419+
415420
:::caution
416421
Root keys such as `openai_base_url`, `model_provider`, and `model_catalog_json` **must** sit before the
417422
first `[table]` header. The injector guarantees that placement, removes its own stale/duplicate

‎src/codex/inject/config-toml.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,72 @@ export function buildProfileFileForTarget(
672672
return lines.join("\n");
673673
}
674674

675+
/**
676+
* Root keys this build owns in either profile shape. They are never carried
677+
* forward: leaving provider-table mode drops `model_provider` from the template,
678+
* and a caller that passes no catalog path drops `model_catalog_json`, so a
679+
* carried copy would point the profile at routing this build no longer writes.
680+
*/
681+
const MANAGED_PROFILE_ROOT_KEYS = new Set([
682+
"model_provider",
683+
"model_catalog_json",
684+
"openai_base_url",
685+
]);
686+
687+
/**
688+
* Root keys the previous profile carried that this build does not write.
689+
*
690+
* Operators add Codex settings such as `stream_idle_timeout_ms` and
691+
* `stream_max_retries` to `opencodex.config.toml`. Rebuilding the file from the
692+
* template dropped them without a word (#8066), so any root key this build does
693+
* not own is carried over, ahead of the first table. Values that span several
694+
* lines are copied whole, because one line of a multi-line value would not
695+
* parse.
696+
*/
697+
export function preserveUnmanagedProfileKeys(existing: string | null, generated: string): string {
698+
if (!existing) return generated;
699+
const generatedKeys = rootKeysOf(generated);
700+
if (generatedKeys.size === 0) return generated;
701+
702+
const existingSource = rootSourceLines(existing);
703+
const carried: string[] = [];
704+
const carriedKeys = new Set<string>();
705+
for (let index = 0; index < existingSource.rootEnd; index += 1) {
706+
if (!existingSource.lines[index]?.structural) continue;
707+
const span = sourceAssignmentSpan(existingSource.lines, index, existingSource.rootEnd);
708+
if (!span) continue;
709+
index = span.end - 1;
710+
if (generatedKeys.has(span.key) || MANAGED_PROFILE_ROOT_KEYS.has(span.key)) continue;
711+
if (carriedKeys.has(span.key)) continue;
712+
carriedKeys.add(span.key);
713+
carried.push(span.text.replace(/\s+$/, ""));
714+
}
715+
if (carried.length === 0) return generated;
716+
717+
const lines = generated.split("\n");
718+
const firstTable = lines.findIndex((line) => /^\s*\[/.test(line));
719+
lines.splice(firstTable === -1 ? lines.length : firstTable, 0, [
720+
"# Root keys OpenCodex does not manage, kept from the previous profile:",
721+
...carried,
722+
"",
723+
].join("\n"));
724+
return lines.join("\n");
725+
}
726+
727+
/** Names of the root assignments in a TOML document, before its first table. */
728+
function rootKeysOf(content: string): Set<string> {
729+
const keys = new Set<string>();
730+
const source = rootSourceLines(content);
731+
for (let index = 0; index < source.rootEnd; index += 1) {
732+
if (!source.lines[index]?.structural) continue;
733+
const span = sourceAssignmentSpan(source.lines, index, source.rootEnd);
734+
if (!span) continue;
735+
keys.add(span.key);
736+
index = span.end - 1;
737+
}
738+
return keys;
739+
}
740+
675741
export function chooseCatalogPathForInjection(
676742
content: string,
677743
requested?: string | null,

‎src/codex/inject/plan.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* post-transition bytes is what keeps the committed file from re-enabling the
1010
* flag the reconcile just turned off.
1111
*/
12+
import { existsSync, readFileSync } from "node:fs";
1213
import { websocketsEnabled } from "../../config";
1314
import {
1415
HISTORY_RELABEL_STANDS_DOWN,
@@ -22,7 +23,7 @@ import {
2223
journaledReplacedRootWebSearch,
2324
} from "../journal";
2425
import { stripJournaledOpenaiBaseUrl } from "../injected-marker";
25-
import { CODEX_CONFIG_PATH, resolveCodexStateDbPath } from "../paths";
26+
import { CODEX_CONFIG_PATH, CODEX_PROFILE_PATH, resolveCodexStateDbPath } from "../paths";
2627
import { transformManagedSubagentDefaults, type WrittenSubagentDefaults } from "../subagent-defaults";
2728
import type { OcxConfig } from "../../types";
2829
import type { CodexWriteCandidate } from "../write-coordination";
@@ -35,6 +36,7 @@ import {
3536
ensureFastModeFeature,
3637
ensureRootWebSearchDisabled,
3738
normalizeServiceTier,
39+
preserveUnmanagedProfileKeys,
3840
removeProfileSection,
3941
setRootModelCatalogPath,
4042
setRootModelProvider,
@@ -323,12 +325,15 @@ export function deriveCodexInjectionPlan(
323325
managedDefaultsMessage = ` ⚠️ ${nativeSubagentDefaultsWarning}\n`;
324326
}
325327

326-
const profileContent = buildProfileFileForTarget(
327-
routingTarget,
328-
catalogPath,
329-
websocketsForRoutingTarget(config, routingTarget),
330-
config?.fastMode,
331-
config?.codexProviderDisplayName,
328+
const profileContent = preserveUnmanagedProfileKeys(
329+
existsSync(CODEX_PROFILE_PATH) ? readFileSync(CODEX_PROFILE_PATH, "utf-8") : null,
330+
buildProfileFileForTarget(
331+
routingTarget,
332+
catalogPath,
333+
websocketsForRoutingTarget(config, routingTarget),
334+
config?.fastMode,
335+
config?.codexProviderDisplayName,
336+
),
332337
);
333338
content = applyEol(content, eol);
334339

‎tests/codex-integration/codex-inject.test.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
DEFAULT_CODEX_PROVIDER_DISPLAY_NAME,
1919
buildProfileFileForTarget,
2020
buildProviderTableBlockForTarget,
21+
preserveUnmanagedProfileKeys,
2122
resolveCodexProviderDisplayName,
2223
} from "../../src/codex/inject/config-toml";
2324
import {
@@ -935,3 +936,95 @@ test("malformed TOML preserves user routing and does not enable context injectio
935936
}
936937
}
937938
});
939+
940+
describe("profile file round trip", () => {
941+
test("keeps the root keys an operator added to the profile", () => {
942+
const generated = [
943+
"# OpenCodex proxy profile — use with: codex --profile opencodex",
944+
"# Routes all model requests through the opencodex proxy at 127.0.0.1:10100",
945+
'model_provider = "opencodex"',
946+
"",
947+
"[features]",
948+
"fast_mode = true",
949+
"",
950+
].join("\n");
951+
const existing = [
952+
'model_provider = "opencodex"',
953+
"stream_idle_timeout_ms = 60000",
954+
"stream_max_retries = 8",
955+
"",
956+
"[features]",
957+
"fast_mode = true",
958+
"",
959+
].join("\n");
960+
961+
const merged = preserveUnmanagedProfileKeys(existing, generated);
962+
963+
expect(merged).toContain("stream_idle_timeout_ms = 60000");
964+
expect(merged).toContain("stream_max_retries = 8");
965+
// Keys this build owns are not duplicated, and carried keys stay above the
966+
// first table so they remain root keys.
967+
expect(merged.match(/model_provider =/g)).toHaveLength(1);
968+
expect(merged.indexOf("stream_idle_timeout_ms")).toBeLessThan(merged.indexOf("[features]"));
969+
});
970+
971+
test("copies a value that continues on another line whole", () => {
972+
const generated = 'model_provider = "opencodex"\n';
973+
const existing = [
974+
'model_provider = "opencodex"',
975+
"notify = [",
976+
' "first",',
977+
' "second",',
978+
"]",
979+
"stream_max_retries = 8",
980+
"",
981+
].join("\n");
982+
983+
const merged = preserveUnmanagedProfileKeys(existing, generated);
984+
985+
expect(merged).toContain('notify = [\n "first",\n "second",\n]');
986+
expect(merged).toContain("stream_max_retries = 8");
987+
// The result still parses, which a half-copied array would not.
988+
expect(() => Bun.TOML.parse(merged)).not.toThrow();
989+
});
990+
991+
test("never carries a key this build owns, even when the template omits it", () => {
992+
const rootOverride = 'openai_base_url = "http://127.0.0.1:10100/v1"\n';
993+
const existing = [
994+
'model_provider = "opencodex"',
995+
'model_catalog_json = "/home/u/.codex/opencodex-catalog.json"',
996+
"stream_max_retries = 8",
997+
"",
998+
].join("\n");
999+
1000+
const merged = preserveUnmanagedProfileKeys(existing, rootOverride);
1001+
1002+
// Provider-table mode wrote model_provider; this build is in root-override
1003+
// mode and must not resurrect it. A null catalog path means the same for
1004+
// model_catalog_json.
1005+
expect(merged).not.toContain("model_provider");
1006+
expect(merged).not.toContain("model_catalog_json");
1007+
expect(merged).toContain("stream_max_retries = 8");
1008+
});
1009+
1010+
test("leaves the generated profile alone when there is nothing to carry", () => {
1011+
const generated = 'model_provider = "opencodex"\n';
1012+
expect(preserveUnmanagedProfileKeys(null, generated)).toBe(generated);
1013+
expect(preserveUnmanagedProfileKeys('model_provider = "opencodex"\n', generated)).toBe(
1014+
generated,
1015+
);
1016+
});
1017+
1018+
test("carries keys through a real profile rebuild", () => {
1019+
const target = standaloneCodexRoutingTarget(10100, {});
1020+
const generated = buildProfileFileForTarget(target, null);
1021+
const firstTable = generated.indexOf("[");
1022+
const prefix = generated.endsWith("\n") ? generated : `${generated}\n`;
1023+
const existing =
1024+
firstTable === -1
1025+
? `${prefix}stream_max_retries = 8\n`
1026+
: `${generated.slice(0, firstTable)}stream_max_retries = 8\n${generated.slice(firstTable)}`;
1027+
1028+
expect(preserveUnmanagedProfileKeys(existing, generated)).toContain("stream_max_retries = 8");
1029+
});
1030+
});

0 commit comments

Comments
 (0)