@@ -3,7 +3,8 @@ import { tmpdir } from "node:os";
33import { join } from "node:path" ;
44import { Database } from "bun:sqlite" ;
55import { afterEach , describe , expect , setDefaultTimeout , test } from "bun:test" ;
6- import { classifyRecoverableHistoryError , countPendingOpencodexHistory , historyBackupPathFor , isRecoverableHistoryError , migrateHistoryToOpenai , restoreLegacyOpenaiHistory , restoredUserEventFor , setAfterNoopPendingCountForTests , setAfterStrictHistoryRolloutAppendForTests , setBeforeHistoryApplyTransactionForTests , setBeforeHistoryBackupConsumeForTests , setBeforeStrictHistoryRolloutAppendForTests , setHistoryDbBusyTimeoutForTests , snapshotCodexHistoryNoop , syncCodexHistoryProvider , withHistoryRetry } from "../../src/codex/history-provider" ;
6+ import { classifyRecoverableHistoryError , countPendingOpencodexHistory , historyBackupPathFor , isRecoverableHistoryError , legacyHistoryBackupPathFor , migrateHistoryToOpenai , resolveExistingHistoryBackupPath , restoreLegacyOpenaiHistory , restoredUserEventFor , setAfterNoopPendingCountForTests , setAfterStrictHistoryRolloutAppendForTests , setBeforeHistoryApplyTransactionForTests , setBeforeHistoryBackupConsumeForTests , setBeforeStrictHistoryRolloutAppendForTests , setHistoryDbBusyTimeoutForTests , snapshotCodexHistoryNoop , syncCodexHistoryProvider , withHistoryRetry } from "../../src/codex/history-provider" ;
7+ import { codexHistoryBackupId , legacyCodexHistoryBackupId , sameCodexHistoryPath } from "../../src/codex/history-manifest" ;
78import { INVALID_HISTORY_BACKUP_FIXTURES , validHistoryBackupFixture } from "../helpers/codex-history-manifest-fixtures" ;
89import { preflightCodexHistoryInjection , setHistoryAppendHooksForTests } from "../../src/codex/history-provider" ;
910
@@ -1514,3 +1515,85 @@ describe("Design B migration helpers", () => {
15141515 db . close ( ) ;
15151516 } ) ;
15161517} ) ;
1518+
1519+ /**
1520+ * #4442: Codex records some rollout paths with the Win32 extended-length prefix
1521+ * (`\\?\C:\...`) and others without. Both spellings name the same file, but the
1522+ * integrity check compared them literally and failed intact sessions. The identity
1523+ * leaf strips the prefix before resolving, and the manifest filename keeps a
1524+ * legacy-name fallback so a backup written under the pre-fix identity is still
1525+ * discovered. These tests fake win32 to pin the path shapes directly; the real
1526+ * platform is restored after each one.
1527+ */
1528+ describe ( "Windows history path identity (#4442)" , ( ) => {
1529+ const originalPlatform = process . platform ;
1530+ afterEach ( ( ) => {
1531+ Object . defineProperty ( process , "platform" , { configurable : true , value : originalPlatform } ) ;
1532+ } ) ;
1533+ const fakeWin32 = ( ) => {
1534+ Object . defineProperty ( process , "platform" , { configurable : true , value : "win32" } ) ;
1535+ } ;
1536+
1537+ test ( "treats extended-length-prefixed spellings as the same file" , ( ) => {
1538+ fakeWin32 ( ) ;
1539+ expect ( sameCodexHistoryPath (
1540+ "\\\\?\\C:\\Users\\a\\.codex\\sessions\\r.jsonl" ,
1541+ "C:\\Users\\a\\.codex\\sessions\\r.jsonl" ,
1542+ ) ) . toBe ( true ) ;
1543+ // The case-insensitive contract still holds across the prefix boundary.
1544+ expect ( sameCodexHistoryPath (
1545+ "c:\\users\\a\\r.jsonl" ,
1546+ "\\\\?\\C:\\Users\\A\\r.jsonl" ,
1547+ ) ) . toBe ( true ) ;
1548+ // The UNC form maps to its bare-share spelling.
1549+ expect ( sameCodexHistoryPath (
1550+ "\\\\?\\UNC\\server\\share\\r.jsonl" ,
1551+ "\\\\server\\share\\r.jsonl" ,
1552+ ) ) . toBe ( true ) ;
1553+ // Prefix normalization must not collapse genuinely different files.
1554+ expect ( sameCodexHistoryPath (
1555+ "\\\\?\\C:\\Users\\a\\r.jsonl" ,
1556+ "C:\\Users\\a\\other.jsonl" ,
1557+ ) ) . toBe ( false ) ;
1558+ // An archived_sessions move is a real path change, not a spelling alias (#4442
1559+ // keeps those cases out of this fix by design).
1560+ expect ( sameCodexHistoryPath (
1561+ "\\\\?\\C:\\Users\\a\\.codex\\archived_sessions\\r.jsonl" ,
1562+ "C:\\Users\\a\\.codex\\sessions\\r.jsonl" ,
1563+ ) ) . toBe ( false ) ;
1564+ } ) ;
1565+
1566+ test ( "hashes both spellings to one backup id while keeping the pre-fix id reachable" , ( ) => {
1567+ fakeWin32 ( ) ;
1568+ const plain = "C:\\Users\\a\\.codex\\state_5.sqlite" ;
1569+ const prefixed = "\\\\?\\C:\\Users\\a\\.codex\\state_5.sqlite" ;
1570+ expect ( codexHistoryBackupId ( prefixed ) ) . toBe ( codexHistoryBackupId ( plain ) ) ;
1571+ // The prefixed spelling hashed to a DIFFERENT manifest name before #4442; that is
1572+ // the file a machine that recorded the prefix already has on disk, so the legacy
1573+ // id must stay computable. Plain spellings are untouched by the whole change.
1574+ expect ( legacyCodexHistoryBackupId ( prefixed ) ) . not . toBe ( codexHistoryBackupId ( prefixed ) ) ;
1575+ expect ( legacyCodexHistoryBackupId ( plain ) ) . toBe ( codexHistoryBackupId ( plain ) ) ;
1576+ } ) ;
1577+
1578+ test ( "discovers a manifest named under the pre-#4442 identity and never replaces it silently" , ( ) => {
1579+ fakeWin32 ( ) ;
1580+ const stateDb = "\\\\?\\C:\\Users\\a\\.codex\\state_5.sqlite" ;
1581+ const canonical = historyBackupPathFor ( stateDb ) ;
1582+ const legacy = legacyHistoryBackupPathFor ( stateDb ) ;
1583+ expect ( legacy ) . not . toBe ( canonical ) ;
1584+
1585+ const present = new Set < string > ( [ legacy ] ) ;
1586+ const exists = ( path : string ) => present . has ( path ) ;
1587+ // Only the legacy name exists: it still shadows the database, so restore and
1588+ // no-op probes keep finding its provenance instead of reading it as absent.
1589+ expect ( resolveExistingHistoryBackupPath ( stateDb , exists ) ) . toBe ( legacy ) ;
1590+ // Both names exist (conflicting old/new manifests): the canonical manifest wins
1591+ // and the legacy file is left in place — kept, never silently replaced.
1592+ present . add ( canonical ) ;
1593+ expect ( resolveExistingHistoryBackupPath ( stateDb , exists ) ) . toBe ( canonical ) ;
1594+ expect ( present . has ( legacy ) ) . toBe ( true ) ;
1595+ // Neither exists: a new manifest is written under the canonical name.
1596+ present . clear ( ) ;
1597+ expect ( resolveExistingHistoryBackupPath ( stateDb , exists ) ) . toBe ( canonical ) ;
1598+ } ) ;
1599+ } ) ;
0 commit comments