Files
magpie/internal/gateway/adaptive_test.go
T
yetoneandggbdpq 6934853b42 gateway: a Chat or Responses client that asks Claude Opus 4.7 or later to reason sees the reasoning, asked as thinking.display "summarized", not left out (#1485, ggbdpq)
Claude Opus 4.7, 4.8, 5 and 5.5, Sonnet 5 and 5.5, Haiku 5.5, Fable and
Mythos default thinking.display to "omitted": a thinking block comes
back with an empty thinking field unless "summarized" is asked for
(Anthropic's thinking docs, Controlling thinking display). buildAnthropic
asked them {"type":"adaptive"} alone, so a Chat client's
reasoning_effort or a Responses client's reasoning (Codex) got a reply
with no reasoning_content and no reasoning summary, while OpenAI's
(summary "auto" in buildResponses) and the Claude subscription's
(--thinking-display summarized) show it, as 4.6 did by default.

buildAnthropic now asks {"type":"adaptive","display":"summarized"}. A
relay that refuses display is asked again without it and from then on
sent none (refuseShapes, as for Claude Code's display). Thinking turned
off later (disabled, between_tools, none) replaces the whole thinking
object, so display never goes with it.

Run: TestTranslatedClaudeReasoningIsShown (a fake Anthropic API that
writes thinking text only for display summarized, and for 4.6 by
default) fails on the old code for Opus 5.5, Opus 4.7 and Fable 5.1 over
Chat and Opus 5.5 over Responses (no display, no reasoning in the
reply); TestTranslatedDisplayLeftOutWhereRefused fails on the old code
and passes with it (a strict relay still answers, then is sent none).
TestAdaptiveThinking and TestFableAndMythosNeverGetABudget expect the
new shape for a built request. The shapes tests
(TestRelayRefusingClaudeCodesNewShapes, TestClaudeCodesNewShapesKeptWhereTaken)
pass. Not tried with a real Anthropic key, Bedrock or Vertex, nor in
the real Codex UI.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-10 17:58:27 +08:00

120 lines
5.2 KiB
Go

package gateway
import (
"encoding/json"
"testing"
)
// A Chat client's max tokens is what the model can write — Pi sends the
// model's own output limit — so the thinking budget fits under it rather
// than max_tokens raised past it, which the upstream refuses with a 400.
// With no cap asked, max_tokens still makes room for the budget.
func TestThinkingBudgetFitsTheClientsCap(t *testing.T) {
for _, c := range []struct {
max int
effort string
wantMax float64
wantBudget float64
wantNoThink bool
}{
{16384, "high", 16384, 15360, false},
{32000, "high", 32000, 24000, false},
{8192, "xhigh", 8192, 7168, false},
{0, "high", 28096, 24000, false},
{2048, "high", 2048, 0, true},
} {
body, _ := json.Marshal(map[string]any{"model": "k", "max_completion_tokens": c.max, "reasoning_effort": c.effort,
"messages": []map[string]any{{"role": "user", "content": "hi"}}})
r, err := parseChat(body)
if err != nil {
t.Fatal(err)
}
var out map[string]any
json.Unmarshal(buildAnthropic(r, "kimi-k3"), &out)
if out["max_tokens"] != c.wantMax {
t.Errorf("%d %s: max_tokens %v, want %v", c.max, c.effort, out["max_tokens"], c.wantMax)
}
th, _ := out["thinking"].(map[string]any)
if c.wantNoThink {
if th != nil {
t.Errorf("%d %s: thinking %v with no room for it", c.max, c.effort, th)
}
continue
}
if th["budget_tokens"] != c.wantBudget {
t.Errorf("%d %s: budget %v, want %v", c.max, c.effort, th["budget_tokens"], c.wantBudget)
}
}
}
func TestAdaptiveThinking(t *testing.T) {
for model, want := range map[string]bool{
"claude-opus-5-5": true, "claude-opus-5": true, "claude-sonnet-4-6": true,
"claude-opus-4-7": true, "anthropic.claude-sonnet-4.6-v1": true, "claude-sonnet-5": true,
"claude-sonnet-4-5-20250929": false, "claude-sonnet-4-20250514": false, "claude-opus-4-1": false,
"claude-3-7-sonnet-20250219": false, "deepseek-v4": false, "claude-haiku-4-5": false,
"claude-opus-5.5": true, "opus-5.5": true, "anthropic/claude-opus-5.5": true, "claude-opus-5-5[1m]": true,
"claude-opus-5-5-20260901": true, "claude-5-5-opus": true, "claude-3-5-sonnet-20241022": false,
"claude-opus-4-20250514": false, "claude-opus-4-1-20250805": false, "kimi-k3": false, "gpt-5.5": false,
} {
if got := adaptiveOnly(model); got != want {
t.Errorf("adaptiveOnly(%q) = %v", model, got)
}
}
var out map[string]any
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "xhigh"}, "claude-opus-5-5"), &out)
if th, _ := json.Marshal(out["thinking"]); string(th) != `{"display":"summarized","type":"adaptive"}` {
t.Errorf("thinking = %s", th)
}
if oc, _ := json.Marshal(out["output_config"]); string(oc) != `{"effort":"xhigh"}` {
t.Errorf("output_config = %s", oc)
}
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "xhigh"}, "claude-opus-4-6"), &out)
if oc, _ := json.Marshal(out["output_config"]); string(oc) != `{"effort":"max"}` {
t.Errorf("4.6 output_config = %s", oc)
}
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "low"}, "claude-sonnet-4-5"), &out)
if th, _ := json.Marshal(out["thinking"]); string(th) != `{"budget_tokens":4096,"type":"enabled"}` {
t.Errorf("old model thinking = %s", th)
}
}
// Z.ai's GLM-5.2 and GLM-5.3 take their effort in output_config, as ZCode
// sends it, fitted to their levels; a budget alone leaves them at theirs
func TestGLMEffortInOutputConfig(t *testing.T) {
for model, want := range map[string]bool{
"GLM-5.3": true, "glm-5.3-flash": true, "GLM-5.2": true, "zai/glm-5.3": true,
"GLM-5-Turbo": false, "glm-5.1": false, "glm-5.30": false, "claude-opus-5-5": false,
} {
if got := effortInOutputConfig.MatchString(model); got != want {
t.Errorf("effortInOutputConfig(%q) = %v", model, got)
}
}
var out map[string]any
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "max"}, "GLM-5.3"), &out)
if oc, _ := json.Marshal(out["output_config"]); string(oc) != `{"effort":"max"}` {
t.Errorf("output_config = %s", oc)
}
out = nil
json.Unmarshal(buildAnthropic(&Request{Thinking: true, Effort: "high"}, "GLM-5-Turbo"), &out)
if out["output_config"] != nil {
t.Errorf("GLM-5-Turbo asked output_config: %v", out["output_config"])
}
levels := []string{"low", "high", "max"}
for body, want := range map[string]string{
// Claude Code's budget for medium, the nearest of GLM-5.3's
`{"thinking":{"type":"enabled","budget_tokens":10000}}`: `{"output_config":{"effort":"high"},"thinking":{"budget_tokens":10000,"type":"enabled"}}`,
`{"thinking":{"type":"adaptive"},"output_config":{"effort":"low","format":"x"}}`: `{"output_config":{"effort":"low","format":"x"},"thinking":{"type":"adaptive"}}`,
`{"thinking":{"type":"adaptive"},"output_config":{"effort":"xhigh"}}`: `{"output_config":{"effort":"max"},"thinking":{"type":"adaptive"}}`,
`{"thinking":{"type":"disabled"}}`: `{"thinking":{"type":"disabled"}}`,
`{"max_tokens":5}`: `{"max_tokens":5}`,
} {
var v any
json.Unmarshal(withOutputEffort([]byte(body), levels), &v)
if got, _ := json.Marshal(v); string(got) != want {
t.Errorf("%s:\n got %s\n want %s", body, got, want)
}
}
}