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.
134 lines
5.2 KiB
Go
134 lines
5.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Codex keeps one ChatGPT sign-in a folder (codex-rs login/src/auth/
|
|
// storage.rs: $CODEX_HOME/auth.json), so a second account is Codex run
|
|
// with CODEX_HOME=~/.codex-work. phantom3792 on X: magpie read only the
|
|
// default folder's account, and an account added from another folder read
|
|
// no quota. This is that layout: ~/.codex signed in to me@example.com (as
|
|
// signIn writes it), ~/.codex-work to work@example.com with an access
|
|
// token about to run out, and logins.json holding the copy an earlier add
|
|
// made, whose refresh token the vendor has since refused.
|
|
func TestCodexOtherHomeAccountReadsQuota(t *testing.T) {
|
|
home := signIn(t)
|
|
t.Setenv("CODEX_HOME", "")
|
|
resetLoginUsage(t)
|
|
work := filepath.Join(home, ".codex-work")
|
|
soon := float64(time.Now().Add(time.Minute).Unix())
|
|
idTok := fakeJWT(map[string]any{"email": "work@example.com",
|
|
"https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "plus", "chatgpt_account_id": "acct-work"}})
|
|
writeFile(t, filepath.Join(work, "auth.json"), map[string]any{
|
|
"auth_mode": "chatgpt", "OPENAI_API_KEY": nil, "last_refresh": "2026-10-20T00:00:00Z",
|
|
"tokens": map[string]any{"id_token": idTok, "access_token": fakeJWT(map[string]any{"exp": soon}),
|
|
"refresh_token": "r-work", "account_id": "acct-work"},
|
|
})
|
|
// the copy an earlier add kept, its refresh token spent since
|
|
dead, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt",
|
|
"tokens": map[string]any{"id_token": idTok, "access_token": fakeJWT(map[string]any{"exp": soon}),
|
|
"refresh_token": "r-dead", "account_id": "acct-work"}})
|
|
if err := writeLogins([]savedLogin{{Agent: "codex", User: "work@example.com", Plan: "plus", Auth: dead}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mainAuth, _ := os.ReadFile(filepath.Join(home, ".codex", "auth.json"))
|
|
|
|
fresh := fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix()), "n": "work-2"})
|
|
var mu sync.Mutex
|
|
var refreshed []string
|
|
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/backend-api/wham/usage" {
|
|
if r.Header.Get("Authorization") != "Bearer "+fresh {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"plan_type":"plus","rate_limit":{"allowed":true,"primary_window":{"used_percent":40,"limit_window_seconds":18000,"reset_after_seconds":600}}}`)
|
|
return
|
|
}
|
|
var body map[string]string
|
|
json.NewDecoder(r.Body).Decode(&body)
|
|
mu.Lock()
|
|
refreshed = append(refreshed, body["refresh_token"])
|
|
mu.Unlock()
|
|
if body["refresh_token"] != "r-work" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
io.WriteString(w, `{"error":"refresh_token_reused"}`)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"id_token": idTok, "access_token": fresh, "refresh_token": "r-work-2"})
|
|
}))
|
|
defer fake.Close()
|
|
oldTok, oldBase := codexTokenURL, CodexBase
|
|
t.Cleanup(func() { codexTokenURL, CodexBase = oldTok, oldBase })
|
|
codexTokenURL, CodexBase = fake.URL, fake.URL+"/backend-api/codex"
|
|
|
|
ForgetAccounts()
|
|
var got *Login
|
|
for _, l := range Logins("codex") {
|
|
if l.User == "work@example.com" {
|
|
l := l
|
|
got = &l
|
|
}
|
|
}
|
|
if got == nil || got.Active {
|
|
t.Fatalf("the ~/.codex-work account isn't listed as a saved one: %+v", Logins("codex"))
|
|
}
|
|
q := LoginUsage(context.Background(), "codex")["work@example.com"]
|
|
if q.Error != "" || len(q.Windows) == 0 {
|
|
t.Fatalf("no quota for the ~/.codex-work account: %+v (refreshed with %v)", q, refreshed)
|
|
}
|
|
mu.Lock()
|
|
for _, r := range refreshed {
|
|
if r == "r-dead" {
|
|
t.Errorf("refreshed with the spent copy's token: %v", refreshed)
|
|
}
|
|
}
|
|
mu.Unlock()
|
|
// renewed where Codex keeps it: Codex there goes on with the new one
|
|
b, _ := os.ReadFile(filepath.Join(work, "auth.json"))
|
|
var a codexAuth
|
|
if json.Unmarshal(b, &a) != nil || a.Tokens.RefreshToken != "r-work-2" || a.Tokens.AccessToken != fresh {
|
|
t.Errorf("~/.codex-work/auth.json not renewed in place: %s", b)
|
|
}
|
|
// never written into ~/.codex: one sign-in in two places signs one out
|
|
if err := SwitchLogin("codex", "work@example.com"); err == nil || !strings.Contains(err.Error(), work) {
|
|
t.Errorf("switch to the other folder's account: %v", err)
|
|
}
|
|
if now, _ := os.ReadFile(filepath.Join(home, ".codex", "auth.json")); string(now) != string(mainAuth) {
|
|
t.Errorf("~/.codex/auth.json was rewritten: %s", now)
|
|
}
|
|
if err := ForgetLogin("codex", "work@example.com"); err == nil || !strings.Contains(err.Error(), "codex logout") {
|
|
t.Errorf("remove the other folder's account: %v", err)
|
|
}
|
|
}
|
|
|
|
// The same account signed in in ~/.codex and in another folder is
|
|
// ~/.codex's, listed once.
|
|
func TestCodexOtherHomeSameAccountListedOnce(t *testing.T) {
|
|
home := signIn(t)
|
|
t.Setenv("CODEX_HOME", "")
|
|
b, _ := os.ReadFile(filepath.Join(home, ".codex", "auth.json"))
|
|
if err := os.MkdirAll(filepath.Join(home, ".codex-copy"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(home, ".codex-copy", "auth.json"), b, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ForgetAccounts()
|
|
ls := Logins("codex")
|
|
if len(ls) != 1 || !ls[0].Active || ls[0].home != "" {
|
|
t.Fatalf("logins %+v", ls)
|
|
}
|
|
}
|