provider: keep idle saved Claude accounts signed in, by Claude Code itself (#4532)
#4532: a saved Claude account switched on but not needed for weeks (switch-only) had nothing run on it, its refresh token sat idle, and it was found signed out when the switch wanted it. Codex's saved sign-ins already had a keep-alive (RenewLogins, every 8 days, one account an hourly look). The same loop now takes saved Claude accounts that are on, not Claude Code's own, not added by setup-token and not already signed out (claudeKeptAlive). For one that is due it has real Claude Code run /usage in the account's own CLAUDE_CONFIG_DIR, the path a card's refresh takes (readSavedClaudeUsage, inSavedClaudeDir), and syncSavedClaude keeps whatever sign-in Claude Code refreshed. magpie asks Anthropic nothing itself: no OAuth or usage endpoint, as removed in9bf6d43527. A run that went through marks Renewed, so the account is not run again for 8 days whether or not Claude Code rotated the token. A refusal goes through NoteClaudeSignInFailure (and syncClaudeDir for an emptied sign-in), so the row says to sign in again the way a request's refusal does, and the keep-alive runs nothing more on it. A network failure marks nothing. Anthropic's refresh-token idle lifetime is not known, so the interval is Codex's conservative 8 days. The reading itself is not kept as the card's cache. `magpie accounts refresh` and its help say the Claude ones are renewed too; providers-accounts.md describes it. Run on mote at93b31f5b7(this tree): go test -timeout 45m -count=1 ./internal/provider/... ./internal/cli/... ok; go test -race -count=10 -run 'TestRenewLogins|WeeklyOnly' ./internal/provider ok; gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet of ./internal/provider/... ./internal/cli/... clean. The new tests also passed with TZ=UTC and TZ=Pacific/Kiritimati. Rebased onto origin/main847f0c470(a GUI-only change) and run again at that tree on mote: provider and cli tests, gofmt, Linux and Windows vet, all ok. Broken by hand: with the Claude branch taken out of the loop's filter, TestRenewLoginsKeepsSavedClaudeSignedIn and TestRenewLoginsClaudeEmptiedSignIn fail ("renewed what wasn't due: []", "emptied: []"); with the NoteClaudeSignInFailure call removed, the refused case fails (Lapsed false). Not tried: real accounts and real Claude Code; the tests use the fake Claude Code runner and a sandbox HOME. Whether a /usage run makes Claude Code refresh a token that is not near expiry is not known; the run counts as the renewal either way. Left out (per #4532's scope): per-model windows in the switch and caps, CLI commands, reset cards.
This commit is contained in:
@@ -253,7 +253,7 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.
|
||||
- Each refresh token has exactly one holder. Vendors rotate tokens on refresh, so two copies of one token would sign each other out. `savedTokenMu` stops two requests refreshing one saved account at once.
|
||||
- A Claude subscription account is used only for Claude Code, through the genuine Claude Code binary (see [Claude subscription bridge](claude-subscription-bridge.md)).
|
||||
- Claude usage asks are ordered by `claudeAskGeneration`, not wall-clock timestamps. `AskClaudeUsage` and a Claude card's `RefreshUsage` advance it while holding the subscription cache then the login cache lock (`markClaudeUsageAsked`). Each cache captures it under its own lock before launching a read and checks it under that lock when publishing: a newer ask prevents `LoginUsage` from keeping the older reading and leaves `SubscriptionUsage` due to read again. Each `claudeWindows` entry records the generation its run answered. The timestamp's zero sentinel, `tried`/`wait`, the 30-second floor, idle behavior and saved-account scope remain unchanged. [`login_usage.go`](../../internal/provider/login_usage.go), [`subscription_usage.go`](../../internal/provider/subscription_usage.go), [`quota_refresh.go`](../../internal/provider/quota_refresh.go).
|
||||
- A saved Claude account's allowance is read only when its own card is refreshed (#1630; `savedRefresh`: `RefreshUsage` for that account, so `cardRefresh.wants` it and the read is asked). The real Claude Code runs `/usage` with `CLAUDE_CONFIG_DIR` set to the account's `claudeSavedDir` (`readSavedClaudeUsage`, through the gateway's `claudeUsage`), so magpie never calls Anthropic with the token itself and the account Claude Code is signed in to is not switched. The Usage page opened, `magpie quota` and `magpie accounts` ask every card and so start no saved account's Claude Code: they keep its last reading, or `errClaudeSaved` before one. Background reads keep the last reading, and the 30-second per-account floor holds for repeated refreshes. Claude Code may refresh the account's token in that directory while it runs (its own lock file keeps two Claude Codes in one directory from refreshing at once). So `inSavedClaudeDir` holds `claudeDirRun(user)` for reading while it runs, which `switchSavedLogin` takes for writing first, and after the run `syncSavedClaude` copies a rotated sign-in back into `logins.json` under `savedTokenMu` and `loginsMu`. A saved account's warm-up runs the same way. [`claude_usage_saved_test.go`](../../internal/provider/claude_usage_saved_test.go) covers the refresh, the other accounts left alone, the floor, background reads, a switch waiting for the run and the rotated sign-in kept; `TestClaudeUsageRunsInSavedAccountDirectory` checks the directory the command runs in.
|
||||
- A saved Claude account's allowance is read only when its own card is refreshed (#1630; `savedRefresh`: `RefreshUsage` for that account, so `cardRefresh.wants` it and the read is asked). The real Claude Code runs `/usage` with `CLAUDE_CONFIG_DIR` set to the account's `claudeSavedDir` (`readSavedClaudeUsage`, through the gateway's `claudeUsage`), so magpie never calls Anthropic with the token itself and the account Claude Code is signed in to is not switched. The Usage page opened, `magpie quota` and `magpie accounts` ask every card and so start no saved account's Claude Code: they keep its last reading, or `errClaudeSaved` before one. Background reads keep the last reading, and the 30-second per-account floor holds for repeated refreshes. Claude Code may refresh the account's token in that directory while it runs (its own lock file keeps two Claude Codes in one directory from refreshing at once). So `inSavedClaudeDir` holds `claudeDirRun(user)` for reading while it runs, which `switchSavedLogin` takes for writing first, and after the run `syncSavedClaude` copies a rotated sign-in back into `logins.json` under `savedTokenMu` and `loginsMu`. A saved account's warm-up runs the same way, and so does the keep-alive (#4532): every eight days (`keepAliveEvery`, as for a ChatGPT sign-in, since how long Anthropic keeps an idle Claude refresh token isn't known), one account a look shared with Codex's, `RenewLogins` runs that `/usage` once for each saved Claude account that is on, has a sign-in rather than a setup-token and isn't signed out (`claudeKeptAlive`, `renewClaude`), so Claude Code refreshes the sign-in if it is due and `syncSavedClaude` keeps it. The run marks `Renewed`, refreshed or not. A refusal is kept as a request's is (`NoteClaudeSignInFailure`, or the emptied sign-in `syncClaudeDir` reads), so the account's row says to sign in again. Its reading isn't kept as the card's (`TestRenewLoginsKeepsSavedClaudeSignedIn`). [`claude_usage_saved_test.go`](../../internal/provider/claude_usage_saved_test.go) covers the refresh, the other accounts left alone, the floor, background reads, a switch waiting for the run and the rotated sign-in kept; `TestClaudeUsageRunsInSavedAccountDirectory` checks the directory the command runs in.
|
||||
- Claude Code's `/usage` prints a reset with no year when it falls this year ("Oct 9, 2:59pm (UTC)"), or, in older versions, as a time alone ("3pm"). `claudeResetTime` reads it as the end of its window, so never further ahead than the window's length (5 hours for the session, 7 days for a week) and `claudeResetSlack`: a date is in the first of last year, this year and next that is less than a day past and no further ahead, a time alone the next one no further ahead. Where none is, as for a reading days old, it is the latest one already past, so the window reads as renewed rather than full for a year. Where the clocks go back and show a time twice, the reading not yet past is taken. A week's time alone read just after its reset still reads as tomorrow's.
|
||||
- A Copilot account is known by its GitHub login and host (`CopilotAccountName`, #1220): `mona` on github.com, `mona@acme.ghe.com` on an enterprise's GHE.com. That name is its `logins.json` user, its row in every list, its usage card and its per-account routing settings, and it is what `SwitchLogin`, `SetLoginOn` and `ForgetLogin` take, so one login on two hosts is two accounts, neither written over by the other nor dropped as the editors' own. One saved before on an enterprise's host under its login alone is read under its name with the host (`copilotSavedName`), keeping its id; the editors' own remembered so is renamed in place, still hidden if it was (`copilotRenamed`). Either rename takes the account's per-account settings in `providers.json` (proxy, models, cap, concurrency) from the bare login to the new name before the new name is written to `logins.json` (`renameSettled`): a setting the new name has already stays, and while `providers.json` can't be read or written the account stays saved under its old name, its settings with it. `magpie accounts switch|forget copilot` takes the full name, the login with `--host <name>.ghe.com`, or the login alone when it names one account.
|
||||
- A Codex account is known by its email, with the plan for a workspace seat (`codexUser`), and a second seat of one email in another workspace by that name with its workspace after it (`codexName`): `me@example.com · Team · ws-two`. The account Codex is signed in to goes by the name it is saved under (`codexLiveName`) everywhere: its provider's `Account.User` (`codexAccount`), `liveLogin`, and its session identity (`SessionIdentities`). Its allowance, rests and sticks (`restKey`), cap, proxy, model list, a gateway key's account list (`LoginID`) and the Routing page all go by that name, so two seats of one email are never read as one (#1424).
|
||||
|
||||
@@ -690,9 +690,11 @@ func openURLInBrowser(url string) {
|
||||
}
|
||||
|
||||
// refreshAccounts: `magpie accounts refresh` renews the sign-in of every
|
||||
// saved Claude and ChatGPT account the agent isn't signed in to now — what
|
||||
// the gateway does once a day, for a magpie that isn't left running (cron,
|
||||
// launchd). A sign-in the vendor refuses is marked as needing a new one.
|
||||
// saved ChatGPT account, and every saved Claude account that is on (Claude
|
||||
// Code's /usage run in its own directory), the agent isn't signed in to
|
||||
// now — what the gateway does every eight days, for a magpie that isn't
|
||||
// left running (cron, launchd). A sign-in the vendor refuses is marked as
|
||||
// needing a new one.
|
||||
func refreshAccounts(asJSON bool) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
@@ -97,7 +97,7 @@ const usage = `magpie — one place to pick every agent's model
|
||||
magpie accounts discovery [on|off] read agents' sign-ins (default on; MAGPIE_NO_ACCOUNTS=1 disables too)
|
||||
magpie accounts import <codex|claude|antigravity|factory> <file>... [--yes] bring in accounts from other tools' files (Codex's auth.json, Cockpit Tools, CLIProxyAPI, Sub2API); the tool a ChatGPT or Claude file came from is signed out of it
|
||||
magpie accounts switch <agent> <email> sign the agent in to another of them
|
||||
magpie accounts refresh renew the saved ChatGPT sign-ins now (the gateway does it daily)
|
||||
magpie accounts refresh renew the saved ChatGPT sign-ins, and the Claude ones that are on, now (the gateway does it every 8 days)
|
||||
magpie accounts checkin WorkBuddy's daily check-in (签到) for each WorkBuddy account, now (Settings can do it daily)
|
||||
magpie accounts project <gemini|antigravity> <email> <project> the Google Cloud project a Google account's requests go to
|
||||
magpie accounts alias <agent> <email> [<name>|--clear] a name of your own for an account, shown on its card, in the menu bar and in magpie quota
|
||||
|
||||
@@ -8,9 +8,19 @@ package provider
|
||||
// it's needed. So whichever magpie runs the gateway renews each such
|
||||
// sign-in about as often as Codex renews its own: every eight days, one
|
||||
// account an hour, never all of them at once. The account the agent is signed in to is the
|
||||
// agent's own to refresh, and is left to it. A Claude account is never
|
||||
// refreshed by magpie: Claude Code refreshes it, whenever it runs on it
|
||||
// (claude_dirs.go).
|
||||
// agent's own to refresh, and is left to it.
|
||||
//
|
||||
// A saved Claude account that is on goes the same way, but is never
|
||||
// refreshed by magpie, nor is Anthropic asked anything with its sign-in
|
||||
// (9bf6d4352): Claude Code refreshes it whenever it runs on it
|
||||
// (claude_dirs.go), so the keep-alive runs Claude Code's /usage once in the
|
||||
// account's own config directory, as its card's refresh does, and keeps
|
||||
// what Claude Code made of the sign-in (syncSavedClaude). One switched on
|
||||
// to stand behind Claude Code's own but not needed for weeks
|
||||
// (switch-only) otherwise had nothing run on it, and was found signed out
|
||||
// only when the switch reached it (#4532). How long Anthropic keeps an idle
|
||||
// Claude refresh token isn't known, so it is run as often as a ChatGPT
|
||||
// one is renewed, every eight days.
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -46,9 +56,9 @@ type Renewal struct {
|
||||
Lapsed bool `json:"lapsed,omitempty"`
|
||||
}
|
||||
|
||||
// RenewLogins renews the saved ChatGPT sign-ins not in the
|
||||
// agent's hands that have gone every or longer without it; every 0 renews
|
||||
// all of them.
|
||||
// RenewLogins renews the saved ChatGPT sign-ins, and the saved Claude
|
||||
// sign-ins that are on, not in the agent's hands that have gone every or
|
||||
// longer without it; every 0 renews all of them.
|
||||
func RenewLogins(ctx context.Context, every time.Duration) []Renewal {
|
||||
return renewLogins(ctx, every, 0)
|
||||
}
|
||||
@@ -73,7 +83,7 @@ func renewLogins(ctx context.Context, every time.Duration, atOnce int) []Renewal
|
||||
out := []Renewal{}
|
||||
tried := 0
|
||||
for _, l := range ls {
|
||||
if l.Agent != "codex" || strings.EqualFold(active[l.Agent], l.User) || leftAlone(l.Agent) {
|
||||
if l.Agent != "codex" && !claudeKeptAlive(l) || strings.EqualFold(active[l.Agent], l.User) || leftAlone(l.Agent) {
|
||||
continue
|
||||
}
|
||||
r := Renewal{Agent: l.Agent, User: l.User}
|
||||
@@ -82,6 +92,10 @@ func renewLogins(ctx context.Context, every time.Duration, atOnce int) []Renewal
|
||||
continue
|
||||
}
|
||||
tried++
|
||||
if l.Agent == "claude" {
|
||||
out = append(out, renewClaude(ctx, r))
|
||||
continue
|
||||
}
|
||||
why := ctx
|
||||
if every > 0 {
|
||||
why = refreshingFor(ctx, codexRefreshScheduled)
|
||||
@@ -98,6 +112,56 @@ func renewLogins(ctx context.Context, every time.Duration, atOnce int) []Renewal
|
||||
return out
|
||||
}
|
||||
|
||||
// claudeKeptAlive says the saved Claude login l is one the keep-alive
|
||||
// runs Claude Code on: switched on, with a sign-in Claude Code refreshes
|
||||
// (a setup-token has none, and lasts a year), and not signed out already,
|
||||
// which its row says.
|
||||
func claudeKeptAlive(l savedLogin) bool {
|
||||
return l.Agent == "claude" && l.On && claudeSetupToken(l.Auth) == "" && claudeSignedOut(l) == ""
|
||||
}
|
||||
|
||||
// renewClaude has Claude Code keep the saved Claude account r.User signed
|
||||
// in: its /usage run once in the account's config directory, where Claude
|
||||
// Code refreshes the sign-in when it is due, and what it made of it kept in
|
||||
// logins.json. A run that went through is the account's renewal, refreshed
|
||||
// or not (a sign-in Claude Code needn't refresh yet is a live one); one
|
||||
// whose sign-in Claude Code was refused is marked as needing a new one, on
|
||||
// the account's row, as a request's refusal is (NoteClaudeSignInFailure).
|
||||
func renewClaude(ctx context.Context, r Renewal) Renewal {
|
||||
version := ClaudeLoginVersion(r.User)
|
||||
_, err := readSavedClaudeUsage(ctx, r.User)
|
||||
if err != nil {
|
||||
NoteClaudeSignInFailure(r.User, version, err.Error())
|
||||
}
|
||||
loginsMu.Lock()
|
||||
defer loginsMu.Unlock()
|
||||
ls := readLogins()
|
||||
for i := range ls {
|
||||
if ls[i].Agent != "claude" || !strings.EqualFold(ls[i].User, r.User) {
|
||||
continue
|
||||
}
|
||||
if why := claudeSignedOut(ls[i]); why != "" {
|
||||
r.Lapsed, r.Err = true, why
|
||||
return r
|
||||
}
|
||||
if err != nil {
|
||||
r.Err = err.Error()
|
||||
return r
|
||||
}
|
||||
ls[i].Renewed = time.Now().UTC().Truncate(time.Second)
|
||||
if werr := writeLogins(ls); werr != nil {
|
||||
r.Err = werr.Error()
|
||||
return r
|
||||
}
|
||||
r.Renewed = true
|
||||
return r
|
||||
}
|
||||
if err != nil {
|
||||
r.Err = err.Error()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// renewalDue says whether a saved sign-in has gone every without being
|
||||
// renewed — since magpie last renewed it, or since it was last seen in the
|
||||
// agent's hands, which kept it fresh — or its refresh token is near its end.
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// #4532: a saved Claude account switched on but not needed for weeks
|
||||
// (switch-only) had nothing run on it, and its refresh token sat idle
|
||||
// until it was found signed out. The keep-alive has Claude Code itself
|
||||
// run on it, in its own config directory, and keeps what Claude Code made
|
||||
// of the sign-in; magpie asks Anthropic nothing (fakeClaudeUsage fails a
|
||||
// test that does).
|
||||
func TestRenewLoginsKeepsSavedClaudeSignedIn(t *testing.T) {
|
||||
claudeThree(t) // team@ and max@ saved, Claude Code on pro@
|
||||
var text atomic.Value
|
||||
text.Store("Current week (all models): 1% used · resets Nov 6 at 11am (Asia/Dubai)")
|
||||
fakeClaudeUsage(t, &text, nil)
|
||||
var mu sync.Mutex
|
||||
var dirs []string
|
||||
var fail atomic.Value
|
||||
fail.Store("")
|
||||
rotate := atomic.Bool{}
|
||||
UsageClaudeVia(func(_ context.Context, dir string) (string, error) {
|
||||
mu.Lock()
|
||||
dirs = append(dirs, dir)
|
||||
mu.Unlock()
|
||||
if msg := fail.Load().(string); msg != "" {
|
||||
return "", errors.New(msg)
|
||||
}
|
||||
if rotate.Load() {
|
||||
// as Claude Code refreshes a sign-in that is due
|
||||
writeFile(t, filepath.Join(dir, ".credentials.json"), map[string]any{"claudeAiOauth": map[string]any{
|
||||
"accessToken": "tok-max-2", "refreshToken": "r-max-2",
|
||||
"expiresAt": time.Now().Add(8 * time.Hour).UnixMilli(), "subscriptionType": "max"}})
|
||||
}
|
||||
return text.Load().(string), nil
|
||||
})
|
||||
runs := func() []string {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
out := slices.Clone(dirs)
|
||||
dirs = nil
|
||||
return out
|
||||
}
|
||||
max, team := "max@x.com", "team@x.com · Acme"
|
||||
if err := SetLoginOn("claude", max, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
saved := func(user string) savedLogin {
|
||||
for _, l := range readLogins() {
|
||||
if l.Agent == "claude" && l.User == user {
|
||||
return l
|
||||
}
|
||||
}
|
||||
t.Fatalf("%s isn't saved", user)
|
||||
return savedLogin{}
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
// seen in Claude Code's hands a moment ago: not due, nothing run
|
||||
if rs := RenewLogins(ctx, keepAliveEvery); len(rs) != 1 || rs[0].User != max || rs[0].Renewed || len(runs()) != 0 {
|
||||
t.Fatalf("renewed what wasn't due: %+v", rs)
|
||||
}
|
||||
|
||||
// due: Claude Code runs once, in max@'s own directory; team@, off,
|
||||
// and pro@, Claude Code's own, are left alone
|
||||
rotate.Store(true)
|
||||
rs := RenewLogins(ctx, 0)
|
||||
if len(rs) != 1 || rs[0].Agent != "claude" || rs[0].User != max || !rs[0].Renewed || rs[0].Err != "" || rs[0].Lapsed {
|
||||
t.Fatalf("renew: %+v", rs)
|
||||
}
|
||||
if got := runs(); len(got) != 1 || got[0] != claudeAccountDir(max) {
|
||||
t.Fatalf("Claude Code ran in %q, want only %q", got, claudeAccountDir(max))
|
||||
}
|
||||
l := saved(max)
|
||||
if c, _ := parseClaudeCredentials(l.Auth); c.OAuth.RefreshToken != "r-max-2" {
|
||||
t.Fatalf("the sign-in Claude Code refreshed isn't kept: %+v", c.OAuth)
|
||||
}
|
||||
if time.Since(l.Renewed) > time.Minute || renewalDue(l, keepAliveEvery, time.Now()) || !renewalDue(l, keepAliveEvery, time.Now().Add(keepAliveEvery+time.Hour)) {
|
||||
t.Fatalf("renewed at %v", l.Renewed)
|
||||
}
|
||||
if c, _ := parseClaudeCredentials(saved(team).Auth); c.OAuth.RefreshToken != "r-team@x.com" {
|
||||
t.Fatalf("team@, switched off, changed: %+v", c.OAuth)
|
||||
}
|
||||
if c, _ := parseClaudeCredentials(saved("pro@x.com").Auth); c.OAuth.RefreshToken != "r-pro@x.com" {
|
||||
t.Fatalf("Claude Code's own account changed: %+v", c.OAuth)
|
||||
}
|
||||
|
||||
// a sign-in Claude Code needn't refresh yet: the run is the renewal,
|
||||
// so the account isn't run again every hour
|
||||
rotate.Store(false)
|
||||
if rs := RenewLogins(ctx, 0); !rs[0].Renewed || len(runs()) != 1 {
|
||||
t.Fatalf("a live sign-in: %+v", rs)
|
||||
}
|
||||
if rs := RenewLogins(ctx, keepAliveEvery); rs[0].Renewed || len(runs()) != 0 {
|
||||
t.Fatalf("run again within its eight days: %+v", rs)
|
||||
}
|
||||
|
||||
// no answer: it may yet renew, so nothing is marked
|
||||
fail.Store("Claude Code: network is unreachable")
|
||||
if rs := RenewLogins(ctx, 0); rs[0].Err == "" || rs[0].Lapsed || rs[0].Renewed || saved(max).Lapsed != "" {
|
||||
t.Fatalf("a network failure lapsed it: %+v", rs)
|
||||
}
|
||||
runs()
|
||||
|
||||
// refused: the account's row says to sign in again, as a request's
|
||||
// refusal does, and the keep-alive runs nothing on it after
|
||||
fail.Store("Claude Code: OAuth session expired and could not be refreshed. Please run /login.")
|
||||
rs = RenewLogins(ctx, 0)
|
||||
if len(rs) != 1 || !rs[0].Lapsed || rs[0].Renewed || !strings.Contains(rs[0].Err, "sign in again") {
|
||||
t.Fatalf("refused: %+v", rs)
|
||||
}
|
||||
for _, lg := range Logins("claude") {
|
||||
if (lg.User == max) != (lg.Lapsed != "") {
|
||||
t.Fatalf("lapsed: %+v", lg)
|
||||
}
|
||||
}
|
||||
runs()
|
||||
if rs := RenewLogins(ctx, 0); len(rs) != 0 || len(runs()) != 0 {
|
||||
t.Fatalf("a signed-out account was run again: %+v", rs)
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code empties the sign-in in its directory when Anthropic refuses
|
||||
// its refresh, and says no more of it: that too is the account signed out.
|
||||
func TestRenewLoginsClaudeEmptiedSignIn(t *testing.T) {
|
||||
claudeThree(t)
|
||||
var text atomic.Value
|
||||
text.Store("Current week (all models): 1% used")
|
||||
fakeClaudeUsage(t, &text, nil)
|
||||
max := "max@x.com"
|
||||
UsageClaudeVia(func(_ context.Context, dir string) (string, error) {
|
||||
writeFile(t, filepath.Join(dir, ".credentials.json"), map[string]any{"claudeAiOauth": map[string]any{
|
||||
"accessToken": "", "refreshToken": "", "expiresAt": 0}})
|
||||
return "", errors.New("Claude Code: Not logged in · Please run /login")
|
||||
})
|
||||
if err := SetLoginOn("claude", max, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rs := RenewLogins(context.Background(), 0)
|
||||
if len(rs) != 1 || !rs[0].Lapsed || rs[0].Renewed {
|
||||
t.Fatalf("emptied: %+v", rs)
|
||||
}
|
||||
for _, lg := range Logins("claude") {
|
||||
if lg.User == max && lg.Lapsed == "" {
|
||||
t.Fatalf("not marked: %+v", lg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A Claude account added by setup-token has no refresh token to keep, and
|
||||
// one switched off isn't run on: only the accounts on are.
|
||||
func TestRenewLoginsLeavesClaudeSetupToken(t *testing.T) {
|
||||
l := savedLogin{Agent: "claude", User: "ci@x.com", On: true, Auth: []byte(`{"setupToken":"sk-ant-oat01-x"}`)}
|
||||
if claudeKeptAlive(l) {
|
||||
t.Fatal("a setup-token account is kept alive")
|
||||
}
|
||||
signedIn := []byte(`{"claudeAiOauth":{"accessToken":"a","refreshToken":"r"}}`)
|
||||
if claudeKeptAlive(savedLogin{Agent: "claude", User: "x", Auth: signedIn}) {
|
||||
t.Fatal("an account switched off is kept alive")
|
||||
}
|
||||
if !claudeKeptAlive(savedLogin{Agent: "claude", User: "x", On: true, Auth: signedIn}) {
|
||||
t.Fatal("an account switched on isn't kept alive")
|
||||
}
|
||||
}
|
||||
@@ -102,8 +102,10 @@ type savedLogin struct {
|
||||
// Project is the Google Cloud project a Gemini CLI or Antigravity
|
||||
// account's requests go to, when the user named one (google.go).
|
||||
Project string `json:"project,omitempty"`
|
||||
// Renewed is when magpie last refreshed a saved account's sign-in, and
|
||||
// Lapsed why the vendor last refused to (logins_on.go, keepalive.go).
|
||||
// Renewed is when magpie last refreshed a saved account's sign-in (a
|
||||
// Claude one: when Claude Code last refreshed it, or ran on it for the
|
||||
// keep-alive), and Lapsed why the vendor last refused to (logins_on.go,
|
||||
// keepalive.go).
|
||||
Renewed time.Time `json:"renewed,omitzero"`
|
||||
Lapsed string `json:"lapsed,omitempty"`
|
||||
// Refused is the Claude credential Anthropic refused (its version, in
|
||||
|
||||
Reference in New Issue
Block a user