Files
magpie/internal/provider/warm_now.go
T
yetone 5bd41301b9 warm-up: All accounts warms every account in use at one press (CoderMaurice on X)
CoderMaurice on X: with several Claude accounts, Warm up now had to be
pressed once per account. With two or more accounts, the Warm up… menu on
the Warm up now row (a Claude Code or Codex provider's editor, the warm-up
section) now starts with All accounts. It sends POST /api/warm-all, and
provider.WarmAll sends Warm up now to every account that is in use (the
one the agent is on, or one turned on beside it) and whose windows aren't
all running already, going by the cached readings the Usage page shares.
It runs three at a time. An account that is turned off or paused, or
already in its windows, is sent nothing. An account whose sign-in lapsed
is sent nothing and is listed as failed, as Warm up now treats it. The
row's line lists the accounts warmed, the ones already in a window, and
each refusal with its reason (in red).
Accounts that are turned off aren't listed. A gateway rest is not read
(the provider package can't see it). An account rested for a used-up
window is still inside that window, so it is skipped as running.

"All accounts" moves from the usage plugin's i18n.js into core i18n.js,
since the core uses it now. It and two new strings are in zh, zh-TW, ja
and de. app.js stays inside its budget: the single-account and
all-accounts results now go through one formatter.

Run at a1968a0b5 (rebased on origin/main aecf27efb), on mica:
- go test -run 'WarmAll|WarmNow|CoreScriptBudget|Budget' ./internal/provider
  ./internal/gui: ok. New: TestWarmAllWarmsEachIdleAccountInUse,
  TestWarmAllOnlyCodexAndClaude, warm-all cases in TestWarmNowAPISaysWhy.
- Broken by hand: with the "running" skip removed from warmAll,
  TestWarmAllWarmsEachIdleAccountInUse FAILs (busy@ and spent@ warmed).
  With the All accounts item removed from the menu, warm-now.test.cjs
  FAILs ("Warm up all warms every account at one press" and the menu
  list assertion).
- GUI, Chromium and WebKit, en/zh/zh-TW/ja/de, 900px and 440px:
  warm-now, i18n-keys, i18n-en-no-cjk, usage-accounts: 54/54 pass. Before
  the rebase, plan-name-fits, provider-prefs-layout and quota-warm also
  passed (54/54 with usage-accounts).
- gofmt -l: clean. go vet, GOOS=linux go vet -tags nogui and
  GOOS=windows go vet of internal/provider and internal/gui: ok.
  go build, GOOS=linux go build -tags nogui, GOOS=windows go build: ok.

Not run: the full go test ./... and the full GUI suite. Both mote and
mica had other sessions' GUI suites running throughout, and this ships
now per the owner's ship-immediately rule. Both will run when a machine
is free. Not tried: a real Warm up all against several real Claude
accounts (the send per account is the unchanged WarmNow).
2026-11-15 11:00:37 +08:00

162 lines
5.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package provider
// Warm up now (a Windows user on Discord): one account's warm-up sent at
// once from Settings › Warm-up and check-in, whatever the warm-up settings
// say. It is the request the scheduled warm-up sends — warmCodexLogin for
// a ChatGPT account, Claude Code's own run (WarmClaudeVia) for a Claude
// one — to that account alone, and what it says back is the result. The
// scheduled warm-up's file is left to the scheduled one: its next look
// reads the window as started, from its own read.
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"sync"
"time"
)
// warmNowWait is how long Warm up now waits on its request, as long as
// the scheduled warm-up gives one look.
const warmNowWait = 2 * time.Minute
// claudeWarmSend is a Claude warm-up's send, Claude Code asked with
// configDir the config directory of a saved account, "" for the one it is
// signed in to. The gateway, which runs Claude Code, sets it (WarmClaudeVia).
var claudeWarmSend func(ctx context.Context, configDir string) error
// WarmClaudeVia sets how a Claude account's warm-up runs Claude Code.
func WarmClaudeVia(f func(ctx context.Context, configDir string) error) { claudeWarmSend = f }
// WarmNow sends agent's ("codex" or "claude") account user the warm-up's
// one tiny request now, and says why it failed. Once it went, what the
// account has left is read again, as after a scheduled warm-up.
func WarmNow(ctx context.Context, agent, user string) error {
var name string
switch agent {
case "codex":
name = "ChatGPT"
case "claude":
name = "Claude"
default:
return fmt.Errorf("%q has no warm-up", agent)
}
ls := Logins(agent)
i := slices.IndexFunc(ls, func(l Login) bool { return strings.EqualFold(l.User, user) })
if user == "" || i < 0 {
return fmt.Errorf("no %s account %q", name, user)
}
if ls[i].Lapsed != "" {
// as the scheduled warm-up leaves it out (codexWarmUsage)
return fmt.Errorf("%s needs signing in again: %s", ls[i].User, ls[i].Lapsed)
}
ctx, cancel := context.WithTimeout(ctx, warmNowWait)
defer cancel()
if agent == "codex" {
if err := warmCodexLogin(ctx, ls[i].User); err != nil {
return err
}
StaleAllowance("codex", ls[i].User)
return nil
}
if claudeWarmSend == nil {
return errors.New("Claude Code isn't available to send the warm-up")
}
if err := warmClaudeLogin(claudeWarmSend)(ctx, ls[i].User); err != nil {
return err
}
claudeWarmedNow(ls[i].User)
return nil
}
// WarmAllResult is what Warm up all came to for one account: Skip says why
// it was left alone ("off" or "running"), Err why its warm-up failed or
// couldn't be sent; both empty, it was warmed.
type WarmAllResult struct {
User string `json:"user"`
Skip string `json:"skip,omitempty"`
Err string `json:"error,omitempty"`
}
// warmAllAtOnce is how many of Warm up all's requests run together: each
// Claude one is a Claude Code run.
const warmAllAtOnce = 3
// WarmAll is Warm up now for each of agent's accounts at once
// (CoderMaurice on X: one press for several Claude accounts). It sends the
// warm-up to every account in use (the one the agent is on, or one turned
// on beside it) whose windows aren't all running already, as the cached
// reads the Usage page shares tell them. An account turned off or paused,
// and one already in its windows, are left alone and listed with why; one
// whose sign-in lapsed is sent nothing and failed, as Warm up now has it.
func WarmAll(ctx context.Context, agent string) ([]WarmAllResult, error) {
name := map[string]string{"codex": "ChatGPT", "claude": "Claude"}[agent]
if name == "" {
return nil, fmt.Errorf("%q has no warm-up", agent)
}
ls := Logins(agent)
if len(ls) == 0 {
return nil, fmt.Errorf("no %s account to warm up", name)
}
usage := LoginUsage(ctx, agent)
return warmAll(ctx, ls, usage, time.Now(), func(ctx context.Context, user string) error { return WarmNow(ctx, agent, user) }), nil
}
// warmAll is WarmAll on ls read as usage at now, each warm-up sent by send.
func warmAll(ctx context.Context, ls []Login, usage map[string]SubscriptionQuota, now time.Time, send func(context.Context, string) error) []WarmAllResult {
var out []WarmAllResult
var due []int
for _, l := range ls {
if l.User == "" || slices.ContainsFunc(out, func(r WarmAllResult) bool { return strings.EqualFold(r.User, l.User) }) {
continue
}
r := WarmAllResult{User: l.User}
switch {
case l.Lapsed != "":
// as Warm up now says of it
r.Err = "needs signing in again: " + l.Lapsed
case !l.Active && !l.On || l.Paused:
r.Skip = "off"
case windowsRunning(usage[l.User], now):
r.Skip = "running"
default:
due = append(due, len(out))
}
out = append(out, r)
}
var wg sync.WaitGroup
slots := make(chan struct{}, warmAllAtOnce)
for _, i := range due {
wg.Add(1)
go func() {
defer wg.Done()
slots <- struct{}{}
defer func() { <-slots }()
if err := send(ctx, out[i].User); err != nil {
out[i].Err = err.Error()
}
}()
}
wg.Wait()
return out
}
// windowsRunning says whether every one of q's own windows with a span is
// running as of now, so a warm-up would start none: false when none is
// known, as for an account never read.
func windowsRunning(q SubscriptionQuota, now time.Time) bool {
n := 0
for _, w := range q.Windows {
if w.Span <= 0 || w.Aside || w.Text || w.Model != "" || w.Lane != "" {
continue
}
if idle(asOf(w, now), now) {
return false
}
n++
}
return n > 0
}