diff --git a/internal/provider/account.go b/internal/provider/account.go index 979c3f2de..578575891 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -420,6 +420,18 @@ type claudeAuth struct { RateLimitTier string `json:"rateLimitTier"` } +// plan names the Claude subscription as Anthropic sells it now: +// Max 20x is Claude Ultra, while Claude Code still says "max" for it +// and tells the two apart only by the rate-limit tier. +func (a claudeAuth) plan() string { return claudePlan(a.SubscriptionType, a.RateLimitTier) } + +func claudePlan(sub, tier string) string { + if sub == "max" && strings.Contains(tier, "max_20x") { + return "ultra" + } + return sub +} + // claudeCredentials keeps the whole credential blob in raw, so refreshing a // token writes back everything else — MCP OAuth state included — untouched. type claudeCredentials struct { @@ -811,9 +823,9 @@ func claudeAccount() (Provider, bool) { if signedOut { return claudeStandInAccount() } - plan := c.OAuth.SubscriptionType + plan := c.OAuth.plan() if statusPlan != "" { - plan = statusPlan + plan = claudePlan(statusPlan, c.OAuth.RateLimitTier) } // named as its saved login is (liveLogin): it is then the account Logins // flags Active, not served a second time beside itself, and its diff --git a/internal/provider/claude_dirs.go b/internal/provider/claude_dirs.go index 7c3815a01..c9b67e28b 100644 --- a/internal/provider/claude_dirs.go +++ b/internal/provider/claude_dirs.go @@ -450,7 +450,7 @@ func takeClaudeDir(l *savedLogin, c claudeCredentials) (bool, error) { } l.Auth = b if c.OAuth.SubscriptionType != "" { - l.Plan = c.OAuth.SubscriptionType + l.Plan = c.OAuth.plan() } l.Renewed = time.Now().UTC().Truncate(time.Second) l.Lapsed, l.Refused = "", "" diff --git a/internal/provider/claude_plan_test.go b/internal/provider/claude_plan_test.go new file mode 100644 index 000000000..d9544f79b --- /dev/null +++ b/internal/provider/claude_plan_test.go @@ -0,0 +1,63 @@ +package provider + +import ( + "encoding/json" + "testing" +) + +// Claude Max 20x is sold as Claude Ultra now, but Claude Code still says +// "max" for both Max plans: the account chip reads its rate-limit tier, or +// a 20x account reads the same as a 5x one. +func TestClaudeMax20xIsUltra(t *testing.T) { + for _, c := range []struct{ sub, tier, want string }{ + {"max", "default_claude_max_20x", "ultra"}, + {"max", "default_claude_max_5x", "max"}, + {"max", "", "max"}, + {"pro", "default_claude_ai", "pro"}, + {"team", "default_claude_max_5x", "team"}, + {"ultra", "default_claude_max_20x", "ultra"}, + } { + if got := claudePlan(c.sub, c.tier); got != c.want { + t.Errorf("claudePlan(%q, %q) = %q, want %q", c.sub, c.tier, got, c.want) + } + } + + auth := func(tier string) []byte { + b, _ := json.Marshal(map[string]any{"claudeAiOauth": map[string]any{ + "accessToken": "a", "refreshToken": "r", "subscriptionType": "max", "rateLimitTier": tier, + }}) + return b + } + c, _ := parseClaudeCredentials(auth("default_claude_max_20x")) + l, err := claudeLogin(c, map[string]any{"emailAddress": "u@example.com"}) + if err != nil || l.Plan != "ultra" { + t.Fatalf("a new 20x sign-in: plan %q, %v; want ultra", l.Plan, err) + } + + // accounts saved before as "max" read by their tier + b, _ := json.Marshal([]savedLogin{ + {Agent: "claude", User: "twenty@example.com", Plan: "max", Auth: auth("default_claude_max_20x")}, + {Agent: "claude", User: "five@example.com", Plan: "max", Auth: auth("default_claude_max_5x")}, + }) + ls, err := parseLogins(b) + if err != nil { + t.Fatal(err) + } + got := map[string]string{} + for _, l := range ls { + got[l.User] = l.Plan + } + if got["twenty@example.com"] != "ultra" || got["five@example.com"] != "max" { + t.Fatalf("saved plans read as %v; want 20x ultra, 5x max", got) + } + + // Claude Code renewing a held Ultra account's sign-in under another's + // profile still says "max": it is the same plan, so the held one is found + other := json.RawMessage(`{"emailAddress":"other@example.com"}`) + held := savedLogin{Agent: "claude", User: "twenty@example.com", Plan: "ultra", Held: true, HeldAs: claudeProfileKey(other), + Auth: auth("default_claude_max_20x"), Profile: json.RawMessage(`{"emailAddress":"twenty@example.com"}`)} + renewed := savedLogin{Agent: "claude", User: "other@example.com", Plan: "max", Auth: auth("default_claude_max_20x"), Profile: other} + if h, ok := claudeRenewedHeld([]savedLogin{held}, renewed); !ok || h.User != held.User { + t.Fatalf("a held Ultra account renewed under another profile isn't found: %v %v", h.User, ok) + } +} diff --git a/internal/provider/logins.go b/internal/provider/logins.go index 0a8fb0477..2fb070fc3 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -213,6 +213,14 @@ func parseLogins(b []byte) ([]savedLogin, error) { // doesn't allow its subscription used outside its client), so one // signed in before is left out, and gone from the file at its next write out = slices.DeleteFunc(out, func(l savedLogin) bool { return l.Agent == "dimagent" }) + for i := range out { + // a Max 20x saved before it was named Ultra + if out[i].Agent == "claude" && out[i].Plan == "max" { + if c, ok := parseClaudeCredentials(out[i].Auth); ok { + out[i].Plan = claudePlan(out[i].Plan, c.OAuth.RateLimitTier) + } + } + } return nameAlike(dedupeLogins(out)), nil } @@ -754,9 +762,9 @@ func liveLogin(agent string) (savedLogin, bool) { if signedOut { return savedLogin{}, false } - l := savedLogin{Agent: agent, Plan: c.OAuth.SubscriptionType, Auth: b} + l := savedLogin{Agent: agent, Plan: c.OAuth.plan(), Auth: b} var acct map[string]any - if l.User, acct = claudeSignedInUser(l.Plan, plan, user); acct != nil { + if l.User, acct = claudeSignedInUser(c.OAuth.SubscriptionType, plan, user); acct != nil { l.Profile, _ = json.Marshal(acct) l.HeldAs = claudeProfileKey(l.Profile) } @@ -1772,7 +1780,7 @@ func claudeRenewedHeld(ls []savedLogin, l savedLogin) (savedLogin, bool) { 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) { + if plan := c.OAuth.plan(); plan != "" && h.Plan != "" && !strings.EqualFold(plan, h.Plan) { return savedLogin{}, false } return h, true diff --git a/internal/provider/signin.go b/internal/provider/signin.go index 08fb27ac3..b9fd3d326 100644 --- a/internal/provider/signin.go +++ b/internal/provider/signin.go @@ -833,7 +833,7 @@ func claudeLogin(c claudeCredentials, acct map[string]any) (savedLogin, error) { return savedLogin{}, err } profile, _ := json.Marshal(acct) - return savedLogin{Agent: "claude", User: claudeUser(email, c.OAuth.SubscriptionType, acct), Plan: c.OAuth.SubscriptionType, Auth: auth, Profile: profile}, nil + return savedLogin{Agent: "claude", User: claudeUser(email, c.OAuth.SubscriptionType, acct), Plan: c.OAuth.plan(), Auth: auth, Profile: profile}, nil } func codexExchange(ctx context.Context, code, verifier, redirect string) (savedLogin, error) {