diff --git a/src/main/codex/codex-session-resume-home.test.ts b/src/main/codex/codex-session-resume-home.test.ts index 912a22695..75d91d7ab 100644 --- a/src/main/codex/codex-session-resume-home.test.ts +++ b/src/main/codex/codex-session-resume-home.test.ts @@ -45,6 +45,121 @@ describe('resolveTrustedCodexSessionResumeHome', () => { ).toBe('c:\\users\\example\\.codex') }) + it('accepts an extended-length Windows rollout and preserves its original path', async () => { + const homePath = 'C:\\Users\\Example\\.codex' + const transcriptPath = + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-session.jsonl' + + await expect( + findTrustedCodexSessionResume({ + sessionId: 'session-a', + transcriptPath, + trustedCodexHomes: [homePath], + ...withoutHomeRanking, + fileIsRegular: (filePath) => filePath === transcriptPath + }) + ).resolves.toEqual({ homePath, transcriptPath }) + }) + + it('rejects unsafe or unrelated Windows namespace paths before probing files', () => { + const fileIsRegular = vi.fn((): boolean => true) + const trustedCodexHomes = ['C:\\Users\\Example\\.codex'] + const rejectedPaths = [ + '\\\\?\\D:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-a.jsonl', + '\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\sessions\\2026\\07\\20\\rollout-a.jsonl', + '\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\sessions\\2026\\07\\20\\rollout-a.jsonl', + '\\\\.\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-a.jsonl', + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-a:stream.jsonl', + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\..\\rollout-a.jsonl' + ] + + for (const transcriptPath of rejectedPaths) { + expect( + resolveTrustedCodexSessionResumeHome({ + transcriptPath, + trustedCodexHomes, + fileIsRegular + }) + ).toBeNull() + } + expect(fileIsRegular).not.toHaveBeenCalled() + }) + + it('rejects a missing or non-regular extended-length rollout', () => { + expect( + resolveTrustedCodexSessionResumeHome({ + transcriptPath: + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-a.jsonl', + trustedCodexHomes: ['C:\\Users\\Example\\.codex'], + fileIsRegular: () => false + }) + ).toBeNull() + }) + + // Why: the sessions root is folded too, so the home side must accept the extended spelling. + it('accepts a normal-form rollout under an extended-length trusted home', () => { + const homePath = '\\\\?\\C:\\Users\\Example\\.codex' + expect( + resolveTrustedCodexSessionResumeHome({ + transcriptPath: 'C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-a.jsonl', + trustedCodexHomes: [homePath], + fileIsRegular: () => true + }) + ).toBe(homePath) + }) + + // Why: the .zst sibling is derived from the persisted path, so a folded + // comparison copy must never leak into the probed or returned path. + it('follows a compressed extended-length rollout without losing the extended spelling', async () => { + const homePath = 'C:\\Users\\Example\\.codex' + const plainPath = + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-session.jsonl' + const compressedPath = `${plainPath}.zst` + + await expect( + findTrustedCodexSessionResume({ + sessionId: 'session-a', + transcriptPath: plainPath, + trustedCodexHomes: [homePath], + ...withoutHomeRanking, + fileIsRegular: (filePath) => filePath === compressedPath + }) + ).resolves.toEqual({ homePath, transcriptPath: compressedPath }) + }) + + // Why: the id scan is a second call site of the same check, once per directory entry. + it('accepts extended-length scan entries but not device-namespace entries', async () => { + const sessionId = '019f81b9-19a9-7651-a8d1-352d9420bd11' + const homePath = 'C:\\Users\\Example\\.codex' + const extendedEntry = `\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-${sessionId}.jsonl` + + await expect( + findTrustedCodexSessionResume({ + sessionId, + transcriptPath: undefined, + trustedCodexHomes: [homePath], + ...withoutHomeRanking, + fileIsRegular: () => true, + listSessionFiles: async function* (): AsyncIterable { + yield extendedEntry + } + }) + ).resolves.toEqual({ homePath, transcriptPath: extendedEntry }) + + await expect( + findTrustedCodexSessionResume({ + sessionId, + transcriptPath: undefined, + trustedCodexHomes: [homePath], + ...withoutHomeRanking, + fileIsRegular: () => true, + listSessionFiles: async function* (): AsyncIterable { + yield `\\\\.\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-${sessionId}.jsonl` + } + }) + ).resolves.toBeNull() + }) + it('rejects paths outside trusted homes or outside the rollout layout', () => { const fileIsRegular = vi.fn((): boolean => true) expect( @@ -225,6 +340,25 @@ describe('resolveTrustedCodexSessionResumeHome', () => { expect(listSessionFiles).not.toHaveBeenCalled() }) + it('does not scan another home after rejecting extended-length provenance', async () => { + const listSessionFiles = vi.fn((): AsyncIterable => { + throw new Error('must not scan') + }) + + await expect( + findTrustedCodexSessionResume({ + sessionId: '019f81b9-19a9-7651-a8d1-352d9420bd11', + transcriptPath: + '\\\\?\\D:\\Other\\.codex\\sessions\\2026\\07\\20\\rollout-019f81b9-19a9-7651-a8d1-352d9420bd11.jsonl', + trustedCodexHomes: ['C:\\Users\\Example\\.codex'], + ...withoutHomeRanking, + fileIsRegular: () => true, + listSessionFiles + }) + ).resolves.toBeNull() + expect(listSessionFiles).not.toHaveBeenCalled() + }) + it('does not scan homes for an untrusted legacy session id shape', async () => { const listSessionFiles = (): AsyncIterable => { throw new Error('must not scan') @@ -446,6 +580,14 @@ describe('claimsCodexRolloutLayout', () => { ).toBe(true) }) + it('is true for an ADS-shaped rollout that provenance trust rejects', () => { + expect( + claimsCodexRolloutLayout( + 'C:\\Users\\example\\.codex\\sessions\\2026\\07\\20\\rollout-a:stream.jsonl' + ) + ).toBe(true) + }) + it('is false for Claude (or other non-Codex) transcript paths', () => { expect( claimsCodexRolloutLayout( diff --git a/src/main/codex/codex-session-resume-home.ts b/src/main/codex/codex-session-resume-home.ts index f4aff76a4..2cd581dbb 100644 --- a/src/main/codex/codex-session-resume-home.ts +++ b/src/main/codex/codex-session-resume-home.ts @@ -8,10 +8,11 @@ import { import { listCodexSessionRolloutFilesIncrementally } from './codex-session-file-listing' // Why: only Codex's dated rollout layout may establish account-home provenance; nested/misplaced JSONL must not select credentials. -const DATED_ROLLOUT_TAIL = String.raw`\d{4}/\d{2}/\d{2}/rollout-[^/]+\.jsonl(?:\.zst)?` -const ROLLOUT_RELATIVE_PATH = new RegExp(`^${DATED_ROLLOUT_TAIL}$`) +const CLAIMED_CODEX_ROLLOUT_TAIL = String.raw`\d{4}/\d{2}/\d{2}/rollout-[^/]+\.jsonl(?:\.zst)?` +const TRUSTED_CODEX_ROLLOUT_TAIL = String.raw`\d{4}/\d{2}/\d{2}/rollout-[^/:]+\.jsonl(?:\.zst)?` +const ROLLOUT_RELATIVE_PATH = new RegExp(`^${TRUSTED_CODEX_ROLLOUT_TAIL}$`) // Why: case-insensitive because trusted-home matching folds Windows path case too. -const CODEX_ROLLOUT_LAYOUT_PATH = new RegExp(`(?:^|/)sessions/${DATED_ROLLOUT_TAIL}$`, 'i') +const CODEX_ROLLOUT_LAYOUT_PATH = new RegExp(`(?:^|/)sessions/${CLAIMED_CODEX_ROLLOUT_TAIL}$`, 'i') /** `resume` pins CODEX_HOME to the account that owns the rollout. `fresh` means * provenance could not be verified, so the caller drops the resume argv — an @@ -22,8 +23,25 @@ export type CodexSessionResumePreparation = | { outcome: 'resume'; codexHomePath: string } | { outcome: 'fresh'; claimedCodexProvenance: boolean } +// Why: fold only Win32's extended drive spelling; \\.\ device namespaces and every other \\?\ form +// (including \\?\UNC\, which is a network share rather than a device) stay unfolded here. +function toCodexTrustedPathComparisonCopy(filePath: string): string | null { + if (filePath.startsWith('\\\\.\\')) { + return null + } + if (!filePath.startsWith('\\\\?\\')) { + return filePath + } + return filePath.match(/^\\\\\?\\([A-Za-z]:[\\/][\s\S]*)$/)?.[1] ?? null +} + function isCodexRolloutInsideSessionsRoot(sessionsRoot: string, filePath: string): boolean { - const relativePath = relativePathInsideRoot(sessionsRoot, filePath) + const comparisonSessionsRoot = toCodexTrustedPathComparisonCopy(sessionsRoot) + const comparisonFilePath = toCodexTrustedPathComparisonCopy(filePath) + if (!comparisonSessionsRoot || !comparisonFilePath) { + return false + } + const relativePath = relativePathInsideRoot(comparisonSessionsRoot, comparisonFilePath) return Boolean(relativePath && ROLLOUT_RELATIVE_PATH.test(relativePath.replace(/\\/g, '/'))) } diff --git a/src/main/codex/codex-session-resume-windows.test.ts b/src/main/codex/codex-session-resume-windows.test.ts new file mode 100644 index 000000000..1f5e1d88c --- /dev/null +++ b/src/main/codex/codex-session-resume-windows.test.ts @@ -0,0 +1,46 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { dirname, join, toNamespacedPath } from 'node:path' +import { tmpdir } from 'node:os' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { prepareCodexSessionResume } from './codex-session-resume-preparation' + +const windowsIt = process.platform === 'win32' ? it : it.skip +const cleanupPaths: string[] = [] + +afterEach(() => { + for (const cleanupPath of cleanupPaths.splice(0)) { + rmSync(cleanupPath, { recursive: true, force: true }) + } +}) + +describe('Codex session resume on native Windows', () => { + windowsIt('resumes an extended-length rollout from its ordinary trusted home', async () => { + const homePath = mkdtempSync(join(tmpdir(), 'orca-codex-resume-')) + cleanupPaths.push(homePath) + const rolloutPath = join( + homePath, + 'sessions', + '2026', + '07', + '20', + 'rollout-019f81b9-19a9-7651-a8d1-352d9420bd11.jsonl' + ) + mkdirSync(dirname(rolloutPath), { recursive: true }) + writeFileSync(rolloutPath, '{"type":"session_meta"}\n') + const transcriptPath = toNamespacedPath(rolloutPath) + const resolveVerifiedResumeHome = vi.fn(async () => homePath) + + await expect( + prepareCodexSessionResume({ + sessionId: '019f81b9-19a9-7651-a8d1-352d9420bd11', + transcriptPath, + trustedCodexHomes: [homePath], + getSelectedAccountCodexHome: () => null, + systemCodexHomePath: homePath, + sharedRuntimeCodexHomePath: null, + resolveVerifiedResumeHome + }) + ).resolves.toEqual({ outcome: 'resume', codexHomePath: homePath }) + expect(resolveVerifiedResumeHome).toHaveBeenCalledWith({ homePath, transcriptPath }) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/pty-connection.test.ts b/src/renderer/src/components/terminal-pane/pty-connection.test.ts index 261916779..2e0ce2b21 100644 --- a/src/renderer/src/components/terminal-pane/pty-connection.test.ts +++ b/src/renderer/src/components/terminal-pane/pty-connection.test.ts @@ -9609,6 +9609,8 @@ describe('connectPanePty', () => { }) transportFactoryQueue.push(transport) const paneKey = makePaneKey('tab-1', LEAF_1) + const transcriptPath = + '\\\\?\\C:\\Users\\Example\\.codex\\sessions\\2026\\07\\20\\rollout-codex-session-1.jsonl' // Why: after restart agentStatusByPaneKey is empty — the persisted sleeping record is the only provider session id source (#5232). mockStoreState = { ...mockStoreState, @@ -9626,7 +9628,11 @@ describe('connectPanePty', () => { tabId: 'tab-1', worktreeId: 'wt-1', agent: 'codex', - providerSession: { key: 'session_id', id: 'codex-session-1' }, + providerSession: { + key: 'session_id', + id: 'codex-session-1', + transcriptPath + }, prompt: 'finish the task', state: 'working', capturedAt: 1, @@ -9660,6 +9666,11 @@ describe('connectPanePty', () => { expect.objectContaining({ sessionId: 'lost-pty', command: "codex '--dangerously-bypass-approvals-and-sandbox' 'resume' 'codex-session-1'", + resumeProviderSession: { + key: 'session_id', + id: 'codex-session-1', + transcriptPath + }, env: expect.objectContaining({ ORCA_PANE_KEY: paneKey, ORCA_TAB_ID: 'tab-1',