Codex CLI Integration
Codex Multi-Auth integrates with the official @openai/codex CLI to share authentication state and account management across both tools.How Sync Works
Storage Files
The Codex CLI stores authentication in two files:Sync Direction
Bidirectional sync between Codex Multi-Auth and Codex CLI: Codex CLI → Multi-Auth (on plugin load):- Read accounts from
~/.codex/accounts.jsonor~/.codex/auth.json - Merge accounts into local storage by account ID, refresh token, or email
- Update active account selection if Codex CLI state is newer
- Write active account tokens to
~/.codex/auth.json - Update
active: trueflag in~/.codex/accounts.json - Set
activeAccountIdandactiveEmailmetadata
Enabling Sync
Environment Variable
Sync is enabled by default. To explicitly control:Custom Storage Paths
Override default paths:Account Reconciliation
Deduplication Logic
When syncing from Codex CLI, accounts are deduplicated using:- Account ID (highest priority)
- Refresh Token
- Email (case-insensitive, normalized)
Active Selection Priority
Active account is determined by timestamp comparison:syncVersion (explicit), 1000ms for file mtime (best-effort).
State Format
accounts.json
auth.json
Sync Version
ThecodexMultiAuthSyncVersion field is a Unix timestamp (milliseconds) written by Codex Multi-Auth to track the last sync operation. This enables deterministic active-account selection when both tools are used concurrently.
Writer Module
setCodexCliActiveSelection
Writes active account to Codex CLI state:true if write succeeded, false if sync is disabled or files missing.
Write Queue
Writes are serialized through a queue to prevent race conditions:Atomic Write Strategy
Writes use temp files + rename for atomic updates:Sync Module
syncAccountStorageFromCodexCli
Reconciles local storage with Codex CLI state:storage: Reconciled storage (may be same ascurrentif no changes)changed:trueif storage was modified
Active Index Normalization
The sync module ensures all active indexes are valid:State Module
loadCodexCliState
Reads Codex CLI state with caching:CodexCliState | null
Cache: 5-second TTL in-memory cache (bypass with forceRefresh: true)
lookupCodexCliTokensByEmail
Retrieve tokens for a specific email:CodexCliTokenCacheEntry | null
Observability
The sync modules track metrics:readAttempts- Calls toloadCodexCliStatereadSuccesses- Successful state loadsreadFailures- Failed reads (malformed JSON, ENOENT)readMisses- No Codex CLI files foundwriteAttempts- Calls tosetCodexCliActiveSelectionwriteSuccesses- Successful writeswriteFailures- Failed writesreconcileAttempts- Calls tosyncAccountStorageFromCodexClireconcileChanges- Reconciliations that modified storagereconcileNoops- Reconciliations with no changesreconcileFailures- Failed reconciliationslegacySyncEnvUses- Uses of deprecatedCODEX_AUTH_SYNC_CODEX_CLI
Concurrency Considerations
File Locking
The sync implementation does not use file locks. Instead:- Atomic writes via temp file + rename
- Retry logic for EBUSY/EPERM errors (Windows antivirus)
- Sync version timestamps for last-write-wins resolution
Live Account Sync
WhenCODEX_LIVE_ACCOUNT_SYNC=1, the plugin watches Codex CLI files:
accounts.json or auth.json.
Migration Scenarios
From Codex CLI to Multi-Auth
- Existing Codex CLI accounts are auto-imported on first Multi-Auth use
- Active selection is preserved based on file timestamps
- No manual migration needed
From Multi-Auth to Codex CLI
- Switch account in Multi-Auth dashboard
- Active selection syncs to
~/.codex/auth.json - Run
codex auth statusto verify
Using Both Tools
- Login via Codex CLI:
codex auth login - Account appears in Multi-Auth dashboard automatically
- Switch account in either tool
- Selection syncs within 5 seconds (or immediately if live sync enabled)
Troubleshooting
Sync Not Working
Check sync status:Conflicting Active Accounts
If active account differs between tools:Permission Errors (Windows)
If writes fail with EBUSY/EPERM:- Close any editors with
~/.codex/*.jsonopen - Temporarily disable antivirus file monitoring for
~/.codex/ - Retry operation
Next Steps
- Automation - Use —json flags for CI/CD integration
- Plugin System - Understand the plugin architecture
- Configuration Reference - All sync-related settings