Skip to content
1 change: 1 addition & 0 deletions config/tsconfig.cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"../src/main/codex/codex-config-settings-preservation.ts",
"../src/main/codex/codex-config-settings-removal.ts",
"../src/main/codex/codex-config-settings-upsert.ts",
"../src/main/codex/codex-daemon-socket-path-guard.ts",
"../src/main/codex/codex-home-paths.ts",
"../src/main/codex/codex-hook-definition.ts",
"../src/main/codex/codex-managed-home-resource-copy-marker.ts",
Expand Down
77 changes: 77 additions & 0 deletions src/main/codex-accounts/codex-config-mirror.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import type * as NodeOs from 'node:os'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { getDefaultSettings } from '../../shared/constants'

const testState = vi.hoisted(() => ({ fakeHomeDir: '' }))

vi.mock('electron', () => ({
app: {
getPath: () => {
throw new Error('tests pass explicit homes')
}
}
}))

vi.mock('node:os', async () => {
const actual = await vi.importActual<typeof NodeOs>('node:os')
return { ...actual, homedir: () => testState.fakeHomeDir }
})

import { CodexConfigMirror } from './codex-config-mirror'

const OVERRIDE_LINE = 'daemon_auto_start = false # orca: CODEX_HOME too long for the daemon socket'

describe('CodexConfigMirror without ~/.codex/config.toml', () => {
let root: string
let managedHomePath: string
let mirror: CodexConfigMirror

beforeEach(() => {
root = mkdtempSync(join(tmpdir(), 'orca-codex-accounts-mirror-'))
testState.fakeHomeDir = join(root, 'user-home')
mkdirSync(testState.fakeHomeDir)
// Long enough to exceed sun_path on every platform, like a real account home.
managedHomePath = join(root, 'a'.repeat(80), 'home')
mkdirSync(managedHomePath, { recursive: true })
const settings = {
...getDefaultSettings(testState.fakeHomeDir),
codexManagedAccounts: [
{
id: 'acct',
email: 'user@example.com',
managedHomePath,
createdAt: 1,
updatedAt: 1,
lastAuthenticatedAt: 1
}
]
}
mirror = new CodexConfigMirror({ getSettings: () => settings }, (path) => path)
})

afterEach(() => {
rmSync(root, { recursive: true, force: true })
})

it('still guards every managed home against the daemon socket limit', () => {
mirror.safeSyncToManagedHomes()

expect(readFileSync(join(managedHomePath, 'config.toml'), 'utf-8')).toBe(
`[features]\n${OVERRIDE_LINE}\n`
)
expect(existsSync(join(testState.fakeHomeDir, '.codex'))).toBe(false)
})

it('adds the guard without touching settings already in the managed home', () => {
writeFileSync(join(managedHomePath, 'config.toml'), 'model = "gpt-5"\n')

mirror.safeSyncIntoManagedHome(managedHomePath, undefined, 'acct')

expect(readFileSync(join(managedHomePath, 'config.toml'), 'utf-8')).toBe(
`model = "gpt-5"\n\n[features]\n${OVERRIDE_LINE}\n`
)
})
})
11 changes: 8 additions & 3 deletions src/main/codex-accounts/codex-config-mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { existsSync, readFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { join } from 'node:path'
import { parseWslUncPath } from '../../shared/wsl-paths'
import { syncSystemConfigIntoManagedCodexHome } from '../codex/codex-config-mirror'
import {
ensureCodexDaemonSocketGuard,
syncSystemConfigIntoManagedCodexHome
} from '../codex/codex-config-mirror'
import { readCodexTopLevelModelProvider } from '../codex/codex-model-provider-config'
import type { Store } from '../persistence'
import { toWindowsWslPath } from '../wsl'
Expand All @@ -17,7 +20,7 @@ export type CanonicalCodexConfig = {

export class CodexConfigMirror {
constructor(
private readonly store: Store,
private readonly store: Pick<Store, 'getSettings'>,
private readonly assertManagedHomePath: (
candidatePath: string,
expectedAccountId?: string
Expand Down Expand Up @@ -105,10 +108,12 @@ export class CodexConfigMirror {
canonicalConfig = this.readForManagedHome(managedHomePath),
expectedAccountId?: string
): void {
const trustedManagedHomePath = this.assertManagedHomePath(managedHomePath, expectedAccountId)
if (canonicalConfig === null) {
// Why: with no ~/.codex/config.toml there is nothing to mirror, but Codex still cannot start in a long home without the daemon guard.
ensureCodexDaemonSocketGuard(trustedManagedHomePath)
return
}
const trustedManagedHomePath = this.assertManagedHomePath(managedHomePath, expectedAccountId)
// Why: every account home is Codex's own CODEX_HOME. Preserve trust Codex
// granted there while refreshing ordinary settings from the lane's source.
syncSystemConfigIntoManagedCodexHome({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const generationRace = vi.hoisted(() => ({
beforeGuardedReplace: null as (() => void) | null
}))

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('./fs-utils', async (importOriginal) => {
const actual = await importOriginal<typeof FsUtils>()
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
testState
} from './runtime-home-service-test-harness'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function expectBaselineKeptWithLaunchDatePending(markerPath: string): void {
).toBe(true)
}

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {
testState
} from './runtime-home-service-test-harness'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
testState
} from './runtime-home-service-test-harness'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
6 changes: 6 additions & 0 deletions src/main/codex-accounts/service-account-add-login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {
} from './service-test-harness'
import { createCanonicalHookTrustFixture } from './service-hook-trust-test-fixtures'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ describe('Codex reset-credit managed-home ownership', () => {
fixture.store.getCodexResetCreditAttemptLedger.mockClear()
fixture.store.replaceCodexResetCreditAttemptLedgerAndFlush.mockClear()
fixture.store.updateSettings.mockClear()
const restarted = fixture.createService()
// Why: construction mirrors config into every managed home; only the retry itself is under test.
fsFaults.resetMkdirCalls()

const restarted = fixture.createService()
await expect(
restarted.consumeRateLimitResetCredit(fixture.idempotencyKey, fixture.expectedScope)
).rejects.toThrow(expectedError)
Expand All @@ -186,13 +187,13 @@ describe('Codex reset-credit managed-home ownership', () => {
fixture.consume.mockClear()
fixture.store.replaceCodexResetCreditAttemptLedgerAndFlush.mockClear()
fixture.store.updateSettings.mockClear()
// Why: construction mirrors config into every managed home; only the replay itself is under test.
const restarted = fixture.createService()
fsFaults.hold(join(realpathSync(fixture.managedHomePath), '.orca-managed-home'))
fsFaults.resetMkdirCalls()

await expect(
fixture
.createService()
.consumeRateLimitResetCredit(fixture.idempotencyKey, fixture.expectedScope)
restarted.consumeRateLimitResetCredit(fixture.idempotencyKey, fixture.expectedScope)
).resolves.toMatchObject({ outcome: 'reset', scope: fixture.expectedScope })

expect(fsFaults.heldReads()).toBe(0)
Expand Down
6 changes: 6 additions & 0 deletions src/main/codex-accounts/service-wsl-accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import {
testState
} from './service-test-harness'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
6 changes: 6 additions & 0 deletions src/main/codex-accounts/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import {
} from './service-test-harness'
import { createCanonicalHookTrustFixture } from './service-hook-trust-test-fixtures'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('../codex/codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
Expand Down
6 changes: 6 additions & 0 deletions src/main/codex/codex-config-mirror.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { tmpdir } from 'node:os'
import type * as NodeOs from 'node:os'
import { join } from 'node:path'

// Why: temp homes exceed sun_path on macOS but not on Linux; keep asserted config bytes host-independent.
vi.mock('./codex-daemon-socket-path-guard', async (importOriginal) => ({
...(await importOriginal<object>()),
applyCodexDaemonSocketGuard: (config: string) => config
}))

const { getPathMock, homedirMock } = vi.hoisted(() => ({
getPathMock: vi.fn<(name: string) => string>(),
homedirMock: vi.fn<() => string>()
Expand Down
Loading
Loading