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
93 changes: 93 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,93 @@
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('leaves a WSL home to launch prep instead of running its blocking ownership check', () => {
const assertManagedHomePath = vi.fn((path: string) => path)
const wslMirror = new CodexConfigMirror(
{ getSettings: () => getDefaultSettings(testState.fakeHomeDir) },
assertManagedHomePath
)

wslMirror.safeSyncIntoManagedHome(
'\\\\wsl.localhost\\Ubuntu\\home\\u\\.local\\share\\orca\\codex-accounts\\acct\\home',
undefined,
'acct'
)

expect(assertManagedHomePath).not.toHaveBeenCalled()
})

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`
)
})
})
12 changes: 10 additions & 2 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 @@ -106,6 +109,11 @@ export class CodexConfigMirror {
expectedAccountId?: string
): void {
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.
// WSL homes are skipped: their ownership check is a blocking wsl.exe call (startup, account switch), and WSL launch prep guards the home it launches.
if (!parseWslUncPath(managedHomePath)) {
ensureCodexDaemonSocketGuard(this.assertManagedHomePath(managedHomePath, expectedAccountId))
}
Comment on lines +112 to +116

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

rg -n 'ensureCodexDaemonSocketGuard|safeSyncIntoManagedHome|syncIntoManagedHome|applyCodexDaemonSocketGuard' src/main/codex-accounts src/main/codex
sed -n '90,145p' src/main/codex-accounts/codex-config-mirror.ts

Repository: stablyai/orca

Length of output: 7862


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- candidate files ---'
git ls-files 'src/main/codex-accounts' 'src/main/codex' | rg '(service|runtime-home|wsl|config-mirror|socket-path|launch|codex)' | head -120
printf '%s\n' '--- outlines ---'
ast-grep outline src/main/codex-accounts/codex-config-mirror.ts
ast-grep outline src/main/codex/codex-config-mirror.ts
printf '%s\n' '--- account mirror and codex guard ---'
cat -n src/main/codex-accounts/codex-config-mirror.ts | sed -n '1,145p'
cat -n src/main/codex/codex-config-mirror.ts | sed -n '1,280p'
printf '%s\n' '--- WSL and launch references ---'
rg -n -C 5 'parseWslUncPath|wsl\.exe|WSL|launch|runtimeHomePath|ensureCodexDaemonSocketGuard|Codex.*start|start.*Codex' src/main/codex-accounts src/main/codex -g '*.ts'

Repository: stablyai/orca

Length of output: 45654


🏁 Script executed:

#!/bin/bash
set -eu
for f in \
  src/main/codex-accounts/runtime-home-service-launch.ts \
  src/main/codex-accounts/runtime-home-service-wsl-core.ts \
  src/main/codex-accounts/runtime-home-service-wsl.ts \
  src/main/codex-accounts/runtime-home-service.ts \
  src/main/codex-accounts/wsl-codex-command.ts \
  src/main/codex-accounts/codex-account-registration.ts \
  src/main/codex-accounts/service.ts \
  src/main/codex/codex-daemon-socket-path-guard.ts \
  src/shared/wsl-paths.ts
do
  if [ -f "$f" ]; then
    printf '\n--- %s ---\n' "$f"
    wc -l "$f"
    rg -n -C 4 'launch|prepare|wsl|WSL|runtimeHome|config\.toml|socket|guard|Codex|command|exec|spawn' "$f" || true
  fi
done
printf '\n--- cross-file call sites ---\n'
rg -n -C 3 'runtime-home-service-launch|prepare.*Wsl|prepare.*WSL|launch.*Wsl|launch.*WSL|ensureCodexDaemonSocketGuard|wsl-codex-command|safeSyncIntoManagedHome' src/main src/shared -g '*.ts'

Repository: stablyai/orca

Length of output: 42374


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- finishWslLaunchPreparation ---'
rg -n -C 12 'finishWslLaunchPreparation' src/main/codex-accounts
printf '%s\n' '--- WSL shell preflight files ---'
git ls-files src/main/codex src/main/runtime/rpc/methods | rg 'managed-wsl|agent-hooks'
for f in \
  src/main/codex/managed-wsl-home-shell-preflight.ts \
  src/main/runtime/rpc/methods/agent-hooks.ts \
  src/main/providers/local-pty-windows-spawn-environment.ts
do
  if [ -f "$f" ]; then
    printf '\n--- %s ---\n' "$f"
    cat -n "$f" | sed -n '1,260p'
  fi
done
printf '%s\n' '--- guard implementation and focused tests ---'
cat -n src/main/codex/codex-daemon-socket-path-guard.ts | sed -n '1,220p'
rg -n -C 8 'long|overlong|socket|daemon_auto_start|missing|absent|config' \
  src/main/codex/codex-daemon-socket-path-guard.test.ts \
  src/main/codex-accounts/runtime-home-wsl-managed-accounts.test.ts \
  src/main/codex-accounts/runtime-home-wsl-session-bridge.test.ts \
  src/main/codex-accounts/codex-config-mirror.test.ts

Repository: stablyai/orca

Length of output: 42800


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- WSL launch sync delegation ---'
cat -n src/main/codex-accounts/runtime-home-service-home-routing.ts | sed -n '230,310p'
printf '%s\n' '--- socket guard implementation ---'
cat -n src/main/codex/codex-daemon-socket-path-guard.ts | sed -n '14,180p'
printf '%s\n' '--- mirror tests around missing source/config ---'
cat -n src/main/codex-accounts/codex-config-mirror.test.ts | sed -n '1,125p'
printf '%s\n' '--- direct launch and missing config references ---'
rg -n -C 8 'config\.toml|missing source|no source|no.*config|daemon_auto_start|socket|guard' \
  src/main/codex-accounts/runtime-home-wsl-managed-accounts.test.ts \
  src/main/codex-accounts/runtime-home-wsl-session-bridge.test.ts \
  src/main/codex-accounts/codex-config-mirror.test.ts \
  src/main/codex/codex-daemon-socket-path-guard.test.ts

Repository: stablyai/orca

Length of output: 41905


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- WSL system-home resolution ---'
rg -n -C 12 'getWslSystemCodexHomePath|joinWslPath|getWslHome' \
  src/main/codex-accounts/runtime-home-service-home-routing.ts \
  src/main/codex-accounts/runtime-home-service-wsl.ts \
  src/main/wsl.ts \
  src/main/wsl/index.ts \
  src/main/wsl/*.ts 2>/dev/null || true
printf '%s\n' '--- launch preparation callers ---'
rg -n -C 8 'prepareForCodexLaunchAsync|prepareForCodexLaunch\(' src/main src/shared -g '*.ts' \
  | head -240
printf '%s\n' '--- selected WSL account test context ---'
cat -n src/main/codex-accounts/runtime-home-wsl-managed-accounts.test.ts | sed -n '150,235p'

Repository: stablyai/orca

Length of output: 41953


🏁 Script executed:

#!/bin/bash
set -eu
printf '%s\n' '--- getWslHome implementation ---'
cat -n src/main/wsl.ts | sed -n '250,330p'
printf '%s\n' '--- production launch callers ---'
rg -n -C 6 'prepareForCodexLaunchAsync|prepareForCodexLaunch' \
  src/main --glob '*.ts' --glob '!**/*.test.ts' | head -180

Repository: stablyai/orca

Length of output: 16968


Guard the selected WSL home when system-home discovery fails.

When getWslHome() returns null, WSL launch preparation returns before calling syncSystemConfigIntoManagedCodexHome. The no-canonical-config branch also skips the guard. An overlong managed WSL home with no runtime config can then start Codex without daemon_auto_start = false, which can cause the SUN_LEN socket-path startup failure.

Apply the guard in WSL launch preparation when the selected managed home has no resolvable system home.

Suggested fix
-import { syncSystemConfigIntoManagedCodexHome } from '../codex/codex-config-mirror'
+import {
+  ensureCodexDaemonSocketGuard,
+  syncSystemConfigIntoManagedCodexHome
+} from '../codex/codex-config-mirror'
...
-    if (!systemHomePath || systemHomePath === runtimeHomePath) {
+    if (!systemHomePath) {
+      ensureCodexDaemonSocketGuard(runtimeHomePath)
+      return
+    }
+    if (systemHomePath === runtimeHomePath) {
       return
     }

return
}
const trustedManagedHomePath = this.assertManagedHomePath(managedHomePath, expectedAccountId)
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