Files
magpie/internal/gateway/small_first_test.go
T
yetone c01988e439 routing: a group's Small context first sends a request to the smallest window that holds it
yg6888 on X (tweet 2123308968848379920): in a group routed Smart, Cursor
Plus's Composer and Grok 4.7 were never used. Not a bug: Smart orders by
allowance tier, then soonest renewal, then listed order, and never weighs
the context window, so the 1M members listed ahead always answered. The
small ones were only ever reached as a fallback.

Smart itself is unchanged; changing its order would move every Smart
user's traffic. A group gets a new routing instead, `fit` (Small context
first; 小上下文优先, 小上下文優先, 小さいコンテキスト優先, Kleiner
Kontext zuerst):

- routes.SmallFirst takes Smart's order and sorts it stably by band (a
  provider set to be used first, the rest with room, the all but used up)
  and then by window, the smaller known first, an unknown one last.
- Its strategy sets ByWindow, so weighRouted fills Seat.Window from
  windowIn (the provider's own Contexts, else the catalog's).
- roomy already leaves out every member the request is estimated past, so
  the first left is the smallest that holds it: a small model is never
  asked for a request too long for it, and keeps its last-resort place as
  under every routing.
- The trace's rows carry `context`; the Routing page says "its 200K
  context window is the smallest that holds this request".
- A group's only: provider.normalize already drops it from a provider and
  the CLI refuses it there. GUI menu, hint, why-lines, TUI and CLI
  (`routing=fit`, small-first) carry it; docs/reference.md, the
  gateway-routing and plugin-runtime pages and the en/zh/ja start pages
  say it, with the search index rebuilt (via docs-index.mjs; #7065).

Run first at the tree rebased on origin/main 6e5882109, then at this
head's (on 856423806; search index rebuilt; gateway/routes vet, routes, rotation, ./internal/gui,
the gateway's new tests and routing-small-first, routing-group-strategy
and i18n-keys 42/42 again), mote over its
cores with another session's full suite on it and mica's disk full, so on
the Linux box (wsl-run) and the Windows box:
- go test -tags nogui ./internal/routes ./internal/cli ./internal/tui
  ./plugins/rotation ./internal/gateway -count=1 -timeout 45m: all ok but
  gateway, whose one red was this commit's own new test asserting a single
  group while an auto group for the shared model exists; fixed (FindGroup
  "group/g") and the window/strategy tests rerun ok, both new ones pass.
  ./internal/provider ok (its only FAIL was the stale search index, since
  rebuilt). ./internal/gui -run Budget ok.
- Broken by hand: SmallFirst returning Smart's order makes
  TestSmallFirstOrdersByWindow and the gateway test red ("a short request
  went to big first"); dropping fit from SaveGroup's whitelist makes the
  gateway test red ("the group didn't keep Small context first").
- GUI (Chromium + WebKit, en zh zh-TW ja de): routing-small-first,
  routing-group-strategy, i18n-keys, routing-cheapest-first,
  routing-free-expiring 112/112 before and after the rebase;
  every routing*, group* and builtins* file (253): 4139 of 4198, the 59
  reds in 11 files that fail the same at origin/main 239da9991 run alone
  (57; routing-many-keys moves between languages each run), all filed:
  #7016, #7097, #6520, #5049, #6512, #4711, #3954, #3799, #4082.
- Windows: CGO_ENABLED=0 go vet -tags nogui ./internal/... ./plugins/...
  ok. gofmt -l clean.

Not tried: the full `go test ./...` and darwin vet (mote busy, mica out
of disk); a real Cursor/Grok account in a group (the window comes from
the same windowIn roomy already trusts). Seen on the way: a recovered
"close of closed channel" in continuation_test.go's doneHold fake, no
test failed from it.
2026-11-19 17:34:30 +08:00

102 lines
3.8 KiB
Go

package gateway
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A group routed Small context first sends a request to the member with
// the smallest context window that holds it (yg6888 on X: Cursor's
// Composer at 200K and Grok 4.7 at 256K never answered in a group of 1M
// models under Smart). The 1M member is listed first, so Smart sends it
// everything; routed fit a short request goes to the small one, and one
// longer than the small one's window to the big one, the small one left
// out as too small. The trace tells each row's window. Smart on the same
// group still goes to the listed first.
func TestSmallContextFirstGroupKeepsTheBigOneForLongRequests(t *testing.T) {
fresh(t)
up := httptest.NewServer(http.HandlerFunc(streamed))
t.Cleanup(up.Close)
to, _ := url.Parse(up.URL)
for _, p := range []provider.Provider{
{ID: "big", Name: "Big", Key: "sk-b", Chat: "https://big.example/v1", Models: []string{"m"}, Contexts: map[string]int{"m": 1_000_000}},
{ID: "small", Name: "Small", Key: "sk-s", Chat: "https://small.example/v1", Models: []string{"m"}, Contexts: map[string]int{"m": 4_000}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
group := func(routing string) {
t.Helper()
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"big/m", "small/m"}, Routing: routing}); err != nil {
t.Fatal(err)
}
}
s := New()
s.client = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
r.URL.Scheme, r.URL.Host = to.Scheme, to.Host
return http.DefaultTransport.RoundTrip(r)
})}
ask := func(text string) *Route {
t.Helper()
code, body := postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"`+text+`"}]}`)
if code != 200 {
t.Fatalf("code %d: %s", code, body)
}
return s.trace.routes[len(s.trace.routes)-1]
}
long := strings.Repeat("lorem ipsum dolor sit amet ", 2000) // ~54K characters, past 4K tokens
group(provider.SmallFirst)
if g, _, ok := provider.FindGroup("group/g"); !ok || g.Routing != provider.SmallFirst {
t.Fatalf("the group didn't keep Small context first: %+v", g)
}
short := ask("hi")
if short.Order[0].Provider != "small" {
t.Fatalf("a short request went to %q first, want small: %+v", short.Order[0].Provider, short.Order)
}
if short.Order[0].Context != 4_000 {
t.Fatalf("the small row doesn't tell its window: %+v", short.Order[0])
}
if len(short.Tries) != 1 || short.Tries[0].ID != short.Order[0].ID {
t.Fatalf("a short request wasn't served by the small one alone: %+v", short.Tries)
}
big := ask(long)
if big.Order[0].Provider != "big" {
t.Fatalf("a long request went to %q first, want big: %+v", big.Order[0].Provider, big.Order)
}
for _, w := range big.Order {
if w.Provider == "small" && w.Window != 4_000 {
t.Fatalf("the small one wasn't left out as too small for the long request: %+v", w)
}
}
// Smart on the same group: the listed first, and no row tells a window
group("")
smart := ask("hi")
if smart.Order[0].Provider != "big" {
t.Fatalf("Smart went to %q first, want the listed first: %+v", smart.Order[0].Provider, smart.Order)
}
for _, w := range smart.Order {
if w.Context != 0 {
t.Fatalf("a Smart row tells a window: %+v", w)
}
}
}
// Small context first is a group's: a provider saved with it keeps Smart.
func TestSmallContextFirstIsAGroupsOnly(t *testing.T) {
fresh(t)
if err := provider.Save(provider.Provider{ID: "rl", Name: "Relay", Key: "k", Chat: "https://relay.example/v1", Models: []string{"m"}, Routing: provider.SmallFirst}); err != nil {
t.Fatal(err)
}
if p, err := provider.Find("rl"); err != nil || p.Routing != "" {
t.Fatalf("a provider kept routing fit, want Smart: %v %+v", err, p)
}
}