Files
magpie/internal/provider/claude_renewed_held_test.go
T
yetone 9595a503a9 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.
2026-11-04 17:12:50 +08:00

110 lines
3.3 KiB
Go

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)
}
})
}