Files
magpie/internal/provider/claude_switch_fresh_test.go
T
yetone 80dc07d021 provider, gateway: switching Claude accounts no longer signs one out — the account switched away from is saved with the sign-in Claude Code has that moment, and the gateway lets go of its runs on the account switched to (0xAncientTwo on X)
The Claude subscription often showed as signed out over the last few days. Claude's refresh tokens are single-use. Once one is refreshed, every other copy holding the old one is dead: the next refresh with it fails with invalid_grant, and Claude Code then empties its sign-in. Two places kept an old copy.

1. A switch saved the outgoing account from magpie's credential cache, which can be up to 10s old. If Claude Code refreshed in that window, the saved login held the spent refresh token. Switching back then signed the account out. The switch now forgets the cache and reads the credential fresh.

2. The gateway keeps idle Claude Code runs in an account's own folder (CLAUDE_CONFIG_DIR) for up to an hour, each holding that account's token in memory. Once the account is handed to Claude Code's own home, both sides refresh the same token, and the loser is signed out. A switch now tells the gateway (provider.ClaudeHandedOver), which ends that account's idle runs. Runs in a home of their own are left alone.

TestClaudeSwitchSavesTheFreshCredential (the refreshed token is saved, and the gateway is told) fails without the change. TestClaudeHandOverLetsGoOfTheAccountsRuns (only the account's idle runs in its folder are closed, matched case-insensitively; -race) does not build without it.

go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass. TestClaudeRunEndsWhenTheClientLeaves failed once under load, then passed 5/5 alone and with the whole package.
2026-10-05 20:42:48 +08:00

53 lines
2.0 KiB
Go

package provider
import (
"path/filepath"
"strings"
"testing"
"time"
)
// 0xAncientTwo on X: the Claude subscription was signed out again and again.
// A switch saves the account it switches away from with the credentials
// Claude Code has for it now: one Claude Code refreshed a moment ago (its
// refresh token rotated, the old one dead) is saved as refreshed, not as
// magpie's look at the credentials up to 10 s before, which Anthropic then
// refuses when the account is used again.
func TestClaudeSwitchSavesTheFreshCredential(t *testing.T) {
home := claudeHome(t)
cred := claudeSignIn(t, home, time.Now().Add(time.Hour))
profile := filepath.Join(home, ".claude.json")
writeFile(t, profile, map[string]any{"oauthAccount": map[string]any{"emailAddress": "a@example.com", "accountUuid": "u-a"}})
rememberLogins(true)
b := func(rt string) {
writeFile(t, cred, map[string]any{"claudeAiOauth": map[string]any{"accessToken": "sk-ant-oat01-" + rt, "refreshToken": "sk-ant-ort01-" + rt,
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "pro"}})
}
b("b")
writeFile(t, profile, map[string]any{"oauthAccount": map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"}})
forgetClaudeCredential()
rememberLogins(true) // magpie has looked at b's credentials just now
b("b2") // Claude Code refreshes b's sign-in
old := ClaudeHandedOver
defer func() { ClaudeHandedOver = old }()
handed := ""
ClaudeHandedOver = func(user string) { handed = user }
if err := SwitchLogin("claude", "a@example.com"); err != nil {
t.Fatal(err)
}
// the gateway is told, to let go of its runs in a's directory
if handed != "a@example.com" {
t.Fatalf("handed over %q", handed)
}
for _, l := range readLogins() {
if l.Agent == "claude" && l.User == "b@example.com" {
if !strings.Contains(string(l.Auth), "sk-ant-ort01-b2") {
t.Fatalf("b saved with a refresh token Claude Code had replaced: %s", l.Auth)
}
return
}
}
t.Fatal("b not saved")
}