Files
magpie/internal/gateway/group_own_routing_test.go
T
yetone 35eea09464 gateway: a provider routed its own way keeps that order inside a group that routes another way (kchen0x on X)
kchen0x had a Smart routing group with Antigravity (9 accounts, set to
按顺序) and a Business account. Requests still went to the 9th account, the
one renewing soonest. planLevel pooled every member's keys and accounts
into one list for any group not routed in order, so the provider's own
routing was never read; Smart then put the soonest renewal first.

Now a member whose provider routes other than Smart, and other than the
group does (ownRouting), is planned as a nested group already is: a unit
weighed by the first account or key it would try (firstAwake), then tried
whole in its own order. A provider left on Smart is pooled as before.
leastHeld, which reorders a rotate group's first request, leaves such a
group alone as it does one holding a nested group (without that, a
rotate group still came back "k2 k1 k3 kb").

GUI hint (GROUP_NEST, 4 languages), start docs (en, zh, ja) and
gateway-routing.md say so.

Run:
- TestProviderKeepsItsOwnRoutingInAGroup covers outer Smart, rotate,
  usage, pace and manual, plus a Smart provider still being pooled. On
  the old planLevel it fails with "k3 k2 k1 kb" (the reported order).
- go test -tags nogui -count=1 -timeout 40m ./...: all ok.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/gateway.
- GUI: i18n-keys, gui-ja/de/zh-tw and every *group*/*routing* test, both
  engines: 765 pass. A first run beside the full go test had 9 webkit
  timing reds (routing-group-fold, routing-keys-fold, routing-ctx-still,
  gateway-key-groups); each file passes alone, and the whole set passes
  when nothing else runs.
2026-10-11 19:54:55 +08:00

106 lines
3.6 KiB
Go

package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A provider set to route its own way keeps it inside a routing group,
// whatever the group's routing, as a group in the group does (kchen0x on
// X: Antigravity In order inside a Smart group still went to its ninth
// account, the one Smart put first). The group weighs it as one, by the
// first it would try, and tries it whole in its own order. One left on
// Smart is weighed key by key with the rest, as before.
func TestProviderKeepsItsOwnRoutingInAGroup(t *testing.T) {
now := time.Now()
week := func(in time.Duration) provider.Allowance {
return provider.Allowance{{Used: 10, Resets: now.Add(in), Span: 7 * 24 * time.Hour}}
}
// Smart alone would go k3, k2, k1: k3 renews soonest
windows := map[string]provider.Allowance{
"sk-test-k1": week(4 * 24 * time.Hour), "sk-test-k2": week(3 * 24 * time.Hour), "sk-test-k3": week(time.Hour),
"sk-test-kb": week(6 * 24 * time.Hour),
}
order := func(s *Server) string {
r := s.trace.routes[len(s.trace.routes)-1]
var out []string
for _, w := range r.Order {
if w.Provider == "biz" { // its one key, told as the provider
out = append(out, "kb")
continue
}
out = append(out, w.Who[len(w.Who)-2:]) // masked: its last two
}
return strings.Join(out, " ")
}
keysOf := func(o string) string { // ag's keys, in the order tried
var out []string
for _, k := range strings.Fields(o) {
if k != "kb" {
out = append(out, k)
}
}
return strings.Join(out, " ")
}
for _, outer := range []string{"", provider.Rotate, provider.LeastUsed, provider.Pace, provider.Manual} {
t.Run("group "+outer, func(t *testing.T) {
fresh(t)
keyWindows(t, windows)
ag, biz := &keyed{}, &keyed{}
serveOn(t, "ag", "sk-test-k1", []string{"m"}, ag, "sk-test-k2", "sk-test-k3")
serveOn(t, "biz", "sk-test-kb", []string{"m"}, biz)
if err := provider.SetRouting("ag", provider.Ordered); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{Name: "Flash", Members: []string{"ag/m", "biz/m"}, Routing: outer, Pick: "ag/m"}); err != nil {
t.Fatal(err)
}
s := New()
for i := range 3 {
code, body := postAs(t, s, "s"+string(rune('a'+i)), `{"model":"group/flash","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 {
t.Fatalf("%d %s", code, body)
}
o := order(s)
if keysOf(o) != "k1 k2 k3" {
t.Fatalf("request %d: %s, ag's keys not in its own order", i+1, o)
}
if outer == provider.Manual {
if o != "k1 k2 k3" { // the member picked alone
t.Fatalf("manual: %s", o)
}
continue
}
if o != "k1 k2 k3 kb" && o != "kb k1 k2 k3" {
t.Fatalf("request %d: %s, ag not tried whole", i+1, o)
}
if outer == "" && o != "k1 k2 k3 kb" {
t.Fatalf("smart: %s, ag's first renews before kb", o)
}
}
if outer == "" && (len(ag.tried) != 3 || ag.tried[0] != "sk-test-k1") {
t.Fatalf("smart sent to %v", ag.tried)
}
})
}
// ag on Smart is weighed key by key with biz, as one pool
fresh(t)
keyWindows(t, windows)
serveOn(t, "ag", "sk-test-k1", []string{"m"}, &keyed{}, "sk-test-k2", "sk-test-k3")
serveOn(t, "biz", "sk-test-kb", []string{"m"}, &keyed{})
if err := provider.SaveGroup(provider.Group{Name: "Flash", Members: []string{"biz/m", "ag/m"}}); err != nil {
t.Fatal(err)
}
s := New()
if code, body := postAs(t, s, "pool", `{"model":"group/flash","messages":[{"role":"user","content":"hi"}]}`); code != 200 {
t.Fatalf("%d %s", code, body)
}
if o := order(s); o != "k3 k2 k1 kb" {
t.Fatalf("pooled: %s", o)
}
}