SessionIdentities lists the identity in Codex's auth.json (and Claude's profile), then one per saved login. Once rememberLogins had saved a copy of the account signed in now, that account was listed twice. QueryPage keys the request cache on this list, so the save that added the copy changed the key, and the next Requests page query dropped every cached chunk and read and priced every session log again, though no identity had changed. Users could see that as one more slow Requests page after magpie saved its first copy of an account: one an agent was signed in to outside magpie (codex login, Claude Code's /login), or the one already signed in when magpie first ran. Switching between saved accounts and signing in through magpie never caused it: rememberLogins replaces a saved account in place, and magpie's own sign-in (addLogin) writes logins.json together with auth.json. The resolver gets nothing from the duplicate: an identical entry never changes which email or login a session resolves to. SessionIdentities now leaves out a saved identity equal to one already in the list. One that differs in any field is still listed, such as another member of the same workspace (the same account id, another email): sessionResolver.codexUser names no member when two identities share a workspace and user id but not the email. TestSessionIdentitiesListTheSavedCurrentAccountOnce saves the account Codex is signed in to with rememberLogins(true), checks that it was saved and that the identities are unchanged, then saves a second member of the same workspace and checks that it is listed. A scratch test on main (c5a54bc7) showed when the list changed: switching between two saved Codex accounts changed it once and the save after that didn't; writing a new account into auth.json changed it, and the save after that changed it again (3 to 4 identities). With this change the second change is gone. Found while characterising TestQueryPageSourceAndIdentityInvalidation: run alone on main with only this change applied, it gets past request_page_test.go:224 "unchanged source rebuilt" (the cache is kept) and stops at request_page_test.go:237, an assumption of the test's own, fixed in #1045. Verified on698da36f(go test -tags nogui under a temp HOME, Go caches pinned), as first onc5a54bc7: - -run '^TestSessionIdentities' ./internal/provider: both PASS, also at local 23:59:59.9, 00:00:30 and 12:00:00. - go test ./internal/provider ok, and ok with -shuffle=11. With -shuffle=22 and 33, TestCopilotSignAndModels fails (account_test.go:403: available after fetch: ...), and at 33 TestPlanQuotas too (planquota_test.go:290: cards: map[]). They fail the same with this commit's test skipped and session_identity.go as on main: -shuffle orders every test before -skip leaves one out, so the others keep their order. Main fails TestCopilotSignAndModels at -shuffle=33 too. It is an order dependence among the provider tests, apart from this change. - Broken on purpose, the new test fails each time. A dedupe on Agent and AccountID alone: "session_identity_test.go:63: another member of the workspace was left out: [{Agent:codex AccountID:acct-1 ... User:me@example.com ...}]". rememberLogins writing nothing: "session_identity_test.go:49: the signed-in account wasn't saved: 0 saved logins". Without the change in session_identity.go: --- FAIL: TestSessionIdentitiesListTheSavedCurrentAccountOnce session_identity_test.go:53: saving the signed-in account changed its identities: before [{Agent:codex AccountID:acct-1 ...}], after [{Agent:codex AccountID:acct-1 ...} {Agent:codex AccountID:acct-1 ...}] On this commit: go test -tags nogui ./... ok, go vet ./... ok, gofmt clean, and -race -count=20 -run '^TestSessionIdentities' ./internal/provider ok.
88 lines
3.7 KiB
Go
88 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSessionIdentitiesReadOnly(t *testing.T) {
|
|
home := signIn(t)
|
|
path := filepath.Join(home, ".codex", "auth.json")
|
|
before, _ := os.ReadFile(path)
|
|
oldAuth, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
|
|
"id_token": fakeJWT(map[string]any{"email": "old@example.com", "https://api.openai.com/auth": map[string]any{"chatgpt_account_id": "old-account", "chatgpt_user_id": "old-user"}}),
|
|
"access_token": "secret-access", "refresh_token": "secret-refresh", "account_id": "old-account"}})
|
|
profile := json.RawMessage(`{"accountUuid":"claude-old","organizationUuid":"org-old","emailAddress":"claude-old@example.com"}`)
|
|
if err := writeLogins([]savedLogin{{Agent: "codex", User: "old@example.com", Auth: oldAuth}, {Agent: "claude", User: "claude-old@example.com", Profile: profile}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids := SessionIdentities(filepath.Dir(path))
|
|
byID := map[string]SessionIdentity{}
|
|
for _, id := range ids {
|
|
byID[id.AccountID] = id
|
|
}
|
|
if byID["acct-1"].User != "me@example.com" || byID["old-account"].UserID != "old-user" || byID["claude-old"].OrganizationID != "org-old" {
|
|
t.Fatalf("identities %+v", ids)
|
|
}
|
|
after, _ := os.ReadFile(path)
|
|
if string(before) != string(after) {
|
|
t.Fatal("identity lookup changed the agent's auth file")
|
|
}
|
|
b, _ := json.Marshal(ids)
|
|
if strings.Contains(string(b), "secret-") {
|
|
t.Fatal("credentials leaked into identity metadata")
|
|
}
|
|
}
|
|
|
|
// Saving the account Codex is signed in to leaves the identities as they
|
|
// were, and a saved account that differs from it is still listed.
|
|
func TestSessionIdentitiesListTheSavedCurrentAccountOnce(t *testing.T) {
|
|
home := signIn(t)
|
|
codexDir := filepath.Join(home, ".codex")
|
|
before := SessionIdentities(codexDir)
|
|
rememberLogins(true) // saves the Codex account signed in now
|
|
if ls := readLogins(); len(ls) != 1 {
|
|
t.Fatalf("the signed-in account wasn't saved: %d saved logins", len(ls))
|
|
}
|
|
after := SessionIdentities(codexDir)
|
|
if len(before) != 1 || !slices.Equal(after, before) {
|
|
t.Fatalf("saving the signed-in account changed its identities: before %+v, after %+v", before, after)
|
|
}
|
|
// another member of the same workspace is another identity, still listed
|
|
mate, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
|
|
"id_token": fakeJWT(map[string]any{"email": "mate@example.com", "https://api.openai.com/auth": map[string]any{"chatgpt_account_id": "acct-1"}}),
|
|
"account_id": "acct-1"}})
|
|
if err := writeLogins(append(readLogins(), savedLogin{Agent: "codex", User: "mate@example.com", Auth: mate})); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ids := SessionIdentities(codexDir); len(ids) != 2 || ids[1].User != "mate@example.com" {
|
|
t.Fatalf("another member of the workspace was left out: %+v", ids)
|
|
}
|
|
}
|
|
|
|
func TestOfficialLoginRequiresExplicitAuthenticationMetadata(t *testing.T) {
|
|
for _, mode := range []string{"chatgpt", "", "custom", "apikey"} {
|
|
b, _ := json.Marshal(map[string]any{"auth_mode": mode, "tokens": map[string]string{
|
|
"account_id": "a", "id_token": fakeJWT(map[string]any{"email": "me@example.com", "https://api.openai.com/auth": map[string]string{"chatgpt_user_id": "u"}}),
|
|
}})
|
|
id, ok := codexSessionIdentity(b)
|
|
if mode == "apikey" {
|
|
if ok {
|
|
t.Fatal("API key mode accepted as a login identity")
|
|
}
|
|
continue
|
|
}
|
|
if !ok || id.OfficialLogin != (mode == "chatgpt") {
|
|
t.Fatalf("mode %q: %+v, %v", mode, id, ok)
|
|
}
|
|
}
|
|
id, ok := claudeSessionIdentity([]byte(`{"accountUuid":"a","organizationUuid":"org","emailAddress":"me@example.com"}`))
|
|
if !ok || !id.OfficialLogin {
|
|
t.Fatal("Claude oauthAccount lost its explicit login method")
|
|
}
|
|
}
|