Files
magpie/internal/provider/claude_dirs.go
T
yetoneandhhh2210 957aa79aa3 provider, gateway: refreshing a saved Claude account's card reads its allowance through Claude Code, without switching to it (#1630, hhh2210)
A saved (not signed-in) Claude account's Usage card read nothing on
Refresh: claudeWindows only ever ran /usage for the account Claude Code
itself is signed in to, so the button did nothing for the rest.

Now the card's own refresh (RefreshUsage for that account, savedRefresh)
runs the real Claude Code's /usage with CLAUDE_CONFIG_DIR set to the
account's claudeSavedDir (readSavedClaudeUsage). magpie still never calls
Anthropic with the token itself, and Claude Code's own account is not
switched. The 30-second per-account floor holds, background reads keep
the last reading, and asks for every card (the Usage page opened, magpie
quota, magpie accounts) start no Claude Code per saved account: they keep
the last reading, or the "not read yet: refresh this account's card"
note.

Claude Code may refresh the account's token in that directory while it
runs, spending the old refresh token. So:
- inSavedClaudeDir holds a per-account RWMutex (claudeDirRun) for the run,
  and switchSavedLogin takes it for writing first, so a switch never hands
  Claude Code a copy the run has just spent;
- syncSavedClaude copies a rotated sign-in from the directory back into
  logins.json right after the run, under savedTokenMu and loginsMu.
Two Claude Codes in one directory are kept from refreshing at once by
Claude Code's own lock file (2.1.296's refresh lock in its config dir).
A saved account's warm-up, which already ran in that directory, now goes
through inSavedClaudeDir too.

The plumbing (configDir through UsageClaudeVia, readClaudeUsage and the
gateway's claudeUsage, and TestClaudeUsageRunsInSavedAccountDirectory)
is from hhh2210's patch 043ee7d7, reviewed line by line. Its gate (any
explicit read reads every saved account) was narrowed to the card's own
refresh, and the lock and sync back are new.

Run: go test -tags nogui ./internal/provider ./internal/gateway . (sandbox
HOME, fake claude via UsageClaudeVia, no real token), gofmt -l, GOOS=linux
and GOOS=windows go vet on the three packages. Broken by hand, each red:
saved never read (all three new tests), any ask reads saved
(TestRefreshSavedClaudeUsage), switch not held and no sync back
(TestSavedClaudeUsageKeepsRotatedSignIn / SavesRotatedSignIn), run in ""
instead of the account's directory. Not tried with a real Claude Code
binary against a real saved account.

Co-authored-by: hhh2210 <hzy2210@gmail.com>
2026-10-11 23:37:34 +08:00

426 lines
14 KiB
Go

package provider
// A saved Claude account in use beside the one Claude Code is signed in to
// runs Claude Code in a config directory of its own (CLAUDE_CONFIG_DIR): its
// sign-in is put there once, from logins.json, and from then on Claude Code
// keeps it, refreshing it as it does its own. magpie never refreshes a
// Claude sign-in nor asks Anthropic anything with one; it only reads back
// what Claude Code left, so logins.json follows.
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/settings"
"golang.org/x/text/unicode/norm"
)
// claudeDirsRoot holds the saved Claude accounts' config directories, and
// the sign-ins being made.
func claudeDirsRoot() string { return filepath.Join(filepath.Dir(Path()), "claude-accounts") }
// claudeAccountDir is the config directory a saved Claude account runs
// Claude Code in.
func claudeAccountDir(user string) string {
sum := sha256.Sum256([]byte(strings.ToLower(strings.TrimSpace(user))))
return filepath.Join(claudeDirsRoot(), hex.EncodeToString(sum[:])[:16])
}
// claudeDirService is the keychain item Claude Code keeps its sign-in in
// when CLAUDE_CONFIG_DIR is dir: its own name, told apart by the directory.
func claudeDirService(dir string) string {
sum := sha256.Sum256([]byte(norm.NFC.String(dir)))
return "Claude Code-credentials-" + hex.EncodeToString(sum[:])[:8]
}
// readClaudeDir is the sign-in Claude Code keeps in config directory dir,
// where it looks first: the keychain on a Mac, then the file.
func readClaudeDir(dir string) (claudeCredentials, bool) {
if settings.AccountDiscoveryOff() {
return claudeCredentials{}, false
}
if claudeKeychain {
out, err := proc.Command("security", "find-generic-password", "-s", claudeDirService(dir), "-a", claudeKeychainAccount(), "-w").Output()
if err == nil {
b, _ := keychainText(bytes.TrimSpace(out))
if c, ok := parseClaudeCredentials(b); c.raw != nil {
// the item there is the sign-in, emptied or not: Claude
// Code reads no file beside it
return c, ok
}
}
}
b, err := readAccountFile(filepath.Join(dir, ".credentials.json"))
if err != nil {
return claudeCredentials{}, false
}
return parseClaudeCredentials(b)
}
// clearClaudeDir takes the sign-in out of config directory dir, so the one
// it was moved to is its only holder.
func clearClaudeDir(dir string) {
if discoveryOff() {
return
}
if claudeKeychain {
_ = proc.Command("security", "delete-generic-password", "-s", claudeDirService(dir), "-a", claudeKeychainAccount()).Run()
}
_ = os.Remove(filepath.Join(dir, ".credentials.json"))
}
// forgetClaudeDir removes a saved account's config directory altogether.
// ClaudeHandedOver is told of a saved Claude account switched in to Claude
// Code itself, whose directory forgetClaudeDir has just removed: a Claude
// Code still running there (the gateway's, waiting for its conversation's
// next turn) holds the refresh token that is Claude Code's own now, and a
// refresh of it there would leave Claude Code's copy refused, the account
// signed out (0xAncientTwo on X). The gateway sets it.
var ClaudeHandedOver func(user string)
func claudeHandedOver(user string) {
if f := ClaudeHandedOver; f != nil {
f(user)
}
}
func forgetClaudeDir(user string) {
dir := claudeAccountDir(user)
clearClaudeDir(dir)
_ = os.RemoveAll(dir)
}
// claudeDirRuns are the runs magpie waits on in a saved Claude account's
// config directory (inSavedClaudeDir), by account: its /usage, a warm-up.
// Claude Code may refresh the sign-in it keeps there as it runs, spending
// the refresh token, so the account isn't handed over to Claude Code
// itself (switchSavedLogin) until they end: Claude Code's own copy would
// be the spent one. Two Claude Codes in one directory don't both refresh
// it: Claude Code takes a lock in its config directory to refresh, and
// reads the sign-in again once it has it.
var claudeDirRuns struct {
sync.Mutex
m map[string]*sync.RWMutex
}
func claudeDirRun(user string) *sync.RWMutex {
key := strings.ToLower(strings.TrimSpace(user))
claudeDirRuns.Lock()
defer claudeDirRuns.Unlock()
if claudeDirRuns.m == nil {
claudeDirRuns.m = map[string]*sync.RWMutex{}
}
mu := claudeDirRuns.m[key]
if mu == nil {
mu = &sync.RWMutex{}
claudeDirRuns.m[key] = mu
}
return mu
}
// inSavedClaudeDir runs f with the config directory of the saved Claude
// account user (claudeSavedDir), and keeps what Claude Code made of the
// sign-in there in logins.json once it is done: a refreshed one is the
// account's now, the one it replaced spent.
func inSavedClaudeDir(user string, f func(dir string) error) error {
mu := claudeDirRun(user)
mu.RLock()
defer mu.RUnlock()
dir, err := claudeSavedDir(user)
if err != nil {
return err
}
defer syncSavedClaude(user)
return f(dir)
}
// syncSavedClaude brings the saved Claude account user in logins.json up
// to what Claude Code keeps in its config directory, making nothing there.
func syncSavedClaude(user string) {
savedTokenMu.Lock()
defer savedTokenMu.Unlock()
loginsMu.Lock()
defer loginsMu.Unlock()
ls := readLogins()
for i := range ls {
if ls[i].Agent == "claude" && strings.EqualFold(ls[i].User, user) {
if _, changed, err := syncClaudeDir(&ls[i]); err == nil && changed {
_ = writeLogins(ls)
}
return
}
}
}
// readSavedClaudeUsage is the allowance of the saved Claude account user:
// Claude Code's /usage run in the account's own config directory, through
// the account's own proxy. Claude Code's own sign-in isn't touched.
func readSavedClaudeUsage(ctx context.Context, user string) ([]QuotaWindow, error) {
ws := []QuotaWindow{}
err := inSavedClaudeDir(user, func(dir string) error {
var err error
ws, err = readClaudeUsage(ViaLogin(ctx, "claude", user), dir)
return err
})
return ws, err
}
// claudeSavedDir is the config directory to run Claude Code in for the
// saved account user: its sign-in is put there from logins.json when there
// is none yet, and what Claude Code has made of it since is read back. An
// account that can't be used (claudeSignedOut) gets none.
func claudeSavedDir(user string) (string, error) {
savedTokenMu.Lock()
defer savedTokenMu.Unlock()
loginsMu.Lock()
defer loginsMu.Unlock()
ls := readLogins()
i := -1
for j := range ls {
if ls[j].Agent == "claude" && strings.EqualFold(ls[j].User, user) {
i = j
}
}
if i < 0 {
return "", fmt.Errorf("no saved claude account %q", user)
}
dir := claudeAccountDir(user)
has, changed, err := syncClaudeDir(&ls[i])
if err != nil {
return "", err
}
if changed {
if err := writeLogins(ls); err != nil {
return "", err
}
}
if why := claudeSignedOut(ls[i]); why != "" {
return "", errors.New(why)
}
if has {
return dir, nil
}
c, _ := parseClaudeCredentials(ls[i].Auth)
b, err := c.marshal()
if err != nil {
return "", err
}
if err := os.MkdirAll(dir, 0o700); err != nil {
return "", err
}
if err := writePrivate(filepath.Join(dir, ".credentials.json"), append(b, '\n')); err != nil {
return "", err
}
// who it is, as Claude Code keeps it beside a sign-in it made
profile := filepath.Join(dir, ".claude.json")
if _, err := os.Stat(profile); os.IsNotExist(err) && len(ls[i].Profile) > 0 {
var acct map[string]any
if json.Unmarshal(ls[i].Profile, &acct) == nil {
pb, _ := json.MarshalIndent(map[string]any{"oauthAccount": acct, "hasCompletedOnboarding": true}, "", " ")
_ = writePrivate(profile, append(pb, '\n'))
}
}
return dir, nil
}
// syncClaudeDir brings the saved Claude login l up to what Claude Code
// keeps in the account's config directory: the sign-in it refreshed to
// there, or, when it emptied that sign-in, Anthropic's refusal of the one
// l has. has says the directory holds a sign-in to run on; changed, that l
// changed.
func syncClaudeDir(l *savedLogin) (has, changed bool, err error) {
c, ok := readClaudeDir(claudeAccountDir(l.User))
switch {
case ok:
changed, err = takeClaudeDir(l, c)
return true, changed, err
case c.cleared():
return false, refuseClaudeLogin(l, claudeLoginVersion(*l)), nil
}
return false, false, nil
}
// takeClaudeDir puts the sign-in Claude Code keeps in an account's config
// directory into its saved login, and says whether that changed it.
func takeClaudeDir(l *savedLogin, c claudeCredentials) (bool, error) {
if old, ok := parseClaudeCredentials(l.Auth); ok && old.OAuth.AccessToken == c.OAuth.AccessToken &&
old.OAuth.RefreshToken == c.OAuth.RefreshToken {
return false, nil
}
b, err := c.marshal()
if err != nil {
return false, err
}
l.Auth = b
if c.OAuth.SubscriptionType != "" {
l.Plan = c.OAuth.SubscriptionType
}
l.Renewed = time.Now().UTC().Truncate(time.Second)
l.Lapsed, l.Refused = "", ""
return true, nil
}
// claudeStandIn is the saved account served in the place of Claude Code's
// own while Claude Code is signed out — the user logged out of it, as it
// tells them to when magpie's token is set beside a claude.ai sign-in.
// magpie keeps the other accounts' sign-ins itself, each its own, so a
// logout signs none of them out of magpie: the stand-in runs Claude Code
// in a config directory of its own, as the others on do. Never the one
// Claude Code was signed in to, whose sign-in went with it
// (claudeLoggedOut, or still Held before rememberLogins next looks: it is
// asked only while Claude Code is signed out); of the others, one that can
// be used, on before off, then the one seen last; one that can't — its
// sign-in gone, or refused — only when no other can; "" when there is none.
func claudeStandIn(ls []savedLogin) string {
user, best := "", -1
var seen time.Time
for _, l := range ls {
if l.Agent != "claude" || l.Held || l.Lapsed != "" && l.Refused == "" {
continue
}
rank := 0
if claudeSignedOut(l) == "" {
rank = 2
if l.On {
rank++
}
}
if rank > best || rank == best && l.Seen.After(seen) {
user, seen, best = l.User, l.Seen, rank
}
}
return user
}
// claudeLogoutLapse is why the account Claude Code was signed in to can't
// be used once Claude Code let go of it: logged out, which revokes the
// sign-in it held, or emptied that sign-in once Anthropic refused it.
const claudeLogoutLapse = "Claude Code is no longer signed in; sign in again in magpie"
// legacyClaudeLogoutLapse is claudeLogoutLapse as magpie wrote it before,
// which took every such lapse for a /logout.
const legacyClaudeLogoutLapse = "Claude Code's /logout signed it out (it revokes the sign-in it holds); sign in again"
// claudeGoneLapse is why a saved Claude account with no sign-in kept can't
// be used.
const claudeGoneLapse = "its sign-in is gone; sign in again"
// claudeLoggedOut marks the account Claude Code held, now that it holds no
// sign-in, as lapsed: magpie's copy of that account is the same sign-in,
// gone too — revoked by a /logout (POST <token URL>/revoke, Claude Code
// 2.1.x's performLogout), or refused by Anthropic, as the sign-in Claude
// Code emptied was. Which it was can't be told from here. Signed in to it
// again, Claude Code holds it once more (rememberLogins). The accounts
// magpie keeps in config directories of their own are sign-ins of their own
// and stay. It says whether ls changed.
func claudeLoggedOut(ls []savedLogin) bool {
changed := false
for i := range ls {
if ls[i].Agent != "claude" || !ls[i].Held {
continue
}
ls[i].Held, changed = false, true
if claudeSignedOut(ls[i]) == "" {
ls[i].Lapsed, ls[i].Refused = claudeLogoutLapse, ""
}
}
return changed
}
// claudeSignedOut is why a saved Claude account can't be used, "" when it
// can: its saved sign-in is gone, Claude Code let go of it
// (claudeLoggedOut), or Anthropic refused the credential it has now
// (claude_auth.go) — a refusal of an earlier one says nothing of it.
func claudeSignedOut(l savedLogin) string {
if l.Lapsed != "" && (l.Refused == "" || l.Refused == claudeLoginVersion(l)) {
if l.Lapsed == legacyClaudeLogoutLapse {
return claudeLogoutLapse
}
return l.Lapsed
}
if _, ok := parseClaudeCredentials(l.Auth); ok {
return ""
}
return claudeGoneLapse
}
// claudeStandInAccount is the Claude Code provider while Claude Code is
// signed out, on claudeStandIn's account; false with no account saved.
func claudeStandInAccount() (Provider, bool) {
loginsMu.Lock()
ls := readLogins()
loginsMu.Unlock()
user := claudeStandIn(ls)
if user == "" {
return Provider{}, false
}
acct := &Account{Agent: "claude", User: user, standIn: true}
for _, l := range ls {
if l.Agent == "claude" && strings.EqualFold(l.User, user) {
acct.Plan = l.Plan
}
}
acct.token = func(context.Context) (string, error) { return claudeSavedDir(user) }
return claudeProvider(acct), true
}
// AgentsOwn says the account is the one the agent itself is signed in to,
// run in the agent's own home with what it keeps there, not with a sign-in
// magpie hands it. Which account that is moves with a switch (SwitchLogin):
// a Claude Code started on it before goes on as whatever Claude Code is
// signed in to by then, as it reads its keychain again every half minute.
func (a *Account) AgentsOwn() bool { return a != nil && a.token == nil }
// ClaudeCodeMovedOff says Claude Code itself is signed in to another
// account than user now: what a Claude Code run in its own home says of
// the account it is on is no longer user's. false when it can't be told.
// Named from its profile, the account is told by its whole name: a Team
// seat and a personal plan of one email ("me@x.com · Acme", "me@x.com")
// are two accounts, and one's usage isn't the other's (netfishx on X).
func ClaudeCodeMovedOff(user string) bool {
on, named := claudeCodeOn()
if on == "" {
return false
}
if named {
return !strings.EqualFold(strings.TrimSpace(on), strings.TrimSpace(user))
}
return !sameClaudeUser(on, user)
}
// claudeCodeOn is the account Claude Code itself is signed in to now, as
// magpie names it; "" when signed out or unknown. named: the name is from
// its profile (~/.claude.json), whole, not the email alone `claude auth
// status` gives.
func claudeCodeOn() (user string, named bool) {
l, ok := liveLogin("claude")
if !ok {
return "", false
}
return l.User, len(l.Profile) > 0
}
// sameClaudeUser: a and b name one account, the one as magpie names a Team
// or Enterprise seat ("me@x.com · Org") and the other as its email alone,
// as `claude auth status` gives it.
func sameClaudeUser(a, b string) bool {
a, b = strings.ToLower(strings.TrimSpace(a)), strings.ToLower(strings.TrimSpace(b))
if a == b {
return true
}
ea, _, _ := strings.Cut(a, " · ")
eb, _, _ := strings.Cut(b, " · ")
return (ea == b || eb == a) && ea != ""
}