Skip to content

fix(codex): preserve managed-account MCP .credentials.json on per-account-home migration (#8440) - #9461

Closed
brennanb2025 wants to merge 10 commits into
brennanb2025/codex-ma-win-login-handle-fixfrom
brennanb2025/codex-ma-mcp-creds-migration
Closed

brennanb2025 wants to merge 10 commits into
brennanb2025/codex-ma-win-login-handle-fixfrom
brennanb2025/codex-ma-mcp-creds-migration

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Problem (#8440)

Managed Codex accounts moved from a shared runtime-home mirror to per-account homes (each home = that account's CODEX_HOME). The legacy→per-account migration migrateLegacySharedAuthToPerAccountHome carried only auth.json from the shared mirror. Codex stores file-mode MCP OAuth tokens in $CODEX_HOME/.credentials.json (separate from auth.json). So an existing managed account that had authed MCP servers got those tokens stranded on upgrade → its MCP servers silently need re-auth.

.credentials.json semantics finding

Ground truth from the codex 0.144.5 binary (rmcp-client/src/oauth.rs), corroborating #8440:

  • Config mcp_oauth_credentials_store: file | keyring | auto | ephemeral.
  • file mode → $CODEX_HOME/.credentials.json, entries keyed by {type,url,headers} under an MCP_OAUTH map. It is MCP-server-scoped (keyed by server URL), NOT account-identity-scoped — the file carries no account identity claim of its own.
  • keyring mode → global macOS Keychain service "Codex MCP Credentials" (not CODEX_HOME-scoped, unaffected). macOS default (auto→keyring) is why [Bug] Critical Bug: OAuth Login Not Persisting for MCPs in Orca Terminals #8440 was cannot-repro on standard macOS; file mode bites Linux/headless/SSH.

Implication for migration: because the file has no identity to match on, we cannot independently identity-match it. The only proof that a shared mirror's .credentials.json belongs to a given account is the co-located, identity-proven auth.json. So we treat it strictly as a companion of the uniquely-identity-matched auth.json.

Chosen handling + why

Carry .credentials.json from the shared mirror into the per-account home at the same proven-ownership gate that governs auth.json:

  • Only after matches.length === 1 && matches[0].account.id === activeHostAccountId (the shared mirror is uniquely proven to be the active account's) — so it lands in exactly one account's home, no cross-account leak. Ambiguous/duplicate identity → nothing carried.
  • Absent source → no-op.
  • Never clobber a .credentials.json already present in the per-account home (a newer store the account authed in its own home going forward).
  • Atomic + 0600, mirroring the auth.json write, so a crash can't leave a partial MCP store or widen perms on sensitive tokens.
  • Idempotent: whole migration is marker-gated (runs once); the absent-dest guard makes the carry itself idempotent even across the function's pre-marker early-return retries.
  • No symlink/live-share machinery — plain per-account-file copy.

Going forward: new MCP auth for a managed account already lands in its per-account home, since that home is CODEX_HOME. Covered by the "already-current" test (store carried without needing an auth rewrite).

Files changed

  • src/main/codex-accounts/legacy-shared-auth-migration.ts — add homePath to the trusted-account record; migrateSharedMcpCredentials() helper; invoke it at the proven-ownership gate before auth freshness branching.
  • src/main/codex-accounts/legacy-shared-auth-migration.test.ts — 5 new sandboxed tests: carry into proven home (0600); carry when auth already-current; absent-source no-op; newer per-account file not clobbered; no leak to non-matching (ambiguous) account.

Test evidence (sandboxed temp homes only — never touches real ~/.codex)

  • npx vitest run --config config/vitest.config.ts src/main/codex-accounts/legacy-shared-auth-migration.test.ts → 11 passed (6 existing + 5 new).
  • Full src/main/codex-accounts/ suite → 172 passed (8 files).
  • pnpm run check:max-lines-ratchet → OK.
  • Node typecheck of config/tsconfig.node.json → clean (exit 0). Note: run with TS 5.9.2 via npx (repo pins typescript@^7, native tsc unavailable in this worktree); change is simple typed additions.

Landing note

This also needs porting to PR-E #9179 (the per-account CODEX_HOME change) for landing — same migration seam and stranding risk.

brennanb2025 and others added 10 commits July 19, 2026 12:19
…ount-home migration (#8440)

Codex file-mode MCP OAuth tokens live in $CODEX_HOME/.credentials.json,
keyed by MCP server URL with no account identity of their own. The legacy
shared-mirror -> per-account-home migration only carried auth.json, so an
existing managed account with authed MCP servers had its tokens stranded on
upgrade and silently needed re-auth.

Carry the shared mirror's .credentials.json into the same identity-proven
per-account home alongside auth.json: only into the single uniquely-matched
active account (no cross-account leak), only when the destination has none yet
(never clobber a newer file the account authed in its own home), atomic 0600,
absent-source no-op. New MCP auth already lands in the per-account home since
that home is CODEX_HOME.
…Windows

The harness built its app with execFileSync('npx', ['electron-vite', ...]),
but npx resolves to a .cmd shim on Windows that execFileSync cannot launch
(ENOENT), so the harness could not build its own app there and required
--skip-build with a prebuilt out/main/index.js.

Extract resolveElectronViteBuildCommand(repoRoot): it runs the repository-local
electron-vite JS entry (node_modules/electron-vite/bin/electron-vite.js) with
the current Node binary (process.execPath), which resolves identically on
macOS, Linux, and Windows with no shell. It throws a clear error if the local
entry is missing (install deps or pass --skip-build). --skip-build behavior is
unchanged.

Add regression coverage asserting the build command uses process.execPath and
the repo-local JS entry (not npx), and that a missing entry fails clearly.
… marker

The auth carry and the MCP .credentials.json carry (#8440) shared one
existence-only v1 marker, so any build that stamped the auth-only marker
first would strand the MCP store forever. The MCP carry now concludes via
its own per-account-mcp-creds-migration-v1.json marker and runs even when
the auth marker is already present; ordering is code-enforced instead of
landing-discipline-enforced.

Also isolate per-account read failures: one stale or deleted account home
no longer aborts the whole migration. The broken account stays in the
unique-identity ambiguity gate via its stored fields but is never read or
written, so the active account still migrates.
… bytes

A raw JSON.parse SyntaxError from loadOAuthCredentials could carry auth
file fragments into logs and the add/reauth error surface. Throw a
sanitized error instead; filesystem errors still propagate unchanged.
…oot guard

The main-process guard now refuses to start with ORCA_E2E_USER_DATA_DIR
set but the real user home, and this was the one caller not updated —
the temporary pairing runtime crashed before emitting its pairing URL.
Resolve symlinks before the disposable-root containment checks so a
symlinked temp parent cannot smuggle the throwaway home inside the
primary home, and give the final cleanup rm Windows retry/force so a
briefly lingering codex handle cannot strand the credential-bearing
root.
The Windows gate-D run proved strict zero-event whole-profile containment
is structurally unreachable with the real-home flag ON: system-default
spawn sites deliberately delete CODEX_HOME so native codex resolves the
real ~/.codex, and on Windows the binary ignores the USERPROFILE sandbox.
Its own volatile runtime churn (root sqlite/WAL/SHM, tmp/, log/) is the
shipped Phase-1 design, not a candidate defect.

--lane-aware-containment records those designed events without aborting
while every other real-home write — auth.json, config.toml,
.credentials.json, hooks.json, sessions/, anything unknown — remains a
hard violation and still aborts the run. Default behavior is unchanged
(strict); the absolute zero-event claim stays carried by macOS runs,
where HOME does sandbox native codex.
… off

--system-default-real-home off seeds and env-pins the flag OFF so every
codex spawn gets an explicit managed CODEX_HOME and native codex never
resolves the OS profile. This is the only Windows configuration where the
strict zero-event whole-profile tripwire is reachable, and it matches the
stable-rollout default; flag-ON runs keep lane-aware classification.
…onale

The rollout ships all codex-home changes at once (no phased rollout), so
flag OFF is the emergency kill-switch lane, not the stable default.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Closing as superseded: #9501 (real-home routing + self-contained multi-account homes) explicitly lists this draft as superseded in its description, and that consolidation merged and shipped in v1.4.149 stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants