provider: Gemini's and Antigravity's offered models are kept per stamp, so the tray's usage read lets the catalog go

visibleQuotas built exposedIDs("gemini", "antigravity") on every usage
read, and each build loaded the whole models.dev catalog through
knownElsewhere → catalog.ListedBy. The tray reads usage every few
minutes, so a tray-only magpie with real accounts kept 5.6 MB of
catalog (of a 12.8 MB live heap) that loadIdle never got to drop.

keptExposedIDs keeps the answer while provider.StampNow is the same
(no provider, account, setting, plugin or catalog change) and for 30
minutes at most, for what a stamp can't see.

Run: go test -run 'ExposedIDs|Quota' ./internal/provider on mote, ok.
Broken by hand: always building turns TestKeptExposedIDsBuiltOncePerChange
red ("built 5 more times while nothing changed").
This commit is contained in:
yetone
2026-11-14 11:47:59 +08:00
parent 90932ed1fa
commit b76016676f
2 changed files with 73 additions and 1 deletions
+31
View File
@@ -23,3 +23,34 @@ func TestExposedIDsBuildsOnlyTheProvidersAsked(t *testing.T) {
t.Fatalf("its offered models: %v", got["asked"])
}
}
// The tray reads usage every few minutes: the offered models of the
// providers whose quotas are narrowed are built once while nothing changes, so the catalog they
// load can be let go, and again after a change, which reaches the answer.
func TestKeptExposedIDsBuiltOncePerChange(t *testing.T) {
isolate(t)
keptExposed.Lock()
keptExposed.ids = nil
keptExposed.Unlock()
if err := Save(Provider{ID: "asked", Name: "Asked", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gemini-a"}}); err != nil {
t.Fatal(err)
}
first := keptExposedIDs("asked", "antigravity")
if !first["asked"]["gemini-a"] {
t.Fatalf("offered: %v", first["asked"])
}
builds := exposedBuilds.Load()
for range 5 {
keptExposedIDs("asked", "antigravity")
}
if n := exposedBuilds.Load() - builds; n != 0 {
t.Fatalf("built %d more times while nothing changed", n)
}
if err := Save(Provider{ID: "asked", Name: "Asked", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gemini-b"}}); err != nil {
t.Fatal(err)
}
got := keptExposedIDs("asked", "antigravity")
if !got["asked"]["gemini-b"] || got["asked"]["gemini-a"] {
t.Fatalf("after the change: %v", got["asked"])
}
}
+42 -1
View File
@@ -418,7 +418,7 @@ func visibleQuotas(all []SubscriptionQuota) []SubscriptionQuota {
}
if q.Provider == "gemini" || q.Provider == "antigravity" {
if chosen == nil {
chosen = exposedIDs("gemini", "antigravity")
chosen = keptExposedIDs("gemini", "antigravity")
}
var base func(string) string
if q.Provider == "antigravity" {
@@ -438,6 +438,47 @@ func visibleQuotas(all []SubscriptionQuota) []SubscriptionQuota {
return out
}
// keptExposed is exposedIDs for Gemini's and Antigravity's quotas, kept
// while StampNow stays the same, and no longer than exposedFor. The tray
// reads usage every few minutes, and building it each time loaded the
// whole catalog (5.6 MB of a tray-only magpie's 12.8 MB live heap) before
// catalog's loadIdle could let it go.
var keptExposed struct {
sync.Mutex
ids map[string]map[string]bool
key string
stamp Stamp
at time.Time
}
// exposedFor bounds how long keptExposed is trusted across what a Stamp
// can't see: a model retired by its date, another app's account file.
const exposedFor = 30 * time.Minute
// exposedBuilds counts keptExposedIDs' builds, for a test.
var exposedBuilds atomic.Int64
// keptExposedIDs is exposedIDs(ids...), built again only after a change
// or for other ids. What it gives is shared: read only.
func keptExposedIDs(ids ...string) map[string]map[string]bool {
key := strings.Join(ids, "\x00")
now := StampNow()
keptExposed.Lock()
defer keptExposed.Unlock()
if keptExposed.ids != nil && keptExposed.key == key && keptExposed.stamp == now && time.Since(keptExposed.at) < exposedFor {
return keptExposed.ids
}
exposedBuilds.Add(1)
built := exposedIDs(ids...)
// a change while it was built: the next read builds it again
if StampNow() == now {
keptExposed.ids, keptExposed.key, keptExposed.stamp, keptExposed.at = built, key, now, time.Now()
} else {
keptExposed.ids = nil
}
return built
}
// exposedIDs is, for each provider of ids, the models magpie offers from
// it. Only those are built: every provider's whole list, each account's
// and plugin's, was built for the two whose quotas are narrowed, on each