Runtime architecture for Codex CLI-first multi-account OAuth with optional plugin-host integration.
- Keep account management simple for end users (
codex auth ...). - Preserve resilient request routing across multiple accounts.
- Support plugin-host request flow without patching host core.
- Keep stateless backend request compatibility (
store: false).
Terminal user
|
| codex auth ...
v
scripts/codex.js
|- handles auth subcommands locally (codex-manager)
|- forwards all other codex commands to @openai/codex
v
lib/codex-manager.ts
|- oauth login flow + dashboard + check/forecast/fix/doctor/report
|- delegates settings panels to lib/codex-manager/settings-hub.ts (~2100 lines)
|- reads/writes ~/.codex/multi-auth/*
|- syncs active account to Codex CLI state files
Plugin-host runtime (optional)
|
v
index.ts (plugin entry)
|- account loading + live sync + session affinity + proactive refresh
|- request transformation + retry + rotation + failover
v
OpenAI OAuth + Codex/ChatGPT backend
| Subsystem | Key files | Responsibility |
|---|---|---|
| CLI wrapper | scripts/codex.js, scripts/codex-multi-auth.js |
Command routing and alias normalization |
| Auth flow | lib/auth/auth.ts, lib/auth/server.ts, lib/auth/browser.ts |
PKCE OAuth flow, callback handling, browser/manual auth path |
| Account manager | lib/codex-manager.ts, lib/accounts.ts |
Dashboard actions, account selection, health operations |
| Settings hub | lib/codex-manager/settings-hub.ts |
Extracted interactive settings panels (~2100 lines); Q = cancel without save |
| Storage/runtime paths | lib/storage.ts, lib/storage/paths.ts, lib/runtime-paths.ts |
Account/settings persistence, migration, path resolution |
| Worktree resolution | lib/storage/paths.ts |
resolveProjectStorageIdentityRoot, linked worktree identity via commondir/gitdir, forged pointer rejection, Windows UNC support |
| Unified settings | lib/unified-settings.ts, lib/dashboard-settings.ts, lib/config.ts |
Shared settings persistence with EBUSY retry + runtime config defaults/overrides |
| Forecast + quota | lib/forecast.ts, lib/quota-probe.ts, lib/quota-cache.ts |
Readiness scoring, live quota probe, cached quota view |
| Resilience runtime | lib/live-account-sync.ts, lib/session-affinity.ts, lib/refresh-guardian.ts, lib/refresh-lease.ts |
No-restart sync, sticky sessions, proactive refresh, cross-process refresh dedupe |
| Failure handling | lib/request/failure-policy.ts, lib/request/stream-failover.ts, lib/request/rate-limit-backoff.ts |
Controlled retry, stream failover, cooldown/backoff |
| Capability/entitlement | lib/capability-policy.ts, lib/entitlement-cache.ts, lib/preemptive-quota-scheduler.ts |
Unsupported-model suppression, policy scoring, quota deferral |
| Plugin-host request bridge | index.ts, lib/request/fetch-helpers.ts, lib/request/request-transformer.ts |
Request shaping, headers, response handling, retry/rotation |
| Repo hygiene | scripts/repo-hygiene.js |
Deterministic cleanup (clean --mode aggressive) and validation (check), CI-gated |
High-level flow:
- Load runtime config and account manager.
- Normalize incoming model/provider request shape.
- Enforce Codex backend invariants:
stream: truestore: false- include
reasoning.encrypted_content
- Strip unsupported payload forms for stateless behavior.
- Select candidate account with health + quota + affinity logic.
- Execute request with timeout/retry/failover policy.
- Update cooldown/rate-limit/session-affinity state.
- Persist updated account/cache state.
Canonical root: ~/.codex/multi-auth.
| File | Purpose |
|---|---|
settings.json |
Unified dashboard + plugin config |
openai-codex-accounts.json |
Main account pool |
openai-codex-accounts.json.bak / .wal |
Backup and recovery journal |
openai-codex-flagged-accounts.json |
Flagged account pool |
quota-cache.json |
Cached quota snapshots |
logs/ |
Plugin logs when logging enabled |
cache/ |
Prompt/cache artifacts |
projects/<key>/ |
Per-project account pools (keyed by repo identity root) |
- TUI v2 is default.
- Palette and accent are configurable.
- Account rows support compact + details views.
- Hotkeys support quick-switch/search/help and per-account actions.
- OAuth callback port remains
1455. - Dist folder is generated output only.
- Non-auth
codexcommands are always forwarded to official Codex CLI. - Canonical account-management commands remain
codex auth .... - Settings Q hotkey = cancel without save; theme live-preview restores baseline on cancel.
- Email dedup is case-insensitive via
normalizeEmailKey()(trim + lowercase). - Windows filesystem operations use
removeWithRetryfor EBUSY/EPERM/ENOTEMPTY safety.