The reporter starts magpie at login with the proxy on "follow system
proxy". magpie comes up before the proxy app. With no system proxy set
yet, netproxy.For gives none, so magpie's own allowance reads went out
direct, from the user's real IP. That reading was then kept for up to
15s. A system proxy that is set but not listening fails the request
("proxyconnect ... connection refused"); it never falls back to direct.
Proxy freshness:
- netproxy.System re-reads a "none" reading after 2s (noneKeep) instead
of 15s (systemKeep, unchanged for a set proxy).
- The plugin host looks again every 2s while it has no proxy
(proxyLookNone), so a host started before the proxy app takes it soon.
- Catalog sync, the update check, plugin updates and token refresh all go
through netproxy.Func / Dispatch, so they use the current proxy too.
Settings -> Allowances -> "Allowance reads": Automatic (default, as
before) or When I ask (settings.QuotaReads = "asked",
POST /api/settings/quota-reads):
- These count as asking (provider.Asked): the Usage page opened or
refreshed (?asked=1), a card's refresh (RefreshUsage), magpie quota,
magpie accounts, magpie quota wait and the TUI's quotas.
- Every other read (quotaReading -> holdUnasked) carries netproxy.Hold.
netproxy.Func, Dispatch's choice proxies and the plugin host's calls
refuse a held request before it leaves.
- The readers return the last reading, dated (AsOf), or "not read yet".
That covers routing's Allowances / AwaitAllowance, the used-up switch,
the menu bar, alerts, Quotas/KeyBalances/PlanQuotas, Codex/Claude/
Cursor/Grok/Kiro usage and KeyAllowance.
- An account refused for its quota (StaleAllowance) or a key refused
(StaleKeyAllowance) is still read, once.
- Warm-up and check-in are unchanged.
- Strings are added in zh, zh-TW, ja and de. The docs are updated:
providers-accounts (Allowance row), provider-plugins (host proxy).
Run:
- go test -tags nogui ./... (sandbox HOME), gofmt -l, and go vet -tags
nogui on GOOS=linux/windows/darwin for ., netproxy, plugin, settings,
provider, gui and tui.
- New tests, each seen red by breaking the change by hand:
- TestSystemProxySetAfterStartIsTakenSoon and TestHeldRequestNeverLeaves
(netproxy).
- TestHostWithNoProxyLooksAgainSoon (plugin; red with 15s).
- TestAllowancesReadOnlyWhenAsked (red without the hold and without
markStaleRead), TestAllowancesReadByThemselvesByDefault and
TestKeyReadsOnlyWhenAsked (red without holdUnasked and without
unheld for a refused key).
- GUI quota-reads.test.cjs in Chromium and WebKit, at 900 and 360px, in
en/zh/zh-TW/ja/de.
- i18n-keys, gui-ja, gui-de, settings-sections, settings-groups,
currency, number-units and tray-bird.
269 lines
8.0 KiB
Go
269 lines
8.0 KiB
Go
package provider
|
|
|
|
// A key's own usage windows, for routing, as Allowances is a subscription
|
|
// account's: a sub2api key its owner gave a 5-hour, day or 7-day limit
|
|
// tells what it has spent of each, as a plan bought with a key (Kimi
|
|
// Code, the GLM Coding Plan, MiniMax's, OpenCode Go, Command Code's:
|
|
// planquota.go) tells its plan's windows, and routing weighs and rests it
|
|
// by them (#1016). They are read behind the request, never waited for, and read
|
|
// again once a minute has passed; until the first reading is back, the
|
|
// one last shown on the key's card stands in for it. A reading that finds
|
|
// a window the key was out of no longer full — its limit raised in the
|
|
// panel, or its usage reset — tells OnRenewed's hooks, as a Codex reset
|
|
// spent does an account's, so what sat out for it comes back.
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var keyAllowances struct {
|
|
sync.Mutex
|
|
m map[string]*keyAllowance // provider id#key id
|
|
// gen is bumped as they are all forgotten: a reading begun before
|
|
// says nothing of the keys as they are now
|
|
gen int
|
|
}
|
|
|
|
type keyAllowance struct {
|
|
a Allowance
|
|
at time.Time // when read; zero to read again at the next ask
|
|
loading bool
|
|
// stale: made stale (StaleKeyAllowance) while a reading was out, which
|
|
// was asked before the key said it was out and is followed by another
|
|
stale bool
|
|
// refused: the key said it was out (StaleKeyAllowance), so it is read
|
|
// again even while allowances are read only when asked (#1518)
|
|
refused bool
|
|
// read is what routing last read of the key, for its card
|
|
// (withKeyReadings), which only the Usage page reads
|
|
read keyReading
|
|
}
|
|
|
|
// keyAllowanceAge is how long a key's windows as read go unasked again;
|
|
// keyNoWindowsAge one's that was read and has none — a pay-as-you-go key
|
|
// at a vendor that sells plans too — which is asked again only now and
|
|
// then, in case a plan was bought since.
|
|
const (
|
|
keyAllowanceAge = time.Minute
|
|
keyNoWindowsAge = 30 * time.Minute
|
|
)
|
|
|
|
// readsKeyWindows says p is a key whose own usage windows magpie reads:
|
|
// one whose Balance URL is a sub2api panel's query for a key, or one at a
|
|
// vendor whose keys tell their plan's windows (planQuotaSourceOf). Said
|
|
// without asking anything, so any other key costs nothing here.
|
|
func readsKeyWindows(p Provider) bool {
|
|
if p.Account != nil || p.Key == "" {
|
|
return false
|
|
}
|
|
if sub2APIKeyLimits(p) {
|
|
return true
|
|
}
|
|
_, ok := planQuotaSourceOf(p)
|
|
return ok
|
|
}
|
|
|
|
func keyAllowanceID(p Provider) string { return p.ID + "#" + keyID(p.Key) }
|
|
|
|
// KeyAllowanceID is what OnRenewed's hooks are told, as the user of agent
|
|
// "", of a key whose own windows magpie reads; empty for any other.
|
|
func KeyAllowanceID(p Provider) string {
|
|
if !readsKeyWindows(p) {
|
|
return ""
|
|
}
|
|
return keyAllowanceID(p)
|
|
}
|
|
|
|
// KeyAllowance is the provider's key in use's own usage windows as last
|
|
// read, asked for again in the background when that was over a minute
|
|
// ago; ok is false for a key without any, or one not read yet. It never
|
|
// waits: a key not read yet goes by the last reading on its card, else
|
|
// counts as not known.
|
|
func KeyAllowance(p Provider) (Allowance, bool) {
|
|
if !readsKeyWindows(p) {
|
|
return nil, false
|
|
}
|
|
id := keyAllowanceID(p)
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
_, had := c.m[id]
|
|
c.Unlock()
|
|
var seed Allowance
|
|
if !had {
|
|
seed = lastKeyAllowance(p) // read before the lock: it takes the cards'
|
|
}
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if c.m == nil {
|
|
c.m = map[string]*keyAllowance{}
|
|
}
|
|
e := c.m[id]
|
|
if e == nil {
|
|
e = &keyAllowance{a: seed}
|
|
c.m[id] = e
|
|
}
|
|
age := keyAllowanceAge
|
|
if len(e.a) == 0 && !e.at.IsZero() {
|
|
age = keyNoWindowsAge
|
|
}
|
|
// with allowances read only when asked, routing goes by what was read
|
|
// last till the user asks, or the key says it is out (#1518)
|
|
if !e.loading && time.Since(e.at) > age && (e.refused || !readsAsked()) {
|
|
e.loading = true
|
|
go readKeyAllowance(p, id, c.gen)
|
|
}
|
|
return e.a, len(e.a) > 0
|
|
}
|
|
|
|
// readKeyAllowance reads p's key's windows into the cache. A reading that
|
|
// fails keeps what was known, and is tried again a minute later: a failed
|
|
// read is not known, never empty.
|
|
func readKeyAllowance(p Provider, id string, gen int) {
|
|
ctx := context.Background()
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
if e := c.m[id]; e != nil && e.refused {
|
|
e.refused, ctx = false, unheld(ctx)
|
|
}
|
|
c.Unlock()
|
|
ctx, seq := quotaReading(ctx)
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
r, err := keyWindows(ctx, p)
|
|
ws := r.ws
|
|
now := time.Now()
|
|
c.Lock()
|
|
e := c.m[id]
|
|
if e == nil || c.gen != gen {
|
|
c.Unlock()
|
|
return // forgotten while it was read: read again
|
|
}
|
|
e.loading, e.at = false, now
|
|
renewed := err == nil && e.keep(allowanceOf(ws, now), SpentShareOf(p.Routing), now)
|
|
if err == nil {
|
|
r.at, r.seq = now, seq
|
|
e.read = r
|
|
}
|
|
if e.stale {
|
|
e.stale, e.loading, e.at = false, true, time.Time{}
|
|
go readKeyAllowance(p, id, gen)
|
|
}
|
|
c.Unlock()
|
|
if renewed {
|
|
tellRenewed("", id)
|
|
}
|
|
}
|
|
|
|
// keep has the key go by a, read at now, and says whether a window it
|
|
// was full in (at share, as routing counts it full) is full no more, or
|
|
// till sooner: its limit raised, its usage reset, or the window started
|
|
// again.
|
|
func (e *keyAllowance) keep(a Allowance, share float64, now time.Time) (renewed bool) {
|
|
was := e.a.Full("", share, now)
|
|
e.a, e.at = a, now
|
|
return was.After(a.Full("", share, now))
|
|
}
|
|
|
|
// keyWindows asks the vendor for p's key's windows, as the key's card
|
|
// is asked (KeyBalances, PlanQuotas): the provider as saved, with the key,
|
|
// rather than as a request has it, which leaves out the base URLs of the
|
|
// protocols a key isn't made for.
|
|
func keyWindows(ctx context.Context, p Provider) (keyReading, error) {
|
|
q := p
|
|
if saved, err := Find(p.ID); err == nil {
|
|
q = *saved
|
|
q.Key = p.Key
|
|
}
|
|
var r keyReading
|
|
var err error
|
|
if src, ok := planQuotaSourceOf(q); ok && !sub2APIKeyLimits(q) {
|
|
r.plan, r.ws, _, err = planKeyWindows(q.Via(ctx), q, src, q.Key)
|
|
return r, err
|
|
}
|
|
var parts []BalancePart
|
|
r.balance, parts, r.ws, _, err = balanceRead(ctx, q)
|
|
r.parts = cardParts(parts)
|
|
return r, err
|
|
}
|
|
|
|
// keyCardTag is what p's key's card is kept on disk by (keepLast): its
|
|
// plan's card, or its balance's.
|
|
func keyCardTag(p Provider) string {
|
|
if _, ok := planQuotaSourceOf(p); ok && !sub2APIKeyLimits(p) {
|
|
return keyTag("plan", p.Key)
|
|
}
|
|
return keyTag("balance", p.Key)
|
|
}
|
|
|
|
// lastKeyAllowance is p's key's windows as its card last read them, kept
|
|
// on disk (keepLast), each started again whose reset has passed.
|
|
func lastKeyAllowance(p Provider) Allowance {
|
|
c := &lastQuotas
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.load()
|
|
q, ok := c.reading(p.ID + "/" + strings.ToLower(keyCardTag(p)))
|
|
if !ok || len(q.Windows) == 0 {
|
|
return nil
|
|
}
|
|
return allowanceOf(q.Windows, time.Now())
|
|
}
|
|
|
|
// noteKeyAllowance keeps the windows the key's card just read, for
|
|
// routing to go by at once.
|
|
func noteKeyAllowance(p Provider, ws []QuotaWindow, now time.Time) {
|
|
if !readsKeyWindows(p) {
|
|
return
|
|
}
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
if c.m == nil {
|
|
c.m = map[string]*keyAllowance{}
|
|
}
|
|
id := keyAllowanceID(p)
|
|
e := c.m[id]
|
|
if e == nil {
|
|
e = &keyAllowance{}
|
|
c.m[id] = e
|
|
}
|
|
renewed := e.keep(allowanceOf(ws, now), SpentShareOf(p.Routing), now)
|
|
c.Unlock()
|
|
if renewed {
|
|
tellRenewed("", id)
|
|
}
|
|
}
|
|
|
|
// StaleKeyAllowance has the next KeyAllowance ask the vendor again for
|
|
// p's key's windows rather than trust what it last said: the key just
|
|
// answered that it is out of them.
|
|
func StaleKeyAllowance(p Provider) {
|
|
if !readsKeyWindows(p) {
|
|
return
|
|
}
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
if e := c.m[keyAllowanceID(p)]; e != nil {
|
|
e.at, e.stale = time.Time{}, e.loading
|
|
e.refused = readsAsked()
|
|
}
|
|
c.Unlock()
|
|
}
|
|
|
|
// forgetKeyAllowances has every key's windows read again at its next ask:
|
|
// a key or its Balance URL changed (ForgetBalances). What was read stands
|
|
// until then — fresher than its card's, which routing's reads never write
|
|
// (they are laid over it only for another magpie, withKeyReadings) — and
|
|
// a key changed is another key, with windows of its own.
|
|
func forgetKeyAllowances() {
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
for _, e := range c.m {
|
|
e.at, e.loading, e.stale = time.Time{}, false, false
|
|
}
|
|
c.gen++
|
|
c.Unlock()
|
|
}
|