diff --git a/src/launcher-homes.ts b/src/launcher-homes.ts index 1c5446f0..191afb34 100644 --- a/src/launcher-homes.ts +++ b/src/launcher-homes.ts @@ -41,15 +41,24 @@ export function isNestedLauncherCodexHome( return existsSync(primary) } +// Same bound as `readFirstLine` in providers/codex.ts. Real Codex +// session_meta first lines are ~22–27 KB; 8 KiB truncates them mid-JSON. +export const FIRST_LINE_READ_CAP = 1024 * 1024 + /** Session id from the first `session_meta` line of a Codex rollout file. * Missing / unreadable files return undefined — callers must not drop those. */ export function rolloutFileSessionId(filePath: string): string | undefined { try { const fd = openSync(filePath, 'r') try { - const buf = Buffer.alloc(8192) + const buf = Buffer.alloc(FIRST_LINE_READ_CAP) const n = readSync(fd, buf, 0, buf.length, 0) - const line = buf.subarray(0, n).toString('utf8').split('\n', 1)[0] ?? '' + if (n === 0) return undefined + const slice = buf.subarray(0, n) + const nl = slice.indexOf(0x0a) + // Hit the 1 MiB cap without a newline: incomplete, not a parseable id. + if (nl === -1 && n === FIRST_LINE_READ_CAP) return undefined + const line = (nl === -1 ? slice : slice.subarray(0, nl)).toString('utf8') if (!line) return undefined const parsed: unknown = JSON.parse(line) if (!parsed || typeof parsed !== 'object') return undefined diff --git a/src/providers/codex.ts b/src/providers/codex.ts index d036c4e6..81bbba8f 100644 --- a/src/providers/codex.ts +++ b/src/providers/codex.ts @@ -12,7 +12,7 @@ import { normalizeContentBlocks } from '../content-utils.js' import { estimateTokensFromChars } from '../token-estimate.js' import type { ToolCall } from '../types.js' import type { Provider, ProbeRoot, SessionSource, SessionParser, ParsedProviderCall } from './types.js' -import { defaultBilledCodexHome, defaultLauncherRoots, isNestedLauncherCodexHome, listRolloutSessionIds, rolloutFileSessionId, sameCodexHome } from '../launcher-homes.js' +import { defaultBilledCodexHome, defaultLauncherRoots, FIRST_LINE_READ_CAP, isNestedLauncherCodexHome, listRolloutSessionIds, rolloutFileSessionId, sameCodexHome } from '../launcher-homes.js' const modelDisplayNames: Record = { 'codex-auto-review': 'Codex Auto Review', @@ -178,7 +178,7 @@ function sanitizeProject(cwd: string): string { // Cap how many bytes we'll read while looking for the first newline. Real // Codex session_meta lines are ~22-27 KB; this leaves plenty of headroom while // keeping memory bounded if a corrupt file has no newline at all. -const FIRST_LINE_READ_CAP = 1024 * 1024 +// FIRST_LINE_READ_CAP is shared with rolloutFileSessionId. async function readFirstLine(filePath: string): Promise { // Codex CLI 0.128+ writes a session_meta line that can exceed 20 KB because diff --git a/tests/launcher-homes.test.ts b/tests/launcher-homes.test.ts index 465f670d..0bc39449 100644 --- a/tests/launcher-homes.test.ts +++ b/tests/launcher-homes.test.ts @@ -8,19 +8,24 @@ import { collectDoctorReport, renderDoctorTable } from '../src/doctor.js' import { createCodexProvider } from '../src/providers/codex.js' import { emptyCache } from '../src/session-cache.js' -function sessionMeta(sessionId: string): string { +function sessionMeta(sessionId: string, extra: Record = {}): string { return JSON.stringify({ type: 'session_meta', timestamp: '2026-04-14T10:00:00Z', - payload: { cwd: '/Users/test/proj', originator: 'codex-cli', session_id: sessionId, model: 'gpt-5.3-codex' }, + payload: { cwd: '/Users/test/proj', originator: 'codex-cli', session_id: sessionId, model: 'gpt-5.3-codex', ...extra }, }) } -async function writeCodexSession(codexDir: string, name: string, sessionId?: string): Promise { +/** Production Codex session_meta first lines are ~22–27 KB. */ +function longSessionMeta(sessionId: string): string { + return sessionMeta(sessionId, { base_instructions: 'x'.repeat(30 * 1024) }) +} + +async function writeCodexSession(codexDir: string, name: string, sessionId?: string, meta?: string): Promise { const id = sessionId ?? name.replace(/^rollout-/, '').replace(/\.jsonl$/, '') const dayDir = join(codexDir, 'sessions', '2026', '04', '14') await mkdir(dayDir, { recursive: true }) - await writeFile(join(dayDir, name), `${sessionMeta(id)}\n`) + await writeFile(join(dayDir, name), `${meta ?? sessionMeta(id)}\n`) } function names(sources: { path: string }[]): string[] { @@ -180,6 +185,42 @@ describe('Codex discover overlap-only nest filter', () => { } }) + it('exported no-arg singleton dedups production-size session_meta first lines', async () => { + const home = root + const primary = join(home, '.codex') + const nested = join(home, '.buzz', '.codex') + const billedA = longSessionMeta('sess-A') + const nestA = longSessionMeta('sess-A') + const nestB = longSessionMeta('sess-B') + expect(Buffer.byteLength(billedA, 'utf8')).toBeGreaterThan(8 * 1024) + expect(Buffer.byteLength(nestA, 'utf8')).toBeGreaterThan(8 * 1024) + expect(Buffer.byteLength(nestB, 'utf8')).toBeGreaterThan(8 * 1024) + await writeCodexSession(primary, 'rollout-billed-A-long.jsonl', 'sess-A', billedA) + await writeCodexSession(nested, 'rollout-nest-A-long.jsonl', 'sess-A', nestA) + await writeCodexSession(nested, 'rollout-nest-B-long.jsonl', 'sess-B', nestB) + const prevHome = process.env.HOME + const prevCodex = process.env.CODEX_HOME + process.env.HOME = home + process.env.CODEX_HOME = nested + vi.resetModules() + try { + const { createCodexProvider: noArgFactory, codex } = await import('../src/providers/codex.js') + expect(names(await noArgFactory().discoverSessions()).sort()).toEqual([ + 'rollout-billed-A-long.jsonl', + 'rollout-nest-B-long.jsonl', + ]) + expect(names(await codex.discoverSessions()).sort()).toEqual([ + 'rollout-billed-A-long.jsonl', + 'rollout-nest-B-long.jsonl', + ]) + } finally { + if (prevHome === undefined) delete process.env.HOME + else process.env.HOME = prevHome + if (prevCodex === undefined) delete process.env.CODEX_HOME + else process.env.CODEX_HOME = prevCodex + } + }) + it('second factory on a realpath-equivalent nest emits no duplicate sources', async () => { const primary = join(root, 'primary') const buzz = join(root, '.buzz')