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.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// 0xAncientTwo on X: the Claude subscription was signed out again and again.
|
|
// A saved account switched in to Claude Code itself has its directory
|
|
// removed; the runs waiting there for their next turn hold its refresh
|
|
// token, now Claude Code's, and are let go, so none refreshes it under
|
|
// Claude Code. Runs of other accounts, and runs in Claude Code's own home
|
|
// (which read its sign-in afresh), wait on.
|
|
func TestClaudeHandOverLetsGoOfTheAccountsRuns(t *testing.T) {
|
|
b := newSubscriptionBridge()
|
|
run := func(owner string) *subscriptionRun {
|
|
r := &subscriptionRun{bridge: b, owner: owner, pending: map[string]chan mcpToolResult{}}
|
|
b.mu.Lock()
|
|
b.idle[owner] = r
|
|
r.idleKey = owner
|
|
b.mu.Unlock()
|
|
return r
|
|
}
|
|
moved := run("claude\x00B@example.com")
|
|
other := run("claude\x00a@example.com")
|
|
own := run("claude\x00b@example.com\x00" + ownHome)
|
|
|
|
provider.ClaudeHandedOver("b@example.com")
|
|
|
|
if !moved.closed {
|
|
t.Fatal("a run in the handed-over account's directory waits on")
|
|
}
|
|
if other.closed || own.closed {
|
|
t.Fatal("another account's run, or Claude Code's own, was let go")
|
|
}
|
|
b.mu.Lock()
|
|
_, kept := b.idle["claude\x00B@example.com"]
|
|
n := len(b.idle)
|
|
b.mu.Unlock()
|
|
if kept || n != 2 {
|
|
t.Fatalf("idle runs after the hand-over: %d, moved kept %v", n, kept)
|
|
}
|
|
}
|