provider: the Claude account Claude Code is signed in to is told by its sign-in, so its /usage goes through its own proxy (#1938, kensenzhao)

kensenzhao: two Claude accounts, each with its own proxy (accountProxies,
#1807). With B signed in, `magpie quota` read B's usage through A's
proxy, while a saved account's read went through its own.

The cause: magpie named the account Claude Code is signed in to from
~/.claude.json's profile. After a switch, a Claude Code started on the
account before writes that account's profile, so the sign-in was B's
while the profile still named A. liveLogin and claudeAccount then called
A the signed-in account. Logins flagged A as active, /usage ran with
ViaLogin(A), which is A's proxy, using B's credentials, and B's
reading was shown on A's card. claudeSignInOfAnother already kept this
crossing out of the saved records (netfishx), but not out of the live
name.

Now liveLogin and claudeAccount correct the profile's name by the
sign-in (claudeSignInOwner). When the profile's account doesn't hold
Claude Code's credential and exactly one other saved account does, that
one is the account signed in. claudeSignInOfAnother and the new owner
share claudeHolders. Everything that names the live account follows:
Active, /usage and its card, settings.json's proxy env, warm-up, model
test and the bridge.

Audit of the other usage readers: every per-account read goes through
loginQuota's ViaLogin(l.User), and the saved Claude read through
ViaLogin at the run. Codex names its live account from auth.json's own
tokens, so it can't cross this way. Grok's reading and Kiro's API-key
reading still use the provider's proxy (noted in 3dc8f00f0), and a
Claude Code in WSL is handed no *_PROXY. Both are left as they are.

Run: TestClaudeUsageGoesThroughTheSignedInAccountsProxy (new) fails
with the correction reverted by hand: pro is active, /usage runs
through 127.0.0.1:7897, and the reading is on pro's card. It passes with
the correction. It clears the kept readings before and after: without that,
TestRefreshSavedClaudeUsage, which runs after it, found max's reading left
over. Both pass together at -count=3. On the rebased head (on d9a050cc1):
go test -tags nogui ./internal/provider, ./internal/gateway (1178s, under
other sessions' load; an earlier run at -timeout 20m ran out mid-suite,
with nothing hung) and ./internal/cli pass. gofmt -l is clean, and GOOS=linux
and GOOS=windows go vet -tags nogui ./internal/provider pass. Rebased again
onto dbcf791e4: ./internal/provider in full, the gateway's
Claude|AccountProxy|KeyProxy tests, gofmt and both vets pass. Before the fix, the reporter's case was reproduced
with the same test, and real Claude Code 2.1.296 was checked to take the
proxy from its env and not from settings.json under --setting-sources "".
Not tried with real accounts behind real proxies.
This commit is contained in:
yetone
2026-10-13 16:11:42 +08:00
parent 110b204313
commit 15105161e0
3 changed files with 106 additions and 8 deletions
+9 -1
View File
@@ -731,7 +731,15 @@ func claudeAccount() (Provider, bool) {
// named as its saved login is (liveLogin): it is then the account Logins
// flags Active, not served a second time beside itself, and its
// allowances and limits are found under its name (#177)
user, _ = claudeSignedInUser(c.OAuth.SubscriptionType, statusPlan, user)
user, acct := claudeSignedInUser(c.OAuth.SubscriptionType, statusPlan, user)
if b, err := c.marshal(); err == nil && user != "" {
// by its sign-in when its profile is another's (claudeSignInOwner)
l := savedLogin{Agent: "claude", User: user, Auth: b}
if acct != nil {
l.Profile, _ = json.Marshal(acct)
}
user = claudeSignInOwner(readLogins(), l).User
}
if user == "" {
user = "Claude account"
if plan != "" {
@@ -0,0 +1,70 @@
package provider
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/netproxy"
)
// #1938 (kensenzhao): two Claude accounts, each with its own proxy. magpie
// switches Claude Code to max, and a Claude Code started on pro writes
// pro's profile after. Claude Code's sign-in is max's, so its /usage goes
// through max's proxy and is max's reading, not through pro's and shown
// as pro's.
func TestClaudeUsageGoesThroughTheSignedInAccountsProxy(t *testing.T) {
prof, _ := claudeThree(t)
if err := Save(Provider{ID: "claude", AccountProxies: map[string]string{"max@x.com": "http://127.0.0.1:17902", "pro@x.com": "http://127.0.0.1:7897"}}); err != nil {
t.Fatal(err)
}
if err := SwitchLogin("claude", "max@x.com"); err != nil {
t.Fatal(err)
}
prof("pro@x.com", "pro@x.com's Organization")
forgetAccountCaches()
for _, l := range Logins("claude") {
if l.Active != (l.User == "max@x.com") {
t.Errorf("%s active %v; Claude Code is signed in to max@x.com", l.User, l.Active)
}
}
var text atomic.Value
text.Store("Current session: 17% used\nCurrent week (all models): 38% used")
fakeClaudeUsage(t, &text, nil)
// no reading of another test's is kept, nor one of this test's left
forget := func() {
lastQuotas.Lock()
lastQuotas.m, lastQuotas.loaded = nil, false
lastQuotas.Unlock()
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
}
forget()
t.Cleanup(forget)
var mu sync.Mutex
var via []string
UsageClaudeVia(func(ctx context.Context, dir string) (string, error) {
mu.Lock()
via = append(via, dir+"|"+netproxy.Choice(ctx))
mu.Unlock()
return text.Load().(string), nil
})
AskClaudeUsage()
read := ""
for _, q := range QuotaReport(Asked(context.Background()), time.Now()) {
if q.Provider == "claude" && len(q.Windows) > 0 {
read += q.User
}
}
mu.Lock()
defer mu.Unlock()
if len(via) != 1 || via[0] != "|http://127.0.0.1:17902" {
t.Errorf("/usage ran %v, want once in Claude Code's own directory through max's proxy", via)
}
if read != "max@x.com" {
t.Errorf("the reading is on %q's card, want max@x.com's", read)
}
}
+27 -7
View File
@@ -677,7 +677,7 @@ func liveLogin(agent string) (savedLogin, bool) {
if l.User == "" {
return savedLogin{}, false
}
return l, true
return claudeSignInOwner(readLogins(), l), true
}
return savedLogin{}, false
}
@@ -1356,27 +1356,47 @@ func forgetAccountCaches() {
// runs and Usage card then were the first's (netfishx on X). The account
// the profile names holding the sign-in already, it is that one's.
func claudeSignInOfAnother(ls []savedLogin, l savedLogin) bool {
same, others := claudeHolders(ls, l)
return !same && len(others) > 0
}
// claudeHolders are the saved Claude accounts holding the sign-in l
// carries: same when l's own record (sameLogin) is one of them, others the
// rest.
func claudeHolders(ls []savedLogin, l savedLogin) (same bool, others []savedLogin) {
if l.Agent != "claude" {
return false
return false, nil
}
c, ok := parseClaudeCredentials(l.Auth)
if !ok {
return false
return false, nil
}
holds := func(x savedLogin) bool {
o, ok := parseClaudeCredentials(x.Auth)
return ok && (c.OAuth.AccessToken != "" && o.OAuth.AccessToken == c.OAuth.AccessToken ||
c.OAuth.RefreshToken != "" && o.OAuth.RefreshToken == c.OAuth.RefreshToken)
}
another := false
for _, x := range ls {
if x.Agent != "claude" || !holds(x) {
continue
}
if sameLogin(x, l) {
return false
return true, nil
}
another = true
others = append(others, x)
}
return another
return false, others
}
// claudeSignInOwner is the account Claude Code is signed in to by its
// sign-in, l named by its profile: when the profile is another's
// (claudeSignInOfAnother) and one saved account alone holds the sign-in,
// 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).
func claudeSignInOwner(ls []savedLogin, l savedLogin) savedLogin {
if same, others := claudeHolders(ls, l); !same && len(others) == 1 {
l.User, l.Profile = others[0].User, others[0].Profile
}
return l
}