The second P3 extension point (docs/subsystems/plugin-runtime.md). A
router strategy orders one pool of routes: a provider's accounts or keys,
or the heads of a group's members. The gateway still lists the pool and
keeps rests, affinity, rules, pins and failover; a strategy only orders.
- internal/routes: Seat, Strategy{Order, Exact, ByUse}, Register (stacks,
the last serves), Find, Served, and Apply, which fails open: a strategy
that panics or answers what isn't a permutation leaves the pool in its
listed order, logged. Smart is the core's; Ordered, Rotate, LeastUsed,
Pace and Weighted are routes.Builtin, moved out of the gateway with the
rotate turns and the smooth weighted round (Round, MoveRound, Forget).
- plugins/rotation (Essential): registers routes.Builtin. InUse counts the
providers and groups providers.json routes by one of them
(provider.Routings), an unreadable file is an error, so it stays on. The
gateway's KeepFresh watches providers.json too.
- Off, a routing no strategy serves is its listed order, and routingOf
reads it as "order" wherever the core asks (a turn's mark, sinks, a
rotate group's sessions, a group's heads, weightedKept).
- GUI: the routing pickers (provider editor, Routing page pools and group
editor) mark the five as data-feature="rotation" (routingSegs), so off
they offer Smart alone. i18n zh, zh-TW, ja, de.
- Docs: plugin-runtime.md router strategy section, gateway-routing.md
(also drops a duplicated sentence about a lone key), reference.md.
Run in the worktree at this change (on origin/main fbfb76149):
- go test -count=1 ./... : 57 packages ok (gateway 587s)
- gofmt -l clean; go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows
- GUI: full suite, chromium and webkit, every language: 9805 tests, 29
red in 6 files. Each red alone the same way on origin/main bc470c6b6
(just fetched): quota-count-wrap, quota-name-translated (#2228),
refusal-retries-setting (#2258), routing-held-member (#2252), usage-fold
(#2163), usage-agents-hide (#2276, from cf455ebd5). routing-save, builtins and
rate-limited-sink pass in both engines.
- internal/gateway TestMain registers the strategies, so every routing test
is the parity run with the plugin on; TestRotationOnAndOff turns it off
(three Rotate sessions start k1,k2,k3 on, k1,k1,k1 off, no turn mark;
Smart still served).
Rebased on origin/main 2b0037f41: go test ./... again, 56 packages ok at the
default timeout; internal/agent and internal/gateway passed with
-timeout 40m (1093s, 1039s: the machine at load ~17 with other sessions'
suites; the 10m panics stood in TestCodexAPIDisconnectStays and
TestCodexHeardWithinItsIdleTimeout, 1-2s in, no stack waiting on them).
Vets and gofmt clean again; routing-save, builtins, rate-limited-sink,
i18n-keys and the GUI files upstream changed pass.
Broken by hand, each test went red then restored:
- Apply's permutation check off: TestApplyFailsOpen
- Register's remove a no-op: TestRegisterStacks
- the plugin's Apply registers nothing: TestOnServesOffDoesNot
- InUse swallows providers.json's error: TestInUse
- routingOf passes an unserved routing through: TestRotationOnAndOff
- routingSegs not marking data-feature: routing-save.test.cjs "Smart alone"
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package rotation
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/pluginrt"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/routes"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
func TestMain(m *testing.M) { testenv.Main(m) }
|
|
|
|
func manifest(t *testing.T) pluginrt.Manifest {
|
|
t.Helper()
|
|
for _, m := range pluginrt.Registered() {
|
|
if m.ID == "rotation" {
|
|
return m
|
|
}
|
|
}
|
|
t.Fatal("rotation not registered")
|
|
return pluginrt.Manifest{}
|
|
}
|
|
|
|
func sandbox(t *testing.T) {
|
|
t.Helper()
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, "cache"))
|
|
}
|
|
|
|
// On, every routing besides Smart is served; off, none is, and Smart still
|
|
// is; on again, all are back.
|
|
func TestOnServesOffDoesNot(t *testing.T) {
|
|
sandbox(t)
|
|
rt := pluginrt.New([]pluginrt.Manifest{manifest(t)})
|
|
defer rt.Stop()
|
|
served := func(want bool) {
|
|
t.Helper()
|
|
for name := range routes.Builtin {
|
|
if routes.Served(name) != want {
|
|
t.Fatalf("%s served: %v, want %v (%+v)", name, !want, want, rt.Statuses())
|
|
}
|
|
}
|
|
if !routes.Served("") {
|
|
t.Fatal("Smart isn't served")
|
|
}
|
|
}
|
|
served(false)
|
|
rt.Reconcile(map[string]bool{"rotation": true}, nil)
|
|
served(true)
|
|
rt.Reconcile(map[string]bool{"rotation": false}, nil)
|
|
served(false)
|
|
rt.Reconcile(map[string]bool{"rotation": true}, nil)
|
|
served(true)
|
|
}
|
|
|
|
// In use while a provider or a group is routed by one of its routings, by
|
|
// providers.json alone; Smart and Manual don't count, and a providers.json
|
|
// that can't be read keeps it on.
|
|
func TestInUse(t *testing.T) {
|
|
sandbox(t)
|
|
m := manifest(t)
|
|
if n, err := m.InUse(); err != nil || n != 0 {
|
|
t.Fatalf("no providers: %d %v", n, err)
|
|
}
|
|
for _, id := range []string{"a", "b"} {
|
|
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Models: []string{"m"}, Chat: "http://127.0.0.1:1/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Hand", Members: []string{"a/m", "b/m"}, Routing: provider.Manual}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := m.InUse(); err != nil || n != 0 {
|
|
t.Fatalf("Smart and Manual: %d %v", n, err)
|
|
}
|
|
if err := provider.SetRouting("a", provider.Rotate); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Pair", Members: []string{"a/m", "b/m"}, Routing: provider.Weighted}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := m.InUse(); err != nil || n != 2 {
|
|
t.Fatalf("a provider in turn and a group by weight: %d %v", n, err)
|
|
}
|
|
if err := os.WriteFile(provider.Path(), []byte("{not json"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, bak := range []string{".bak", ".lastgood"} {
|
|
os.Remove(provider.Path() + bak)
|
|
}
|
|
if _, err := m.InUse(); err == nil {
|
|
t.Fatal("an unreadable providers.json counted as known")
|
|
}
|
|
}
|