Isolate Codex hooks in Orca runtime home (#2350)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-05-25 15:07:40 -07:00
committed by GitHub
co-authored by Orca
parent 3a40c62169
commit a2c71461b4
59 changed files with 3923 additions and 722 deletions
@@ -0,0 +1,98 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import type * as NodeOs from 'node:os'
import { join } from 'path'
const { getPathMock, homedirMock } = vi.hoisted(() => ({
getPathMock: vi.fn<(name: string) => string>(),
homedirMock: vi.fn<() => string>()
}))
vi.mock('electron', () => ({
app: {
getPath: getPathMock
}
}))
vi.mock('node:os', async () => {
const actual = await vi.importActual<typeof NodeOs>('node:os')
return {
...actual,
homedir: homedirMock
}
})
import {
getCodexSessionDirectories,
getCodexSessionsDirectory,
listCodexSessionFiles
} from './scanner'
const originalCodexHome = process.env.CODEX_HOME
let fakeHomeDir: string
let userDataDir: string
let previousUserDataPath: string | undefined
beforeEach(() => {
delete process.env.CODEX_HOME
fakeHomeDir = mkdtempSync(join(tmpdir(), 'orca-codex-usage-home-'))
userDataDir = mkdtempSync(join(tmpdir(), 'orca-codex-usage-user-data-'))
previousUserDataPath = process.env.ORCA_USER_DATA_PATH
process.env.ORCA_USER_DATA_PATH = userDataDir
homedirMock.mockReturnValue(fakeHomeDir)
getPathMock.mockImplementation((name: string) => {
if (name === 'userData') {
return userDataDir
}
throw new Error(`unexpected app.getPath(${name})`)
})
})
afterEach(() => {
rmSync(fakeHomeDir, { recursive: true, force: true })
rmSync(userDataDir, { recursive: true, force: true })
if (originalCodexHome === undefined) {
delete process.env.CODEX_HOME
} else {
process.env.CODEX_HOME = originalCodexHome
}
if (previousUserDataPath === undefined) {
delete process.env.ORCA_USER_DATA_PATH
} else {
process.env.ORCA_USER_DATA_PATH = previousUserDataPath
}
vi.clearAllMocks()
})
describe('getCodexSessionsDirectory', () => {
it('defaults to Orca-managed Codex runtime sessions', () => {
expect(getCodexSessionsDirectory()).toBe(
join(userDataDir, 'codex-runtime-home', 'home', 'sessions')
)
})
it('ignores an ambient CODEX_HOME override', () => {
process.env.CODEX_HOME = '/tmp/explicit-codex-home'
expect(getCodexSessionsDirectory()).toBe(
join(userDataDir, 'codex-runtime-home', 'home', 'sessions')
)
})
})
describe('listCodexSessionFiles', () => {
it('scans both Orca-managed and system Codex session homes', async () => {
const runtimeSessionsDir = join(userDataDir, 'codex-runtime-home', 'home', 'sessions')
const systemSessionsDir = join(fakeHomeDir, '.codex', 'sessions')
mkdirSync(runtimeSessionsDir, { recursive: true })
mkdirSync(systemSessionsDir, { recursive: true })
const runtimeSessionPath = join(runtimeSessionsDir, 'runtime.jsonl')
const systemSessionPath = join(systemSessionsDir, 'system.jsonl')
writeFileSync(runtimeSessionPath, '{}\n', 'utf-8')
writeFileSync(systemSessionPath, '{}\n', 'utf-8')
expect(getCodexSessionDirectories()).toEqual([runtimeSessionsDir, systemSessionsDir])
expect(await listCodexSessionFiles()).toEqual([runtimeSessionPath, systemSessionPath].sort())
})
})
+12 -1
View File
@@ -1,4 +1,15 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
const { getPathMock } = vi.hoisted(() => ({
getPathMock: vi.fn<(name: string) => string>()
}))
vi.mock('electron', () => ({
app: {
getPath: getPathMock
}
}))
import { attributeCodexUsageEvent, parseCodexUsageRecord } from './scanner'
describe('parseCodexUsageRecord', () => {
+21 -11
View File
@@ -1,11 +1,11 @@
/* eslint-disable max-lines -- Why: Codex discovery, incremental parsing, attribution, and aggregation all depend on the same event-normalization rules. Keeping them together makes the duplicate-snapshot logic easier to audit when usage totals look wrong. */
import { homedir } from 'os'
import { basename, join, win32, posix } from 'path'
import { createReadStream } from 'fs'
import { realpath, readdir, stat } from 'fs/promises'
import { createInterface } from 'readline'
import type { Repo } from '../../shared/types'
import { areWorktreePathsEqual } from '../ipc/worktree-logic'
import { getOrcaManagedCodexHomePath, getSystemCodexHomePath } from '../codex/codex-home-paths'
import type {
CodexUsageAttributedEvent,
CodexUsageDailyAggregate,
@@ -51,7 +51,6 @@ type CodexUsageDeltaResolution =
| { kind: 'event'; delta: CodexUsageRawUsage; nextTotals: CodexUsageRawUsage | null }
| { kind: 'baseline'; nextTotals: CodexUsageRawUsage }
const DEFAULT_CODEX_SESSIONS_DIR = join(homedir(), '.codex', 'sessions')
const YIELD_EVERY_FILES = 10
function ensureNumber(value: unknown): number {
@@ -108,19 +107,30 @@ async function walkJsonlFiles(dirPath: string): Promise<string[]> {
}
export function getCodexSessionsDirectory(): string {
const codexHome = process.env.CODEX_HOME?.trim()
if (codexHome) {
return join(codexHome, 'sessions')
}
return DEFAULT_CODEX_SESSIONS_DIR
// Why: Orca-launched Codex processes receive an Orca-owned CODEX_HOME, so
// callers that need the primary runtime path should not consult ambient
// shell CODEX_HOME.
return join(getOrcaManagedCodexHomePath(), 'sessions')
}
export function getCodexSessionDirectories(): string[] {
// Why: upgraded users still have ordinary Codex history under ~/.codex, while
// new Orca-launched sessions are written under Orca's managed runtime home.
return [getCodexSessionsDirectory(), join(getSystemCodexHomePath(), 'sessions')].filter(
(dirPath, index, allDirPaths) => allDirPaths.indexOf(dirPath) === index
)
}
export async function listCodexSessionFiles(): Promise<string[]> {
try {
return (await walkJsonlFiles(getCodexSessionsDirectory())).sort()
} catch {
return []
const files: string[] = []
for (const dirPath of getCodexSessionDirectories()) {
try {
files.push(...(await walkJsonlFiles(dirPath)))
} catch {
// Missing or unreadable history in one home should not hide the other.
}
}
return [...new Set(files)].sort()
}
export async function getProcessedFileInfo(filePath: string): Promise<CodexUsageProcessedFile> {