Skip to content

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

agent: whether a model value is magpie's is kept while the catalog stays the same

yetonef962cfa75b1b
diff --git a/internal/agent/ismagpie_cache_test.go b/internal/agent/ismagpie_cache_test.go
new file mode 100644
index 000000000..6922aa536
--- /dev/null
+++ b/internal/agent/ismagpie_cache_test.go
@@ -0,0 +1,40 @@
+package agent
+
+import (
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// isMagpie answers again from what it kept while nothing the catalog is
+// built from changed: the window's drift poll asked it for every agent's
+// values every 15s, outside any request holding the catalog, and each
+// answer built every provider's entries again (~6 MB a poll). A provider
+// saved since is seen at once.
+func TestIsMagpieKeptWhileCatalogStays(t *testing.T) {
+ syncHome(t)
+ if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}}); err != nil {
+ t.Fatal(err)
+ }
+ if !isMagpie("oa/gpt-5.5") {
+ t.Fatal("oa/gpt-5.5 not magpie's")
+ }
+ if isMagpie("nb/x") {
+ t.Fatal("nb/x magpie's before it is added")
+ }
+ allocs := testing.AllocsPerRun(20, func() {
+ isMagpie("oa/gpt-5.5")
+ isMagpie("nb/x")
+ })
+ // StampNow's look at three files; a catalog built was thousands
+ if allocs > 200 {
+ t.Fatalf("%.0f allocations for two answers already given", allocs)
+ }
+
+ if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
+ t.Fatal(err)
+ }
+ if !isMagpie("nb/x") {
+ t.Fatal("a provider saved since not seen")
+ }
+}
diff --git a/internal/agent/route.go b/internal/agent/route.go
index 46cd6378a..905a159f2 100644
--- a/internal/agent/route.go
+++ b/internal/agent/route.go
@@ -4,6 +4,8 @@ import (
"cmp"
"slices"
"strings"
+ "sync"
+ "time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/gateway"
@@ -129,8 +131,59 @@ func isMagpie(v string) bool {
if pid, _, ok := strings.Cut(v, "/"); ok && dryProviders[pid] {
return true
}
- _, _, ok := provider.Resolve(v)
- return ok && strings.Contains(v, "/")
+ if !strings.Contains(v, "/") {
+ return false
+ }
+ return magpieRefs.get(v, func() bool {
+ _, _, ok := provider.Resolve(v)
+ return ok
+ })
+}
+
+// magpieRefs keeps isMagpie's answers while provider.StampNow stays the
+// same, for a minute at most. Outside a request that holds the catalog,
+// each Resolve built every provider's entries again: the window's drift
+// poll, every 15s, asked it for each agent's values, ~6 MB a poll (the
+// procmem heap profile: 235 MB of 1.37 GB in 10 minutes of GUI use).
+var magpieRefs stampedRefs
+
+// stampedRefs is a model value → answer, dropped when the stamp moves,
+// after a minute (what the stamp can't see: an account another program
+// signed in to), or once it holds more values than any config has.
+type stampedRefs struct {
+ mu sync.Mutex
+ stamp provider.Stamp
+ at time.Time
+ of map[string]bool
+}
+
+const (
+ stampedRefsFor = time.Minute
+ stampedRefsMax = 4096
+)
+
+func (s *stampedRefs) get(v string, build func() bool) bool {
+ st := provider.StampNow()
+ s.mu.Lock()
+ if s.stamp != st || time.Since(s.at) >= stampedRefsFor || len(s.of) >= stampedRefsMax {
+ s.stamp, s.at, s.of = st, time.Now(), nil
+ }
+ if ok, found := s.of[v]; found {
+ s.mu.Unlock()
+ return ok
+ }
+ s.mu.Unlock()
+ // built from what is there after st was taken: never older than st
+ ok := build()
+ s.mu.Lock()
+ if s.stamp == st {
+ if s.of == nil {
+ s.of = map[string]bool{}
+ }
+ s.of[v] = ok
+ }
+ s.mu.Unlock()
+ return ok
}
func firstOf(xs []string) string {