Files
magpie/internal/gateway/claude_handover_test.go
T
yetone 403323af7f claude: a switch waits for another magpie's runs in the account's directory (#4532)
StringKe's audit, item 2. claudeDirRun is an in-process RWMutex, so
`magpie accounts switch` from the CLI never waited for the app's /usage,
warm-up or keep-alive run in the saved account's directory: it handed
Claude Code the refresh token that run could still spend, and removed
the directory under it. And the gateway's hand-over let go only of idle
runs; a run in a turn when the switch came was kept for its next turn,
waiting up to idleLongest in the removed directory with the same token.

- inSavedClaudeDir also holds a lock file of its own beside the
  account's directory (markClaudeRun, <dir>.run-<pid>-<n>.lock,
  filelock), removed as it ends.
- switchSavedLogin waits on every such file (waitClaudeRuns) up to
  claudeRunWait (90s), then fails with errClaudeRunning and leaves the
  sign-in as it was. A file whose run ended without removing it is free
  and taken out.
- handedOver marks the account's runs in a turn (subscriptionRun.handed);
  keepIdle ends such a run instead of keeping it.

Shelved (parked) sessions hold no process or token; a later turn starts
a new run under the account the gateway picks then, so nothing changes
there.

Run (mote, nogui):
- new tests TestClaudeSwitchWaitsForAnotherMagpiesRun,
  TestClaudeSwitchRunHeldAndLeft, TestSavedClaudeRunHoldsItsLock,
  TestClaudeHandOverEndsTheAccountsBusyRuns, plus TestClaudeSwitch*,
  TestSavedClaude*, TestClaudeHandOver*: ok.
- Broken by hand (wait call, run mark and the handed check taken out,
  helpers kept): all four new tests red ("switched while another magpie
  ran Claude Code…", "got <nil>", "a left lock file kept", "run locks []",
  "a run of the handed-over account was kept for its next turn").
- go vet darwin/linux/windows for provider and gateway: ok.
- Windows (win-run): the three provider tests and
  TestSavedClaudeUsageKeepsRotatedSignIn: ok.

Not tried: two real magpie processes with a real Claude Code (the other
process is a second open of the lock file, which flock and LockFileEx
treat as another holder; filelock's own test covers a real second
process). Not covered, filed as #4750: a run queued behind the switch
makes the handed-over account's directory again, and a run in a turn
can still refresh during that turn.
2026-11-01 15:25:56 +08:00

79 lines
2.5 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, pending: map[string]chan mcpToolResult{}}
r.owner.Store(&owner)
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)
}
}
// #4532: a run in a turn as its account is handed over isn't kept for the
// next turn when this one ends: kept, it waited up to idleLongest in the
// removed directory with the refresh token that is Claude Code's now.
// Another account's run in a turn is kept as before.
func TestClaudeHandOverEndsTheAccountsBusyRuns(t *testing.T) {
b := newSubscriptionBridge()
run := func(owner string) *subscriptionRun {
r := &subscriptionRun{bridge: b, token: owner, pending: map[string]chan mcpToolResult{}}
r.owner.Store(&owner)
b.mu.Lock()
b.runs[owner] = r // in a turn: a run, not waiting
b.mu.Unlock()
return r
}
moved := run("claude\x00b@example.com")
other := run("claude\x00a@example.com")
provider.ClaudeHandedOver("b@example.com")
if b.keepIdle(moved, "next", "") || !moved.closed {
t.Fatal("a run of the handed-over account was kept for its next turn")
}
if !b.keepIdle(other, "other", "") || other.closed {
t.Fatal("another account's run in a turn was let go")
}
b.mu.Lock()
_, kept := b.idle["next"]
b.mu.Unlock()
if kept {
t.Fatal("the handed-over account's run waits for a next turn")
}
}