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"]) + } +}