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
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
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
49 changes: 32 additions & 17 deletions src/main/codex/codex-config-mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { readCodexSettingsBaseline } from './config-settings-baseline'
import { getCodexConfigSyncStatus, reportCodexConfigSyncOutcome } from './config-sync-stall'
import { preserveRuntimeConflictValues } from './codex-config-settings-preservation'
import { applyCodexDaemonSocketGuard } from './codex-daemon-socket-path-guard'
import {
deduplicateProjectTomlSections,
getProjectTrustLevel,
Expand Down Expand Up @@ -124,26 +125,30 @@ export function syncSystemConfigIntoLegacySharedCodexHome(
}
const rawSystemConfig =
systemConfigObservation.kind === 'present' ? systemConfigObservation.value : ''
// Why: a missing cloud-synced source is not proof the user cleared config.
if (rawSystemConfig.trim() === '') {
return
}

const sourceConfigDir = resolveCodexConfigMirrorSourceDirectory(homes.systemHomePath)
const runtimeConfigObservation = observeAgentStateFile(runtimeConfigPath)
if (runtimeConfigObservation.kind === 'indeterminate') {
throw runtimeConfigObservation.error
}
const runtimeConfigBeforeMirror =
runtimeConfigObservation.kind === 'present' ? runtimeConfigObservation.value : null
const nextRuntimeConfig =
runtimeConfigBeforeMirror !== null
? mergeSystemCodexConfigIntoRuntime(
runtimeConfigBeforeMirror,
prepareSystemConfigForRuntimeMirror(rawSystemConfig, sourceConfigDir)
)
: prepareSystemConfigForFreshRuntimeMirror(rawSystemConfig, sourceConfigDir)
if (runtimeConfigBeforeMirror === nextRuntimeConfig) {
// Why: a missing cloud-synced source is not proof the user cleared config.
let mirroredRuntimeConfig = runtimeConfigBeforeMirror ?? ''
if (rawSystemConfig.trim() !== '') {
const sourceConfigDir = resolveCodexConfigMirrorSourceDirectory(homes.systemHomePath)
mirroredRuntimeConfig =
runtimeConfigBeforeMirror !== null
? mergeSystemCodexConfigIntoRuntime(
runtimeConfigBeforeMirror,
prepareSystemConfigForRuntimeMirror(rawSystemConfig, sourceConfigDir)
)
: prepareSystemConfigForFreshRuntimeMirror(rawSystemConfig, sourceConfigDir)
}
// Why: retained pre-rollout panes still use this home, so a refresh must keep the daemon guard.
const nextRuntimeConfig = applyCodexDaemonSocketGuard(
mirroredRuntimeConfig,
homes.runtimeHomePath
)
if ((runtimeConfigBeforeMirror ?? '') === nextRuntimeConfig) {
return
}
// Why: stage first, then compare immediately before replace so a retained
Expand Down Expand Up @@ -181,6 +186,12 @@ function syncSystemConfigIntoManagedCodexHomeUnsafe(
// it would erase every ordinary setting from an existing managed runtime, and
// a 0-byte file is what a half-written or unhydrated cloud-synced home shows.
if (rawSystemConfig.trim() === '') {
// Why: no mirror write happens here, but the daemon guard must still land.
const runtimeConfigBefore = runtimeConfigExists ? runtimeConfigObservation.value : null
const guarded = applyCodexDaemonSocketGuard(runtimeConfigBefore ?? '', runtimeHomePath)
if (guarded !== (runtimeConfigBefore ?? '')) {
writeFileAtomicallyIfUnchanged(runtimeConfigPath, runtimeConfigBefore, guarded)
}
return runtimeConfigExists
? { status: 'skipped-missing-source' }
: { status: 'mirrored', preservedConflictKeys: new Set() }
Expand All @@ -190,7 +201,10 @@ function syncSystemConfigIntoManagedCodexHomeUnsafe(
if (!runtimeConfigExists) {
writeFileAtomically(
runtimeConfigPath,
prepareSystemConfigForFreshRuntimeMirror(rawSystemConfig, sourceConfigDir)
applyCodexDaemonSocketGuard(
prepareSystemConfigForFreshRuntimeMirror(rawSystemConfig, sourceConfigDir),
runtimeHomePath
)
)
return { status: 'mirrored', preservedConflictKeys: new Set() }
}
Expand All @@ -203,8 +217,9 @@ function syncSystemConfigIntoManagedCodexHomeUnsafe(
mergeSystemCodexConfigIntoRuntime(runtimeConfig, systemConfig),
promotionPlan.runtimeValuesToPreserve
)
if (preserved.content !== runtimeConfig) {
writeFileAtomically(runtimeConfigPath, preserved.content)
const nextRuntimeConfig = applyCodexDaemonSocketGuard(preserved.content, runtimeHomePath)
if (nextRuntimeConfig !== runtimeConfig) {
writeFileAtomically(runtimeConfigPath, nextRuntimeConfig)
}
return { status: 'mirrored', preservedConflictKeys: preserved.keys }
}
Expand Down
Loading
Loading