DD asked for weighted round robin as a group routing, next to In turn.
A provider's keys could already route by weight (#841). Groups could not.
What changes:
- provider: Group.Weights maps a member's spelling to its weight, 1 to
1000. A member left out, or set to 0, weighs 1. SaveGroup accepts
routing "weight". It also cleans weights the way it cleans fast and
off: it strips :fast, drops weights of members that are gone and of
members weighing 1, and refuses a weight below 0 or above 1000.
Weights follow a member when it is renamed (RenameMember, Rename,
RenameGroup) and survive Share/Import.
- gateway: planLevel treats each member of a weight-routed group as one
unit: a model with its keys in its provider's order, or a nested group
routed its own way. weightedUnits orders the units by smooth WRR on a
round per group ("group/<id>"), sharing wrrPick with the keys. The
round's pick goes first and the rest stay in member order for
failover. A unit whose head is resting leaves the round. When a
conversation is kept on another member (session/turn/cache/strict),
the charge moves to that member (wrrSeat, wrrMove).
- cli: routing=weight (also weighted, wrr). weight=<model>=<n>[,...]
sets weights and keeps the rest; weight= on its own clears them. The
group view shows "· weight N" for each member.
- tui: By weight is in the group routing list.
- gui: the group editor offers By weight. Each member then shows
"Weight N · S%", its share among the members that are on. Clicking it
edits the number in place: Enter or blur keeps it, Escape cancels,
and a value outside 1 to 1000 shows a warning. The trace header uses
group wording for a weight-routed group. Strings are added in zh,
zh-TW, ja and de.
- docs: reference.md, gateway-routing.md, and site start pages in en,
zh and ja.
Run:
- go test -tags nogui ./internal/provider ./internal/cli ./internal/tui:
ok.
- ./internal/gateway: ok in 988s with -timeout 45m. The first run hit
the default 10m timeout at load average ~170. The test running at
that moment had been running for 2s; it was not a hang.
- go vet -tags nogui on those packages, for darwin, GOOS=linux and
GOOS=windows: clean. gofmt -l: clean.
- New group-member-weight.test.cjs: 8/8 in Chromium and WebKit, en and
zh at 1100, en and de at 420. The WebKit de 420 case also passed six
runs at once.
- gui-zh-tw, gui-ja, gui-de, i18n-keys, routing-*, group-*,
panel-routing*, settings-groups and agent-model-groups: all green
except two.
- routing-steady (WebKit) is #1804. It is red 2/3 alone on origin/main
e1b70c13d too.
- routing-save-blank (chromium en) was red under load only, and 3/3
green alone. The overshoot in its wheel loop is filed as #1924.
Broken by hand, each red, then restored:
- byWeight forced false:
--- FAIL: TestWeightedGroup (weighted_group_test.go:69 its turn ...)
- cleanWeights call skipped:
--- FAIL: TestSaveGroupWeights (groupweight_test.go:39 weights map[...
an/claude-opus-5-5:0 ... oa/gpt-6.1-sol:high:fast:3 ...])
- CLI weight case renamed:
--- FAIL: TestGroupWeightCLI (unknown field "weight")
- GUI chip not appended: group-member-weight chromium en 1100 timed out
on the weight button.
- Not red: the test's "moved nothing" check. A scroll forced on click,
or padding added above, did not turn it red, because the view's own
scroll guard and scroll anchoring hold the clicked row in place. It
only shows that the row stays under the pointer.
Not tried: real accounts through a running gateway. The routing is
proven by the plan-level test with synthetic providers only.
126 lines
4.2 KiB
Go
126 lines
4.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A group routed by weight (#1910, DD on Discord): members weighing 3, 1
|
|
// and 4 take three, one and four of every eight requests, each tried
|
|
// whole — a member's keys in its provider's order, a group in it in its
|
|
// own — the next one the fallback. A member resting leaves the round,
|
|
// the others sharing it by their weights, and a conversation kept on a
|
|
// member counts against that member, so new ones make up the share.
|
|
func TestWeightedGroup(t *testing.T) {
|
|
forgetSinks(t)
|
|
s := &Server{}
|
|
url := "http://127.0.0.1:1/v1"
|
|
a := provider.Provider{ID: "wa", Chat: url, Key: "k-a"}
|
|
b := provider.Provider{ID: "wb", Chat: url, Key: "k-b1", Keys: []provider.KeyAccount{{Key: "k-b2", Weight: 50}}, Routing: provider.Ordered}
|
|
c := provider.Provider{ID: "wc", Chat: url, Key: "k-c"}
|
|
d := provider.Provider{ID: "wd", Chat: url, Key: "k-d"}
|
|
sub := provider.Group{ID: "wsub", Routing: provider.Ordered}
|
|
g := provider.Group{ID: "wg", Routing: provider.Weighted, Members: []string{"wa/m", "wb/m:high", "group/wsub"},
|
|
Weights: map[string]int{"wa/m": 3, "group/wsub": 4}}
|
|
ms := []provider.Member{
|
|
{ID: "wa/m", Path: []string{"wa/m"}, Provider: a, Model: "m"},
|
|
{ID: "wb/m:high", Path: []string{"wb/m:high"}, Provider: b, Model: "m", Effort: "high"},
|
|
{ID: "group/wsub", Path: []string{"group/wsub", "wc/m"}, Via: []provider.Group{sub}, Provider: c, Model: "m"},
|
|
{ID: "group/wsub", Path: []string{"group/wsub", "wd/m"}, Via: []provider.Group{sub}, Provider: d, Model: "m"},
|
|
}
|
|
t.Cleanup(func() {
|
|
wrr.Lock()
|
|
delete(wrr.m, provider.GroupPrefix+g.ID)
|
|
wrr.Unlock()
|
|
})
|
|
unitOf := map[string]string{"wa": "a", "wb": "b", "wc": "sub", "wd": "sub"}
|
|
plan := func() []candidate {
|
|
t.Helper()
|
|
cs, pl := s.planGroup(g, ms, provider.Chat)
|
|
if len(cs) != 5 || len(pl.order) != 5 {
|
|
t.Fatalf("%d planned, %d in the trace: %s", len(cs), len(pl.order), restsOf(cs))
|
|
}
|
|
// each member whole, in its own order
|
|
var ids []string
|
|
for _, c := range cs {
|
|
ids = append(ids, c.p.ID)
|
|
}
|
|
joined := strings.Join(ids, " ")
|
|
if !strings.Contains(joined, "wb wb") || !strings.Contains(joined, "wc wd") {
|
|
t.Fatalf("a member split up: %s", joined)
|
|
}
|
|
if i := slices.Index(ids, "wb"); cs[i].p.Key != "k-b1" || cs[i+1].p.Key != "k-b2" {
|
|
t.Fatalf("wb's keys not in its own order: %s", restsOf(cs))
|
|
}
|
|
if !pl.order[0].Turn || slices.ContainsFunc(pl.order[1:], func(w Weighed) bool { return w.Turn }) {
|
|
t.Fatalf("its turn: %+v", pl.order)
|
|
}
|
|
return cs
|
|
}
|
|
var seq []string
|
|
counts := func(n int, keep func(i int, cs []candidate) candidate) map[string]int {
|
|
got := map[string]int{}
|
|
seq = nil
|
|
for i := range n {
|
|
cs := plan()
|
|
took := cs[0]
|
|
if keep != nil {
|
|
took = keep(i, cs)
|
|
}
|
|
got[unitOf[took.p.ID]]++
|
|
seq = append(seq, unitOf[took.p.ID])
|
|
}
|
|
return got
|
|
}
|
|
if got := counts(8*25, nil); got["a"] != 75 || got["b"] != 25 || got["sub"] != 100 {
|
|
t.Fatalf("3:1:4 over 200: %v", got)
|
|
}
|
|
// smoothly: never the same member three times running
|
|
if strings.Contains(strings.Join(seq, " "), "sub sub sub") {
|
|
t.Fatalf("not smooth: %v", seq[:8])
|
|
}
|
|
|
|
// wa resting: wb and the group in it share its requests 1 : 4, and
|
|
// wa is tried last
|
|
var aKey string
|
|
for _, c := range plan() {
|
|
if c.p.ID == "wa" {
|
|
aKey = c.restKey()
|
|
}
|
|
}
|
|
restingUntil.Lock()
|
|
restingUntil.m[aKey] = time.Now().Add(time.Hour)
|
|
restingUntil.Unlock()
|
|
if cs := plan(); cs[len(cs)-1].p.ID != "wa" {
|
|
t.Fatalf("the resting member isn't last: %s", restsOf(cs))
|
|
}
|
|
wrr.Lock()
|
|
delete(wrr.m, provider.GroupPrefix+g.ID)
|
|
wrr.Unlock()
|
|
if got := counts(5*20, nil); got["a"] != 0 || got["b"] != 20 || got["sub"] != 80 {
|
|
t.Fatalf("with wa resting, 1:4 over 100: %v", got)
|
|
}
|
|
rested(aKey)
|
|
|
|
// a conversation kept on wb each round of eight: the round counts it
|
|
// against wb
|
|
wrr.Lock()
|
|
delete(wrr.m, provider.GroupPrefix+g.ID)
|
|
wrr.Unlock()
|
|
got := counts(8*10, func(i int, cs []candidate) candidate {
|
|
if i%8 != 0 || cs[0].p.ID == "wb" {
|
|
return cs[0]
|
|
}
|
|
at := slices.IndexFunc(cs, func(c candidate) bool { return c.p.ID == "wb" })
|
|
weightedKept(cs[0], cs[at])
|
|
return cs[at]
|
|
})
|
|
if got["a"] != 30 || got["b"] != 10 || got["sub"] != 40 {
|
|
t.Fatalf("with wb kept: %v", got)
|
|
}
|
|
}
|