koohoko5924 (v0.1.3057): after a warm-up the card said "just updated"
over windows read before it. claudeWarmedNow cleared the kept
reading's time (e.at) and kept its windows, so claudeReadAt gave nil
and readNow stamped them now. Nothing read the account again either:
claudeWindows' next read waits on when /usage last ran (e.tried),
which the warm-up left alone.
claudeWarmedNow now clears e.tried, which makes the next read due as
for an account never read, and keeps e.at, so until that read the card
shows the reading's own time. Codex's warmed hook only drops its cache
(its usage is a fresh API read), so it has no such sibling.
Run (mote, at this tree): go test ./internal/provider -run
'TestClaudeWarmedReadAgain|Warm|Claude' ok; gofmt -l clean.
Broken by hand: on the old claudeWarmedNow TestClaudeWarmedReadAgain is
red ("the kept reading's time is <nil>"); keeping e.at but not clearing
e.tried, red ("warmed, not read again", the 10% reading kept).
Not tried: a real warm-up on a real Claude account.
88 lines
3.4 KiB
Go
88 lines
3.4 KiB
Go
package provider
|
|
|
|
// Starting a Claude account's next window as soon as the last one resets,
|
|
// as codex_warmup.go does a ChatGPT account's: a Claude window, the 5-hour
|
|
// one and the weekly one, starts at the account's first request after it
|
|
// resets. The request goes through Claude Code itself, as the gateway's
|
|
// Claude Subscription does, so it is Claude Code's own; the gateway, which
|
|
// runs Claude Code, says how.
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func claudeWarmPath() string { return filepath.Join(filepath.Dir(Path()), "claude-warmup.json") }
|
|
|
|
// claudeWarmUsage is each Claude account's windows, the cached reads the
|
|
// Usage page shares; an account whose sign-in lapsed is left out.
|
|
func claudeWarmUsage(ctx context.Context) map[string]SubscriptionQuota {
|
|
u := LoginUsage(ctx, "claude")
|
|
for _, l := range Logins("claude") {
|
|
if l.Lapsed != "" {
|
|
delete(u, l.User)
|
|
}
|
|
}
|
|
return u
|
|
}
|
|
|
|
// warmClaudeLogin is a warm-up's send for the Claude accounts: send asks
|
|
// Claude Code, with configDir the config directory of a saved account
|
|
// (claude_dirs.go), "" for the one Claude Code is signed in to.
|
|
func warmClaudeLogin(send func(ctx context.Context, configDir string) error) func(context.Context, string) error {
|
|
return func(ctx context.Context, user string) error {
|
|
// the account's own proxy, which send's Claude Code runs with too
|
|
ctx = ViaLogin(ctx, "claude", user)
|
|
ls := Logins("claude")
|
|
i := slices.IndexFunc(ls, func(l Login) bool { return strings.EqualFold(l.User, user) })
|
|
if i < 0 {
|
|
return fmt.Errorf("no Claude account %q", user)
|
|
}
|
|
if !ls[i].Active {
|
|
return inSavedClaudeDir(ls[i].User, func(dir string) error { return send(ctx, dir) })
|
|
}
|
|
return send(ctx, "")
|
|
}
|
|
}
|
|
|
|
// claudeWindowsExpected is the windows every Claude account has: Anthropic
|
|
// leaves them out (null) for an account never used.
|
|
var claudeWindowsExpected = []QuotaWindow{{Name: "5 hours", Span: 5 * time.Hour}, {Name: "7 days", Span: 7 * 24 * time.Hour}}
|
|
|
|
// claudeWarmedNow has what the Claude account user has left read again,
|
|
// not taken from the last few minutes' read: the window a warm-up started
|
|
// has its reset known, and routing ranks the account by it at once. The
|
|
// next read is due as for an account never read (claudeWindows), and what
|
|
// is kept keeps its own time until then: cleared, the card stamped the
|
|
// windows read before the warm-up as read just now, and no read was due
|
|
// (koohoko5924).
|
|
func claudeWarmedNow(user string) {
|
|
key := strings.ToLower(user)
|
|
c := &claudeUsage
|
|
c.Lock()
|
|
if e, ok := c.m[key]; ok {
|
|
e.tried = time.Time{}
|
|
c.m[key] = e
|
|
}
|
|
c.Unlock()
|
|
StaleAllowance("claude", user)
|
|
}
|
|
|
|
// ClaudeWarmed is when magpie last started a window of each Claude account,
|
|
// by user as kept (lower-cased).
|
|
func ClaudeWarmed() map[string]time.Time { return codexWarmedIn(claudeWarmPath()) }
|
|
|
|
// KeepClaudeWindowsWarm starts the Claude accounts' windows as they reset,
|
|
// while settings say to, each with one request send makes.
|
|
func KeepClaudeWindowsWarm(ctx context.Context, send func(ctx context.Context, configDir string) error) {
|
|
w := codexWarmer{path: claudeWarmPath(), now: time.Now, usage: claudeWarmUsage, send: warmClaudeLogin(send),
|
|
expect: claudeWindowsExpected, warmed: claudeWarmedNow}
|
|
keepWarm(ctx, "claude", w, func() (string, []string) { s := settings.Load(); return s.ClaudeWarmup, s.ClaudeWarmAts })
|
|
}
|