Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: whether a model is served is answered from catalog names kept while the catalog stays the same

yetone5daea73c6e7b
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index b7eccdc04..e8ab620a5 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -197,10 +197,8 @@ func unserved(asked string) bool {
return false
}
id := strings.TrimSuffix(asked, "[1m]")
- for _, e := range provider.Catalog() {
- if e.ID == id || e.Model == id {
- return false
- }
+ if catalogNames.get(struct{}{}, namesInCatalog)[id] {
+ return false
}
if pid, _, ok := strings.Cut(id, "/"); ok {
if p, err := provider.Find(pid); err == nil && p.On() {
@@ -210,6 +208,22 @@ func unserved(asked string) bool {
return true
}
+// catalogNames is every catalog entry's id and model id, kept by
+// provider.StampNow. unserved asks it on every request an agent sends
+// (standIn), and each one built the whole catalog to look: 2.26 GB of the
+// 9.8 GB a sandbox gateway allocated in 10 minutes of ~1,000 requests
+// (procmem heap profile), with nothing changed between them.
+var catalogNames stamped[struct{}, map[string]bool]
+
+func namesInCatalog() map[string]bool {
+ cat := provider.Catalog()
+ out := make(map[string]bool, 2*len(cat))
+ for i := range cat {
+ out[cat[i].ID], out[cat[i].Model] = true, true
+ }
+ return out
+}
+
// switchedOff says a request for model went nowhere because p, which
// serves it, is switched off in magpie (provider.Provider.Off).
func switchedOff(p provider.Provider, model string) string {
diff --git a/internal/gateway/unserved_kept_test.go b/internal/gateway/unserved_kept_test.go
new file mode 100644
index 000000000..d0ec96f66
--- /dev/null
+++ b/internal/gateway/unserved_kept_test.go
@@ -0,0 +1,49 @@
+package gateway
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// Whether a model an agent asks for is served here is answered from the
+// catalog's names kept by provider.StampNow: every request an agent sends
+// asks it (standIn), and each one built the whole catalog to look, 2.26 GB
+// of 9.8 GB in 10 minutes of ~1,000 requests. A provider saved since is
+// seen at once.
+func TestUnservedKeptWhileCatalogStays(t *testing.T) {
+ setup(t, provider.Chat, &fake{})
+ models := make([]string, 1000)
+ for i := range models {
+ models[i] = fmt.Sprintf("m%d", i)
+ }
+ if err := provider.Save(provider.Provider{ID: "big", Name: "Big", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
+ t.Fatal(err)
+ }
+ if unserved("m1") || unserved("big/m150") || !unserved("claude-sonnet-4-6") || !unserved("nope/claude-sonnet-4-6") {
+ t.Fatal("served wrong before anything is kept")
+ }
+ // ids with a provider in them, as magpie writes into agents' configs:
+ // a bare one asks the routing groups first, which the request's
+ // routing builds too
+ allocs := testing.AllocsPerRun(20, func() {
+ unserved("big/m150")
+ unserved("nope/claude-sonnet-4-6")
+ })
+ // measured against one catalog build in the same run, since the
+ // package's other tests leave background work allocating beside it:
+ // two answers built it twice (~2x), kept they are StampNow's look at
+ // three files and Find's of the providers for the id no entry has
+ built := testing.AllocsPerRun(5, func() { provider.Catalog() })
+ if allocs*3 > built {
+ t.Fatalf("%.0f allocations for two answers already given; one catalog build is %.0f", allocs, built)
+ }
+
+ if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: "http://127.0.0.1:1", Models: []string{"claude-sonnet-4-6"}}); err != nil {
+ t.Fatal(err)
+ }
+ if unserved("claude-sonnet-4-6") {
+ t.Fatal("a model of a provider saved since is still unserved")
+ }
+}