mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 02:39:13 +00:00
fix(codex): drop only overlapping nest rollouts
A second createCodexProvider(nest, {primaryDir, launcherRoots})
filters shared rollout-*.jsonl basenames. The production singleton
honors CODEX_HOME and never hides unique nest sessions because
~/.codex exists.
This commit is contained in:
parent
4296db737b
commit
17bb4bfdda
3 changed files with 135 additions and 46 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { existsSync } from 'fs'
|
||||
import { existsSync, readdirSync, realpathSync } from 'fs'
|
||||
import { homedir } from 'os'
|
||||
import { join, resolve, sep } from 'path'
|
||||
|
||||
|
|
@ -9,23 +9,63 @@ export type LauncherNote = {
|
|||
verdict: string
|
||||
}
|
||||
|
||||
function resolveHome(dir: string): string {
|
||||
try {
|
||||
return realpathSync(dir)
|
||||
} catch {
|
||||
return resolve(dir)
|
||||
}
|
||||
}
|
||||
|
||||
/** True when `dir` is a Codex home nested under a launcher nest, and a distinct
|
||||
* primary Codex home exists. Scanning it would double-count the billed seat. */
|
||||
* primary Codex home exists. Gate for overlap-only filtering on a *second*
|
||||
* provider instance — it does not by itself drop sessions. */
|
||||
export function isNestedLauncherCodexHome(
|
||||
dir: string,
|
||||
opts: { primaryDir: string; launcherRoots: string[] },
|
||||
): boolean {
|
||||
const resolved = resolve(dir)
|
||||
const primary = resolve(opts.primaryDir)
|
||||
const resolved = resolveHome(dir)
|
||||
const primary = resolveHome(opts.primaryDir)
|
||||
if (resolved === primary) return false
|
||||
const underLauncher = opts.launcherRoots.some(root => {
|
||||
const r = resolve(root)
|
||||
const r = resolveHome(root)
|
||||
return resolved === r || resolved.startsWith(r + sep)
|
||||
})
|
||||
if (!underLauncher) return false
|
||||
return existsSync(primary)
|
||||
}
|
||||
|
||||
/** Rollout basenames under a Codex home (sessions/YYYY/MM/DD + archived). */
|
||||
export function listRolloutBasenames(codexDir: string): Set<string> {
|
||||
const names = new Set<string>()
|
||||
const take = (file: string) => {
|
||||
if (file.startsWith('rollout-') && file.endsWith('.jsonl')) names.add(file)
|
||||
}
|
||||
const sessionsDir = join(codexDir, 'sessions')
|
||||
try {
|
||||
for (const year of readdirSync(sessionsDir)) {
|
||||
if (!/^\d{4}$/.test(year)) continue
|
||||
const yearDir = join(sessionsDir, year)
|
||||
for (const month of readdirSync(yearDir)) {
|
||||
if (!/^\d{2}$/.test(month)) continue
|
||||
const monthDir = join(yearDir, month)
|
||||
for (const day of readdirSync(monthDir)) {
|
||||
if (!/^\d{2}$/.test(day)) continue
|
||||
for (const file of readdirSync(join(monthDir, day))) take(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// missing tree
|
||||
}
|
||||
try {
|
||||
for (const file of readdirSync(join(codexDir, 'archived_sessions'))) take(file)
|
||||
} catch {
|
||||
// missing archive
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
export function defaultBilledCodexHome(): string {
|
||||
return join(homedir(), '.codex')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 } from '../launcher-homes.js'
|
||||
import { defaultBilledCodexHome, isNestedLauncherCodexHome, listRolloutBasenames } from '../launcher-homes.js'
|
||||
|
||||
const modelDisplayNames: Record<string, string> = {
|
||||
'codex-auto-review': 'Codex Auto Review',
|
||||
|
|
@ -1336,13 +1336,20 @@ export function createCodexProvider(
|
|||
): Provider {
|
||||
const dir = getCodexDir(codexDir)
|
||||
const primaryDir = opts?.primaryDir ?? defaultBilledCodexHome()
|
||||
const skipNested = isNestedLauncherCodexHome(dir, {
|
||||
primaryDir,
|
||||
launcherRoots: opts?.launcherRoots ?? defaultLauncherRoots(),
|
||||
})
|
||||
// One tree for discovery and probeRoots. A nest CODEX_HOME with a distinct
|
||||
// billed home redirects the default factory; an explicit nest factory is empty.
|
||||
const scannedDir = skipNested ? (codexDir === undefined ? primaryDir : null) : dir
|
||||
// Production singleton (no args) honors CODEX_HOME / override as the one
|
||||
// root. Unique nest sessions must never be dropped because ~/.codex exists.
|
||||
// Only a second factory — createCodexProvider(nestPath, {primaryDir,
|
||||
// launcherRoots}) — may drop overlapping rollout-*.jsonl basenames.
|
||||
const secondPrimary = opts?.primaryDir
|
||||
const secondRoots = opts?.launcherRoots
|
||||
const filterOverlap =
|
||||
codexDir !== undefined &&
|
||||
secondPrimary !== undefined &&
|
||||
secondRoots !== undefined &&
|
||||
isNestedLauncherCodexHome(dir, {
|
||||
primaryDir: secondPrimary,
|
||||
launcherRoots: secondRoots,
|
||||
})
|
||||
|
||||
return {
|
||||
name: 'codex',
|
||||
|
|
@ -1362,16 +1369,17 @@ export function createCodexProvider(
|
|||
// Same `dir` discoverSessionsInDir walks: <codexDir>/sessions (dated
|
||||
// rollout files) and <codexDir>/archived_sessions. Honors CODEX_HOME.
|
||||
async probeRoots(): Promise<ProbeRoot[]> {
|
||||
if (!scannedDir) return []
|
||||
return [
|
||||
{ path: join(scannedDir, 'sessions'), label: 'sessions' },
|
||||
{ path: join(scannedDir, 'archived_sessions'), label: 'archived' },
|
||||
{ path: join(dir, 'sessions'), label: 'sessions' },
|
||||
{ path: join(dir, 'archived_sessions'), label: 'archived' },
|
||||
]
|
||||
},
|
||||
|
||||
async discoverSessions(): Promise<SessionSource[]> {
|
||||
if (!scannedDir) return []
|
||||
return discoverSessionsInDir(scannedDir)
|
||||
const sources = await discoverSessionsInDir(dir)
|
||||
if (!filterOverlap) return sources
|
||||
const primaryNames = listRolloutBasenames(primaryDir)
|
||||
return sources.filter(source => !primaryNames.has(basename(source.path)))
|
||||
},
|
||||
|
||||
createSessionParser(source: SessionSource, seenKeys: Set<string>): SessionParser {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { mkdtemp, mkdir, writeFile, rm } from 'fs/promises'
|
||||
import { mkdtemp, mkdir, writeFile, rm, symlink } from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import { tmpdir } from 'os'
|
||||
|
||||
|
|
@ -22,6 +22,10 @@ async function writeCodexSession(codexDir: string, name = 'rollout-sess-001.json
|
|||
await writeFile(join(dayDir, name), `${sessionMeta()}\n`)
|
||||
}
|
||||
|
||||
function names(sources: { path: string }[]): string[] {
|
||||
return sources.map(s => s.path.split('/').pop()!)
|
||||
}
|
||||
|
||||
describe('isNestedLauncherCodexHome', () => {
|
||||
it('skips a Codex home under .buzz when a distinct primary home exists', async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'launch-'))
|
||||
|
|
@ -42,14 +46,26 @@ describe('isNestedLauncherCodexHome', () => {
|
|||
expect(isNestedLauncherCodexHome(nested, { primaryDir: nested, launcherRoots: [buzz] })).toBe(false)
|
||||
await rm(root, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('treats a realpath-equivalent nest as the same home', async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'launch-'))
|
||||
const primary = join(root, 'real-codex')
|
||||
const buzz = join(root, '.buzz')
|
||||
const nested = join(buzz, '.codex')
|
||||
await mkdir(primary, { recursive: true })
|
||||
await mkdir(buzz, { recursive: true })
|
||||
await symlink(primary, nested)
|
||||
expect(isNestedLauncherCodexHome(nested, { primaryDir: primary, launcherRoots: [buzz] })).toBe(false)
|
||||
await rm(root, { recursive: true, force: true })
|
||||
})
|
||||
})
|
||||
|
||||
describe('Codex discover skips nested Buzz home', () => {
|
||||
describe('Codex discover overlap-only nest filter', () => {
|
||||
let root: string
|
||||
beforeEach(async () => { root = await mkdtemp(join(tmpdir(), 'codex-buzz-')) })
|
||||
afterEach(async () => { await rm(root, { recursive: true, force: true }) })
|
||||
|
||||
it('counts only the primary tree when a Buzz nest also has rollouts', async () => {
|
||||
it('keeps a distinct nest rollout on the second factory', async () => {
|
||||
const primary = join(root, 'primary')
|
||||
const buzz = join(root, '.buzz')
|
||||
const nested = join(buzz, '.codex')
|
||||
|
|
@ -57,11 +73,48 @@ describe('Codex discover skips nested Buzz home', () => {
|
|||
await writeCodexSession(nested, 'rollout-buzz.jsonl')
|
||||
const real = createCodexProvider(primary, { primaryDir: primary, launcherRoots: [buzz] })
|
||||
const nest = createCodexProvider(nested, { primaryDir: primary, launcherRoots: [buzz] })
|
||||
expect((await real.discoverSessions()).map(s => s.path.split('/').pop())).toEqual(['rollout-primary.jsonl'])
|
||||
expect(await nest.discoverSessions()).toEqual([])
|
||||
expect(names(await real.discoverSessions())).toEqual(['rollout-primary.jsonl'])
|
||||
expect(names(await nest.discoverSessions())).toEqual(['rollout-buzz.jsonl'])
|
||||
})
|
||||
|
||||
it('default factory with CODEX_HOME in .buzz walks ~/.codex, not the nest', async () => {
|
||||
it('filters only a nest rollout whose basename overlaps the primary tree', async () => {
|
||||
const primary = join(root, 'primary')
|
||||
const buzz = join(root, '.buzz')
|
||||
const nested = join(buzz, '.codex')
|
||||
await writeCodexSession(primary, 'rollout-shared.jsonl')
|
||||
await writeCodexSession(nested, 'rollout-shared.jsonl')
|
||||
await writeCodexSession(nested, 'rollout-buzz-only.jsonl')
|
||||
const nest = createCodexProvider(nested, { primaryDir: primary, launcherRoots: [buzz] })
|
||||
expect(names(await nest.discoverSessions())).toEqual(['rollout-buzz-only.jsonl'])
|
||||
})
|
||||
|
||||
it('default factory with CODEX_HOME=nest and a stub ~/.codex still discovers the nest', async () => {
|
||||
const home = root
|
||||
const primary = join(home, '.codex')
|
||||
const nested = join(home, '.buzz', '.codex')
|
||||
await mkdir(primary, { recursive: true })
|
||||
await writeCodexSession(nested, 'rollout-buzz.jsonl')
|
||||
const prevHome = process.env.HOME
|
||||
const prevCodex = process.env.CODEX_HOME
|
||||
process.env.HOME = home
|
||||
process.env.CODEX_HOME = nested
|
||||
try {
|
||||
const provider = createCodexProvider()
|
||||
expect(names(await provider.discoverSessions())).toEqual(['rollout-buzz.jsonl'])
|
||||
const roots = await provider.probeRoots!()
|
||||
expect(roots.map(r => r.path)).toEqual([
|
||||
join(nested, 'sessions'),
|
||||
join(nested, 'archived_sessions'),
|
||||
])
|
||||
} 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('default factory with CODEX_HOME=nest does not drop unique nest rollouts when ~/.codex has its own', async () => {
|
||||
const home = root
|
||||
const primary = join(home, '.codex')
|
||||
const nested = join(home, '.buzz', '.codex')
|
||||
|
|
@ -73,12 +126,7 @@ describe('Codex discover skips nested Buzz home', () => {
|
|||
process.env.CODEX_HOME = nested
|
||||
try {
|
||||
const provider = createCodexProvider()
|
||||
expect((await provider.discoverSessions()).map(s => s.path.split('/').pop())).toEqual(['rollout-primary.jsonl'])
|
||||
const roots = await provider.probeRoots!()
|
||||
expect(roots.map(r => r.path)).toEqual([
|
||||
join(primary, 'sessions'),
|
||||
join(primary, 'archived_sessions'),
|
||||
])
|
||||
expect(names(await provider.discoverSessions())).toEqual(['rollout-buzz.jsonl'])
|
||||
} finally {
|
||||
if (prevHome === undefined) delete process.env.HOME
|
||||
else process.env.HOME = prevHome
|
||||
|
|
@ -87,23 +135,13 @@ describe('Codex discover skips nested Buzz home', () => {
|
|||
}
|
||||
})
|
||||
|
||||
it('keeps a sole CODEX_HOME nest when ~/.codex does not exist', async () => {
|
||||
const home = root
|
||||
const nested = join(home, '.buzz', '.codex')
|
||||
it('explicit nest path without second-factory opts is not silently emptied', async () => {
|
||||
const primary = join(root, 'primary')
|
||||
const nested = join(root, '.buzz', '.codex')
|
||||
await writeCodexSession(primary, 'rollout-primary.jsonl')
|
||||
await writeCodexSession(nested, 'rollout-buzz.jsonl')
|
||||
const prevHome = process.env.HOME
|
||||
const prevCodex = process.env.CODEX_HOME
|
||||
process.env.HOME = home
|
||||
process.env.CODEX_HOME = nested
|
||||
try {
|
||||
const provider = createCodexProvider()
|
||||
expect((await provider.discoverSessions()).map(s => s.path.split('/').pop())).toEqual(['rollout-buzz.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
|
||||
}
|
||||
const provider = createCodexProvider(nested)
|
||||
expect(names(await provider.discoverSessions())).toEqual(['rollout-buzz.jsonl'])
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -126,6 +164,9 @@ describe('doctor launchers', () => {
|
|||
expect(table).toContain('Launchers')
|
||||
expect(table).toContain('buzz')
|
||||
expect(table).toContain('billed via Codex')
|
||||
const buzzLine = table.split('\n').find(line => /^\s*buzz\s/.test(line))
|
||||
expect(buzzLine).toBeDefined()
|
||||
expect(buzzLine).not.toMatch(/\d+\s+session/)
|
||||
await rm(home, { recursive: true, force: true })
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue