provider: a Claude account made first stays first when Claude Code renews its sign-in under another's profile (motui9535)
motui9535 on Discord: after making another Claude account first in Providers, a refresh at the top of Usage turned the previous account back into the first. Here is how. Make first switches Claude Code to the account (say Max). A Claude Code started on the previous account (Pro) before the switch keeps writing Pro's profile into ~/.claude.json afterwards. While the credentials are still the ones a saved record holds, claudeSignInOwner names the sign-in by them (#1938, netfishx). The Usage refresh then runs /usage in Claude Code's own config, and that renews the sign-in. The new tokens are held by no record, so the sign-in was named after the stale profile: Pro. rememberLogins then saved Max's renewed tokens under Pro's record and marked Pro active. Max's record kept a dead refresh token, and Pro's record now held Max. The fix: the held record now keeps the profile magpie read at its last look (HeldAs). A sign-in held by no record is the held account's own renewal when all three of these hold: - its profile is that same profile; - the profile names another account; - its plan is not another's. A /login writes its own account's profile, so it is still the new account's. Tests: TestClaudeMadeFirstStaysFirstWhenRenewedUnderAnothersProfile (the reporter's sequence, then a SwitchWhenSpent look) and TestClaudeLoginAnewIsTheNewAccounts (a /login with a new profile, or with another plan, is the new account's). Run on mote: - go test -count=1 -timeout 45m ./internal/provider ./internal/gui ./internal/cli: ok. - go vet for darwin, linux and windows on ./internal/provider: ok. - gofmt -l: clean. Broken by hand: - claudeRenewedHeld never applied: the first test goes red ("first is pro@x.com", pro holds tok-max@x.com-renewed). - No plan guard: the "its plan" subtest goes red. Not tried: - The reporter's real accounts and a real Claude Code renewing its token. The sequence is reproduced with files in a sandbox HOME, and that Claude Code renews during /usage is inferred, not observed. - A /login back to the very account the stale profile names, on the same plan, still reads as the held account's renewal.
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// claudeLook is magpie's next look at Claude Code's sign-in.
|
||||
func claudeLook() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
forgetAccountCaches()
|
||||
rememberLogins(true)
|
||||
}
|
||||
|
||||
func claudeActive(t *testing.T) string {
|
||||
t.Helper()
|
||||
for _, l := range Logins("claude") {
|
||||
if l.Active {
|
||||
return l.User
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// claudeHolding is each saved Claude account's access token.
|
||||
func claudeHolding() map[string]string {
|
||||
out := map[string]string{}
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "claude" {
|
||||
c, _ := parseClaudeCredentials(l.Auth)
|
||||
out[claudeEmail(l.User)] = c.OAuth.AccessToken
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// motui9535 on Discord: Max made first in Providers, Claude Code is
|
||||
// switched to it; a Claude Code started on the Pro before writes the Pro's
|
||||
// profile after; then a refresh of Usage runs /usage, which renews the
|
||||
// sign-in, and Providers showed the Pro first again, its record holding
|
||||
// the Max's renewed tokens. Renewed under the profile magpie read at its
|
||||
// last look, the sign-in is the Max's still.
|
||||
func TestClaudeMadeFirstStaysFirstWhenRenewedUnderAnothersProfile(t *testing.T) {
|
||||
prof, creds := claudeThree(t)
|
||||
if err := SwitchLogin("claude", "max@x.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claudeLook()
|
||||
prof("pro@x.com", "pro@x.com's Organization")
|
||||
claudeLook()
|
||||
if got := claudeActive(t); got != "max@x.com" {
|
||||
t.Fatalf("under the Pro's stale profile, first is %q, want max@x.com", got)
|
||||
}
|
||||
creds("max@x.com-renewed", "max")
|
||||
claudeLook()
|
||||
if got := claudeActive(t); got != "max@x.com" {
|
||||
t.Errorf("after Claude Code renewed the Max's sign-in, first is %q, want max@x.com", got)
|
||||
}
|
||||
want := map[string]string{"team@x.com": "tok-team@x.com", "max@x.com": "tok-max@x.com-renewed", "pro@x.com": "tok-pro@x.com"}
|
||||
for e, tok := range claudeHolding() {
|
||||
if tok != want[e] {
|
||||
t.Errorf("%s holds %s, want %s", e, tok, want[e])
|
||||
}
|
||||
}
|
||||
if _, err := SwitchWhenSpent(t.Context(), "claude"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := claudeActive(t); got != "max@x.com" {
|
||||
t.Errorf("after a look for a spent account, first is %q, want max@x.com", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A /login is not taken for a renewal: it writes its own account's
|
||||
// profile, or its plan is another's.
|
||||
func TestClaudeLoginAnewIsTheNewAccounts(t *testing.T) {
|
||||
t.Run("its profile", func(t *testing.T) {
|
||||
prof, creds := claudeThree(t)
|
||||
if err := SwitchLogin("claude", "max@x.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claudeLook()
|
||||
prof("pro@x.com", "pro@x.com's Organization")
|
||||
creds("pro@x.com-anew", "pro")
|
||||
claudeLook()
|
||||
if got := claudeActive(t); got != "pro@x.com" {
|
||||
t.Errorf("signed in anew to the Pro, first is %q", got)
|
||||
}
|
||||
if h := claudeHolding(); h["pro@x.com"] != "tok-pro@x.com-anew" || h["max@x.com"] != "tok-max@x.com" {
|
||||
t.Errorf("records: %v", h)
|
||||
}
|
||||
})
|
||||
t.Run("its plan", func(t *testing.T) {
|
||||
prof, creds := claudeThree(t)
|
||||
if err := SwitchLogin("claude", "max@x.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
claudeLook()
|
||||
prof("pro@x.com", "pro@x.com's Organization")
|
||||
claudeLook()
|
||||
creds("pro@x.com-anew", "pro")
|
||||
claudeLook()
|
||||
if got := claudeActive(t); got != "pro@x.com" {
|
||||
t.Errorf("signed in anew to the Pro under its own profile, first is %q", got)
|
||||
}
|
||||
if h := claudeHolding(); h["pro@x.com"] != "tok-pro@x.com-anew" || h["max@x.com"] != "tok-max@x.com" {
|
||||
t.Errorf("records: %v", h)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -128,6 +128,12 @@ type savedLogin struct {
|
||||
// magpie last looked: its saved copy is that very sign-in, which
|
||||
// Claude Code's /logout revokes (claudeLoggedOut).
|
||||
Held bool `json:"held,omitempty"`
|
||||
// HeldAs is, on the Held account, the account ~/.claude.json named
|
||||
// when magpie last looked (claudeProfileKey): it can be another's, a
|
||||
// Claude Code started on that one writing its profile after a switch
|
||||
// (claudeSignInOwner). A sign-in renewed under that same profile is
|
||||
// the Held account's still (claudeRenewedHeld).
|
||||
HeldAs string `json:"heldAs,omitempty"`
|
||||
// was is the name a Copilot account read under a new one had in the
|
||||
// file (#1220): its per-account settings are still kept by it, and
|
||||
// move to the new one before the new name is written (renameSettled).
|
||||
@@ -715,6 +721,7 @@ func liveLogin(agent string) (savedLogin, bool) {
|
||||
var acct map[string]any
|
||||
if l.User, acct = claudeSignedInUser(l.Plan, plan, user); acct != nil {
|
||||
l.Profile, _ = json.Marshal(acct)
|
||||
l.HeldAs = claudeProfileKey(l.Profile)
|
||||
}
|
||||
if l.User == "" {
|
||||
return savedLogin{}, false
|
||||
@@ -884,7 +891,7 @@ func rememberLogins(force bool) {
|
||||
l.Seen = now
|
||||
if agent == "claude" {
|
||||
for i := range ls {
|
||||
ls[i].Held = false
|
||||
ls[i].Held, ls[i].HeldAs = false, ""
|
||||
}
|
||||
l.Held = true
|
||||
}
|
||||
@@ -1679,9 +1686,61 @@ func claudeHolders(ls []savedLogin, l savedLogin) (same bool, others []savedLogi
|
||||
// that one. Named by the profile, the account switched to was taken for
|
||||
// the one before: its /usage went through the other's proxy and was shown
|
||||
// on the other's card (#1938).
|
||||
//
|
||||
// Held by none, the sign-in is new: Claude Code signed in anew, or renewed
|
||||
// the one it had, whose tokens are new too. Under a profile that names
|
||||
// another account than the one Claude Code held at the last look, and the
|
||||
// very profile it named then (HeldAs), it is the held account's renewed:
|
||||
// a /login writes its own account's profile, while a Claude Code started
|
||||
// on the other account before a switch kept writing that one's. Named by
|
||||
// the profile, the account the user made first was saved with the other's
|
||||
// name and shown as that one, and the other's record took its tokens
|
||||
// (motui9535: made first in Providers, then a refresh of Usage, whose
|
||||
// /usage renewed the sign-in, turned the first back).
|
||||
func claudeSignInOwner(ls []savedLogin, l savedLogin) savedLogin {
|
||||
if same, others := claudeHolders(ls, l); !same && len(others) == 1 {
|
||||
same, others := claudeHolders(ls, l)
|
||||
switch {
|
||||
case !same && len(others) == 1:
|
||||
l.User, l.Profile = others[0].User, others[0].Profile
|
||||
case !same && len(others) == 0:
|
||||
if h, ok := claudeRenewedHeld(ls, l); ok {
|
||||
l.User, l.Profile = h.User, h.Profile
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// claudeRenewedHeld is the Held account whose sign-in Claude Code renewed,
|
||||
// when l, held by no saved account, is that (claudeSignInOwner): its
|
||||
// profile is the one read at the last look, and names another account
|
||||
// than the held one; the plan, when both say one, the held one's.
|
||||
func claudeRenewedHeld(ls []savedLogin, l savedLogin) (savedLogin, bool) {
|
||||
key := claudeProfileKey(l.Profile)
|
||||
if l.Agent != "claude" || key == "" {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
c, ok := parseClaudeCredentials(l.Auth)
|
||||
if !ok {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
for _, h := range ls {
|
||||
if h.Agent != "claude" || !h.Held || h.HeldAs != key || sameLogin(h, l) || claudeSetupToken(h.Auth) != "" {
|
||||
continue
|
||||
}
|
||||
if plan := c.OAuth.SubscriptionType; plan != "" && h.Plan != "" && !strings.EqualFold(plan, h.Plan) {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
return h, true
|
||||
}
|
||||
return savedLogin{}, false
|
||||
}
|
||||
|
||||
// claudeProfileKey is the account a ~/.claude.json profile names, by
|
||||
// email and organization: "" for none.
|
||||
func claudeProfileKey(profile json.RawMessage) string {
|
||||
email, org := claudeWho(profile)
|
||||
if strings.TrimSpace(email) == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(email)) + " " + org
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user