From f0b0153e92b64d6a95d1d266aa3ecf962ed45ce8 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 20:45:20 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20routing=20group's=20own=20reason?= =?UTF-8?q?ing=20levels=20are=20the=20most=20its=20members=20are=20asked?= =?UTF-8?q?=20for,=20whoever=20asks=20(#671:=20=E6=88=91=E5=9C=A8=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=87=8C=E9=99=90=E5=88=B6=E4=BA=86=E6=9C=80=E9=AB=98?= =?UTF-8?q?=E5=88=B0high=EF=BC=8C=E4=BD=86=E6=98=AF=E4=BB=96=E7=AA=81?= =?UTF-8?q?=E7=A0=B4=E4=BA=86=E8=BF=99=E4=B8=AA=E9=99=90=E5=88=B6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A group whose 推理档位 is set to its own levels (Group.Levels, #295) only listed them to agents: nothing bounded what was sent. The reporter's group offers none…high with 推理强度 "每轮判断" (effort auto, GPT-6-Luna deciding), and gpt-6.1-sol went out at xhigh: the decision model picks among low, medium, high and xhigh, and that pick was fitted to the member's own levels (fitLevel) only, never to the group's. The same held for an agent asking for a level the group doesn't offer (Pi or Codex keeping xhigh from another model, Claude Code's output_config max or a 60000-token thinking budget) and for a model asked for at an effort by its suffix (group/x:max). Now every try of a group's member is bounded by the levels the group offers for it (groupLevels: the group's own, else the outermost group in it that names some): the turn's pick, the suffix's effort and the agent's own level (agentEffort, in Chat's, Responses' or Anthropic's words, thinking budgets included) each become the nearest level the group offers (withinLevels, fitEffort's rule) before the member's own fitting; ultra counts as offered where max is. A member the user fixed at an effort ("provider/model:xhigh") is the user's explicit choice and keeps it. A group without its own levels is unchanged. TestGroupLevelsCapEffort (a none…high group: auto picking xhigh, on the turn's first request and its tool rounds; the agent asking xhigh, max or ultra in Chat and Responses; Anthropic adaptive effort max and a 60000 budget; group/r:max; a member fixed at xhigh) fails on the old code (auto xhigh sent as xhigh) and passes. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried against a real account. --- internal/gateway/decide.go | 41 ++++++++++++++ internal/gateway/gateway.go | 16 +++++- internal/gateway/grouplevels_test.go | 82 ++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) diff --git a/internal/gateway/decide.go b/internal/gateway/decide.go index ece8a6e8f..68413ad2b 100644 --- a/internal/gateway/decide.go +++ b/internal/gateway/decide.go @@ -751,3 +751,44 @@ func withFixedEffort(proto provider.Protocol, body []byte, effort string) []byte } return body } + +// groupLevels are the reasoning levels the group offers for the member a +// candidate is of, where the user named them (Group.Levels, #295): the +// group's own, else those of the outermost group in it that names some. +// nil when none is named. They bound what the candidate is asked for +// (withinLevels): the agent, a model's suffix or the turn's pick asking +// for a level the group doesn't offer is sent the nearest it does (#671). +func groupLevels(g provider.Group, ms []provider.Member, c candidate) []string { + if len(g.Levels) > 0 { + return g.Levels + } + for _, m := range ms { + if m.Provider.ID != c.p.ID || m.Model != c.model || m.Effort != c.effort { + continue + } + if i := slices.IndexFunc(m.Via, func(v provider.Group) bool { return len(v.Levels) > 0 }); i >= 0 { + return m.Via[i].Levels + } + } + return nil +} + +// withinLevels is want, when levels offer it, else the level of levels +// nearest it (fitEffort). Codex's ultra is max with agents of its own, so +// a group that offers max offers it too. +func withinLevels(want string, levels []string) string { + if want == "" || len(levels) == 0 || slices.Contains(levels, want) || want == "ultra" && slices.Contains(levels, "max") { + return want + } + return fitEffort(want, levels) +} + +// agentEffort is the reasoning level a request asks for in its own API's +// words: Chat's and Responses' as the agent named it (minimal and none +// kept), Anthropic's as requestEffort reads it. "" when it names none. +func agentEffort(proto provider.Protocol, body []byte) string { + if proto == provider.Anthropic { + return requestEffort(proto, body) + } + return strings.ToLower(strings.TrimSpace(bodyEffort(proto, body))) +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f9ef49223..7756ecf84 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1366,7 +1366,14 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro picked := false // the effort asked for in place of the agent's // a member fixed at an effort (#189), else the model asked for at // one (#536) - fixed := cmp.Or(c.effort, askedEffort) + // the levels the group offers (Group.Levels) bound what the agent, + // a model's suffix or the turn's pick asks for (#671); a member the + // user fixed at an effort is asked for it + var levels []string + if isGroup { + levels = groupLevels(g, ms, c) + } + fixed := cmp.Or(c.effort, withinLevels(askedEffort, levels)) if fixed != "" { // a member fixed at an effort is asked for it, at the level its // model has nearest, whatever the agent asked or the turn's @@ -1376,9 +1383,14 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // the level this model has nearest to the one picked; one whose // levels aren't known isn't asked for more than high, which // every vendor with levels takes - if b := withEffort(from, attemptBody, fitLevel(effort, c.p.Efforts(c.model))); !bytes.Equal(b, attemptBody) { + if b := withEffort(from, attemptBody, fitLevel(withinLevels(effort, levels), c.p.Efforts(c.model))); !bytes.Equal(b, attemptBody) { attemptBody, picked = b, true } + } else if e := agentEffort(from, attemptBody); e != "" && e != "none" { + // the agent's own level, where the group doesn't offer it + if want := withinLevels(e, levels); want != e { + attemptBody = withEffort(from, attemptBody, want) + } } // a member sent fast is, in its vendor's words, where its model // has a fast mode; else it goes as the agent asked diff --git a/internal/gateway/grouplevels_test.go b/internal/gateway/grouplevels_test.go index 655e414be..20dc6e6d9 100644 --- a/internal/gateway/grouplevels_test.go +++ b/internal/gateway/grouplevels_test.go @@ -83,3 +83,85 @@ func TestGroupLevelsNamed(t *testing.T) { } } } + +// The levels a group offers bound what its members are asked for (#671): a +// group offering none to high whose decision model picks xhigh for a turn, +// an agent asking for xhigh or max anyway (in Chat, Responses or +// Anthropic's words), or a model asked for at max by its suffix, is sent +// high — what the member takes nearest within them — never more. A +// member the user fixed at an effort keeps it. +func TestGroupLevelsCapEffort(t *testing.T) { + s, a, _, j := jevved(t, provider.EffortAuto) + all := []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"} + if err := provider.SetModelEfforts("a/small", all); err != nil { + t.Fatal(err) + } + g, _, _ := provider.FindGroup("group/r") + g.Levels = []string{"none", "minimal", "low", "medium", "high"} + if err := provider.SaveGroup(g); err != nil { + t.Fatal(err) + } + // the turn's pick + j.score = 3 // xhigh + _, r := postOK(t, s, "s1", chat("design a lock-free queue", nil, 0, `,"reasoning_effort":"medium"`)) + if r.Rule == nil || r.Rule.Pick != "xhigh" { + t.Fatalf("picked %+v", r.Rule) + } + if sent := sentBody(t, a); sent["reasoning_effort"] != "high" || r.Tries[0].Effort != "high" { + t.Fatalf("auto xhigh: sent %v, traced %+v", sent["reasoning_effort"], r.Tries) + } + // and its tool rounds + postOK(t, s, "s1", chat("design a lock-free queue", nil, 2, `,"reasoning_effort":"medium"`)) + if sent := sentBody(t, a); sent["reasoning_effort"] != "high" { + t.Fatalf("auto xhigh within the turn: sent %v", sent["reasoning_effort"]) + } + + // the agent's own level, with nothing picked + g, _, _ = provider.FindGroup("group/r") + g.Effort = "" + if err := provider.SaveGroup(g); err != nil { + t.Fatal(err) + } + for asked, want := range map[string]string{"xhigh": "high", "max": "high", "ultra": "high", "high": "high", "low": "low"} { + postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"`+asked+`"`)) + if sent := sentBody(t, a); sent["reasoning_effort"] != want { + t.Errorf("chat asked %s: sent %v", asked, sent["reasoning_effort"]) + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"group/r","input":"hi","reasoning":{"effort":"`+asked+`","summary":"auto"}}`))) + if rec.Code != 200 { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + if sent := sentBody(t, a); sent["reasoning_effort"] != want { + t.Errorf("responses asked %s: sent %v", asked, sent["reasoning_effort"]) + } + } + 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":"max"},"messages":[{"role":"user","content":"hi"}]}`); sent["reasoning_effort"] != "high" { + t.Errorf("anthropic effort max: sent %v", sent["reasoning_effort"]) + } + if sent := anthropic(`{"model":"group/r","max_tokens":64000,"thinking":{"type":"enabled","budget_tokens":60000},"messages":[{"role":"user","content":"hi"}]}`); sent["reasoning_effort"] != "high" { + t.Errorf("anthropic budget 60000: sent %v", sent["reasoning_effort"]) + } + + // a model's suffix + postOK(t, s, "", strings.Replace(chat("hi", nil, 0, `,"reasoning_effort":"low"`), `"group/r"`, `"group/r:max"`, 1)) + if sent := sentBody(t, a); sent["reasoning_effort"] != "high" { + t.Errorf("group/r:max: sent %v", sent["reasoning_effort"]) + } + + // a member fixed at an effort is the user's own choice + setMembers(t, []string{"a/small:xhigh", "b/big"}) + postOK(t, s, "", chat("hi", nil, 0, `,"reasoning_effort":"low"`)) + if sent := sentBody(t, a); sent["reasoning_effort"] != "xhigh" { + t.Errorf("fixed xhigh: sent %v", sent["reasoning_effort"]) + } +}