Files
magpie/internal/gateway/decide_group_test.go
T
yetone b1396d2360 gateway, provider, gui: a routing group of decision models answers /v1/systemone, failing over between them (ARNO on Discord)
ARNO asked for decision models to get the group feature too: /v1/systemone
took only "<provider>/<model>" (xxx/jev-latest), so one refusing key or
vendor failed the call with nothing to fall back on.

A routing group may now be of decision models alone (provider.Group.Decides,
provider/decide_group.go). SaveGroup refuses one mixed with models that hold
a conversation, a decision group inside a chat group, and patterns, rules,
classifier, effort, levels, fast mode or a member effort on it. It is kept
out of the agents' catalog (groupEntries), and a chat request to it gets a
400 pointing at /v1/systemone.

POST /v1/systemone with "model":"group/<id>" (or the group's id or name, as
GroupFor reads them) goes to serveDecisionGroup (gateway/decide_group.go):
- the members' keys and accounts are planned with planGroup in the group's
  routing order, resting ones last; perKeyBarred no longer drops a
  decision-only provider's keys (it speaks no chat protocol), so each key
  is a try of its own;
- a gateway key's model and account limits and pause rules hold as for chat;
- a retryable failure that isn't the request's own (shape/prompt/protection)
  rests the candidate via restAfter and the next is asked; a transport error
  rests it as a 502; the last one's answer and Retry-After are passed on;
- every try is a Try of one route in the routing log; one usage row for the
  answer.
A group classifier naming a decision group fails over the same way
(askDecisionGroup; systemOne now returns decideRefused with the vendor's
status, headers and body, and postDecide returns the headers).

The GUI group editor offers a group of decision models only decision models
(groups.deciders), a chat group no decision model or decision group, a new
one both, and a decision model's row has no reasoning picker (groupJSON
.decides). The TUI member picker and `magpie group add` follow the same rule.
#1514's plugin_decide_test and TestMixedDecisionProvider now assert a mixed group is refused and a group
of decision models saves. Docs: reference.md (routing groups),
gateway-routing.md, provider-plugins.md.

Run:
- go test -tags nogui -timeout 40m . ./internal/provider ./internal/gateway
  ./internal/gui ./internal/tui: ok
- new: TestSystemOneDecisionGroupFailsOver (httptest System One fakes: a
  provider with two keys answering 429 and 503, then a second provider
  answering 200: tries ka1,ka2,kb, both failures rested and ask by name
  goes to kb first; all failing passes the last answer on; chat to the
  group is 400), TestDecisionGroupClassifierFailsOver, TestDecisionGroup,
  TestGroupAddDecisionModels, TestMemberOptionsDecisionGroup,
  group-decide-members.test.cjs (Chromium and WebKit, en and zh; no new
  strings)
- each broken by hand and seen red: no dispatch (404), no perKeyBarred
  change (ka2 skipped), no classifier failover, no rest, decision group in
  the catalog, CLI resolver, TUI and GUI picker filters
- GUI: group-*, routing-effort*, routing-levels, routing-save*,
  routing-intent-entry, routing-manual, i18n-keys in both engines: 169 pass
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on the
  touched packages
- after rebasing on bc2496dca, the five packages again: ok. The first run
  had TestMaxRPMWaitsForRoomInTheMinute red once under load; it passed
  -count=3 alone and in the rerun, and nothing here touches rpm.go.
Not tried against a real TypeSafe or Bailian account.
2026-10-11 01:37:18 +08:00

179 lines
6.0 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// decideFleet is System One APIs whose every key answers as status says
// (200 when unset), and the keys asked in turn.
type decideFleet struct {
mu sync.Mutex
status map[string]int
asked []string
}
func (f *decideFleet) set(key string, code int) {
f.mu.Lock()
defer f.mu.Unlock()
f.status[key] = code
}
func (f *decideFleet) took() []string {
f.mu.Lock()
defer f.mu.Unlock()
out := f.asked
f.asked = nil
return out
}
func (f *decideFleet) serve() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/systemone" {
http.NotFound(w, r)
return
}
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
var q struct{ Model string }
json.NewDecoder(r.Body).Decode(&q)
f.mu.Lock()
f.asked = append(f.asked, key)
code := f.status[key]
f.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
if code != 0 && code != 200 {
w.WriteHeader(code)
io.WriteString(w, `{"detail":{"message":"no capacity for `+key+`"}}`)
return
}
json.NewEncoder(w).Encode(map[string]any{"model": q.Model, "answers": map[string]any{
"levels": map[string]any{"type": "noul", "noul": 0},
"intent": map[string]any{"type": "choice", "choice": "bug", "confidence": 0.9}},
"usage": map[string]int{"input_tokens": 10, "output_tokens": 2}})
}))
}
// A routing group of decision models answers /v1/systemone (ARNO on
// Discord): its keys and accounts asked in the group's order, one that
// fails as a chat's would resting and the next asked, every try in the
// routing log; asked by "group/<id>" or by the group's name.
func TestSystemOneDecisionGroupFailsOver(t *testing.T) {
fresh(t)
f := &decideFleet{status: map[string]int{}}
a, b := f.serve(), f.serve()
defer a.Close()
defer b.Close()
for _, p := range []provider.Provider{
// a decision provider alone, two keys: each one a try of its own
{ID: "ja", Name: "JA", Key: "ka1", Keys: []provider.KeyAccount{{Key: "ka2"}}, Decide: a.URL + "/v1"},
{ID: "jb", Name: "JB", Key: "kb", Decide: b.URL + "/v1"},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{ID: "jevs", Name: "Jevs", Members: []string{"ja/jev-latest", "jb/jev-latest"}}); err != nil {
t.Fatal(err)
}
s := New()
call := func(path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("Authorization", "Bearer magpie")
s.Handler().ServeHTTP(rec, req)
return rec
}
const ask = `{"model":%q,"state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}`
f.set("ka1", 429)
f.set("ka2", 503)
rec := call("/v1/systemone", strings.Replace(ask, "%q", `"group/jevs"`, 1))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"choice":"bug"`) {
t.Fatalf("group: %d %s", rec.Code, rec.Body)
}
if got := strings.Join(f.took(), ","); got != "ka1,ka2,kb" {
t.Fatalf("asked %s, want ka1,ka2,kb", got)
}
if n := len(s.trace.routes); n != 1 {
t.Fatalf("traced %d", n)
}
r := s.trace.routes[0]
if r.Model != "group/jevs" || r.Group == nil || r.Provider != "jb" || !r.Done || r.Status != 200 || len(r.Tries) != 3 {
t.Fatalf("route %+v", r)
}
for i, want := range []int{429, 503, 200} {
try := r.Tries[i]
if try.Status != want || try.Model != "jev-latest" || (want != 200) != (try.Rest != nil) {
t.Errorf("try %d: %+v, want %d, a rest when it failed", i, try, want)
}
}
if len(r.Usage) != 1 || r.Usage[0].Provider != "jb" {
t.Fatalf("usage %+v", r.Usage)
}
// the two that failed rest: by the group's name, the one that answered
// goes first
rec = call("/v1/systemone", strings.Replace(ask, "%q", `"Jevs"`, 1))
if rec.Code != 200 {
t.Fatalf("by name: %d %s", rec.Code, rec.Body)
}
if got := strings.Join(f.took(), ","); got != "kb" {
t.Fatalf("by name asked %s, want kb", got)
}
// all failing: every one tried, those resting last, and the last
// one's answer passed on
f.set("kb", 429)
rec = call("/v1/systemone", strings.Replace(ask, "%q", `"group/jevs"`, 1))
if got := strings.Join(f.took(), ","); got != "kb,ka1,ka2" {
t.Fatalf("all failing asked %s, want kb,ka1,ka2", got)
}
if rec.Code != 503 || !strings.Contains(rec.Body.String(), "no capacity for ka2") {
t.Fatalf("all failing: %d %s", rec.Code, rec.Body)
}
// a chat request to it is told where it is asked
rec = call("/v1/chat/completions", `{"model":"group/jevs","messages":[{"role":"user","content":"hi"}]}`)
if rec.Code != 400 || !strings.Contains(rec.Body.String(), "/v1/systemone") {
t.Fatalf("chat to a decision group: %d %s", rec.Code, rec.Body)
}
}
// A decision group as a chat group's classifier fails over the same way.
func TestDecisionGroupClassifierFailsOver(t *testing.T) {
fresh(t)
f := &decideFleet{status: map[string]int{"kc": 503}}
a, b := f.serve(), f.serve()
defer a.Close()
defer b.Close()
for _, p := range []provider.Provider{
{ID: "jc", Name: "JC", Key: "kc", Decide: a.URL + "/v1"},
{ID: "jd", Name: "JD", Key: "kd", Decide: b.URL + "/v1"},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{ID: "judges", Name: "Judges", Members: []string{"jc/jev-latest", "jd/jev-latest"}}); err != nil {
t.Fatal(err)
}
s := New()
if v, err := s.askClassifier("group/judges", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil || v.Intent != "bug" {
t.Fatalf("classify: %+v %v", v, err)
}
asked := f.took()
if len(asked) < 2 || asked[0] != "kc" || asked[len(asked)-1] != "kd" {
t.Fatalf("asked %v", asked)
}
// jc rests: the next classification asks jd first
if _, err := s.askClassifier("group/judges", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil {
t.Fatal(err)
}
if asked := f.took(); len(asked) == 0 || asked[0] != "kd" {
t.Fatalf("after the rest asked %v", asked)
}
}