Files
magpie/docs
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
..