fallback: a model unchecked in its provider's list is not a fallback to go to (#3911)

The sibling of the group fix before this: a provider's Fallback list
resolved each entry with Resolve, so a fallback the user unchecked in
its own provider's list was still sent to, while the fallback editor
says "No provider serves {id} now; it is skipped". plan's fallbacks,
and the places that read the same list (carefulOf, native compaction's
carrier check, unredactedRoute, codexFallsBack), now resolve through
provider.ResolveRoute, which skips it. ResolveRoute scans the entries
once for the one model rather than building a map per request.

Test: fallback_unpicked_test.go, plan/m answering 429 with fallbacks
a/x (unchecked) and b/x goes to b and never to a.

Run (mote): red on the previous commit's tree ("from ka", a tried
[ka]); green with this one. Full suite as recorded in the previous
commit, which ran at this tree: 76 packages ok, internal/plugin's
TestGatewayHookMixedPiPackage red at load ~134 (#2865), passing alone.
go vet darwin/linux/windows clean.

Not tried: a real provider with a fallback in the app.

Co-authored-by: timonwong <timon86.wang@gmail.com>
This commit is contained in:
yetone
2026-10-28 17:34:16 +08:00
co-authored by timonwong
parent f561cc5d9b
commit 25fc92d59a
8 changed files with 41 additions and 9 deletions
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -129,7 +129,7 @@ func carefulOf(p provider.Provider, model string, ms []provider.Member) ([]provi
}
ps, models = append(ps, p), append(models, model)
for _, id := range p.Fallback {
if fp, fm, ok := provider.Resolve(id); ok {
if fp, fm, ok := provider.ResolveRoute(id); ok {
ps, models = append(ps, fp), append(models, fm)
}
}
+1 -1
View File
@@ -1415,7 +1415,7 @@ func (s *Server) codexFallsBack(model string, body []byte) bool {
return false
}
for _, id := range p.Fallback {
if _, _, ok := provider.Resolve(id); ok {
if _, _, ok := provider.ResolveRoute(id); ok {
return true
}
}
+1 -1
View File
@@ -432,7 +432,7 @@ func (s *Server) plan(p provider.Provider, model string, from provider.Protocol)
}
seen := map[string]bool{p.ID + "/" + model: true}
for _, id := range p.Fallback {
fp, fm, ok := provider.Resolve(id)
fp, fm, ok := provider.ResolveRoute(id)
if !ok || seen[fp.ID+"/"+fm] {
continue
}
@@ -0,0 +1,31 @@
package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A provider's fallback the user unchecked in its own provider's list is
// skipped as a group's member is (#3911): the fallback editor says "No
// provider serves it now; it is skipped", and the gateway did send there.
func TestFallbackSkipsModelRemovedFromProviderPicks(t *testing.T) {
fresh(t)
plan, a, b := &keyed{fail: map[string]int{"kp": 429}}, &keyed{}, &keyed{}
serveOn(t, "a", "ka", []string{"x", "y"}, a)
serveOn(t, "b", "kb", []string{"x"}, b)
up := httptest.NewServer(plan)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "plan", Name: "Plan", Key: "kp", Models: []string{"m"}, Chat: up.URL + "/v1", Fallback: []string{"a/x", "b/x"}}); err != nil {
t.Fatal(err)
}
if err := provider.SetModels("a", []string{"y"}); err != nil {
t.Fatal(err)
}
code, body := postAs(t, New(), "", `{"model":"plan/m","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from kb") || len(a.tried) != 0 || len(plan.tried) != 1 {
t.Fatalf("plan/m, its fallback a/x unchecked: %d %s, plan tried %v, a tried %v, b tried %v", code, body, plan.tried, a.tried, b.tried)
}
}
+1 -1
View File
@@ -118,7 +118,7 @@ func (s *Server) nativeCompactionFor(asked string) bool {
}
seen := map[string]bool{p.ID + "/" + model: true}
for _, id := range p.Fallback {
fp, fm, ok := provider.Resolve(id)
fp, fm, ok := provider.ResolveRoute(id)
if !ok || seen[fp.ID+"/"+fm] {
continue
}
+1 -1
View File
@@ -305,7 +305,7 @@ func unredactedRoute(p provider.Provider, isGroup bool, ms []provider.Member) bo
}
for _, id := range p.Fallback {
// one that resolves to nothing isn't tried (plan)
if fp, _, ok := provider.Resolve(id); ok && !fp.SkipsRedaction() {
if fp, _, ok := provider.ResolveRoute(id); ok && !fp.SkipsRedaction() {
return false
}
}
+4 -3
View File
@@ -942,9 +942,10 @@ func unpicked(find entryOf, p Provider, m string) bool {
return len(p.Models) > 0 && find(p.ID, m) < 0 && !p.DecidesModel(m)
}
// ResolveRoute is Resolve for a model a route names, as a routing
// group's member, as the group reaches it: not one the user unchecked
// (unpicked).
// ResolveRoute is Resolve for a model a route names, a routing group's
// member or a provider's fallback, as routing reaches it: not one the
// user unchecked (unpicked). A request naming the model itself goes by
// Resolve, which still takes it.
func ResolveRoute(id string) (Provider, string, bool) {
entries := providerEntries()
p, m, ok := resolveIn(entries, strings.TrimSuffix(strings.TrimSpace(id), "[1m]"))