Files
magpie/internal/gateway/groupeffortmap_test.go
T
yetone 3441035a2e routing: a group's member maps the reasoning level asked to the one sent
A Routing group that relays one long task across vendors sent every
member the same effort word, but one vendor's high is another's xhigh
(LuckymanJC on Discord). A member that follows the group may now carry
a map from the level asked to the level sent: Group.EffortMaps, json
effortMaps, keyed by the member's id as Members spells it.

- Default: no map, or a level the map doesn't name, goes as asked, so
  every group saved before is unchanged and nothing of the user's is
  overridden. A member fixed at an effort (or with several levels) and
  a rule's UseEffort still win over the map.
- SaveGroup keeps a map only for a plain member of the group (not
  :effort, not :a+b, not group/), drops pairs that send what was
  asked, lowercases, and refuses a word that isn't a reasoning level.
- Carried by member rename (groupfast RenameMember and the all-groups
  rename), provider rename, fold (and its fingerprint), share/import
  and CanonicalRef; on a collision the map already spelled so wins.
- Gateway: the level asked is the suffix's/agent pick, then the turn's
  pick, then the request's own (askedLevel: agentEffort, plus none for
  an Anthropic request with thinking disabled); the target is sent with
  withFixedEffort, fitted to the model's catalog levels unless none, and
  the trace's try records mappedFrom. Per candidate, so fallback and
  retries send each member its own; count_tokens sends no member effort.
- The group lists a mapped member's own levels plus the map's keys to
  agents (mappedLevels in buildGroupEntries).
- GUI: the member's reasoning menu has "Map levels…" (app menus only):
  a level asked, then As asked or one of the model's levels. The chip
  reads "Mapped reasoning", its title the pairs. Follow the group or a
  fixed level drops the map. zh, zh-TW, ja and de.
- docs/subsystems/gateway-routing.md says all of it.

Run (mote, at this tree):
- gofmt -l: clean. go vet (provider, gateway, gui), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test ./internal/provider/: ok. go test -run
  'Effort|Level|Group|Fallback|Rule|Auto|Fixed' ./internal/gateway/: ok.
- node --test group-member-effort-map (30: chromium + webkit, en zh
  zh-TW ja de, 1100 and 420px) and group-member-levels: pass;
  i18n-en-no-cjk: pass. i18n-keys' "a plugin's words are its own" is
  red only for the three words it is red for on origin/main 922cc0ccf
  (#5332, #5426, #5482); this change's words are its own (two that Go
  source said too were reworded).
- go test -run 'Group|Budget|I18n' ./internal/gui/: ok.

Broken by hand, each red, then restored:
- gateway mappedLevel(nil, in): every TestMemberEffortMap* red.
- mappedLevels left out of buildGroupEntries: both levels tests red.
- group_share's rename left out: the import case red.
- RenameMember's rename left out: its case red.
- SaveGroup's cleanEffortMaps call left out: TestSaveGroupEffortMaps red.
- the editor save without effortMaps: 30 of 30 GUI red; moveTo's map
  cleanup left out: 10 of 30 red.

Not tried: a real cross-vendor relay with real accounts (the mapping is
checked on the request bodies the gateway sends to fake upstreams). The
CLI and TUI keep the map but have no editor for it. The full Go suite
and whole GUI suite run after the push, per the loop rules.

Reported-by: LuckymanJC (Discord)
2026-11-07 20:19:50 +08:00

202 lines
8.0 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// setEffortMaps gives group/r's members (as Members spells them) these
// effort maps.
func setEffortMaps(t *testing.T, maps map[string]map[string]string) {
t.Helper()
g, _, _ := provider.FindGroup("group/r")
g.EffortMaps = nil
for id, to := range maps {
g.SetMemberEffortMap(id, to)
}
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
// A member with an effort map (LuckymanJC on Discord: one vendor's high
// is another's xhigh) is sent the level its map names for the one asked,
// in each API's words, and a level the map doesn't name as it was asked.
func TestMemberEffortMap(t *testing.T) {
s, a, _ := ruled(t)
if err := provider.SetModelEfforts("a/small", []string{"low", "medium", "high", "xhigh"}); err != nil {
t.Fatal(err)
}
setEffortMaps(t, map[string]map[string]string{"a/small": {"high": "xhigh", "low": "medium", "none": "low"}})
for _, c := range []struct{ asked, want, from string }{
{"high", "xhigh", "high"},
{"low", "medium", "low"},
{"medium", "medium", ""}, // not in the map: as asked
{"xhigh", "xhigh", ""},
{"none", "low", "none"},
} {
out, r := postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"`+c.asked+`"`))
if sent := sentBody(t, a); !strings.Contains(out, "from ka") || sent["reasoning_effort"] != c.want {
t.Errorf("chat asked %s: sent %v", c.asked, sent["reasoning_effort"])
}
if len(r.Tries) != 1 || r.Tries[0].Effort != c.want || r.Tries[0].MappedFrom != c.from || r.Tries[0].Fixed != "" {
t.Errorf("chat asked %s: traced %+v", c.asked, r.Tries)
}
}
// a request asking for no reasoning (a session title) is left without
postOK(t, s, "", chat("hi", nil, 0, ""))
if sent := sentBody(t, a); sent["reasoning_effort"] != nil {
t.Errorf("no ask: sent %v", sent["reasoning_effort"])
}
// Responses
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"group/r","input":"hi","reasoning":{"effort":"high","summary":"auto"}}`)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
if sent := sentBody(t, a); sent["reasoning_effort"] != "xhigh" {
t.Errorf("responses high: sent %v", sent["reasoning_effort"])
}
// Anthropic: its effort, its budget, and thinking turned off ("none")
anthropic := func(body string) map[string]any {
t.Helper()
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
return sentBody(t, a)
}
if sent := anthropic(`{"model":"group/r","max_tokens":64000,"thinking":{"type":"adaptive"},"output_config":{"effort":"high"},"messages":[{"role":"user","content":"hi"}]}`); sent["reasoning_effort"] != "xhigh" {
t.Errorf("anthropic effort high: sent %v", sent["reasoning_effort"])
}
if sent := anthropic(`{"model":"group/r","max_tokens":64000,"thinking":{"type":"enabled","budget_tokens":4096},"messages":[{"role":"user","content":"hi"}]}`); sent["reasoning_effort"] != "medium" {
t.Errorf("anthropic budget 4096 (low): sent %v", sent["reasoning_effort"])
}
if sent := anthropic(`{"model":"group/r","max_tokens":64000,"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"hi"}]}`); sent["reasoning_effort"] != "low" {
t.Errorf("anthropic thinking off: sent %v", sent["reasoning_effort"])
}
// a model's suffix is asked through the map too
postOK(t, s, "", strings.Replace(chat("hi", nil, 0, `,"reasoning_effort":"low"`), `"group/r"`, `"group/r:high"`, 1))
if sent := sentBody(t, a); sent["reasoning_effort"] != "xhigh" {
t.Errorf("group/r:high: sent %v", sent["reasoning_effort"])
}
// a level the model hasn't is sent at its nearest
setEffortMaps(t, map[string]map[string]string{"a/small": {"high": "max"}})
postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"high"`))
if sent := sentBody(t, a); sent["reasoning_effort"] != "xhigh" {
t.Errorf("high to max on a model up to xhigh: sent %v", sent["reasoning_effort"])
}
}
// Without a map a member is sent the level asked, as before; each
// member's map is its own, the one fallen back to included.
func TestMemberEffortMapPerMember(t *testing.T) {
s, a, b := ruled(t)
if err := provider.SetModelEfforts("b/big", []string{"low", "medium", "high", "xhigh"}); err != nil {
t.Fatal(err)
}
postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"high"`))
if sent := sentBody(t, a); sent["reasoning_effort"] != "high" {
t.Fatalf("no map: sent %v", sent["reasoning_effort"])
}
setEffortMaps(t, map[string]map[string]string{"b/big": {"high": "xhigh"}})
postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"high"`))
if sent := sentBody(t, a); sent["reasoning_effort"] != "high" {
t.Fatalf("a without a map: sent %v", sent["reasoning_effort"])
}
a.mu.Lock()
a.fail = 500
a.mu.Unlock()
out, r := postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"high"`))
if sent := sentBody(t, b); !strings.Contains(out, "from kb") || sent["reasoning_effort"] != "xhigh" {
t.Fatalf("fallback to b: sent %v (%s)", sent, out)
}
last := r.Tries[len(r.Tries)-1]
if last.Model != "big" || last.Effort != "xhigh" || last.MappedFrom != "high" || r.Tries[0].MappedFrom != "" {
t.Fatalf("fallback traced %+v", r.Tries)
}
}
// The map is asked after the group's classifier picks the turn's level
// (effort=auto): the pick is what the member is asked for.
func TestMemberEffortMapOverAuto(t *testing.T) {
s, a, _, j := jevved(t, provider.EffortAuto)
if err := provider.SetModelEfforts("a/small", []string{"low", "medium", "high", "xhigh"}); err != nil {
t.Fatal(err)
}
setEffortMaps(t, map[string]map[string]string{"a/small": {"high": "xhigh"}})
j.score = 2.4 // high
_, r := postOK(t, s, "s1", chat("redesign the scheduler", nil, 0, `,"reasoning_effort":"low"`))
if sent := sentBody(t, a); sent["reasoning_effort"] != "xhigh" {
t.Fatalf("sent %v", sent["reasoning_effort"])
}
if r.Rule == nil || r.Rule.Pick != "high" || r.Tries[0].MappedFrom != "high" || r.Tries[0].Effort != "xhigh" {
t.Fatalf("traced %+v %+v", r.Rule, r.Tries)
}
}
// A member fixed at an effort keeps it: a map on it is not kept.
func TestMemberEffortMapNotOnFixed(t *testing.T) {
s, a, _ := ruled(t)
setMembers(t, []string{"a/small:low", "b/big"})
setEffortMaps(t, map[string]map[string]string{"a/small:low": {"high": "xhigh"}})
if g, _, _ := provider.FindGroup("group/r"); g.EffortMaps != nil {
t.Fatalf("kept %v", g.EffortMaps)
}
postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"high"`))
if sent := sentBody(t, a); sent["reasoning_effort"] != "low" {
t.Fatalf("sent %v", sent["reasoning_effort"])
}
}
// /v1/models offers, for a member with a map, the levels its map is asked
// for besides its model's own.
func TestMemberEffortMapLevelsListed(t *testing.T) {
s, _, _ := ruled(t)
if err := provider.SetModelEfforts("a/small", []string{"low", "medium", "high"}); err != nil {
t.Fatal(err)
}
if err := provider.SetModelEfforts("b/big", []string{"medium", "high", "xhigh"}); err != nil {
t.Fatal(err)
}
setEffortMaps(t, map[string]map[string]string{"a/small": {"xhigh": "high"}})
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
got := levelsListed(t, rec.Body.Bytes(), "group/r")
if strings.Join(got, ",") != "medium,high,xhigh" {
t.Fatalf("levels %v", got)
}
}
// levelsListed are the reasoning levels /v1/models' body lists for id.
func levelsListed(t *testing.T, body []byte, id string) []string {
t.Helper()
var list struct {
Data []struct {
ID string `json:"id"`
Levels []struct {
Effort string `json:"effort"`
} `json:"supported_reasoning_levels"`
} `json:"data"`
}
if err := json.Unmarshal(body, &list); err != nil {
t.Fatalf("%v: %s", err, body)
}
for _, m := range list.Data {
if m.ID == id {
var out []string
for _, l := range m.Levels {
out = append(out, l.Effort)
}
return out
}
}
t.Fatalf("%s not listed: %s", id, body)
return nil
}