From c7c19d49d57f0c2b745dec355bbe202d149e328b Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 01:54:58 +0800 Subject: [PATCH] provider: asking whether a model decides copies no list DecidesModel answered for a remote magpie, and for a provider that lists its decision models (OpenRouter's), by building decideListed's whole list (catalog.Live copies every chat model) and scanning it. Every model of such a provider is asked this from available(), pluginDecisions and the decider list, so a list of n models was copied n times per look: 0.43 GB of the 4.1 GB a real config allocated over 20 polls of /api/drift, /api/state and /api/providers (v0.1.2430, DecidesModel -> decideListed -> catalog.Live -> catalog.Chat). It now looks the model up in the list as kept (catalog.LiveByID, which readLive indexes once per read), and catalog.LiveListed says whether the list has any model, for the Jev fallback decideListed has. The answers are decideListed's: a remote magpie's models its list marks Decides; a listed decision list's models; with none listed, OpenRouter's Jev unless the server marks its own. TestDecidesModelCopiesNoList: 2000 decision models, each ask under a twentieth of the list's bytes, for both kinds, and the answers above. Red on the old code (an ask 869,336 bytes, the list 869,829). Run at 6a900e0d0 plus this change, on mote: go vet (darwin, windows, linux) and gofmt of internal/provider and internal/catalog clean; go test -count=1 -timeout 45m ./internal/provider ./internal/catalog ./internal/agent ./internal/gateway all ok (126s, 0.6s, 612s, 1305s). --- internal/catalog/live.go | 6 ++ internal/provider/decide.go | 13 +++- internal/provider/decide_copy_test.go | 87 +++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 internal/provider/decide_copy_test.go diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 821d7a94f..5191b69eb 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -92,6 +92,12 @@ func LiveByID(provider, model string) []Model { return slices.Clone(f.byID[model]) } +// LiveListed is whether Live lists any model, without the list copied. +func LiveListed(provider string) bool { + f, err := readLive(provider) + return err == nil && len(f.byID) > 0 +} + // Live returns the model list last fetched from the provider, if any: the // models to talk to, without the ones that draw (LiveDrawers). func Live(provider string) (models []Model, fetched time.Time, ok bool) { diff --git a/internal/provider/decide.go b/internal/provider/decide.go index 7463c5c82..1eec37e93 100644 --- a/internal/provider/decide.go +++ b/internal/provider/decide.go @@ -95,9 +95,18 @@ func (p Provider) DecidesModel(model string) bool { if p.bailianApart() { return model == BailianDecision } - if p.IsRemoteMagpie() || p.listsDecisions() { + // decideListed's answer, without its list copied for each model + // asked about: a provider's every model was asked, a copy of the whole + // list each time (0.4 GB of a real config's 4 GB over 20 polls) + if p.IsRemoteMagpie() { + return slices.ContainsFunc(catalog.LiveByID(p.ID, model), func(m catalog.Model) bool { return m.Decides }) + } + if p.listsDecisions() { // OpenRouter's Jev Router (typesafe/jev-router) is a chat model - return slices.ContainsFunc(p.decideListed(), func(m catalog.Model) bool { return m.ID == model }) + if id := decisionsID(p.ID); catalog.LiveListed(id) { + return len(catalog.LiveByID(id, model)) > 0 + } + return !p.serverMarksDecisions() && model == OpenRouterJev } return jevID(model) || p.DecideVia() == ViaCloudflare && CloudflareClef(model) } diff --git a/internal/provider/decide_copy_test.go b/internal/provider/decide_copy_test.go new file mode 100644 index 000000000..0298ffca7 --- /dev/null +++ b/internal/provider/decide_copy_test.go @@ -0,0 +1,87 @@ +package provider + +import ( + "fmt" + "os" + "runtime" + "testing" + "time" + + "github.com/yetone/magpie/internal/catalog" +) + +// Each of a provider's models is asked whether it decides, and each ask +// copied the provider's whole decision list (0.4 GB of a real config's +// 4 GB over 20 polls of the Providers and Agents pages). An ask reads the +// list kept, and answers as the list does. +func TestDecidesModelCopiesNoList(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + p := Provider{ID: "or-many", Name: "OpenRouter", Key: "k", Chat: "https://openrouter.ai/api/v1", Decide: "https://openrouter.ai/api/v1"} + if !p.listsDecisions() { + t.Fatal("not a provider that lists its decision models") + } + // none listed yet: OpenRouter's Jev + if !p.DecidesModel(OpenRouterJev) || p.DecidesModel("liquid/d1") { + t.Fatal("with no list, only Jev decides") + } + var ms []catalog.Model + for i := range 2000 { + ms = append(ms, catalog.Model{ID: fmt.Sprintf("vendor/decider-%d", i)}) + } + if err := catalog.SaveLive(decisionsID(p.ID), p.Decide, ms); err != nil { + t.Fatal(err) + } + fetchedEarlier(t, catalog.LivePath(decisionsID(p.ID))) + if !p.DecidesModel("vendor/decider-1999") || p.DecidesModel(OpenRouterJev) || p.DecidesModel("vendor/chat") { + t.Fatal("decision models are not the list's") + } + listed := bytesPer(func() { p.decideListed() }) + if n := bytesPer(func() { p.DecidesModel("vendor/decider-1999") }); n*20 > listed { + t.Fatalf("an ask took %d bytes, the list %d", n, listed) + } + + // a remote magpie's: the models its list marks + r := normalize(Provider{ID: "office", Preset: RemoteMagpiePreset, Chat: "http://127.0.0.1:1/typesafe"}) + ms = ms[:0] + for i := range 2000 { + ms = append(ms, catalog.Model{ID: fmt.Sprintf("m-%d", i), Decides: i%2 == 0}) + } + if err := catalog.SaveLive(r.ID, r.Chat, ms); err != nil { + t.Fatal(err) + } + fetchedEarlier(t, catalog.LivePath(r.ID)) + if !r.DecidesModel("m-1998") || r.DecidesModel("m-1999") || r.DecidesModel("m-x") { + t.Fatal("a remote magpie's decision models are not the ones its list marks") + } + listed = bytesPer(func() { r.decideListed() }) + if n := bytesPer(func() { r.DecidesModel("m-1998") }); n*20 > listed { + t.Fatalf("a remote magpie's ask took %d bytes, the list %d", n, listed) + } +} + +// bytesPer is what one call of f allocates, in bytes, over 20 calls. +func bytesPer(f func()) uint64 { + f() + var a, b runtime.MemStats + runtime.ReadMemStats(&a) + for range 20 { + f() + } + runtime.ReadMemStats(&b) + return (b.TotalAlloc - a.TotalAlloc) / 20 +} + +// fetchedEarlier dates f an hour back, as a list fetched earlier is: a file +// written just now is read again on each look, its time too recent to +// tell a later write apart. +func fetchedEarlier(t *testing.T, f string) { + t.Helper() + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(f, old, old); err != nil { + t.Fatal(err) + } +}