Skip to content

Commit f61de72

Browse files
committed
fix(codex): preserve external history on async restore
1 parent 2468502 commit f61de72

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎src/codex/inject.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,10 @@ export async function restoreNativeCodexAsync(): Promise<{
11941194
success: boolean;
11951195
message: string;
11961196
}> {
1197-
const inline = restoreNativeCodex({ skipHistory: true });
1197+
const inline = restoreNativeCodexCore({ skipHistory: true });
1198+
if (!inline.restoreHistory) {
1199+
return { success: inline.success, message: inline.message };
1200+
}
11981201
const outcome = await runCodexHistoryJob({
11991202
...resolveCodexHistoryJobTarget(),
12001203
operation: deriveCodexHistoryOperation({
@@ -1217,16 +1220,18 @@ export async function restoreNativeCodexAsync(): Promise<{
12171220
return { success: inline.success, message: `${inline.message}${historyMsg}` };
12181221
}
12191222

1220-
export function restoreNativeCodex(options: { skipHistory?: boolean } = {}): {
1223+
function restoreNativeCodexCore(options: { skipHistory?: boolean } = {}): {
12211224
success: boolean;
12221225
message: string;
1226+
restoreHistory: boolean;
12231227
} {
12241228
const activeProvider = currentExternalCodexModelProvider();
12251229
if (activeProvider) {
12261230
removeJournal();
12271231
return {
12281232
success: true,
12291233
message: `External Codex provider ${tomlString(activeProvider)} preserved; no native restore was needed.`,
1234+
restoreHistory: false,
12301235
};
12311236
}
12321237
const journal = restoreJournalState();
@@ -1272,7 +1277,15 @@ export function restoreNativeCodex(options: { skipHistory?: boolean } = {}): {
12721277
: history.ejectedRows
12731278
? ` ${history.ejectedRows} opencodex history thread(s) were ejected to openai so native Codex can resume them.`
12741279
: "";
1275-
return { success: cfg.success, message: `${msg}${historyMsg}` };
1280+
return { success: cfg.success, message: `${msg}${historyMsg}`, restoreHistory: true };
1281+
}
1282+
1283+
export function restoreNativeCodex(options: { skipHistory?: boolean } = {}): {
1284+
success: boolean;
1285+
message: string;
1286+
} {
1287+
const result = restoreNativeCodexCore(options);
1288+
return { success: result.success, message: result.message };
12761289
}
12771290

12781291
export function getCodexConfigPath(): string {

‎tests/codex-history-job.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ test("an overrun Worker returns a typed timeout rather than hanging", async () =
147147
*/
148148
test("the synchronous restore body is gated on skipHistory", () => {
149149
const source = readFileSync(join(import.meta.dir, "..", "src", "codex", "inject.ts"), "utf8");
150-
const body = source.slice(source.indexOf("export function restoreNativeCodex("));
150+
const body = source.slice(source.indexOf("function restoreNativeCodexCore("));
151151
const historyCall = body.indexOf("syncCodexHistoryProvider(\"openai\"");
152152
expect(historyCall).toBeGreaterThan(-1);
153153

@@ -157,5 +157,5 @@ test("the synchronous restore body is gated on skipHistory", () => {
157157
expect(gate).toBeLessThan(historyCall);
158158

159159
// And the async wrapper is the thing that sets it.
160-
expect(source).toContain("restoreNativeCodex({ skipHistory: true })");
160+
expect(source).toContain("restoreNativeCodexCore({ skipHistory: true })");
161161
});

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ function runRestore(codexHome: string, ocxHome: string): { stdout: string; statu
4242
return { stdout: result.stdout?.trim() ?? "", status: result.status ?? 1 };
4343
}
4444

45+
function runRestoreAsync(codexHome: string, ocxHome: string): { stdout: string; status: number } {
46+
const script = `
47+
const { restoreNativeCodexAsync } = require("./src/codex/inject");
48+
restoreNativeCodexAsync().then(result => console.log(JSON.stringify(result)));
49+
`;
50+
const result = spawnSync(process.execPath, ["--eval", script], {
51+
cwd: repoRoot,
52+
env: { ...process.env, CODEX_HOME: codexHome, OPENCODEX_HOME: ocxHome },
53+
encoding: "utf8",
54+
});
55+
return { stdout: result.stdout?.trim() ?? "", status: result.status ?? 1 };
56+
}
57+
4558
describe("injectCodexConfig integration (Design B)", () => {
4659
let codexHome: string;
4760
let ocxHome: string;
@@ -505,6 +518,35 @@ describe("injectCodexConfig integration (Design B)", () => {
505518
expect(existsSync(journalPath)).toBe(false);
506519
});
507520

521+
test("async restore preserves external provider history", () => {
522+
const configPath = join(codexHome, "config.toml");
523+
writeFileSync(configPath, 'model_provider = "custom"\n', "utf8");
524+
const rolloutPath = join(codexHome, "rollout-custom.jsonl");
525+
const rollout = `${JSON.stringify({
526+
type: "session_meta",
527+
payload: { id: "thread-custom", model_provider: "opencodex", source: "cli" },
528+
})}\n`;
529+
writeFileSync(rolloutPath, rollout, "utf8");
530+
const dbPath = join(codexHome, "state_5.sqlite");
531+
const db = new Database(dbPath);
532+
db.run(`CREATE TABLE threads (
533+
id TEXT PRIMARY KEY, rollout_path TEXT NOT NULL, model_provider TEXT NOT NULL,
534+
source TEXT NOT NULL, first_user_message TEXT NOT NULL, has_user_event INTEGER NOT NULL
535+
)`);
536+
db.run(`INSERT INTO threads VALUES ('thread-custom', ?, 'opencodex', 'cli', 'hello', 1)`, rolloutPath);
537+
db.close();
538+
539+
const result = runRestoreAsync(codexHome, ocxHome);
540+
541+
expect(result.status).toBe(0);
542+
expect(JSON.parse(result.stdout).message).toContain('External Codex provider "custom" preserved');
543+
const restored = new Database(dbPath, { readonly: true });
544+
expect(restored.query("SELECT model_provider FROM threads WHERE id = 'thread-custom'").get())
545+
.toEqual({ model_provider: "opencodex" });
546+
restored.close();
547+
expect(readFileSync(rolloutPath, "utf8")).toBe(rollout);
548+
});
549+
508550
test("provider selected through a legacy root profile is also preserved", () => {
509551
const original = [
510552
'profile = "work"',

0 commit comments

Comments
 (0)