Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: a pick a plugin's list leaves for a while comes back when it is listed again (多喝热水 on Discord)
yetone8febe3a50c1b
diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex cc0cf0e30..4038f6581 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -25,7 +25,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi| Native compaction | `NativeCompaction` (`nativeCompaction` in providers.json, the editor's *Compaction* row, `magpie provider set <id> compaction=native\|magpie`; #1099) has Codex's compaction asked of the provider as Codex sent it and its sealed item returned unchanged. Off by default. Only a provider whose wire carries it can be set so (`CarriesCompaction`: a Responses API, a ChatGPT account, GitHub Copilot; not another agent's sign-in or a plugin's), and `CompactsOn(model)` also needs the model asked on Responses. `SetNativeCompaction`, the CLI and the GUI's save refuse it elsewhere. An account keeps it through `Save`'s picks (`withPicks`), and `AddCopy` carries it. The gateway's half is in [gateway-routing](gateway-routing.md). | [`native_compaction.go`](../../internal/provider/native_compaction.go) || providers.json | `Path` is `providers.json` in magpie's config folder. A read that fails returns `ErrUnreadable` and is never treated as empty. `store` writes atomically through a rename and drops the cached catalog (`catalog.Touched`). | [`provider.go`](../../internal/provider/provider.go) || The list | `All` merges stored providers with `Accounts()`. An entry with no URL stores the user's picks for an account. The order is `file.Order`. Reads within one request share one build (`Hold`, #746). | [`provider.go`](../../internal/provider/provider.go) |-| Model picks | `Models` are the models the user picked for agents; none means the vendor's list, up to 24 (`Exposed`), unless the user chose Select none (`NoModels`, `magpie provider models <id> none`): then none of its models is served, not to agents, not to any routing group (auto groups included), not by a `provider/model` id, not as a classifier, while its balance and usage are still read (`ServesNone`, #1818). The editor's serves control names it Usage only, beside Vendor's list and Picked only, and the card says "usage only" (#2068); Picked only from there picks the models the vendor's list serves, to unpick from. A file without `noModels` reads as before, and a pick clears it. `Save` keeps beside new picks the models listed then (`PickedFrom`, `listedWith`), and the same picks saved again keep it. When the picks held every model listed then, one the vendor lists since is served first beside them (`withNewlyListed`, `Picks`); one listed then and left unpicked stays out. Picks saved before `PickedFrom` was kept are taken to hold every model when they hold every one released up to the newest of them (models.dev's `release_date`), so a Claude account whose user picked them all is served Haiku 5.5 (wakaka). The editor and the TUI start from `Picks`, so a later Save keeps the new model. | [`models.go`](../../internal/provider/models.go), [`provider.go`](../../internal/provider/provider.go) |+| Model picks | `Models` are the models the user picked for agents; none means the vendor's list, up to 24 (`Exposed`), unless the user chose Select none (`NoModels`, `magpie provider models <id> none`): then none of its models is served, not to agents, not to any routing group (auto groups included), not by a `provider/model` id, not as a classifier, while its balance and usage are still read (`ServesNone`, #1818). The editor's serves control names it Usage only, beside Vendor's list and Picked only, and the card says "usage only" (#2068); Picked only from there picks the models the vendor's list serves, to unpick from. A file without `noModels` reads as before, and a pick clears it. `Save` keeps beside new picks the models listed then (`PickedFrom`, `listedWith`), and the same picks saved again keep it. When the picks held every model listed then, one the vendor lists since is served first beside them (`withNewlyListed`, `Picks`); one listed then and left unpicked stays out. Picks saved before `PickedFrom` was kept are taken to hold every model when they hold every one released up to the newest of them (models.dev's `release_date`), so a Claude account whose user picked them all is served Haiku 5.5 (wakaka). The editor and the TUI start from `Picks`, so a later Save keeps the new model. While some picks are set aside (the list is known to be short), a save with new picks keeps the models the earlier `PickedFrom` had beside those listed now, so a model the user saw and left unpicked isn't new when that list has it again; a list short of no pick is taken as it is. A pick a list fetched again stops listing (`Refetch`, `KeepListsFresh`, and a plugin's hourly `KeepPluginsListed`) leaves `Models` for `Delisted` (`dropGonePicks`), not served, and goes back among the picks when a later list has it (`relistPicks`): Our Free Model's plugin lists its account channels' models only once its service has read them after a restart, and its free pools only while they answer, so each magpie update had its user pick every model again (多喝热水 on Discord). Every save keeps `Delisted` but for what it picks again (`delistedWith`); Select none clears it. A provider whose every pick is in `Delisted` serves none of its models (`waitsForPicks`), not the vendor's list. `KeepPluginsListed` asks again after `relistSoon` (5 minutes) when a list left a pick out. `TestPluginPicksComeBackWithTheirList`, `TestEveryPickDelistedServesNone` and `TestRelayPicksComeBackWithTheirList` cover it. | [`models.go`](../../internal/provider/models.go), [`provider.go`](../../internal/provider/provider.go) || List schedule and rules | A provider of the user's own (not an account, a plugin's or a decision-only one) can set `RefreshEvery` (`magpie provider refresh-every <id> 6h`, or 1d, or off, at least 10m) and `AutoInclude` (`magpie provider auto-include <id> 'gpt-*' 're:claude-.*'`, or none; a glob with `*`, any case, or `re:` matching the whole id; a plain id is refused) (#2135). The gateway (`magpie serve` or the app) looks each minute and fetches a list once `Fetched` is that old, through the same `Refetch` as Fetch models (`KeepListsFresh`); a failed fetch waits `min(every, 15m)`. `RefetchTaking` adds to the picks a model the fetch newly lists that a rule matches (`takeMatching`), starting from what is served now (`Exposed`), so nothing served is taken away and a model the user unticks stays out (it is not new to the next fetch). Setting the rules takes the current list's matches. Every save that doesn't set them keeps them, the window's editor among them; the editor has no fields for them yet. | [`autolist.go`](../../internal/provider/autolist.go), [`models.go`](../../internal/provider/models.go), [`providers_cli.go`](../../internal/cli/providers_cli.go) || Keys | Several keys per provider. Requests go to the first key that is on; the gateway moves on when it is out or rate limited. `Key` stays the first, so other code reads it as before. A key can be made for one protocol (`KeyProtocol`). A key made for a protocol its provider has no URL for is skipped (`perKeyBarred`); when every key is, the provider is still tried whole on the URLs it has, and the candidate carries `noURL` so it isn't quiet: Routing's row (`Weighed.NoURL`) says "key set to {api} only, no {api} URL · sent on the others", and the editor's key row shows a "No {api} URL" chip (`keyLacksURL`; #1702, `TestKeyForAProtocolWithNoURLIsSaid`, `key-nourl.test.cjs`). A key's value can be replaced in place (`ReplaceKey`, #1869: a key rotated at the vendor): it keeps its name, place, protocol, weight and on or off, and what was set for it by its fingerprint (`AccountModels`, `AccountProxies`, `AccountConcurrency`) and the gateway keys held to it (`access.ReplaceAccount`) move to the new one; the GUI's `keys/replace` also lifts its rests (`UnrestAll`). Its past requests stay under the old fingerprint, by the same name. A provider may hold thousands of keys (@NetRozee on X: a thousand froze magpie). Nothing goes over its keys once for each key: `KeysOn` drops a repeat by a set, a row's rest keys come from `RestKeys` (whether several are on, found once), and whether each key serves a model from `Serving`, which reads the lists once and keeps each model's keys as a set until the lists are read anew (`keyLists`). `KeysListed` and `ListsRead` count them for the tests (`TestProviderInfoListsKeysOnce`, `TestPerKeyReadsListsOnce`). A provider's keys have their balances, plans and remote limits read `keyReadsAtOnce` (8) at a time (`keySlots`); other providers' beside them. The editor's per-key proxy lines fold as its Accounts list does (`proxyLines`, `provider-many-keys-proxy.test.cjs`). Asking a key's models (`fetchPerKey`) still goes one key after another. | [`keys.go`](../../internal/provider/keys.go) || Google Vertex AI | `google-vertex` (`VertexPreset`, `IsVertex`) is a provider of the user's own with no key. `Provider.Vertex` holds `project`, `location` (`global`, `us`, `eu` or a region; `global` when empty), `credentials` (a file; empty for Application Default Credentials) and `impersonate` (a service account's email). Save refuses a key given it, then normalizes them (`Normal`) and checks each (`check`); `normalize` clears the four URLs and any key put in `providers.json` by hand, which stands in for neither a token nor a project and leaves the provider as found savable, so the provider's one address is `vertexBase`, `https://<host>/v1/projects/<project>/locations/<location>`, its host following the location (`vertexHost`). It speaks Gemini alone (`Speaks`, `Base`). It is `Ready` once that address can be made, never from a key or an account; until then the app, `magpie providers` and the TUI say it needs a project, and `magpie provider <id>` says how to give it one. `hasEndpoint` counts it as a provider of the user's own, not a subscription's picks. An import link naming it is refused (`ParseImport`): its project and credentials are the user's own, which no link hands over. `Sign` refuses any other address and sets the bearer token before the user's `Headers`; `AuthHeaders` gives it no key header. Its models are its location's (`vertexModels`, in `planModels`): the preset has `NoList`, because Vertex AI has no list that says what a location serves. The lists are in the [reference](../reference.md#google-vertex-ai). | [`vertex.go`](../../internal/provider/vertex.go), [`provider.go`](../../internal/provider/provider.go), [`account.go`](../../internal/provider/account.go), [`rename.go`](../../internal/provider/rename.go), [`models.go`](../../internal/provider/models.go), [`importurl.go`](../../internal/provider/importurl.go) |diff --git a/internal/provider/models.go b/internal/provider/models.goindex be33d6441..f2ccc3e3e 100644--- a/internal/provider/models.go+++ b/internal/provider/models.go@@ -257,9 +257,18 @@ func (p Provider) refetch(ctx context.Context) ([]catalog.Model, []string, map[slog.Println(p.ID + ": " + derr.Error())}}- if err != nil || len(before) == 0 {+ if err != nil {return ms, nil, before, err}+ if len(before) == 0 {+ // nothing to drop against, but a pick set aside comes back+ if now := liveIDs(p.ID); len(now) > 0 && len(p.Delisted) > 0 {+ if _, derr := dropGonePicks(p.ID, nil, now); derr != nil {+ log.Println(p.ID + ": " + derr.Error())+ }+ }+ return ms, nil, before, nil+ }now := liveIDs(p.ID)if len(now) == 0 {return ms, nil, before, nil@@ -284,7 +293,9 @@ func liveIDs(id string) map[string]bool {}// dropGonePicks takes out of the saved provider's picks those before had-// and now doesn't, and answers them.+// and now doesn't, and answers them. They are set aside in its Delisted,+// not forgotten, and those of its Delisted now has go back among the picks+// (relistPicks): a list that shrank for a while gives back what it took.func dropGonePicks(id string, before, now map[string]bool) ([]string, error) {f, err := read()if err != nil {@@ -294,21 +305,56 @@ func dropGonePicks(id string, before, now map[string]bool) ([]string, error) {if i < 0 {return nil, nil}+ q := &f.Providers[i]var kept, dropped []string- for _, m := range f.Providers[i].Models {+ for _, m := range q.Models {if before[m] && !now[m] {dropped = append(dropped, m)} else {kept = append(kept, m)}}- if len(dropped) == 0 {+ back := relistPicks(q, now)+ if len(dropped) == 0 && !back {return nil, nil}- f.Providers[i].Models = kept+ if len(dropped) > 0 {+ q.Models = kept+ for _, m := range dropped {+ if !slices.Contains(q.Delisted, m) {+ q.Delisted = append(q.Delisted, m)+ }+ }+ }return dropped, store(f)}+// relistPicks puts back among q's picks those of its Delisted the list now+// has, and says whether it put any back.+func relistPicks(q *Provider, now map[string]bool) bool {+ if len(q.Delisted) == 0 || len(now) == 0 {+ return false+ }+ var still, back []string+ for _, m := range q.Delisted {+ if now[m] {+ back = append(back, m)+ } else {+ still = append(still, m)+ }+ }+ if len(back) == 0 {+ return false+ }+ for _, m := range back {+ if !slices.Contains(q.Models, m) {+ q.Models = append(q.Models, m)+ }+ }+ q.Delisted = still+ return true+}+// fetch is Fetch without the picks looked after.func (p Provider) fetch(ctx context.Context) ([]catalog.Model, error) {ctx = p.Via(ctx)@@ -1382,11 +1428,16 @@ func (p Provider) keyLists() (*keyLists, bool) {// none of its models are served, its balance and usage still read.func (p Provider) ServesNone() bool { return p.NoModels && len(p.Models) == 0 }+// waitsForPicks is a provider whose every pick its list stopped listing+// (Delisted, none left in Models): none are served until one comes back,+// not the vendor's whole list as for a provider nothing was picked of.+func (p Provider) waitsForPicks() bool { return len(p.Models) == 0 && len(p.Delisted) > 0 }+// Exposed lists the models magpie offers to agents for this provider: the// user's picks; else the preset's; else everything, when that is few; none// after Select none (ServesNone).func (p Provider) Exposed() []catalog.Model {- if p.ServesNone() {+ if p.ServesNone() || p.waitsForPicks() {return nil}avail := p.Available()diff --git a/internal/provider/plugin_delisted_test.go b/internal/provider/plugin_delisted_test.gonew file mode 100644index 000000000..0689367f1--- /dev/null+++ b/internal/provider/plugin_delisted_test.go@@ -0,0 +1,243 @@+package provider++import (+ "context"+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "sync/atomic"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/plugin"+ "github.com/yetone/magpie/internal/settings"+)++// ourFree is a provider listing ids as Our Free Model's plugin does: its+// free pools' models, and its account channels' as "channel/model".+func ourFree(ids ...string) []plugin.Provider {+ p := plugin.Provider{ID: "our-free-model", Spec: "magpie-our-free-model", Name: "Our Free Model",+ NPM: "@ai-sdk/openai-compatible"}+ for _, id := range ids {+ p.Models = append(p.Models, plugin.Model{ID: id, Name: id, NPM: "@ai-sdk/openai-compatible"})+ }+ return []plugin.Provider{p}+}++// Our Free Model's service, started again with magpie after an update,+// lists its free pools at once and its account channels' models only once+// it has read them, so the list the gateway's magpie read a minute after+// it started was short. The picks it didn't have were dropped for good,+// and its user picked every model again after each update (多喝热水 on+// Discord). They are set aside now, not served while the list leaves them+// out, and picked again when it lists them; the loop asks again soon after+// a list left a pick out, not an hour later.+func TestPluginPicksComeBackWithTheirList(t *testing.T) {+ claudeHome(t)+ t.Setenv("MAGPIE_BUN", "")+ write := func(name string, v any) {+ t.Helper()+ b, _ := json.Marshal(v)+ if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {+ t.Fatal(err)+ }+ }+ write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": "magpie-our-free-model"}}})+ write("plugin-auth.json", map[string]any{"our-free-model": map[string]any{"type": "api", "key": "managed"}})+ full := ourFree("pool-a", "pool-b", "qoder/claude-x", "kiro/claude-y", "kiro/claude-z")+ plugin.UseCached(full)+ t.Cleanup(func() { plugin.UseCached(nil) })+ // picked in the editor from the full list: kiro/claude-z left unpicked+ if err := Save(Provider{ID: "our-free-model", Models: []string{"pool-a", "qoder/claude-x", "kiro/claude-y"}}); err != nil {+ t.Fatal(err)+ }++ answers := make(chan []plugin.Provider)+ asked := make(chan struct{})+ oldList, oldFirst, oldEvery, oldSoon := relistPlugins, relistFirst, relistEvery, relistSoon+ relistPlugins = func(ctx context.Context) ([]plugin.Provider, error) {+ select {+ case asked <- struct{}{}:+ case <-ctx.Done():+ return nil, ctx.Err()+ }+ select {+ case ps := <-answers:+ plugin.UseCached(ps) // as plugin.Providers keeps it+ return ps, nil+ case <-ctx.Done():+ return nil, ctx.Err()+ }+ }+ // an hour between lists, unless one left a pick out+ relistFirst, relistEvery, relistSoon = time.Millisecond, time.Hour, time.Millisecond+ ctx, cancel := context.WithCancel(context.Background())+ done := make(chan struct{})+ t.Cleanup(func() {+ cancel()+ <-done+ relistPlugins, relistFirst, relistEvery, relistSoon = oldList, oldFirst, oldEvery, oldSoon+ })+ go func() { defer close(done); KeepPluginsListed(ctx) }()+ wait := func(what string) {+ t.Helper()+ select {+ case <-asked:+ case <-time.After(10 * time.Second):+ t.Fatal(what)+ }+ }+ wait("the plugins weren't asked")+ answers <- ourFree("pool-a", "pool-b") // the channels not read yet+ // asked again soon: the hour's wait would fail this+ wait("a list that left picks out wasn't asked for again soon")++ p, err := Find("our-free-model")+ if err != nil {+ t.Fatal(err)+ }+ if !slices.Equal(p.Models, []string{"pool-a"}) || !slices.Equal(p.Delisted, []string{"qoder/claude-x", "kiro/claude-y"}) {+ t.Fatalf("short list: picks %v, set aside %v", p.Models, p.Delisted)+ }+ if got := servedIDs(t, "our-free-model"); !slices.Equal(got, []string{"pool-a"}) {+ t.Fatalf("served while the list is short: %v", got)+ }+ // the editor saves something else, sending no Delisted: kept+ p.Models = []string{"pool-a", "pool-b"}+ if err := Save(*p); err != nil {+ t.Fatal(err)+ }++ answers <- full+ select {+ case <-asked:+ t.Fatal("asked again soon after a list that left nothing out")+ case <-time.After(200 * time.Millisecond):+ }+ p, err = Find("our-free-model")+ if err != nil {+ t.Fatal(err)+ }+ if !slices.Equal(p.Models, []string{"pool-a", "pool-b", "qoder/claude-x", "kiro/claude-y"}) || len(p.Delisted) != 0 {+ t.Fatalf("full list again: picks %v, set aside %v", p.Models, p.Delisted)+ }+ if got := servedIDs(t, "our-free-model"); slices.Contains(got, "kiro/claude-z") {+ t.Fatalf("a model left unpicked is served: %v", got)+ }+}++// Every pick set aside: none of the plugin's models is served, not the+// whole list as for a provider nothing was ever picked of; Select none+// forgets what was set aside.+func TestEveryPickDelistedServesNone(t *testing.T) {+ claudeHome(t)+ t.Setenv("MAGPIE_BUN", "")+ if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {+ t.Fatal(err)+ }+ for name, v := range map[string]any{+ "plugins.json": map[string]any{"plugins": []map[string]any{{"spec": "magpie-our-free-model"}}},+ "plugin-auth.json": map[string]any{"our-free-model": map[string]any{"type": "api", "key": "managed"}},+ } {+ b, _ := json.Marshal(v)+ if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {+ t.Fatal(err)+ }+ }+ before := ourFree("pool-a", "kiro/claude-y")+ plugin.UseCached(before)+ t.Cleanup(func() { plugin.UseCached(nil) })+ if err := Save(Provider{ID: "our-free-model", Models: []string{"kiro/claude-y"}}); err != nil {+ t.Fatal(err)+ }+ now := ourFree("pool-a")+ plugin.UseCached(now)+ if got := dropGonePluginPicks(before, now)["our-free-model"]; !slices.Equal(got, []string{"kiro/claude-y"}) {+ t.Fatalf("dropped %v", got)+ }+ if got := servedIDs(t, "our-free-model"); len(got) != 0 {+ t.Fatalf("served with every pick set aside: %v", got)+ }+ // another setting saved: still waiting for its picks+ p, _ := Find("our-free-model")+ p.Off = false+ p.MaxRPM = 10+ if err := Save(*p); err != nil {+ t.Fatal(err)+ }+ if got := servedIDs(t, "our-free-model"); len(got) != 0 {+ t.Fatalf("served after another setting was saved: %v", got)+ }+ p, _ = Find("our-free-model")+ p.Models, p.NoModels = nil, true+ if err := Save(*p); err != nil {+ t.Fatal(err)+ }+ if p, _ = Find("our-free-model"); len(p.Delisted) != 0 {+ t.Fatalf("Select none kept %v set aside", p.Delisted)+ }+}++// The same for a provider of the user's own whose list is fetched again,+// by Fetch models or by itself (RefreshEvery): a relay that leaves a model+// out of one list gives the pick back with the list that has it.+func TestRelayPicksComeBackWithTheirList(t *testing.T) {+ oneHome(t)+ var short atomic.Bool+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if !strings.HasSuffix(r.URL.Path, "/models") {+ http.NotFound(w, r)+ return+ }+ ids := []string{"m-a", "m-b", "m-c"}+ if short.Load() {+ ids = []string{"m-a", "m-c"}+ }+ var data []map[string]string+ for _, id := range ids {+ data = append(data, map[string]string{"id": id, "object": "model"})+ }+ _ = json.NewEncoder(w).Encode(map[string]any{"object": "list", "data": data})+ }))+ t.Cleanup(srv.Close)+ if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: srv.URL + "/v1"}); err != nil {+ t.Fatal(err)+ }+ refetch := func() []string {+ t.Helper()+ p, err := Find("relay")+ if err != nil {+ t.Fatal(err)+ }+ _, dropped, err := p.Refetch(context.Background())+ if err != nil {+ t.Fatal(err)+ }+ return dropped+ }+ refetch()+ p, _ := Find("relay")+ p.Models = []string{"m-a", "m-b"}+ if err := Save(*p); err != nil {+ t.Fatal(err)+ }+ short.Store(true)+ if got := refetch(); !slices.Equal(got, []string{"m-b"}) {+ t.Fatalf("dropped %v", got)+ }+ if got := servedIDs(t, "relay"); !slices.Equal(got, []string{"m-a"}) {+ t.Fatalf("served while m-b isn't listed: %v", got)+ }+ short.Store(false)+ refetch()+ if p, _ = Find("relay"); !slices.Equal(p.Models, []string{"m-a", "m-b"}) || len(p.Delisted) != 0 {+ t.Fatalf("listed again: picks %v, set aside %v", p.Models, p.Delisted)+ }+}diff --git a/internal/provider/plugin_relist.go b/internal/provider/plugin_relist.goindex a3acf0c5a..5d488a197 100644--- a/internal/provider/plugin_relist.go+++ b/internal/provider/plugin_relist.go@@ -9,6 +9,11 @@ package provider// the user's picks too, as Refetch does for a built-in's. A list that// fell back (the vendor's couldn't be read) is the one told before// (plugin.keepListed): nothing is dropped for it.+//+// A pick that leaves is set aside (Delisted), not forgotten, and comes back+// when a list has it again: Our Free Model's list is short for a while+// after its service starts and while a free pool is down, and each magpie+// update left its user to pick every model again (多喝热水 on Discord).import ("context"@@ -20,10 +25,13 @@ import ()// relistEvery is how often the gateway's magpie lists the plugins'-// models again; relistFirst is how long after it starts it first does.+// models again; relistFirst is how long after it starts it first does;+// relistSoon is how soon it asks again after a list left out a pick, as+// a plugin's service just started may not have listed everything yet.var (relistEvery = time.HourrelistFirst = time.Minute+ relistSoon = 5 * time.Minute)// relistPlugins asks the plugins for their providers and models again,@@ -53,6 +61,7 @@ func KeepPluginsListed(ctx context.Context) {if len(before) == 0 {before = plugin.Cached()}+ next := relistEveryc, cancel := context.WithTimeout(ctx, 2*time.Minute)now, err := relistPlugins(c)cancel()@@ -60,11 +69,12 @@ func KeepPluginsListed(ctx context.Context) {log.Printf("listing the plugins' models again: %s (the last lists stay)", err)} else if now != nil {for id, gone := range dropGonePluginPicks(before, now) {- log.Printf("%s: %v left its plugin's list, and its picks", id, gone)+ log.Printf("%s: %v left its plugin's list, and its picks until it lists them again", id, gone)+ next = relistSoon}before = keptListed(before, now)}- t.Reset(relistEvery)+ t.Reset(next)}}@@ -91,12 +101,10 @@ func pluginListedIDs(ps []plugin.Provider) map[string]map[string]bool {func dropGonePluginPicks(before, now []plugin.Provider) map[string][]string {was, is := pluginListedIDs(before), pluginListedIDs(now)out := map[string][]string{}- for id, b := range was {- n, ok := is[id]- if !ok {- continue // not listed now, or fell back: nothing is known gone- }- dropped, err := dropGonePicks(id, b, n)+ // by what is listed now: not listed now, or fell back, nothing is known+ // gone; listed now and not before, a pick set aside may come back+ for id, n := range is {+ dropped, err := dropGonePicks(id, was[id], n)if err != nil {log.Println(id + ": " + err.Error())}diff --git a/internal/provider/plugin_relist_test.go b/internal/provider/plugin_relist_test.goindex a24928a48..379a930b8 100644--- a/internal/provider/plugin_relist_test.go+++ b/internal/provider/plugin_relist_test.go@@ -50,7 +50,7 @@ func TestPluginsListedAgainWhileRunning(t *testing.T) {answers := make(chan []plugin.Provider)asked := make(chan struct{})- oldList, oldFirst, oldEvery := relistPlugins, relistFirst, relistEvery+ oldList, oldFirst, oldEvery, oldSoon := relistPlugins, relistFirst, relistEvery, relistSoon// asked says the loop asks again, so all it did with the last answer// is donerelistPlugins = func(ctx context.Context) ([]plugin.Provider, error) {@@ -69,13 +69,13 @@ func TestPluginsListedAgainWhileRunning(t *testing.T) {return nil, ctx.Err()}}- relistFirst, relistEvery = time.Millisecond, time.Millisecond+ relistFirst, relistEvery, relistSoon = time.Millisecond, time.Millisecond, time.Millisecondctx, cancel := context.WithCancel(context.Background())done := make(chan struct{})t.Cleanup(func() {cancel()<-done- relistPlugins, relistFirst, relistEvery = oldList, oldFirst, oldEvery+ relistPlugins, relistFirst, relistEvery, relistSoon = oldList, oldFirst, oldEvery, oldSoon})go func() { defer close(done); KeepPluginsListed(ctx) }()select {diff --git a/internal/provider/provider.go b/internal/provider/provider.goindex 5f3882968..51cec5edb 100644--- a/internal/provider/provider.go+++ b/internal/provider/provider.go@@ -360,6 +360,15 @@ type Provider struct {// listed since is served beside picks that held every one listed then// (withNewlyListed), and one listed then and left unpicked stays out.PickedFrom []string `json:"pickedFrom,omitempty"`+ // Delisted are picks the provider's list stopped listing (dropGonePicks):+ // out of Models, so not served, until a list has them again, when they+ // go back among the picks (relistPicks). A plugin's list shrinks for a+ // while and comes back (多喝热水 on Discord: Our Free Model lists its+ // account channels' models only once its service has read them after a+ // restart, and a free pool it finds down leaves its list until it is+ // up), and the user's picks came back with it no more. Every save keeps+ // it but for what it picks again; Select none clears it.+ Delisted []string `json:"delisted,omitempty"`// RefreshEvery, for a provider of the user's own, is how often the// gateway fetches its model list by itself ("6h", "1d"; none, only when// asked), as Fetch models does (#2135). See autolist.go.@@ -586,6 +595,7 @@ func (p Provider) clone() Provider {p.Fallback = slices.Clone(p.Fallback)p.Models = slices.Clone(p.Models)p.PickedFrom = slices.Clone(p.PickedFrom)+ p.Delisted = slices.Clone(p.Delisted)p.AutoInclude = slices.Clone(p.AutoInclude)p.Headers = maps.Clone(p.Headers)p.ExtraBody = maps.Clone(p.ExtraBody)@@ -663,7 +673,7 @@ func allProviders() []Provider {// the provider keeps the same settings (#1899: a second WorkBuddy AI// account queued 62s with the wait set to 1s).func withPicks(a, pk Provider) Provider {- a.PickedFrom = pk.PickedFrom+ a.PickedFrom, a.Delisted = pk.PickedFrom, pk.Delisteda.Models, a.Unlisted, a.Off, a.Fallback, a.Routing, a.Affinity, a.KeepLogin, a.KeepLoginAs, a.Contexts, a.Family = pk.Models, pk.Unlisted, pk.Off, pk.Fallback, pk.Routing, pk.Affinity, pk.KeepLogin, pk.KeepLoginAs, pk.Contexts, pk.Familya.SwitchOnly = pk.SwitchOnlya.Sink, a.NoModels = pk.Sink, pk.NoModels@@ -791,7 +801,7 @@ func Save(p Provider) error {if p.ID == "kiro" {key = p.Key}- p = Provider{ID: p.ID, Key: key, Models: p.Models, NoModels: p.NoModels, PickedFrom: p.PickedFrom, Unlisted: p.Unlisted, Off: p.Off, Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, Contexts: p.Contexts, Family: p.Family, Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, MaxConcurrency: p.MaxConcurrency, AccountConcurrency: p.AccountConcurrency, QueueLimit: p.QueueLimit, QueueWait: p.QueueWait, MaxRPM: p.MaxRPM, QuotaRest: p.QuotaRest, QuotaResetAt: p.QuotaResetAt, RestCap: p.RestCap, PinUpstream: p.PinUpstream, NativeCompaction: p.NativeCompaction, Hidden: hiddenAccount(p.ID), Quiet: quietAccount(p.ID), Tucked: tuckedAccount(p.ID)}+ p = Provider{ID: p.ID, Key: key, Models: p.Models, NoModels: p.NoModels, PickedFrom: p.PickedFrom, Delisted: p.Delisted, Unlisted: p.Unlisted, Off: p.Off, Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, Contexts: p.Contexts, Family: p.Family, Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountNames: p.AccountNames, MaxConcurrency: p.MaxConcurrency, AccountConcurrency: p.AccountConcurrency, QueueLimit: p.QueueLimit, QueueWait: p.QueueWait, MaxRPM: p.MaxRPM, QuotaRest: p.QuotaRest, QuotaResetAt: p.QuotaResetAt, RestCap: p.RestCap, PinUpstream: p.PinUpstream, NativeCompaction: p.NativeCompaction, Hidden: hiddenAccount(p.ID), Quiet: quietAccount(p.ID), Tucked: tuckedAccount(p.ID)}} else {p.AccountProxies = keyProxies(p) // a provider of keys proxies each key apartif subscriptionID(p.ID) && !stored(p.ID) {@@ -828,6 +838,7 @@ func Save(p Provider) error {was = &f.Providers[i]}p.PickedFrom = listedWith(p, was)+ p.Delisted = delistedWith(p, was)if was != nil {if p.Was == nil {p.Was = was.Was@@ -864,9 +875,34 @@ func sameStored(a, b Provider) bool {return err == nil && bytes.Equal(x, y)}+// delistedWith is what p.Delisted is saved as: what was set aside before,+// as an editor sends none and couldn't show them to unpick, less what p+// picks again; none after Select none, or the vendor's list picked in+// place of picks (no picks saved over some).+func delistedWith(p Provider, was *Provider) []string {+ if p.ServesNone() || len(p.Models) == 0 && was != nil && len(was.Models) > 0 {+ return nil+ }+ d := p.Delisted+ if d == nil && was != nil {+ d = was.Delisted+ }+ d = slices.DeleteFunc(slices.Clone(d), func(id string) bool { return slices.Contains(p.Models, id) })+ if len(d) == 0 {+ return nil+ }+ return d+}+// listedWith is what p.PickedFrom is saved as: none without picks; what was// kept with the picks when they are the same picks; and the models listed-// now when the picks are new, as the user picked them from that list.+// now when the picks are new, as the user picked them from that list. While+// some picks are set aside, the list is known to be short, so those listed+// when the picks were saved before are kept too: a model the user saw and+// left unpicked isn't new when the list that left it out for a while has it+// again (an Our Free Model channel not read yet when the picks were saved).+// A list short of no pick is taken as it is: what it left out and lists+// again later is new (TestClaudePicksOfEveryModelTakeOnesListedSince).func listedWith(p Provider, was *Provider) []string {if len(p.Models) == 0 {return nil@@ -882,6 +918,13 @@ func listedWith(p Provider, was *Provider) []string {for _, m := range from.Available() {ids = append(ids, m.ID)}+ if was != nil && len(ids) > 0 && len(delistedWith(p, was)) > 0 {+ for _, id := range was.PickedFrom {+ if !slices.Contains(ids, id) {+ ids = append(ids, id)+ }+ }+ }return ids}