davezfr on X: Codex signed in to their account 1 showed "magpie" as the account in the Codex app's settings menu; signed in to account 2 it showed account 2. It used to show account 1. Cause. When the account Codex is signed in to is held (out of its allowance, no credits: the app greys Send), magpie becomes Codex's provider (codex.out, #540) without requires_openai_auth, and the app reads no account and shows the provider's name. The sync decides that from CodexUsedUpKnown, the readings kept in memory (loginUsageCache), and those were taken as they were made, however old. Account 1 had run out earlier; its reading said held. Codex signed back in to it after its window's reset still read as held, so magpie took over and the app showed "magpie". The reading kept on disk already ended such a hold at the reset (lastQuotasT.reading); the in-memory one, and the kept reading the background look gets while allowances are read only when asked, never did. And the loop's fresh read ("room") told the sync nothing: noteCodexUsedUp compared it with the loop's last answer, given for account 2 (also room), not with what the sync had gone by. Fix. - holdEnded (quota_last.go) is the one rule: a hold that came with a window used up when read ends once that window has started again; a hold with no window used up stands till a fresh read. The disk reading uses it as before. - loginUsageEntry.asOf applies it to readings from memory, as of when each was made: loginReading (LoginUsage, someLoginUsage, CodexUsedUp, the switch), loginReadingKnown (CodexUsedUpKnown, codexHeldKnown) and the Usage page's cards (withAccountReadings). A window whose reset had passed when it was read doesn't overrule the vendor's allowed false. - noteCodexUsedUp(known, now) also tells the sync when the fresh read differs from what CodexUsedUpKnown said before it, in the loop and in codexReadNow. Users already affected: on the first look after updating, the account's reading has its hold ended at its reset, and the sync puts Codex back beside its sign-in; the app shows the account again (after its app-server restart, as the takeover's end already does). An account that really is held still has magpie as Codex's provider till its reset: that is the takeover itself, which the app shows as "magpie". Tests (red on the old code, at origin/main142994b21): - TestCodexHoldEndsAtItsWindowsReset: a held reading from 3h ago whose 5h window reset a minute ago. Old code: "CodexUsedUpKnown = true on a hold whose used-up window started again a minute ago". Also checks codexHeldKnown, CodexUsedUp with reads held back, a hold before its reset (held) and one with no window used up (held). - TestCodexUsedUpToldWhenKnownChanges: with `|| known != now` taken out by hand it is red ("told 0 times, want 1"). - TestNoteCodexUsedUp takes the new signature, unchanged otherwise. Run: full suite (go test -timeout 45m ./...) on mica at this change on142994b21: every package ok but internal/gateway, whose reds were all TempDir "no space left on device" (mica's disk filled mid-run; the logged doneHold panic is #6690, recovered by net/http). After rebasing onb754cb095, on mote: internal/provider ok, internal/agent ok, internal/gateway ok but TestRefusalAfterShownThinkingGoesToTheNext/ chat_1301_to_responses (#5761, open; -count 10 alone ok). gofmt -l clean, GOOS=linux go vet -tags nogui ./... ok, GOOS=windows go vet ./... ok. Old code: TestCodexHoldEndsAtItsWindowsReset red on origin/main; TestCodexUsedUpToldWhenKnownChanges red with '|| known != now' removed. Not tried: a real ChatGPT account through a real reset with the Codex app open; the reporter's account 1 may also simply be out now, which the app shows as "magpie" till its reset, as before.
353 lines
11 KiB
Go
353 lines
11 KiB
Go
package provider
|
|
|
|
// The cards another magpie is told (CachedCards) as fresh as this magpie
|
|
// knows them, not only as the Usage page last read them (jorben, #1313).
|
|
// A magpie that serves only other magpies (`magpie serve` in a container)
|
|
// has nobody on its Usage page, so the page's cards were never read
|
|
// there, while the same accounts' allowances are read all the time
|
|
// behind the requests: the account switching and the Codex warm-up
|
|
// (LoginUsage), routing (Allowances, KeyAllowance). Those readings now
|
|
// stand on the cards in place of an older one, an account or key with no
|
|
// card yet gets one from them, and after a restart, before anything is
|
|
// read, the cards are the ones kept on disk (quotas.json, as of when they
|
|
// were read). Nothing here asks a vendor.
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
)
|
|
|
|
// cardFace is what a card is named and drawn with.
|
|
type cardFace struct{ name, icon string }
|
|
|
|
// accountCard is the face of the card the Usage page gives each account
|
|
// of agent, as its usage is read (LoginUsage): a plugin's ("plugin:<id>")
|
|
// as the plugin's cards are.
|
|
func accountCard(agent string) cardFace {
|
|
if pp, ok := pluginOfAgent(agent); ok && strings.HasPrefix(agent, "plugin:") {
|
|
return pluginCardFace(pp)
|
|
}
|
|
switch agent {
|
|
case "claude":
|
|
return cardFace{"Claude Code", "claude-color"}
|
|
case "codex":
|
|
return cardFace{"Codex", "codex-color"}
|
|
case "copilot":
|
|
return cardFace{"Copilot", "githubcopilot"}
|
|
case QoderCNID:
|
|
return cardFace{"Qoder CN", "qoder"}
|
|
case "gemini", "antigravity":
|
|
app, _ := googleAppOf(agent)
|
|
return cardFace{app.name, app.icon}
|
|
}
|
|
if c, ok := movedCards[agent]; ok {
|
|
return cardFace{c.name, c.icon}
|
|
}
|
|
return cardFace{}
|
|
}
|
|
|
|
// KnownFace is the name and icon magpie knows the provider id by while it
|
|
// isn't set up: a built-in subscription signed out of, or with account
|
|
// discovery off ("claude" is Claude Code), or a preset's removed since.
|
|
// The usage log keeps the id of every provider a call went to, and the
|
|
// Usage page names those by this rather than by the bare id (@ByronVon1
|
|
// on X: "claude" in the Provider filter). "" for an id magpie doesn't
|
|
// know, such as a custom provider removed since.
|
|
func KnownFace(id string) (name, icon string) {
|
|
if !strings.HasPrefix(id, "plugin:") {
|
|
if f := accountCard(id); f.name != "" {
|
|
return f.name, f.icon
|
|
}
|
|
}
|
|
if pr := Preset(id); pr != nil {
|
|
return pr.Name, pr.Icon
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
func pluginCardFace(pp plugin.Provider) cardFace {
|
|
name, icon := pluginCard(pp)
|
|
return cardFace{name, icon}
|
|
}
|
|
|
|
// cardAgents are the agents whose accounts each have a card of their own
|
|
// on the Usage page, read as LoginUsage reads them. Cursor's account is
|
|
// known by asking its CLI, Grok's are read by home, and a Kiro key has
|
|
// one card for itself: their cards stay as the page read them.
|
|
var cardAgents = []string{"claude", "codex", "copilot", "kiro", "zcode", "workbuddy", WorkBuddyAIID,
|
|
CommandCodePlanID, "qoder", QoderCNID, "zed", "devin", "factory", MiMoID, "gemini", "antigravity"}
|
|
|
|
// cardLogins are the accounts the Usage page has a card of its own for,
|
|
// as LoginUsage reads them, a built-in moved onto its plugin by the
|
|
// plugin's.
|
|
func cardLogins() []Login {
|
|
agents := []string{}
|
|
for _, id := range cardAgents {
|
|
if PluginServes(id) || id == "kiro" && kiroKey() != "" {
|
|
continue
|
|
}
|
|
agents = append(agents, id)
|
|
}
|
|
for _, pp := range plugin.Cached() {
|
|
agents = append(agents, pluginAgent(pp))
|
|
}
|
|
seen := map[string]bool{}
|
|
var out []Login
|
|
for _, agent := range agents {
|
|
ls, _ := usageLogins(agent)
|
|
for _, l := range ls {
|
|
if k := l.Agent + "/" + strings.ToLower(l.User); l.User != "" && !seen[k] {
|
|
seen[k] = true
|
|
out = append(out, l)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// withAccountReadings is cards with each account's latest reading, made
|
|
// for the Usage page or behind it (LoginUsage), in place of an older
|
|
// one. An account without a card gets one, from its reading, else from
|
|
// the one kept on disk (quotas.json) as of when it was made.
|
|
func withAccountReadings(cards []SubscriptionQuota) []SubscriptionQuota {
|
|
ls := cardLogins()
|
|
if len(ls) == 0 {
|
|
return cards
|
|
}
|
|
read := map[string]SubscriptionQuota{}
|
|
c := &loginUsageCache
|
|
now := time.Now()
|
|
c.Lock()
|
|
for _, l := range ls {
|
|
k := l.Agent + "/" + strings.ToLower(l.User)
|
|
if e, ok := c.m[k]; ok {
|
|
read[k] = e.asOf(now).read
|
|
}
|
|
}
|
|
c.Unlock()
|
|
out := slices.Clone(cards)
|
|
for _, l := range ls {
|
|
id := loginProvider(l)
|
|
q, ok := read[l.Agent+"/"+strings.ToLower(l.User)]
|
|
i := slices.IndexFunc(out, func(x SubscriptionQuota) bool {
|
|
return x.From == "" && x.Provider == id && strings.EqualFold(x.User, l.User)
|
|
})
|
|
if i >= 0 {
|
|
// the same reading (perLogin's) or a later one
|
|
if ok && q.readSeq >= out[i].readSeq {
|
|
q.Provider, q.Name, q.Icon, q.User = id, out[i].Name, out[i].Icon, out[i].User
|
|
out[i] = q
|
|
}
|
|
out[i] = withHeardClaude(out[i], l)
|
|
continue
|
|
}
|
|
if !ok {
|
|
if q, ok = lastReading(id + "/" + strings.ToLower(l.User)); !ok {
|
|
q = SubscriptionQuota{Plan: l.Plan, Windows: []QuotaWindow{}}
|
|
}
|
|
}
|
|
if q = withHeardClaude(q, l); !ok && q.readSeq == 0 {
|
|
continue
|
|
}
|
|
f := accountCard(l.Agent)
|
|
q.Provider, q.Name, q.Icon, q.User = id, f.name, f.icon, l.User
|
|
out = append(out, q)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// withHeardClaude is q, the card of the Claude account l, with what
|
|
// Claude Code last told of its allowance as it answered (NoteClaudeLimits)
|
|
// when it told it after q was read: the windows a request's answer gave
|
|
// are kept apart from the readings (claudeUsage), and telling them drops
|
|
// the account's reading (StaleAllowance), so a magpie only requests reach
|
|
// (`magpie serve` for a Remote magpie) had a card for nobody (#2206,
|
|
// SuInk). A reading started after it stands, as it is newer.
|
|
func withHeardClaude(q SubscriptionQuota, l Login) SubscriptionQuota {
|
|
if l.Agent != "claude" {
|
|
return q
|
|
}
|
|
claudeUsage.Lock()
|
|
e, ok := claudeUsage.m[strings.ToLower(l.User)]
|
|
claudeUsage.Unlock()
|
|
if !ok || e.heardSeq <= q.readSeq || len(e.ws) == 0 {
|
|
return q
|
|
}
|
|
at := e.heard
|
|
q.Windows, q.ReadAt, q.AsOf, q.Error, q.readSeq = e.windows(time.Now()), &at, nil, "", e.heardSeq
|
|
return q
|
|
}
|
|
|
|
// lastReading is the reading kept on disk under key (keepLast), with its
|
|
// date (AsOf).
|
|
func lastReading(key string) (SubscriptionQuota, bool) {
|
|
c := &lastQuotas
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.load()
|
|
return c.reading(key)
|
|
}
|
|
|
|
// cardKey is one of p's keys as its plan and balance cards are read with
|
|
// it: the key, and the account the cards name it by ("" for a provider's
|
|
// one card), as PlanQuotas and KeyBalances name them.
|
|
type cardKey struct {
|
|
user string
|
|
key string
|
|
}
|
|
|
|
// cardKeys are the keys p's plan and balance cards are read with, in the
|
|
// order requests try them: the key in use first, then each other one on.
|
|
// One entry per key, even two of one name: each card is read and kept by
|
|
// its key (keyTag), and a map by the name made the second of one name
|
|
// drop out — one card told another magpie where the Usage page has two.
|
|
func cardKeys(p Provider) []cardKey {
|
|
others := 0
|
|
for _, k := range p.Keys {
|
|
if !k.Off && k.Key != "" && k.Key != p.Key {
|
|
others++
|
|
}
|
|
}
|
|
name := func(n, key string) string {
|
|
if n == "" {
|
|
return Mask(key)
|
|
}
|
|
return n
|
|
}
|
|
first := cardKey{key: p.Key}
|
|
if others > 0 {
|
|
first.user = name(p.KeyName, p.Key)
|
|
}
|
|
out := []cardKey{first}
|
|
for _, k := range p.Keys {
|
|
if !k.Off && k.Key != "" && k.Key != p.Key {
|
|
out = append(out, cardKey{user: name(k.Name, k.Key), key: k.Key})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// keyCardProviders are the providers whose keys can have a plan or
|
|
// balance card.
|
|
func keyCardProviders() []Provider {
|
|
var out []Provider
|
|
for _, p := range All() {
|
|
if !p.Hidden && !p.Off && p.Account == nil && p.Key != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// keptKeyCards are the plan and balance cards of the keys magpie has, as
|
|
// they were last read and kept on disk (quotas.json): what another magpie
|
|
// is shown before this one reads them again after a restart.
|
|
func keptKeyCards(ps []Provider) (plans, balances []SubscriptionQuota) {
|
|
plans, balances = []SubscriptionQuota{}, []SubscriptionQuota{}
|
|
for _, p := range ps {
|
|
for _, ck := range cardKeys(p) {
|
|
for _, kind := range []string{"plan", "balance"} {
|
|
q, ok := lastReading(p.ID + "/" + keyTag(kind, ck.key))
|
|
if !ok || !strings.EqualFold(q.User, ck.user) {
|
|
continue // kept under the name it had then
|
|
}
|
|
q.Provider, q.Name, q.Icon = p.ID, p.Name, p.Icon
|
|
if kind == "plan" {
|
|
plans = append(plans, q)
|
|
} else {
|
|
balances = append(balances, q)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return plans, balances
|
|
}
|
|
|
|
// withKeyReadings is the plan or balance cards (kind) with the windows
|
|
// routing read of their keys since (KeyAllowance), and what was read with
|
|
// them, in place of the card's own, which only the Usage page reads. A key
|
|
// routing has read that has no card yet gets one.
|
|
func withKeyReadings(cards []SubscriptionQuota, ps []Provider, kind string) []SubscriptionQuota {
|
|
out := slices.Clone(cards)
|
|
for _, p := range ps {
|
|
if !strings.HasPrefix(keyCardTag(p), kind+" ") {
|
|
continue // the key's windows are its other card's
|
|
}
|
|
// each card goes to one key: two keys of one name have a card
|
|
// each, in the order cardKeys gives them (PlanQuotas and
|
|
// KeyBalances make them in that order)
|
|
claimed := map[int]bool{}
|
|
for _, ck := range cardKeys(p) {
|
|
i := -1
|
|
for j, q := range out {
|
|
if !claimed[j] && q.From == "" && q.Provider == p.ID && strings.EqualFold(q.User, ck.user) {
|
|
i = j
|
|
break
|
|
}
|
|
}
|
|
if i >= 0 {
|
|
claimed[i] = true
|
|
}
|
|
r := keyRead(p.ID + "#" + keyID(ck.key))
|
|
if r.seq == 0 {
|
|
continue
|
|
}
|
|
var q SubscriptionQuota
|
|
switch {
|
|
case i >= 0 && r.seq <= out[i].readSeq:
|
|
continue // the card was read since
|
|
case i >= 0:
|
|
q = out[i]
|
|
case len(r.ws) == 0 && r.balance == "":
|
|
continue // a key without a plan, nor a balance read
|
|
default:
|
|
q = SubscriptionQuota{Provider: p.ID, Name: p.Name, Icon: p.Icon, User: ck.user}
|
|
}
|
|
at := r.at
|
|
q.Windows, q.ReadAt, q.AsOf, q.Error, q.readSeq = slices.Clone(r.ws), &at, nil, "", r.seq
|
|
if q.Windows == nil {
|
|
q.Windows = []QuotaWindow{}
|
|
}
|
|
if r.plan != "" {
|
|
q.Plan = r.plan
|
|
}
|
|
if r.balance != "" {
|
|
q.Balance, q.BalanceParts, q.BalanceSplit = r.balance, r.parts, r.split
|
|
}
|
|
if i >= 0 {
|
|
out[i] = q
|
|
} else {
|
|
claimed[len(out)] = true
|
|
out = append(out, q)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// keyRead is what routing last read of the key id (provider id#key id).
|
|
func keyRead(id string) keyReading {
|
|
c := &keyAllowances
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
if e := c.m[id]; e != nil {
|
|
return e.read
|
|
}
|
|
return keyReading{}
|
|
}
|
|
|
|
// keyReading is a key's windows as routing read them, with the plan or
|
|
// balance read with them, for its card.
|
|
type keyReading struct {
|
|
ws []QuotaWindow
|
|
plan string
|
|
balance string
|
|
parts []BalancePart
|
|
split []BalancePart
|
|
at time.Time
|
|
seq uint64 // quotaReading's, begun as it was asked
|
|
}
|