provider: the switch off a spent Claude account reads saved accounts it has no reading of (cn_Vincentzyx on X)
cn_Vincentzyx on X: Claude Code not connected to magpie, only its
sign-in switched ("Only switch Claude Code's sign-in"), the accounts in
the order Settings' Make first gave them. The account Claude Code was on
ran out and it was never moved. ef2b2d1e3 (#1907) let switch-only move
a sign-in kept on one account; this is the other half.
Cause: NextLogin moves on only to a spare whose allowance is known and
not spent. A saved (not signed-in) Claude account's reading comes from
claudeWindows(ctx, user, false), which runs Claude Code's /usage for it
only on a card refresh (savedRefresh) and otherwise returns what was
kept, or errClaudeSaved when nothing was. Under switch-only no request
of a saved account passes the gateway (KeptToAgent), so
NoteClaudeLimits never hears of it either. A spare Claude Code wasn't
signed in to since (and with no reading on disk in last_quotas) was
"not read yet" on every look, and the switch never found one with
room.
Now, once the account Claude Code is on is spent, NextLogin reads such
a spare (no reading, or a read that failed for a reason other than the
account's own) through readSpare: the existing saved-account path,
Claude Code's own /usage run in the account's directory
(readSavedClaudeUsage), the same run a card refresh makes. Spares are
taken in order, one at a time, and reading stops at the first with
room. claudeWindows lets this read through (spareRead) only while it
has no windows for the account: once, then again only after
claudeUsageWait (5-15 min) while its read keeps failing, and never
under the 30s floor or while allowances are read only when asked
(holding). Nothing about how Claude Code is run, or the requests'
entrypoint, is changed.
Codex: its saved spares are read through their saved tokens
(savedLoginToken + wham/usage) in the same someLoginUsage call, with no
seen-in-use condition, so it had no such gap
(TestCodexSwitchOnlyStillSwitchesTheSignIn covers a spare magpie never
read before).
Run (mote, at this tree, rebased on c941e0877):
- TestClaudeSwitchOnlyReadsUnreadSpares (new) green, -count=3.
- go test ./internal/provider -count=1 -timeout 45m: ok (129s)
- gofmt -l clean; go vet ./internal/provider for darwin, linux, windows.
Broken by hand:
- NextLogin's readSpare call disabled: the new test is red
(got ""→"" ok=false, the old behaviour).
- claudeWindows' spare gate reduced to spareRead(ctx) alone (no
ws/wait check): red, "a spare run again on the next look".
Not tried: a real account switch with the reporter's accounts; this was
run in sandbox HOMEs with a stand-in for Claude Code's /usage only. With
Settings > Usage > Allowance reads at "When I ask", nothing is read in
the background, the switch included, as before: an unread spare stays
unknown there.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -202,7 +202,8 @@ func usedPast(q SubscriptionQuota, share float64, now time.Time) bool {
|
||||
// one it is on: the account magpie moved it off, once that has room again
|
||||
// (back); else, when the one it is on is spent, the first of its other
|
||||
// accounts that are on in magpie, in the order they were saved, whose
|
||||
// allowance is known and not spent. ok is false when the agent should stay.
|
||||
// allowance is known and not spent. A saved Claude account not read yet is
|
||||
// read then (readSpare). ok is false when the agent should stay.
|
||||
func NextLogin(ctx context.Context, agent string) (from, to string, back, ok bool) {
|
||||
if leftAlone(agent) {
|
||||
return "", "", false, false // magpie has no say in its sign-in, or was told to leave it be
|
||||
@@ -280,13 +281,36 @@ func NextLogin(ctx context.Context, agent string) (from, to string, back, ok boo
|
||||
u[user] = q
|
||||
}
|
||||
for _, l := range spares {
|
||||
if q, known := u[l.User]; known && q.Error == "" && !q.Held && !usedPast(q, share, now) && !capped(l.User, q) {
|
||||
q, known := u[l.User]
|
||||
if agent == "claude" && (!known || unreadSpare(q)) {
|
||||
// a saved Claude account magpie has no reading of: read now,
|
||||
// by Claude Code's /usage in its own directory, one account at
|
||||
// a time and only till one has room
|
||||
q, known = readSpare(ctx, l)
|
||||
}
|
||||
if known && q.Error == "" && !q.Held && !usedPast(q, share, now) && !capped(l.User, q) {
|
||||
return from, l.User, false, true
|
||||
}
|
||||
}
|
||||
return "", "", false, false
|
||||
}
|
||||
|
||||
// unreadSpare says q, a saved Claude account's reading, has nothing to go
|
||||
// by: never read (a /usage not run), or a read that failed for a reason
|
||||
// not the account's own. Its /usage is then run (readSpare).
|
||||
func unreadSpare(q SubscriptionQuota) bool {
|
||||
return q.Error != "" && (readDeferred(q.Error) || !claudeUsageDenied.MatchString(q.Error))
|
||||
}
|
||||
|
||||
// readSpare is the reading of the saved Claude account l, its /usage run
|
||||
// now when claudeWindows allows (spareRead: once, then again only after a
|
||||
// wait), rather than the minute's cached "not read yet".
|
||||
func readSpare(ctx context.Context, l Login) (SubscriptionQuota, bool) {
|
||||
forgetLoginReading(l)
|
||||
q, known := someLoginUsage(readingSpare(ctx), l.Agent, l.User)[l.User]
|
||||
return q, known
|
||||
}
|
||||
|
||||
// SwitchWhenSpent signs agent in to the next of its accounts when the one
|
||||
// it is on is spent, or back to the one it moved off once that has room
|
||||
// (NextLogin), and answers the account it signed it in to, "" when it
|
||||
|
||||
@@ -781,10 +781,11 @@ func AskUsage() {
|
||||
// Claude Code's own /usage. The account Claude Code is signed in to
|
||||
// (active) is read when the user asked since it last was, or when the last
|
||||
// reading's claudeUsageWait is up and Claude Code was used since. A saved
|
||||
// account is read only when its card is refreshed (savedRefresh), by Claude
|
||||
// Code run in the account's own config directory (readSavedClaudeUsage),
|
||||
// Claude Code's own sign-in left as it is (#1630). Any other time it is
|
||||
// what was kept.
|
||||
// account is read only when its card is refreshed (savedRefresh), or when
|
||||
// the switch off a spent account has no reading of it (spareRead), by
|
||||
// Claude Code run in the account's own config directory
|
||||
// (readSavedClaudeUsage), Claude Code's own sign-in left as it is (#1630).
|
||||
// Any other time it is what was kept.
|
||||
// magpie itself never asks Anthropic.
|
||||
func claudeWindows(ctx context.Context, user string, active bool) ([]QuotaWindow, error) {
|
||||
key := strings.ToLower(user)
|
||||
@@ -796,8 +797,12 @@ func claudeWindows(ctx context.Context, user string, active bool) ([]QuotaWindow
|
||||
c.Unlock()
|
||||
due := e.tried.IsZero() || claudeAsked.Load() != 0 && asked > e.asked ||
|
||||
active && now.Sub(e.tried) >= e.wait && (e.heard.After(e.tried) || claudeUsedSince(e.tried))
|
||||
// a saved account the switch off a spent one has no reading of
|
||||
// (spareRead): read once, and again only after a wait while its read
|
||||
// keeps failing, never every look
|
||||
spare := !active && spareRead(ctx) && e.ws == nil && (e.tried.IsZero() || now.Sub(e.tried) >= e.wait)
|
||||
// nor, with allowances read only when asked, one nobody asked for
|
||||
read := (active || savedRefresh(ctx, user)) && due && now.Sub(e.tried) >= claudeAskFloor && !holding(ctx)
|
||||
read := ((active || savedRefresh(ctx, user)) && due || spare) && now.Sub(e.tried) >= claudeAskFloor && !holding(ctx)
|
||||
c.Lock()
|
||||
// one reading an ask or a wait, its first caller's; the others keep
|
||||
// to it
|
||||
@@ -896,6 +901,24 @@ func savedRefresh(ctx context.Context, user string) bool {
|
||||
return ok && wasAsked(ctx) && r.wants("claude", user)
|
||||
}
|
||||
|
||||
type spareReadKey struct{}
|
||||
|
||||
// readingSpare is ctx for the switch off a spent account (NextLogin)
|
||||
// reading a saved Claude account it has nothing on: with Claude Code kept
|
||||
// to its own sign-in (switch-only), no request of a saved account passes
|
||||
// magpie to tell its allowance, and one never signed in to in Claude Code
|
||||
// since had no reading at all, so the switch found no account with room
|
||||
// and never moved (cn_Vincentzyx on X).
|
||||
func readingSpare(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, spareReadKey{}, true)
|
||||
}
|
||||
|
||||
// spareRead says ctx is readingSpare's.
|
||||
func spareRead(ctx context.Context) bool {
|
||||
b, _ := ctx.Value(spareReadKey{}).(bool)
|
||||
return b
|
||||
}
|
||||
|
||||
// elapsed is ws as of now: a window that has reset since it was read
|
||||
// starts again from nothing.
|
||||
func elapsed(ws []QuotaWindow, now time.Time) []QuotaWindow {
|
||||
|
||||
@@ -3,12 +3,15 @@ package provider
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -342,3 +345,107 @@ func TestClaudeSwitchOnlyMovesASignInKeptOnOne(t *testing.T) {
|
||||
t.Fatalf("Claude Code is signed in with %v", o)
|
||||
}
|
||||
}
|
||||
|
||||
// cn_Vincentzyx on X: Claude Code not connected to magpie, only its
|
||||
// sign-in switched, the accounts in the order Make first gave them. The
|
||||
// account Claude Code was on ran out and it stayed there: no request of a
|
||||
// saved account passes magpie then, so one Claude Code wasn't signed in
|
||||
// to since had no reading, and the switch counted no account known to
|
||||
// have room. A saved account with no reading is read by Claude Code's own
|
||||
// /usage, in its own directory, in order and only till one has room, and
|
||||
// one read isn't read again on the next look.
|
||||
func TestClaudeSwitchOnlyReadsUnreadSpares(t *testing.T) {
|
||||
claudeThree(t) // pro@x.com signed in; team and max saved
|
||||
var text atomic.Value
|
||||
text.Store("Current session: 100% used")
|
||||
runs := fakeClaudeUsage(t, &text, nil)
|
||||
forget := func() {
|
||||
lastQuotas.Lock()
|
||||
lastQuotas.m, lastQuotas.loaded = nil, false
|
||||
lastQuotas.Unlock()
|
||||
loginUsageCache.Lock()
|
||||
loginUsageCache.m = nil
|
||||
loginUsageCache.Unlock()
|
||||
}
|
||||
forget()
|
||||
t.Cleanup(forget)
|
||||
if err := SetSwitchOnly("claude", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var spares []string
|
||||
for _, l := range Logins("claude") {
|
||||
if !l.Active {
|
||||
if err := SetLoginOn("claude", l.User, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spares = append(spares, l.User)
|
||||
}
|
||||
}
|
||||
if len(spares) != 2 {
|
||||
t.Fatalf("spares: %v", spares)
|
||||
}
|
||||
// the first in line can't be read now (Claude Code failed), the
|
||||
// second has room
|
||||
used := map[string]string{"": "100", claudeAccountDir(spares[1]): "10"}
|
||||
ran := map[string]int{}
|
||||
var mu sync.Mutex
|
||||
UsageClaudeVia(func(_ context.Context, dir string) (string, error) {
|
||||
runs.Add(1)
|
||||
mu.Lock()
|
||||
ran[dir]++
|
||||
mu.Unlock()
|
||||
if used[dir] == "" {
|
||||
return "", errors.New("Claude Code: network is unreachable")
|
||||
}
|
||||
return "Current session: " + used[dir] + "% used", nil
|
||||
})
|
||||
for _, u := range spares {
|
||||
if q := LoginUsage(context.Background(), "claude")[u]; q.Error != errClaudeSaved.Error() {
|
||||
t.Fatalf("%s read before the switch looked: %+v", u, q)
|
||||
}
|
||||
}
|
||||
from, to, back, ok := NextLogin(context.Background(), "claude")
|
||||
if !ok || back || to != spares[1] || !strings.HasPrefix(from, "pro@x.com") {
|
||||
t.Fatalf("got %q→%q back=%v ok=%v, want on to %q", from, to, back, ok, spares[1])
|
||||
}
|
||||
mu.Lock()
|
||||
if ran[""] != 1 || ran[claudeAccountDir(spares[0])] != 1 || ran[claudeAccountDir(spares[1])] != 1 {
|
||||
t.Fatalf("/usage runs by directory: %v", ran)
|
||||
}
|
||||
mu.Unlock()
|
||||
// the next look, a few minutes on (the minute's cache gone, past the
|
||||
// 30s floor): the failed one isn't run again before its wait, the
|
||||
// one read isn't run again at all
|
||||
look := func() {
|
||||
t.Helper()
|
||||
loginUsageCache.Lock()
|
||||
loginUsageCache.m = nil
|
||||
loginUsageCache.Unlock()
|
||||
claudeUsage.Lock()
|
||||
for k, e := range claudeUsage.m {
|
||||
e.tried = e.tried.Add(-3 * time.Minute)
|
||||
claudeUsage.m[k] = e
|
||||
}
|
||||
claudeUsage.Unlock()
|
||||
if _, to, _, ok := NextLogin(context.Background(), "claude"); !ok || to != spares[1] {
|
||||
t.Fatalf("next look: to %q ok=%v", to, ok)
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
failed, read := ran[claudeAccountDir(spares[0])], ran[claudeAccountDir(spares[1])]
|
||||
mu.Unlock()
|
||||
look()
|
||||
mu.Lock()
|
||||
if ran[claudeAccountDir(spares[0])] != failed || ran[claudeAccountDir(spares[1])] != read {
|
||||
t.Fatalf("a spare run again on the next look: %v", ran)
|
||||
}
|
||||
mu.Unlock()
|
||||
// past its wait (usageTestWait), the one that failed is tried again
|
||||
look()
|
||||
look()
|
||||
mu.Lock()
|
||||
if ran[claudeAccountDir(spares[0])] != failed+1 || ran[claudeAccountDir(spares[1])] != read {
|
||||
t.Fatalf("after the wait: %v", ran)
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user