Files
magpie/internal/provider/served.go
T
yetone f5c0626770 tray, usage: the menu bar's account in use is the one that answered last, and the Usage page's cards are told when each did (okingkee on X)
okingkee on X: three Codex accounts, the menu bar set to follow the account
in use. A used its allowance up and the gateway went on to B, but the menu
bar stayed on A; changing the first account moved it, the gateway moving
on did not. trayInUseCard asked InUseLogin, which reckons from the account
the agent is signed in to, and that lags the gateway (and under Smart it
picks any account).

- provider: SubscriptionQuota gains lastServedAt, set by Quotas
  (cardsServed) from served.json the way /v1/magpie/quotas already was,
  on this computer's own cards only and on copies, the caches untouched.
  withServed's matching moved into servedBy.at, shared by both.
  This also makes the Usage page and tray panel's "show the account that
  answered last in full" (ae6b4c93) work: it read q.lastServedAt, which the
  window's API never sent, so it always showed the first.
- tray: a "<provider>|*" card takes the provider's account that answered
  within the last hour (trayServedFresh), else InUseLogin's as before.

Run:
- TestTrayInUseFollowsWhoAnswered (A at 100% answered 40 min ago, B 1 min
  ago → B; both stale → InUseLogin's A; one account → it) fails with the
  served pick disabled and passes; TestCardsLastServed (subscription by
  user, balance with no user, a remote card left alone, inputs unwritten)
  passes; TestTrayInUse and TestQuotasLastServed unchanged and pass.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... pass.
- go test ./... passes except internal/gateway, which hit the 10m default
  timeout under this machine's load; run alone with -timeout 20m it passes
  (726s). No JS changed, so no GUI test run.
2026-10-10 22:45:01 +08:00

206 lines
5.7 KiB
Go

package provider
// When each subscription account, plan and key last answered a request
// through the gateway (#570): magpie quota --json and the gateway's GET
// /v1/magpie/quotas tell it as each Quota's lastServedAt, and mark the
// latest last, so a menu-bar widget can show the one in use — under
// routing Smart nobody is first for good. It is kept on disk (served.json,
// next to the providers), so the CLI, a process of its own, reads what the
// gateway noted, and a restart doesn't forget it.
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// servedKeep is how long an answer is remembered: a key or account gone
// since is dropped from the file after it.
const servedKeep = 30 * 24 * time.Hour
var servedAt = struct {
sync.Mutex
m map[string]time.Time // ServedID → when it last answered
path string // the file m goes with: another (a test's HOME) starts afresh
}{m: map[string]time.Time{}}
// ours is servedAt.m for the file at path. Called with servedAt held.
func ours(path string) map[string]time.Time {
if servedAt.path != path {
servedAt.path, servedAt.m = path, map[string]time.Time{}
}
return servedAt.m
}
func servedPath() string { return filepath.Join(filepath.Dir(Path()), "served.json") }
// ServedID names who answers for p, as its Quota is found by: an account
// by its agent and user ("codex@a@b.c"), a key by its provider and the
// key's id ("deepseek#k1a2b3c"), else the provider.
func ServedID(p Provider) string {
if a := p.Account; a != nil {
return loginProvider(Login{Agent: a.Agent}) + "@" + strings.ToLower(a.User)
}
if p.Key != "" {
return p.ID + "#" + KeyID(p.Key)
}
return p.ID
}
// NoteServed records that p just answered a request, at at.
func NoteServed(p Provider, at time.Time) {
id := ServedID(p)
path := servedPath()
servedAt.Lock()
defer servedAt.Unlock()
mine := ours(path)
if at.Before(mine[id]) {
return
}
mine[id] = at
m := readServed(path)
for k, t := range mine {
if t.After(m[k]) {
m[k] = t
}
}
for k, t := range m {
if at.Sub(t) > servedKeep {
delete(m, k)
}
}
if b, err := json.Marshal(m); err == nil && os.MkdirAll(filepath.Dir(path), 0o700) == nil {
writeFileAtomic(path, b)
}
}
// LastServed is when each ServedID last answered: what this process noted
// and what the file holds, whichever gateway wrote it.
func LastServed() map[string]time.Time {
path := servedPath()
m := readServed(path)
servedAt.Lock()
for k, t := range ours(path) {
if t.After(m[k]) {
m[k] = t
}
}
servedAt.Unlock()
for k, t := range m {
if time.Since(t) > servedKeep {
delete(m, k)
}
}
return m
}
func readServed(path string) map[string]time.Time {
m := map[string]time.Time{}
if b, err := os.ReadFile(path); err == nil {
json.Unmarshal(b, &m)
}
if m == nil {
m = map[string]time.Time{}
}
return m
}
// withServed sets each quota's LastServedAt from served, and Last on the
// latest (on each of them, when a key's plan and balance tie).
func withServed(qs []Quota, served map[string]time.Time) []Quota {
if len(served) == 0 {
return qs
}
by := servedBy{served: served}
var latest time.Time
for i := range qs {
q := &qs[i]
if at := by.at(q.Provider, q.Kind, q.User); !at.IsZero() {
t := at.UTC()
q.LastServedAt = &t
if at.After(latest) {
latest = at
}
}
}
for i := range qs {
if qs[i].LastServedAt != nil && qs[i].LastServedAt.Equal(latest) {
qs[i].Last = true
}
}
return qs
}
// servedBy finds when a card's account, plan or key last answered, in
// served (LastServed's map).
type servedBy struct {
served map[string]time.Time
keys map[string][]KeyInfo // provider → its keys, read once, when a plan or balance asks
}
// at is when the card of provider's kind ("subscription", "plan" or
// "balance") told by user last answered; zero when it hasn't.
func (s *servedBy) at(provider, kind, user string) time.Time {
var at time.Time
latestOf := func(id string) {
if t, ok := s.served[id]; ok && t.After(at) {
at = t
}
}
switch {
case kind == "subscription" && user != "":
latestOf(provider + "@" + strings.ToLower(user))
case user == "":
// the one account or key it has, or any of them: a balance
// one account has whichever key asks
for id, t := range s.served {
if (id == provider || strings.HasPrefix(id, provider+"@") || strings.HasPrefix(id, provider+"#")) && t.After(at) {
at = t
}
}
default:
// a plan's or balance's key, by the name or mask it is told by
if s.keys == nil {
s.keys = map[string][]KeyInfo{}
for _, p := range All() {
s.keys[p.ID] = p.KeyList()
}
}
for _, k := range s.keys[provider] {
if k.Name == user || k.Name == "" && k.Masked == user {
latestOf(provider + "#" + k.ID)
}
}
}
return at
}
// cardsServed sets LastServedAt on the Usage page's cards, this
// computer's own (a remote magpie's answer there, not here), as
// withServed does on the gateway's: what the Usage page and the tray
// panel show in full, and what the menu bar's "account in use" follows,
// is the account that answered last. Each is a copy; the caches the
// cards come from are left as they were.
func cardsServed(subs, plans, balances []SubscriptionQuota, served map[string]time.Time) []SubscriptionQuota {
out := make([]SubscriptionQuota, 0, len(subs)+len(plans)+len(balances))
by := servedBy{served: served}
for _, g := range []struct {
kind string
qs []SubscriptionQuota
}{{"subscription", subs}, {"plan", plans}, {"balance", balances}} {
for _, q := range g.qs {
if q.From == "" && len(served) > 0 {
if at := by.at(q.Provider, g.kind, q.User); !at.IsZero() {
t := at.UTC()
q.LastServedAt = &t
}
}
out = append(out, q)
}
}
return out
}