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.
105 lines
3.4 KiB
Go
105 lines
3.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Allowances read only when asked (#1518, RooobinYe): with Settings →
|
|
// Usage → Allowance reads at "When I ask", magpie asks no vendor for an
|
|
// account's allowance or a key's balance on its own — not for the Usage
|
|
// page's or tray's timed refresh, the menu bar, alerts, routing, or the
|
|
// switch to an account with room. Each shows the reading kept from the
|
|
// last time it was read, as of then (keepLast). A read the user asked
|
|
// for (Asked) goes out as before, and so does one of an account the
|
|
// vendor just turned away for its quota (StaleAllowance), which routing
|
|
// must not keep sending to.
|
|
//
|
|
// magpie started at login comes up before the proxy app does, and its own
|
|
// reads went out direct from the user's real address.
|
|
|
|
type askedKey struct{}
|
|
|
|
// Asked is ctx for a read the user asked for: the Usage page opened or
|
|
// refreshed, a card's refresh, `magpie quota`, `magpie accounts`.
|
|
func Asked(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, askedKey{}, true)
|
|
}
|
|
|
|
func wasAsked(ctx context.Context) bool {
|
|
b, _ := ctx.Value(askedKey{}).(bool)
|
|
return b
|
|
}
|
|
|
|
// errNotAsked is what a read held back says: "not read yet", so keepLast
|
|
// gives the reading kept from the last time instead.
|
|
var errNotAsked = errors.New("not read yet: magpie reads allowances only when you ask (Settings → Usage)")
|
|
|
|
// readsAsked is whether the user has allowances read only when asked.
|
|
func readsAsked() bool { return settings.Load().QuotaReads == "asked" }
|
|
|
|
// heldRead is whether a read made with ctx is held back: the user has
|
|
// allowances read only when asked, and ctx is not one they asked for.
|
|
func heldRead(ctx context.Context) bool { return !wasAsked(ctx) && readsAsked() }
|
|
|
|
// holdUnasked is ctx, its requests held (netproxy.Hold) when it is a read
|
|
// held back: whatever a vendor's read goes through, nothing of it leaves.
|
|
func holdUnasked(ctx context.Context) context.Context {
|
|
if netproxy.Held(ctx) == nil && heldRead(ctx) {
|
|
return netproxy.Hold(ctx, errNotAsked)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
// staleReads are the accounts (agent/user) the vendor turned away for
|
|
// their quota since they were last read (StaleAllowance): read even while
|
|
// allowances are read only when asked.
|
|
var staleReads struct {
|
|
sync.Mutex
|
|
m map[string]bool
|
|
}
|
|
|
|
func markStaleRead(agent, user string) {
|
|
staleReads.Lock()
|
|
defer staleReads.Unlock()
|
|
if staleReads.m == nil {
|
|
staleReads.m = map[string]bool{}
|
|
}
|
|
staleReads.m[agent+"/"+strings.ToLower(user)] = true
|
|
}
|
|
|
|
// takeStaleRead says whether key is to be read though not asked, and
|
|
// forgets it: one read answers it.
|
|
func takeStaleRead(key string) bool {
|
|
staleReads.Lock()
|
|
defer staleReads.Unlock()
|
|
if staleReads.m[key] {
|
|
delete(staleReads.m, key)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isStaleRead(key string) bool {
|
|
staleReads.Lock()
|
|
defer staleReads.Unlock()
|
|
return staleReads.m[key]
|
|
}
|
|
|
|
// holding is whether a read made with ctx sends nothing: held back
|
|
// (heldRead), or made under one that was (quotaReading put the hold on).
|
|
func holding(ctx context.Context) bool {
|
|
return netproxy.Held(ctx) != nil || heldRead(ctx)
|
|
}
|
|
|
|
// unheld is ctx for a read that goes out though not asked for: an
|
|
// account the vendor turned away for its quota (StaleAllowance).
|
|
func unheld(ctx context.Context) context.Context {
|
|
return netproxy.Hold(Asked(ctx), nil)
|
|
}
|