Files
magpie/internal/provider/key_allowance.go
T
yetone 1bc61b66d7 provider: DeepSeek's balance says its granted and topped-up parts under the total (#2511)
wwcraft_ on Discord asked for DeepSeek's 赠金 apart from what was topped
up. /user/balance gives each currency's granted_balance and
topped_up_balance beside total_balance; the card's main figure stays the
total, and the two parts are told under it as secondary detail, the way a
balance's other parts already show (no new layout). The GUI's card and
menu bar panel, the TUI's quota page and `magpie quota` say them, labels
in every language (en, zh, zh-TW, ja, de). A reply without the parts, or one
with nothing granted, reads as before.

Real reply, read once from the owner's DeepSeek key on 10-16:
{"currency":"CNY","total_balance":"134.13","granted_balance":"34.14",
"topped_up_balance":"99.98"}. Fed through readDeepSeek/readDeepSeekSplit:
"¥134.13", Granted ¥34.14, Topped up ¥99.98.

Run (at b07b518e7, this commit plus #2512's on origin/main 63be47717):
- gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: clean; go build: ok.
- go test -timeout 45m ./... with pipefail, sandbox HOME: 63 packages ok,
  provider, tui, cli, gui and usage among them. agent, gateway and library
  hit the 45m timeout at machine load ~270 on 14 cores; the tests running
  at the panic were 1s-1m14s old (TestCodexLoginAPI,
  TestLoopGuardLeavesOrdinaryRepliesAlone, TestZedMCP), not a hang. Rerun
  alone: library ok (1295s); agent and gateway timed out at 45m again, and
  ok with -timeout 150m: agent ok (5529s); gateway red only in TestIntentClassifierGroup (#1813), TestMaxRPMWaitsForRoomInTheMinute (#1791) and TestMaxRPMGroupPrefersMemberWithRoom (#2520), known timing reds under load, each passing alone here -count=3 (19s).
- GUI, Chromium and WebKit: balance-parts (DeepSeek's parts, en and zh,
  panel too) 4/4. Every GUI file touching balances, usage, sessions or
  i18n (176 files, 4 at a time): red only in model-id-space (#2536),
  provider-advanced-fold (#2552) and one provider-detect-draft case.
  The first two fail on origin/main 63be47717 run alone. The third was
  page.goto's 5s load timeout in the parallel run; run alone it passes
  20/20 three times here and on main, and this diff doesn't touch the
  provider editor (app.js changes are the balance card and the Usage
  ranking only).
- Broken by hand: dropping readDeepSeekSplit from DeepSeek's source
  turns TestDeepSeekBalanceSplit red (gift's split = "").
- Rebased onto origin/main ba132d68b (13 commits; chat.go, gateway.go, app.js, i18n.js among theirs): gofmt and go vet ./internal/... clean; Credits/DeepSeek/Balance/Ledger/RowFlags tests in usage, gateway, provider, cli, tui ok; plan-credits 12/12, balance-parts 4/4, i18n-keys 3/3 in both engines.
2026-10-16 20:11:33 +08:00

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.split, 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()
}