diff --git a/internal/provider/session_identity.go b/internal/provider/session_identity.go index e772ea988..b9ba019b5 100644 --- a/internal/provider/session_identity.go +++ b/internal/provider/session_identity.go @@ -6,6 +6,7 @@ import ( "cmp" "encoding/json" "path/filepath" + "slices" "github.com/yetone/magpie/internal/filememo" ) @@ -24,7 +25,10 @@ type SessionIdentity struct { func SessionIdentities(codexDir string) []SessionIdentity { out := currentSessionIdentities(codexDir) for _, l := range readLogins() { - if id, ok := savedSessionIdentity(l); ok { + // The account signed in now is saved too once rememberLogins has + // seen it. It is listed once, so the usage cache keyed on this list + // isn't thrown away when that copy is saved. + if id, ok := savedSessionIdentity(l); ok && !slices.Contains(out, id) { out = append(out, id) } } diff --git a/internal/provider/session_identity_test.go b/internal/provider/session_identity_test.go index fb80d71dd..4f7ab5a28 100644 --- a/internal/provider/session_identity_test.go +++ b/internal/provider/session_identity_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "slices" "strings" "testing" ) @@ -37,6 +38,32 @@ func TestSessionIdentitiesReadOnly(t *testing.T) { } } +// 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{