Commit Graph
2 Commits
Author SHA1 Message Date
yetone 73d17f90ce codex: every CODEX_HOME folder's sessions count, its account renewed there
phantom3792 on X: with several ChatGPT accounts kept the way Codex
supports, magpie counted only the default profile's usage, and the
accounts added from the other profiles read no quota.

Codex's mechanism, from openai/codex at 970111f6cb62:
- One sign-in per folder. In codex-rs/login/src/auth/storage.rs the
  sign-in is $CODEX_HOME/auth.json, or the keychain item that
  compute_store_key(codex_home) names.
- Sessions live in the same folder. rollout/src/lib.rs writes sessions/
  and archived_sessions/ under codex_home.
- `--profile <name>` only lays $CODEX_HOME/<name>.config.toml over
  config.toml (config/src/loader/mod.rs), and the sign-in stays shared.
So a second account is Codex run with CODEX_HOME=~/.codex-work, and
magpie read only the folder it runs with.

There were two causes:
- Sessions: codexFiles() walked CodexDir() alone. The sessions list,
  traces and the usage ledger (sessions.Calls) never saw other folders'
  rollouts.
- Quota: an account added by importing another folder's auth.json is
  refreshed from magpie's copy. The import rotates the refresh token,
  which leaves two holders of one token, and whichever refreshes first
  signs the other out. The saved account goes "sign-in has expired" and
  its quota read fails.

The fix:
- appdir.CodexHomes finds the folders: CodexHome first, then folders
  named codex* or .codex* in the home folder or beside CodexHome, or
  one level down in one (~/.codex-profiles/work), that hold auth.json
  or sessions/.
- sessions: codexFiles and Dirs read every folder.
- provider: rememberLogins saves each folder's account with Home set
  to it (codexHomeLogins). The one ~/.codex is signed in to stays that
  folder's.
- renewSaved renews such an account where that folder's Codex keeps
  it (codexHeldIn, as heldByCodex does for ~/.codex). This one funnel
  covers quota, gateway, warm-up, keep-alive and revoked sign-ins.
  codexLoginAuth reads its expiry from the same place.
- switchSavedLogin, NextLogin's spares and forgetLogin leave such an
  account alone. Their errors name the folder and the
  CODEX_HOME=... codex command.
- docs/subsystems/providers-accounts.md gains a row.

Run (worktree at 42ab93551 plus this diff, sandbox HOME, pinned
caches, log kept whole):
- go test -tags nogui -timeout 45m ./internal/appdir/
  ./internal/sessions/ ./internal/provider/ ./internal/usage/
  ./internal/cli/: all ok.
- gofmt -l internal/: clean.
- GOOS=linux and GOOS=windows go vet on appdir, sessions and provider:
  clean. go vet -tags nogui ./internal/gui/: clean.

Rebased onto 13f907013, which changes internal/sessions only. At that
head, sessions and appdir passed in full, and provider -run 'TestCodex|Login'
passed.

Broken by hand, each red:
- codexOtherHomes returning nil: TestCodexOtherHomeAccountReadsQuota
  fails with the reporter's symptom ("sign-in has expired", refreshed
  with the copy's spent token).
- renewSaved without the home path: ~/.codex-work/auth.json is not
  renewed in place.
- switchSavedLogin without its refusal: ~/.codex/auth.json is
  rewritten to the other account.
- CodexDirs returning CodexDir alone: TestCodexOtherHomeSessionsCounted
  lists 1 session, not 2.

Not tried:
- A real second CODEX_HOME with real accounts. The fixtures copy the
  real auth.json shape and the rollout layout, not real tokens.
- A folder outside the home folder that isn't next to CODEX_HOME.
- Codex keeping the other folder's sign-in in the keychain (the reader
  is the same readCodexLogin per folder).
- An entry whose folder was signed out keeps Home and goes back to
  being refreshed from its copy.

Not done (#3530): Codex thread names (session_index.jsonl) and a
config sqlite_home are still read from CODEX_HOME only.
2026-10-25 07:42:13 +08:00
yetone d47c0fea0b agent, provider: with CODEX_HOME set, a model picked for Codex goes to $CODEX_HOME/config.toml, the file Codex reads, and its sign-in is read and switched there (#1513, Wakkana, sunxwei)
Wakkana on Discord (macOS, Codex CLI 0.162.1) keeps Codex in a CODEX_HOME
of their own. Picking a model in magpie wrote ~/.codex/config.toml and
~/.codex/magpie-models.json, which Codex never reads, while magpie read the
pick back from the same file and showed it applied.

Checked each claim in the source: codexIn built its folder from
filepath.Join(at.home, ".codex"); so did StandIn, the compact-window tune
and KeepCodexDaemonCurrent; codexAuthPath (logins.go), codexAccount
(account.go) and the Usage card's auth.json (subscription_usage.go) read a
hardcoded ~/.codex, and since the app-server restart takes its home from
codexAuthPath, it restarted ~/.codex's daemon too. sessions.CodexDir,
catalog.CodexHome, library's codexDir and import_codex's config path
already read CODEX_HOME, each with its own copy of the rule.

Now there is one rule, appdir.CodexHome / CodexHomeIn: $CODEX_HOME through
appdir.LookupEnv (so the value proc.UserPath lends the desktop app from the
login shell counts, and a relative one reads as unset), else ~/.codex.
Every place that reads or writes Codex's files goes through it:
config.toml and the catalog (codexIn, so detection, Dir, models_cache,
history, logs, threads too), StandIn, tune, the daemon check, auth.json
read/switch/sign-in/sign-out, the import's own-catalog check, sessions,
the models cache, library targets (AGENTS.md, skills, MCP, rtk). A WSL
distro's Codex keeps ~/.codex (place.codexHome), since magpie can't read
a distro's variables. Left as they are, on purpose: library's project-level
.codex/config.toml (Codex reads that in the project whatever CODEX_HOME
is), sessions/wsl.go and wsl.go's distro probes, cliupdate's standalone
install path, and gateway/prompt.go's "/.codex/" path heuristic for a
Claude Code memory file. Codex itself doesn't expand "~" in CODEX_HOME
(0.162.0: 'CODEX_HOME points to "~/x", but that path does not exist').

Tests:
- TestCodexHomeEnvTakesThePick (agent): CODEX_HOME set, pick fakeup/m1:
  config.toml and magpie-models.json land in CODEX_HOME, Get reads it back,
  StandIn and tune use it, nothing exists under ~/.codex. Old codexIn:
  FAIL "Codex's files at …/001/.codex/config.toml, want under
  …/codex-home"; old tune.go: FAIL "tune didn't write
  $CODEX_HOME/config.toml"; old StandIn: FAIL "stand-in … is """.
- TestCodexHomeEnvSignIn (provider): auth.json, config import path and
  daemon home are CODEX_HOME's; two accounts listed from it; SwitchLogin
  writes it; ~/.codex never created. Old account.go: FAIL "sign-in in
  $CODEX_HOME not read"; old codexAuthPath: FAIL "auth.json at
  …/.codex/auth.json".
- TestRestartCodexDaemon and gui's TestCodexDaemonRestartRoute asserted the
  old ~/.codex daemon under CODEX_HOME=/somewhere/else; they now want
  /somewhere/else. TestFolderVarsAreListed scans appdir/codex.go too, so
  its CODEX_HOME canary still finds the read.

Real use, sandbox HOME + CODEX_HOME=<sb>/custom-codex, MAGPIE_ADDR on a
free port with a stand-in listener: `magpie codex fakeup/m1` (this build)
wrote custom-codex/config.toml + magpie-models.json, no <sb>/home/.codex;
real codex 0.162.0 with that CODEX_HOME: `codex debug models` listed
fakeup/m1, `codex exec` sent POST /v1/responses model=fakeup/m1 to magpie's
address. The origin/main build in the same sandbox wrote
<sb>/home/.codex/{config.toml,magpie-models.json} and left
custom-codex/config.toml at model = "gpt-5.5" (the report). The GUI app
was not run.

#1513 (sunxwei, native Windows 0.1.1157, CODEX_HOME=F:\codex-home) is the
same bug. Windows has no shell to borrow from: a user-level variable is in
the environment Explorer hands the Start-menu app, which is what
appdir.LookupEnv reads, and a drive-letter path is rooted. On the Windows
box: the agent, provider and appdir test binaries (GOOS=windows) pass
TestCodexHomeEnvTakesThePick and TestCodexHomeEnvSignIn with CODEX_HOME a
C:\ temp folder; this build's magpie.exe codex fakeup/m1 in a sandbox
USERPROFILE with CODEX_HOME=D:\codexhome-sb\custom-codex wrote config.toml
(model, magpie provider, catalog) and magpie-models.json there and created
no %USERPROFILE%\.codex. Codex itself isn't installed there, so it was
not run on Windows.

Run: go test -tags nogui ./... in a temp HOME (all ok after the three test
updates above; re-ran agent, provider, gui, catalog, sessions, library,
appdir, usage, codexcat after rebase; gateway after rebase hit the 10m
default timeout at load ~150 and passed with -timeout 25m in 656s),
gofmt -l clean, go vet +
GOOS=linux + GOOS=windows go vet -tags nogui on the touched packages.
2026-10-10 22:36:28 +08:00