diff --git a/internal/agent/route.go b/internal/agent/route.go index 0f5e831cd..a110f35f6 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -1,6 +1,7 @@ package agent import ( + "cmp" "slices" "strings" @@ -47,6 +48,26 @@ func viaMagpie(agent, prefix string) []Option { own[e.Provider.ID] = a.User == provider.AgentUser(agent) } } + // firstUser: the account a request for a subscription's model goes to + // first, which the note names. A Codex or Claude Code subscription is + // the account the agent is signed in to (Account.User), but its + // requests go to the first in magpie's order: another one when that + // one is paused, or when the agent is kept signed in to an account + // that isn't first (#1957, parkavenue9639: every codex/gpt-* read + // "A · via magpie" while each went to B). Asked once a provider. + firsts := map[string]string{} + firstUser := func(p provider.Provider) string { + a := p.Account + if a.Agent != p.ID || a.StandIn() { + return a.User + } + u, ok := firsts[p.ID] + if !ok { + u = cmp.Or(provider.FirstLogin(a.Agent), a.User) + firsts[p.ID] = u + } + return u + } fast := provider.FastPicks(agent) for _, e := range shown { if e.Group != "" { @@ -56,7 +77,7 @@ func viaMagpie(agent, prefix string) []Option { } note := e.Provider.Name + " · via magpie" if a := e.Provider.Account; a != nil { - note = a.User + " · via magpie" + note = firstUser(e.Provider) + " · via magpie" } o := Option{Value: prefix + e.ID, Label: e.Name, Note: note, Icon: e.Provider.Icon, Group: e.Provider.Name, Ref: e.ID, Free: e.Free, Rate: e.Rate, RateWas: e.RateWas, Context: e.Context, own: own[e.Provider.ID], diff --git a/internal/agent/via_first_account_test.go b/internal/agent/via_first_account_test.go new file mode 100644 index 000000000..12c423a36 --- /dev/null +++ b/internal/agent/via_first_account_test.go @@ -0,0 +1,72 @@ +package agent + +import ( + "encoding/base64" + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// #1957 (parkavenue9639): Codex signed in to one account, another on in +// magpie. Claude Code's /model described every codex/gpt-* by the account +// Codex is signed in to, while magpie sent each request to the first in +// its own order: the other one when the signed-in one is paused, or when +// Codex is kept signed in to an account that isn't first. The note names +// the account a request goes to first. +func TestViaMagpieNamesTheAccountFirstInOrder(t *testing.T) { + claims := func(m map[string]any) string { + b, _ := json.Marshal(m) + return "h." + base64.RawURLEncoding.EncodeToString(b) + ".s" + } + auth := func(email, acct string) map[string]any { + return map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{ + "id_token": claims(map[string]any{"email": email}), + "access_token": claims(map[string]any{"exp": time.Now().Add(time.Hour).Unix()}), + "refresh_token": "r-" + acct, "account_id": acct}} + } + me, _ := json.Marshal(auth("me@example.com", "acct-1")) + home, _ := codexHome(t, string(me), "model = \"gpt-5.5\"\n") + os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"), []byte(`{"models":[{"slug":"gpt-5.5","display_name":"GPT-5.5","priority":1}]}`), 0o644) + b, _ := json.Marshal([]map[string]any{{"agent": "codex", "user": "spare@example.com", "on": true, + "seen": time.Now(), "auth": auth("spare@example.com", "acct-2")}}) + os.MkdirAll(filepath.Dir(provider.Path()), 0o755) + os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), b, 0o600) + provider.ForgetAccounts() + t.Cleanup(provider.ForgetAccounts) + note := func() string { + t.Helper() + for _, o := range viaMagpie("claude", "") { + if o.Value == "codex/gpt-5.5" { + return o.Note + } + } + t.Fatal("no codex/gpt-5.5 among Claude Code's models via magpie") + return "" + } + if got := note(); got != "me@example.com · via magpie" { + t.Fatalf("signed in to the first: %q", got) + } + // kept signed in to spare, me still first in the order + if err := provider.SetKeepLoginAs("codex", "spare@example.com"); err != nil { + t.Fatal(err) + } + provider.ForgetAccounts() + if got := note(); got != "me@example.com · via magpie" { + t.Fatalf("kept signed in to spare, me first: %q", got) + } + // let go, Codex is on me again; me paused, spare takes its requests + if err := provider.SetKeepLogin("codex", false); err != nil { + t.Fatal(err) + } + if err := provider.SetLoginOn("codex", "me@example.com", false); err != nil { + t.Fatal(err) + } + provider.ForgetAccounts() + if got := note(); got != "spare@example.com · via magpie" { + t.Fatalf("signed in to me, paused: %q", got) + } +} diff --git a/internal/gateway/codex_accounts_test.go b/internal/gateway/codex_accounts_test.go index 1c66c85b6..5b5d9c314 100644 --- a/internal/gateway/codex_accounts_test.go +++ b/internal/gateway/codex_accounts_test.go @@ -159,6 +159,10 @@ func TestCodexPausedOwnAccountPassedOver(t *testing.T) { if code != 200 || strings.Join(tried, ",") != "acct-2" { t.Fatalf("%d %s tried %v", code, body, tried) } + // the account a model's note names as first (#1957) is the one tried + if got := provider.FirstLogin("codex"); got != "spare@example.com" { + t.Fatalf("first %q", got) + } // the other off: the paused one is all there is, and is used if err := provider.SetLoginOn("codex", "spare@example.com", false); err != nil { t.Fatal(err) diff --git a/internal/gateway/keep_login_as_test.go b/internal/gateway/keep_login_as_test.go index db98074c6..ff70e4f68 100644 --- a/internal/gateway/keep_login_as_test.go +++ b/internal/gateway/keep_login_as_test.go @@ -47,6 +47,10 @@ func TestKeptLoginStandsAtItsPlace(t *testing.T) { if got := provider.InUseLogin("codex"); got != "me@example.com" { t.Fatalf("in use %q", got) } + // the account a model's note names as first (#1957) is the one tried + if got := provider.FirstLogin("codex"); got != "me@example.com" { + t.Fatalf("first %q", got) + } // let go, Codex is signed in to the first again, and it goes first if err := provider.SetKeepLogin("codex", false); err != nil { t.Fatal(err) diff --git a/internal/provider/logins.go b/internal/provider/logins.go index 649402cf7..089fed27e 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -898,6 +898,24 @@ func Logins(agent string) []Login { // Kept signed in to one of the user's choosing, it is the first in the // order that is in use. func InUseLogin(agent string) string { + return inUseLogin(agent, loginRoom(agent)) +} + +// FirstLogin is the account of an agent's first in the gateway's order, +// as InUseLogin, by the order alone: the one the agent is signed in to +// unless it is paused, else the first other one on; kept signed in to one +// of the user's choosing, the first on in the order (#1957). An account +// spent is still named, so a list that names it isn't rewritten each time +// an allowance is read. "" when the agent has none, or is none whose +// own sign-in is paused for its others (Claude Code's and Codex's). +func FirstLogin(agent string) string { + if !slices.Contains(loginAgents, agent) { + return "" + } + return inUseLogin(agent, nil) +} + +func inUseLogin(agent string, room func(user string) (known, spent bool)) string { ls := Logins(agent) if keptAs(agent) != "" { for _, l := range ls { @@ -906,7 +924,7 @@ func InUseLogin(agent string) string { } } } - return inUseOf(ls, loginRoom(agent)) + return inUseOf(ls, room) } func inUseOf(ls []Login, room func(user string) (known, spent bool)) string {