provider: a plugin account whose model list fell back serves every model

bitssly on X (商汤小浣熊, v0.1.4573, Windows 10): three accounts ticked,
In order, and sn-kimi-k3-1-pro's chain held two of them. The third, a
one_time_purchase account, was in no Routing row for the model.

An account's own list comes from its plugin's models hook. When the
hook can't read the vendor's list for that account (raccoon's
model_catalog failing for its token), it hands back the plugin's
defaults, and the host marks the account fellBack. keepListed puts back
the last list read for it, but an account that never had one kept the
defaults as its "plan", and pluginLists read them as such: Raccoon's
defaults have sn-kimi-k3 but not sn-kimi-k3-1-pro, so AccountsFor put
the account in left, which is asked only after a listing account
refuses the model as unserved, never after a 429. The host's own
comment says an account whose list can't be read is taken to have them
all, as a built-in whose fetch failed (catalog.Live not ok) is.

toldList now reads a fell-back account's list as none told, in each
place that reads plugin.Account.Models: pluginLists (routing),
pluginAccountCatalog and pluginServedCatalog (the models it offers),
and pluginAccountListed (a key's models prefill).

Test: TestFellBackPluginAccountServesEveryModel, the reporter's shape
(three raccoon accounts, the middle one fell back to the plugin's
defaults): red on the old code ("sn-kimi-k3-1-pro goes to [Signed in
Signed in (3)] (left out 1, barred 0)"), green now; a list really told
still leaves sn-kimi-k3 out of the two accounts that lack it.

Run on mote: go test ./internal/provider ./internal/plugin -count=1
-timeout 45m: ok, ok. gofmt -l clean.
Not run: the whole suite (mote at load 41, mica at 44); the gateway
package reads this through AccountsFor only.
Not tried: a real Raccoon account (none here) - which vendor answer made
the reporter's account fall back is not known; the plugin side marks
its fallback explicitly in magpie-community/plugins.
This commit is contained in:
yetone
2026-11-20 10:54:42 +08:00
parent 76b490576f
commit 7dc85f26e6
3 changed files with 99 additions and 4 deletions
+1 -1
View File
@@ -193,7 +193,7 @@ func pluginAccountListed(p Provider, ref string) []string {
return nil
}
for _, l := range pluginLogins(pp) {
if accountKey(l.User) != accountKey(ref) || l.acct.Models == nil {
if accountKey(l.User) != accountKey(ref) || toldList(l.acct) == nil {
continue
}
out := make([]string, 0, len(l.acct.Models))
@@ -0,0 +1,81 @@
package provider
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// An account whose list fell back (its plugin couldn't read the vendor's
// list for it and gave its own defaults back) doesn't say what its plan
// has: it serves every model, as a built-in account whose fetch failed
// does, never only the plugin's short defaults. bitssly on X (商汤小浣熊,
// v0.1.4573): three accounts ticked, the one_time_purchase one left out
// of sn-kimi-k3-1-pro's chain, as the plugin's defaults lack the model.
func TestFellBackPluginAccountServesEveryModel(t *testing.T) {
claudeHome(t)
t.Setenv("MAGPIE_BUN", "")
spec := "@magpie-community/opencode-raccoon-auth"
write := func(name string, v any) {
t.Helper()
b, _ := json.Marshal(v)
os.MkdirAll(settings.Dir(), 0o700)
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": spec}}})
write("plugin-auth.json", map[string]map[string]any{
"raccoon": {"type": "oauth", "access": "a1", "refresh": "r1", "expires": 4102444800000},
"raccoon#2": {"type": "oauth", "access": "a2", "refresh": "r2", "expires": 4102444800000},
"raccoon#3": {"type": "oauth", "access": "a3", "refresh": "r3", "expires": 4102444800000},
})
live := []string{"sn-glm-5-3", "sn-kimi-k3-1-pro", "sn-sensenova-6-8-flash"}
defaults := []string{"raccoon-8c4485", "sn-glm-5-3", "sn-kimi-k3", "sn-sensenova-6-8-flash"}
plugin.UseCached([]plugin.Provider{{ID: "raccoon", Spec: spec, Name: "商汤小浣熊",
Methods: []plugin.Method{{Type: "oauth", Label: "Sign in"}},
Accounts: []plugin.Account{
{Key: "raccoon", Type: "oauth", Models: live},
{Key: "raccoon#2", Type: "oauth", Models: defaults, FellBack: true, ListError: "HTTP 403"},
{Key: "raccoon#3", Type: "oauth", Models: live},
},
Models: listedModels(t, `[{"id": "raccoon-8c4485", "name": "Raccoon-Work"}, {"id": "sn-glm-5-3", "name": "GLM-5-3"}, {"id": "sn-kimi-k3", "name": "Kimi-K3"}, {"id": "sn-kimi-k3-1-pro", "name": "Kimi-K3.1-Pro"}, {"id": "sn-sensenova-6-8-flash", "name": "SenseNova-6.8-Flash"}]`),
}})
t.Cleanup(func() { plugin.UseCached(nil) })
saveLogins(t,
savedLogin{Agent: "plugin:raccoon", User: "free-a", Home: "raccoon", On: true},
savedLogin{Agent: "plugin:raccoon", User: "paid-b", Home: "raccoon#2", On: true},
savedLogin{Agent: "plugin:raccoon", User: "free-c", Home: "raccoon#3", On: true},
)
ps := pluginAccounts()
i := slices.IndexFunc(ps, func(p Provider) bool { return p.ID == "raccoon" })
if i < 0 {
t.Fatalf("no raccoon provider in %v", ps)
}
out, left, barred := ps[i].AccountsFor("sn-kimi-k3-1-pro", nil)
var users []string
for _, a := range out {
users = append(users, a.Account.User)
}
if len(out) != 3 || len(left) != 0 || len(barred) != 0 {
t.Fatalf("sn-kimi-k3-1-pro goes to %v (left out %d, barred %d), want all three accounts", users, len(left), len(barred))
}
// a list the plugin really told still leaves a model out
_, left, _ = ps[i].AccountsFor("sn-kimi-k3", nil)
if len(left) != 2 {
t.Fatalf("sn-kimi-k3 leaves out %d accounts, want the two whose own list lacks it", len(left))
}
for _, l := range pluginLogins(mustCached(t, "raccoon")) {
if l.acct.Key != "raccoon#2" {
continue
}
if got := pluginAccountListed(ps[i], l.User); got != nil {
t.Fatalf("the fell-back account's own list is %v, want none told", got)
}
}
}
+17 -3
View File
@@ -473,13 +473,27 @@ func pluginPrice(m plugin.Model) (catalog.Price, bool) {
func pluginAccountCatalog(pp plugin.Provider, key string) []catalog.Model {
all := pluginCatalog(pp)
for _, a := range pp.Accounts {
if a.Key == key && a.Models != nil {
if a.Key == key && toldList(a) != nil {
return slices.DeleteFunc(all, func(m catalog.Model) bool { return !slices.Contains(a.Models, m.ID) })
}
}
return all
}
// toldList is the models account a has as its plugin told them, nil when
// it told none: a list that fell back (the plugin's own defaults, its
// vendor's list for the account unread, with no list read before for
// keepListed to keep) says nothing of the account's plan, as a built-in
// account whose fetch failed is taken to have every model. bitssly on X
// (商汤小浣熊): one of three accounts fell back, and Routing left it out
// of every model the plugin's defaults lack.
func toldList(a plugin.Account) []string {
if a.FellBack {
return nil
}
return a.Models
}
// PluginListError is why the plugin couldn't list the account at key's
// models, "" when it did or kept the list it had: what it shows instead
// is its short defaults (Cursor's Auto alone).
@@ -516,7 +530,7 @@ func pluginServedCatalog(pp plugin.Provider, keys []string) []catalog.Model {
listed := map[string]bool{}
for _, k := range keys {
i := slices.IndexFunc(pp.Accounts, func(a plugin.Account) bool { return a.Key == k })
if i < 0 || pp.Accounts[i].Models == nil {
if i < 0 || toldList(pp.Accounts[i]) == nil {
return all
}
for _, m := range pp.Accounts[i].Models {
@@ -534,7 +548,7 @@ func (a *Account) pluginLists(model string) bool {
pp = cur
}
for _, ac := range pp.Accounts {
if ac.Key == a.pluginKey && ac.Models != nil {
if ac.Key == a.pluginKey && toldList(ac) != nil {
return slices.Contains(ac.Models, model)
}
}