Repository navigation
Codex upgrade migration never overwrites chats or drops prompt history #45294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brennanb2025
merged 32 commits into
main
from
brennanb2025/codex-legacy-migration-no-overwrite
Nov 23, 2026
Merged
Changes from 13 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
95de52f
fix(codex): legacy migration never overwrites transcripts or drops hi…
brennanb2025 cb03fef
fix(codex): import legacy prompt history only while no Codex thread i…
brennanb2025 e5b7009
fix(codex): never write a live shared history during the legacy migra…
brennanb2025 e29d179
fix(codex): keep legacy prompt history aside before anything else in …
brennanb2025 614be27
fix(codex): retry the legacy history import from Orca's kept copies
brennanb2025 c62e27c
fix(codex): run the kept legacy history import independently of sessi…
brennanb2025 acedce5
fix(codex): keep one real copy of each legacy history, published by r…
brennanb2025 1e35b8b
fix(codex): refresh the kept legacy history and refuse linked kept fo…
brennanb2025 1148b24
fix(codex): set the earlier kept history aside when Codex trimmed the…
brennanb2025 190d347
fix(codex): account removal keeps prompt history; import reads accoun…
brennanb2025 6e2f608
fix(codex): skip the history import where hard links are refused; say…
brennanb2025 048ae71
fix(codex): retry on unreadable account histories; classify only link…
brennanb2025 c08949a
fix(codex): mark the migration after the session copies; retry only a…
brennanb2025 392a18f
fix(codex): read linked account histories; ignore account folders wit…
brennanb2025 75dee20
fix(codex): mark the migration done only once history settles; keep a…
brennanb2025 7caad8a
fix(codex): keep every entry a linked prompt history passes through; …
brennanb2025 351c00f
fix(codex): follow a linked prompt history one component at a time; s…
brennanb2025 deddf1f
fix(codex): preserve each account history in the runtime home, then m…
brennanb2025 0c1abd9
fix(codex): preserve account histories off the main thread, before ma…
brennanb2025 653270d
fix(codex): delete only the logins from a home whose history is a lin…
brennanb2025 c184003
fix(codex): run the session pass after the history copies, and write …
brennanb2025 6ce1919
Merge remote-tracking branch 'origin/main' into brennanb2025/codex-le…
brennanb2025 9361342
fix(codex): copy histories, sessions and the marker in one synchronou…
brennanb2025 f53e6d6
fix(codex): remove only listed login files beside a linked history; m…
brennanb2025 3cc96ac
fix(codex): leave a linked secrets folder's files alone; reuse one te…
brennanb2025 96cf8ec
fix(codex): remove nothing from a home whose history is a link; copy …
brennanb2025 0c37e1d
refactor(codex): main's migration marker, capped session retries, Win…
brennanb2025 b5e6170
fix(codex): keep session conflict copies only from a session pass tha…
brennanb2025 2e64257
fix(codex): a failing session pass removes the conflict copies it made
brennanb2025 29ca9ef
test(codex): skip the linked account history test on Windows
brennanb2025 441e1c0
Merge origin/main into brennanb2025/codex-legacy-migration-no-overwrite
brennanb2025 992489f
test(codex): check session copies and the shared history right after …
brennanb2025 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/main/codex-accounts/runtime-home/legacy-host/legacy-prompt-history-import.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import { randomUUID } from 'node:crypto' | ||
| import { link, lstat, open, readFile, readdir, rm } from 'node:fs/promises' | ||
| import { join } from 'node:path' | ||
| import { errnoCodeOrUnknown } from '../../../../shared/errors/errno-code' | ||
| import { track } from '../../../telemetry/client' | ||
| import type { CODEX_LEGACY_HISTORY_IMPORT_OUTCOMES } from '../../../../shared/telemetry/schemas/telemetry-codex-legacy-history-event-schemas' | ||
|
|
||
| type LegacyHistoryImportOutcome = (typeof CODEX_LEGACY_HISTORY_IMPORT_OUTCOMES)[number] | ||
|
|
||
| /** Launches that try the history import before it gives up; account histories stay either way. */ | ||
| export const MAX_HISTORY_IMPORT_ATTEMPTS = 3 | ||
|
|
||
| export type LegacyMigrationMarker = { | ||
| completedAt?: number | ||
| migratedHomeCount?: number | ||
| historyImportPending?: true | ||
| historyImportAttempts?: number | ||
| } | ||
|
|
||
| export function parseLegacyMigrationMarker(text: string): LegacyMigrationMarker { | ||
| const parsed: unknown = JSON.parse(text) | ||
| const marker: LegacyMigrationMarker = {} | ||
| if (!parsed || typeof parsed !== 'object') { | ||
| return marker | ||
| } | ||
| if ('completedAt' in parsed && typeof parsed.completedAt === 'number') { | ||
| marker.completedAt = parsed.completedAt | ||
| } | ||
| if ('migratedHomeCount' in parsed && typeof parsed.migratedHomeCount === 'number') { | ||
| marker.migratedHomeCount = parsed.migratedHomeCount | ||
| } | ||
| if ('historyImportPending' in parsed && parsed.historyImportPending === true) { | ||
| marker.historyImportPending = true | ||
| } | ||
| if ('historyImportAttempts' in parsed && typeof parsed.historyImportAttempts === 'number') { | ||
| marker.historyImportAttempts = parsed.historyImportAttempts | ||
| } | ||
| return marker | ||
| } | ||
|
|
||
| async function pathExists(path: string): Promise<boolean> { | ||
| try { | ||
| await lstat(path) | ||
| return true | ||
| } catch (error) { | ||
| if (errnoCodeOrUnknown(error) === 'ENOENT') { | ||
| return false | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| // Every account's own history file, ownership marker or not: a removed or shared home keeps it. | ||
| async function listAccountHistories(accountsRoot: string): Promise<string[]> { | ||
| const accounts = await readdir(accountsRoot, { withFileTypes: true }).catch((error: unknown) => { | ||
| if (errnoCodeOrUnknown(error) === 'ENOENT') { | ||
| return [] | ||
| } | ||
| throw error | ||
| }) | ||
| const paths: string[] = [] | ||
| for (const account of accounts.filter((entry) => entry.isDirectory())) { | ||
| const historyPath = join(accountsRoot, account.name, 'home', 'history.jsonl') | ||
| // Only a missing file means no history; any other error retries rather than skip an account. | ||
| const stats = await lstat(historyPath).catch((error: unknown) => { | ||
| if (errnoCodeOrUnknown(error) === 'ENOENT') { | ||
| return null | ||
| } | ||
| throw error | ||
| }) | ||
| if (stats?.isFile()) { | ||
| paths.push(historyPath) | ||
| } | ||
| } | ||
| return paths.sort() | ||
| } | ||
|
|
||
| /** | ||
| * Codex may be appending to or trimming a shared history.jsonl that exists, so only a missing one | ||
| * is created from the account histories, which are only read, and never over one Codex creates | ||
| * meanwhile. Rejects on failure, so the migration retries on the next launch. | ||
| */ | ||
| export async function importLegacyHistoriesIfSharedAbsent( | ||
| accountsRoot: string, | ||
| runtimeHistoryPath: string | ||
| ): Promise<void> { | ||
| const accountHistoryPaths = await listAccountHistories(accountsRoot) | ||
| if (accountHistoryPaths.length === 0) { | ||
| return | ||
| } | ||
| let outcome: LegacyHistoryImportOutcome = 'failed' | ||
| try { | ||
| outcome = await publishLegacyHistoriesIfAbsent(accountHistoryPaths, runtimeHistoryPath) | ||
| } finally { | ||
| trackLegacyHistoryImport(outcome) | ||
| } | ||
| } | ||
|
|
||
| async function publishLegacyHistoriesIfAbsent( | ||
| accountHistoryPaths: readonly string[], | ||
| runtimeHistoryPath: string | ||
| ): Promise<LegacyHistoryImportOutcome> { | ||
| if (await pathExists(runtimeHistoryPath)) { | ||
| return 'skipped_existing' | ||
| } | ||
| const seenLines = new Set<string>() | ||
| for (const accountHistoryPath of accountHistoryPaths) { | ||
| for (const line of (await readFile(accountHistoryPath, 'utf-8')).split('\n')) { | ||
| if (line) { | ||
| seenLines.add(line) | ||
| } | ||
| } | ||
| } | ||
| if (seenLines.size === 0) { | ||
| return 'nothing_to_import' | ||
| } | ||
| return publishWithoutOverwrite(`${[...seenLines].join('\n')}\n`, runtimeHistoryPath) | ||
| } | ||
|
|
||
| // exFAT, FAT32 and some network or FUSE mounts refuse hard links. | ||
| const HARD_LINK_UNSUPPORTED = new Set(['EPERM', 'ENOTSUP', 'EOPNOTSUPP', 'ENOSYS', 'EXDEV']) | ||
|
|
||
| async function publishWithoutOverwrite( | ||
| contents: string, | ||
| targetPath: string | ||
| ): Promise<LegacyHistoryImportOutcome> { | ||
| const tmpPath = `${targetPath}.${process.pid}.${randomUUID()}.tmp` | ||
| try { | ||
| const handle = await open(tmpPath, 'wx', 0o600) | ||
| try { | ||
| await handle.writeFile(contents, 'utf-8') | ||
| await handle.sync() | ||
| } finally { | ||
| await handle.close() | ||
| } | ||
| return await linkIntoFreeName(tmpPath, targetPath) | ||
| } finally { | ||
| await rm(tmpPath, { force: true }) | ||
| } | ||
| } | ||
|
|
||
| // A hard link publishes only into a free name: one Codex created meanwhile stays as it is. | ||
| async function linkIntoFreeName( | ||
| tmpPath: string, | ||
| targetPath: string | ||
| ): Promise<LegacyHistoryImportOutcome> { | ||
| try { | ||
| await link(tmpPath, targetPath) | ||
| return 'imported' | ||
| } catch (error) { | ||
| const code = errnoCodeOrUnknown(error) | ||
| if (code === 'EEXIST') { | ||
| return 'skipped_existing' | ||
| } | ||
| // No safe way to publish without replacing: every other way leaves a name Codex can append | ||
| // into mid-write. The account histories stay in their homes, so nothing is lost. | ||
| if (HARD_LINK_UNSUPPORTED.has(code)) { | ||
| return 'skipped_no_hard_links' | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| function trackLegacyHistoryImport(outcome: LegacyHistoryImportOutcome): void { | ||
| try { | ||
| track('codex_legacy_history_import', { | ||
| outcome, | ||
| platform: | ||
| process.platform === 'win32' || process.platform === 'linux' ? process.platform : 'darwin' | ||
| }) | ||
| } catch { | ||
| // Telemetry never costs the migration. | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.