Skip to content

Commit 51f8d57

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 keys (stream_idle_timeout_ms, stream_max_retries, ...) vanished on the next start without a message. Carry root keys this build does not write into the rebuilt profile, ahead of the first table, and leave the generated file untouched when there is nothing to carry.
1 parent 1783523 commit 51f8d57

3 files changed

Lines changed: 147 additions & 7 deletions

File tree

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

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

675+
/**
676+
* Root keys the previous profile carried that this build does not write.
677+
*
678+
* Operators add Codex settings such as `stream_idle_timeout_ms` and
679+
* `stream_max_retries` to `opencodex.config.toml`. Rebuilding the file from the
680+
* template dropped them without a word (#8066), so any key this build does not
681+
* own is carried over, in place, ahead of the first table.
682+
*/
683+
/** True when one line holds a whole TOML value (no open array, table, or string). */
684+
function isSelfContainedTomlLine(text: string): boolean {
685+
let quote: string | null = null;
686+
let depth = 0;
687+
for (let index = 0; index < text.length; index += 1) {
688+
const char = text[index]!;
689+
if (quote) {
690+
if (char === "\\" && quote === '"') index += 1;
691+
else if (char === quote) quote = null;
692+
continue;
693+
}
694+
if (char === '"' || char === "'") quote = char;
695+
else if (char === "[" || char === "{") depth += 1;
696+
else if (char === "]" || char === "}") depth -= 1;
697+
else if (char === "#" ) break;
698+
}
699+
return quote === null && depth === 0;
700+
}
701+
702+
export function preserveUnmanagedProfileKeys(existing: string | null, generated: string): string {
703+
if (!existing) return generated;
704+
const generatedKeys = new Set<string>();
705+
const generatedSource = rootSourceLines(generated);
706+
for (const line of generatedSource.lines.slice(0, generatedSource.rootEnd)) {
707+
if (!line.structural) continue;
708+
const key = rootAssignmentKey(line.text);
709+
if (key) generatedKeys.add(key);
710+
}
711+
if (generatedKeys.size === 0) return generated;
712+
713+
const carried: string[] = [];
714+
const carriedKeys = new Set<string>();
715+
const existingSource = rootSourceLines(existing);
716+
for (const line of existingSource.lines.slice(0, existingSource.rootEnd)) {
717+
if (!line.structural) continue;
718+
const key = rootAssignmentKey(line.text);
719+
if (!key || generatedKeys.has(key) || carriedKeys.has(key)) continue;
720+
// A value that continues on the next line cannot be carried as one line
721+
// without cutting it in half, so leave it out rather than corrupt the file.
722+
if (!isSelfContainedTomlLine(line.text)) continue;
723+
carriedKeys.add(key);
724+
carried.push(line.text.replace(/\s+$/, ""));
725+
}
726+
if (carried.length === 0) return generated;
727+
728+
const lines = generated.split("\n");
729+
const firstTable = lines.findIndex((line) => /^\s*\[/.test(line));
730+
lines.splice(firstTable === -1 ? lines.length : firstTable, 0, [
731+
"# Keys OpenCodex does not manage, kept from the previous profile:",
732+
...carried,
733+
"",
734+
].join("\n"));
735+
return lines.join("\n");
736+
}
737+
675738
export function chooseCatalogPathForInjection(
676739
content: string,
677740
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: 72 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,74 @@ 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("skips a carried key whose value continues on another line", () => {
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).not.toContain("notify");
986+
expect(merged).toContain("stream_max_retries = 8");
987+
});
988+
989+
test("leaves the generated profile alone when there is nothing to carry", () => {
990+
const generated = 'model_provider = "opencodex"\n';
991+
expect(preserveUnmanagedProfileKeys(null, generated)).toBe(generated);
992+
expect(preserveUnmanagedProfileKeys('model_provider = "opencodex"\n', generated)).toBe(
993+
generated,
994+
);
995+
});
996+
997+
test("carries keys through a real profile rebuild", () => {
998+
const target = standaloneCodexRoutingTarget(10100, {});
999+
const generated = buildProfileFileForTarget(target, null);
1000+
const firstTable = generated.indexOf("[");
1001+
const prefix = generated.endsWith("\n") ? generated : `${generated}\n`;
1002+
const existing =
1003+
firstTable === -1
1004+
? `${prefix}stream_max_retries = 8\n`
1005+
: `${generated.slice(0, firstTable)}stream_max_retries = 8\n${generated.slice(firstTable)}`;
1006+
1007+
expect(preserveUnmanagedProfileKeys(existing, generated)).toContain("stream_max_retries = 8");
1008+
});
1009+
});

0 commit comments

Comments
 (0)