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 at42ab93551plus 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 onto13f907013, 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.
113 lines
3.6 KiB
Go
113 lines
3.6 KiB
Go
package appdir
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// CodexHome is the folder Codex CLI keeps its files in: config.toml,
|
|
// auth.json, the model catalog magpie writes, sessions, AGENTS.md and
|
|
// skills. It is $CODEX_HOME when that is set, as codex-rs's find_codex_home
|
|
// reads it, else ~/.codex. Every place magpie reads or writes Codex's files
|
|
// goes through here (Wakkana on Discord: with CODEX_HOME set, a model picked
|
|
// in magpie went to ~/.codex/config.toml, which Codex never read).
|
|
// CODEX_HOME is read through LookupEnv, so the value a login shell lends the
|
|
// desktop app (proc.UserPath) counts, and a relative one, which Codex would
|
|
// take from whatever folder it runs in, reads as unset.
|
|
func CodexHome() string {
|
|
home, _ := os.UserHomeDir()
|
|
return CodexHomeIn(home)
|
|
}
|
|
|
|
// CodexHomeIn is CodexHome with home as the user's home folder.
|
|
func CodexHomeIn(home string) string {
|
|
if d := Getenv("CODEX_HOME"); d != "" {
|
|
return d
|
|
}
|
|
return filepath.Join(home, ".codex")
|
|
}
|
|
|
|
// CodexHomes are every Codex folder of the user's on this computer:
|
|
// CodexHome first, then the others. Codex keeps one ChatGPT sign-in a
|
|
// folder (codex-rs login/src/auth/storage.rs: auth.json, or the keychain
|
|
// item named by the folder, under CODEX_HOME) and writes its sessions
|
|
// there too (rollout/src/lib.rs: sessions/, archived_sessions/), and
|
|
// `--profile <name>` only lays $CODEX_HOME/<name>.config.toml over
|
|
// config.toml (config/src/loader/mod.rs), the sign-in shared. So more than
|
|
// one account is Codex run with CODEX_HOME set to a folder of each's,
|
|
// ~/.codex-work beside ~/.codex, and magpie read only the one it runs
|
|
// with (phantom3792 on X: only the default profile's sessions counted,
|
|
// and the others' accounts read no quota).
|
|
//
|
|
// Nothing records those folders, so they are looked for where they are
|
|
// made: in the home folder, and beside CodexHome, a folder whose name
|
|
// starts with "codex" or ".codex", or one folder down in such a folder
|
|
// (~/.codex-profiles/work), that holds auth.json or sessions/.
|
|
func CodexHomes() []string {
|
|
home, _ := os.UserHomeDir()
|
|
return CodexHomesIn(home)
|
|
}
|
|
|
|
// CodexHomesIn is CodexHomes with home as the user's home folder.
|
|
func CodexHomesIn(home string) []string {
|
|
main := CodexHomeIn(home)
|
|
out := []string{main}
|
|
seen := map[string]bool{sameDir(main): true}
|
|
add := func(dir string) {
|
|
if k := sameDir(dir); !seen[k] {
|
|
seen[k] = true
|
|
out = append(out, dir)
|
|
}
|
|
}
|
|
roots := []string{home}
|
|
if p := filepath.Dir(main); sameDir(p) != sameDir(home) {
|
|
roots = append(roots, p)
|
|
}
|
|
for _, root := range roots {
|
|
if root == "" {
|
|
continue
|
|
}
|
|
es, _ := os.ReadDir(root)
|
|
for _, e := range es {
|
|
name := strings.ToLower(strings.TrimPrefix(e.Name(), "."))
|
|
if !strings.HasPrefix(name, "codex") {
|
|
continue
|
|
}
|
|
dir := filepath.Join(root, e.Name())
|
|
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
|
|
continue
|
|
}
|
|
if codexHomeAt(dir) {
|
|
add(dir)
|
|
continue
|
|
}
|
|
subs, _ := os.ReadDir(dir)
|
|
for _, s := range subs {
|
|
if sub := filepath.Join(dir, s.Name()); codexHomeAt(sub) {
|
|
add(sub)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// codexHomeAt says dir is a folder Codex has run in: it signed in there
|
|
// (auth.json) or kept a session there.
|
|
func codexHomeAt(dir string) bool {
|
|
if fi, err := os.Stat(filepath.Join(dir, "auth.json")); err == nil && fi.Mode().IsRegular() {
|
|
return true
|
|
}
|
|
fi, err := os.Stat(filepath.Join(dir, "sessions"))
|
|
return err == nil && fi.IsDir()
|
|
}
|
|
|
|
// sameDir is the key two names of one folder share: where its links lead.
|
|
func sameDir(dir string) string {
|
|
if r, err := filepath.EvalSymlinks(dir); err == nil {
|
|
dir = r
|
|
}
|
|
return filepath.Clean(dir)
|
|
}
|