diff --git a/internal/provider/exposed_ids_test.go b/internal/provider/exposed_ids_test.go index 3dba11e25..8974d3420 100644 --- a/internal/provider/exposed_ids_test.go +++ b/internal/provider/exposed_ids_test.go @@ -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"]) + } +} diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index d0bab4046..f5fbd0520 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -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