Skip to content

Commit ab0aa1d

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 ab0aa1d

3 files changed

Lines changed: 107 additions & 7 deletions

File tree

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,47 @@ 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+
export function preserveUnmanagedProfileKeys(existing: string | null, generated: string): string {
684+
if (!existing) return generated;
685+
const generatedKeys = new Set<string>();
686+
const generatedSource = rootSourceLines(generated);
687+
for (const line of generatedSource.lines.slice(0, generatedSource.rootEnd)) {
688+
if (!line.structural) continue;
689+
const key = rootAssignmentKey(line.text);
690+
if (key) generatedKeys.add(key);
691+
}
692+
if (generatedKeys.size === 0) return generated;
693+
694+
const carried: string[] = [];
695+
const carriedKeys = new Set<string>();
696+
const existingSource = rootSourceLines(existing);
697+
for (const line of existingSource.lines.slice(0, existingSource.rootEnd)) {
698+
if (!line.structural) continue;
699+
const key = rootAssignmentKey(line.text);
700+
if (!key || generatedKeys.has(key) || carriedKeys.has(key)) continue;
701+
carriedKeys.add(key);
702+
carried.push(line.text.replace(/\s+$/, ""));
703+
}
704+
if (carried.length === 0) return generated;
705+
706+
const lines = generated.split("\n");
707+
const firstTable = lines.findIndex((line) => /^\s*\[/.test(line));
708+
lines.splice(firstTable === -1 ? lines.length : firstTable, 0, [
709+
"# Keys OpenCodex does not manage, kept from the previous profile:",
710+
...carried,
711+
"",
712+
].join("\n"));
713+
return lines.join("\n");
714+
}
715+
675716
export function chooseCatalogPathForInjection(
676717
content: string,
677718
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: 54 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,56 @@ 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("leaves the generated profile alone when there is nothing to carry", () => {
972+
const generated = 'model_provider = "opencodex"\n';
973+
expect(preserveUnmanagedProfileKeys(null, generated)).toBe(generated);
974+
expect(preserveUnmanagedProfileKeys('model_provider = "opencodex"\n', generated)).toBe(
975+
generated,
976+
);
977+
});
978+
979+
test("carries keys through a real profile rebuild", () => {
980+
const target = standaloneCodexRoutingTarget(10100, {});
981+
const generated = buildProfileFileForTarget(target, null);
982+
const firstTable = generated.indexOf("[");
983+
const prefix = generated.endsWith("\n") ? generated : `${generated}\n`;
984+
const existing =
985+
firstTable === -1
986+
? `${prefix}stream_max_retries = 8\n`
987+
: `${generated.slice(0, firstTable)}stream_max_retries = 8\n${generated.slice(firstTable)}`;
988+
989+
expect(preserveUnmanagedProfileKeys(existing, generated)).toContain("stream_max_retries = 8");
990+
});
991+
});

0 commit comments

Comments
 (0)