mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-12-03 03:29:51 +00:00
fix(codex): read a complete session_meta first line
rolloutFileSessionId sliced 8192 bytes then JSON.parse. Production session_meta first lines are 22-27 KB, so both trees returned undefined ids and the overlapping nest source was kept. Read up to the existing 1 MiB first-line cap and parse payload.session_id from the complete line. Pin billed-A / nest-A / nest-B at 30 KB on the exported no-arg singleton.
This commit is contained in:
parent
2c87ee3eaa
commit
ada8ce2ac4
3 changed files with 58 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<string, string> = {
|
||||
'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<CodexEntry | null> {
|
||||
// Codex CLI 0.128+ writes a session_meta line that can exceed 20 KB because
|
||||
|
|
|
|||
|
|
@ -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, unknown> = {}): 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<void> {
|
||||
/** 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<void> {
|
||||
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')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue