From 9f11558543a68fffe341aecc87026f06ff0cc87a Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 06:02:44 -0700 Subject: [PATCH] perf: append codex migration diagnostics (#4147) --- .../codex-accounts/runtime-home-service.test.ts | 16 ++++++++++++++-- src/main/codex-accounts/runtime-home-service.ts | 12 ++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main/codex-accounts/runtime-home-service.test.ts b/src/main/codex-accounts/runtime-home-service.test.ts index b1ae2a138..7f9cba3c6 100644 --- a/src/main/codex-accounts/runtime-home-service.test.ts +++ b/src/main/codex-accounts/runtime-home-service.test.ts @@ -2771,6 +2771,8 @@ describe('CodexRuntimeHomeService', () => { const runtimeSessionsDir = join(getRuntimeCodexHomePath(), 'sessions') mkdirSync(runtimeSessionsDir, { recursive: true }) writeFileSync(join(runtimeSessionsDir, 'session.json'), '{"turns":[1]}', 'utf-8') + mkdirSync(join(runtimeSessionsDir, 'nested'), { recursive: true }) + writeFileSync(join(runtimeSessionsDir, 'nested', 'session.json'), '{"turns":[2]}', 'utf-8') const managedHomePath = createManagedAuth( testState.userDataDir, 'account-1', @@ -2779,6 +2781,8 @@ describe('CodexRuntimeHomeService', () => { const legacySessionsDir = join(managedHomePath, 'sessions') mkdirSync(legacySessionsDir, { recursive: true }) writeFileSync(join(legacySessionsDir, 'session.json'), '{"turns":[1,2]}', 'utf-8') + mkdirSync(join(legacySessionsDir, 'nested'), { recursive: true }) + writeFileSync(join(legacySessionsDir, 'nested', 'session.json'), '{"turns":[2,3]}', 'utf-8') const store = createStore(createSettings()) const { CodexRuntimeHomeService } = await import('./runtime-home-service') @@ -2790,9 +2794,17 @@ describe('CodexRuntimeHomeService', () => { ).toBe('{"turns":[1,2]}') expect( readFileSync( - join(testState.userDataDir, 'codex-runtime-home', 'migration-diagnostics.jsonl'), + join(runtimeSessionsDir, 'nested', 'session.orca-legacy-account-1.json'), 'utf-8' ) - ).toContain('"type":"session-conflict"') + ).toBe('{"turns":[2,3]}') + const diagnostics = readFileSync( + join(testState.userDataDir, 'codex-runtime-home', 'migration-diagnostics.jsonl'), + 'utf-8' + ) + .trim() + .split('\n') + expect(diagnostics).toHaveLength(2) + expect(diagnostics[0]).toContain('"type":"session-conflict"') }) }) diff --git a/src/main/codex-accounts/runtime-home-service.ts b/src/main/codex-accounts/runtime-home-service.ts index 67d8a130c..b72225229 100644 --- a/src/main/codex-accounts/runtime-home-service.ts +++ b/src/main/codex-accounts/runtime-home-service.ts @@ -3,6 +3,7 @@ contract for Codex inside Orca. Keeping path resolution, system-default snapshots, auth materialization, and recovery together prevents account-switch semantics from drifting across PTY launch, login, and quota fetch paths. */ import { + appendFileSync, copyFileSync, existsSync, chmodSync, @@ -995,10 +996,13 @@ export class CodexRuntimeHomeService { private appendMigrationDiagnostic(record: Record): void { const diagnosticsPath = this.getMigrationDiagnosticsPath() - const existingContents = existsSync(diagnosticsPath) - ? readFileSync(diagnosticsPath, 'utf-8') - : '' - writeFileAtomically(diagnosticsPath, `${existingContents}${JSON.stringify(record)}\n`) + try { + appendFileSync(diagnosticsPath, `${JSON.stringify(record)}\n`, { encoding: 'utf-8' }) + } catch (error) { + // Why: conflict diagnostics are useful, but must not make the one-shot + // migration fail after the session file has already been preserved. + console.warn('[codex-runtime-home] Failed to append migration diagnostic:', error) + } } private captureSystemDefaultSnapshot(options: { force: boolean }): void {