Files
magpie/internal/provider/claude_usage_proxy_test.go
T
yetone 15105161e0 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.
2026-10-13 16:11:42 +08:00

71 lines
2.1 KiB
Go

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