Files
magpie/internal/gateway/group_output_test.go
T
yetone 7df9345903 routing: a group tells agents the longest reply a member of it gives, and each member is asked for no more than its own (ARNO on Discord: 发现group 模型的maxTokens是按照最小的来设定的,导致agent经常跑一半发生错误 "Response was truncated before completion.")
A group's catalog entry took the smallest member's output limit, so /v1/models and the agents' catalogs told agents that, and every reply through the group was capped at it, even from the members that write far longer ones. The group's Output is now its largest member's (as its context window already is, #700), and the gateway, for a group request, lowers the agent's max tokens to each member's own known limit (its catalog Output: the user's per-model setting, else the vendor's list or models.dev) only when the request asks for more: max_tokens / max_completion_tokens (Chat), max_output_tokens (Responses), generationConfig.maxOutputTokens (Gemini), and for Anthropic max_tokens with the thinking budget kept under it (budget = limit-1, or thinking off when no 1024+ budget fits). A request asking for less, or a member with no known limit, goes as before.

TestGroupOutputPerMember (members at 4096 and 32000: the group says 32000; asked for 20000, the 4096 one gets 4096 and the 32000 one 20000; asked for 1000, 1000) fails on the old code at either half; TestWithMaxOutputAnthropicThinking. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn, TestUserPath and TestClaudeSignedOut failed under load and pass alone).
2026-10-04 22:48:06 +08:00

99 lines
3.2 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A group tells agents the longest reply a member of it gives, and each
// member is asked for no more than its own (ARNO on Discord: a group's
// maxTokens was its smallest member's, and replies were truncated).
func TestGroupOutputPerMember(t *testing.T) {
fresh(t)
asked := map[string]map[string]any{}
for _, id := range []string{"a", "b"} {
id := id
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var q map[string]any
b, _ := io.ReadAll(r.Body)
json.Unmarshal(b, &q)
asked[id] = q
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"id":"x","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`)
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: id, Name: strings.ToUpper(id), Key: "k", Models: []string{"m"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
}
if err := provider.SetModelOutput("a/m", 4096); err != nil {
t.Fatal(err)
}
if err := provider.SetModelOutput("b/m", 32000); err != nil {
t.Fatal(err)
}
for _, g := range []provider.Group{{Name: "G", Members: []string{"a/m", "b/m"}}, {Name: "H", Members: []string{"b/m", "a/m"}}} {
g.Routing = provider.Ordered
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
s := New()
// told the longest
var told int
for _, e := range provider.Served() {
if e.ID == provider.GroupPrefix+"g" {
told = e.Output
}
}
if told != 32000 {
t.Fatalf("the group says %d", told)
}
// a asked for its own 4096, b for the 20000 the agent asked
for _, g := range []string{"g", "h"} {
if code, body := postAs(t, s, "", `{"model":"group/`+g+`","max_tokens":20000,"messages":[{"role":"user","content":"hi"}]}`); code != 200 {
t.Fatalf("%d %s", code, body)
}
}
if a, b := asked["a"]["max_tokens"], asked["b"]["max_tokens"]; a != float64(4096) || b != float64(20000) {
t.Fatalf("a asked %v, b asked %v", a, b)
}
// a request under a member's limit goes as it is
postAs(t, s, "", `{"model":"group/g","max_tokens":1000,"messages":[{"role":"user","content":"hi"}]}`)
if a := asked["a"]["max_tokens"]; a != float64(1000) {
t.Fatalf("a asked %v", a)
}
}
func TestWithMaxOutputAnthropicThinking(t *testing.T) {
out := withMaxOutput(provider.Anthropic, []byte(`{"max_tokens":32000,"thinking":{"type":"enabled","budget_tokens":16000}}`), 8192)
var v struct {
MaxTokens int `json:"max_tokens"`
Thinking struct {
Type string `json:"type"`
Budget int `json:"budget_tokens"`
} `json:"thinking"`
}
json.Unmarshal(out, &v)
if v.MaxTokens != 8192 || v.Thinking.Type != "enabled" || v.Thinking.Budget != 8191 {
t.Fatalf("%s", out)
}
out = withMaxOutput(provider.Anthropic, []byte(`{"max_tokens":32000,"thinking":{"type":"enabled","budget_tokens":16000}}`), 1000)
json.Unmarshal(out, &v)
if v.MaxTokens != 1000 || v.Thinking.Type != "disabled" {
t.Fatalf("%s", out)
}
if out := withMaxOutput(provider.Responses, []byte(`{"max_output_tokens":100}`), 8192); string(out) != `{"max_output_tokens":100}` {
t.Fatalf("%s", out)
}
}