agent: Claude Code's model notes name the Codex account magpie goes to first, not the one Codex is signed in to (#1957, parkavenue9639)

With the signed-in ChatGPT account paused for the others ("this
account off, the others on"), the gateway sends codex/<model> to the
first account still on, yet Claude Code's /model list still said
"<signed-in user> · via magpie". The note now names the account that
comes first in the gateway's order: provider.FirstLogin gives
inUseLogin without the allowance-room check (the note is written once,
not per request), so a kept "keep signed in to X" still names the
account at its place, and a paused own account is passed over.

Not changed: magpie still writes openai_base_url for Codex failover
when several accounts are on. That is the failover itself; the
"Only switch Codex's sign-in" tick (#1907, v0.1.1224) already keeps
magpie out of Codex's config for those who don't want it.

Run: TestViaMagpieNamesTheAccountFirstInOrder (new) fails with the
note reverted to a.User and passes with the fix;
TestCodexPausedOwnAccountPassedOver and TestKeptLoginStandsAtItsPlace
also check FirstLogin against the account the gateway served. Full
internal/agent tests, gateway/provider login tests, gofmt -l, go vet
for darwin/linux/windows (-tags nogui).
Not tried: a real Claude Code /model list with two real ChatGPT seats.
This commit is contained in:
yetone
2026-10-13 17:02:05 +08:00
parent 0c2dad1e12
commit 5b9530b567
5 changed files with 121 additions and 2 deletions
+22 -1
View File
@@ -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],
+72
View File
@@ -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)
}
}
+4
View File
@@ -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)
+4
View File
@@ -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)
+19 -1
View File
@@ -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 {