Files
magpie/internal/gateway/sub2api_think_off_test.go
T
yetone 7cc393bcc1 gateway: a relay reading thinking off as effort none, refused naming its levels, is asked with thinking left to the model (#1465)
MengXiangxi's Claude Code auto mode on a Sub2API's gpt-6.1-sol blocked
every action: "Sub2API: gpt-6.1-sol does not support reasoning effort
"none"; use low, medium, high, xhigh or max" (400).

Cause: Claude Code's classifier (and every side query magpie turns
thinking off for, thinkingOffUnlessAsked) goes to Sub2API's Messages API
as it was sent, with thinking {type: disabled}. Sub2API's
anthropicReasoningEffort reads disabled as effort "none", and
ValidateGPT61SolReasoningEffort turns none away for gpt-6.1-sol
(Wei-Shaw/sub2api, backend/internal/pkg/openai/constants.go). magpie's
Anthropic retry for a refused thinking-off knew only the alwaysThinks,
mandatoryReasoning and thinkingOffRefused wordings, so this 400 went to
Claude Code. The refusal is about the model on that relay, not the
account: the same request with thinking left out is taken.

Change:
- refusesThinkingOff (anthropic.go) is the one list of thinking-off
  refusals, now also one turning a quoted "none" away while naming the
  levels taken (effortLevelsNamed && noneQuoted). Both routes use it: a
  relayed Messages request (passthrough) and a request translated to an
  Anthropic endpoint (forwardTranslated, thinkingBackOn).
- The relayed route now remembers the refusal (markUnfit(offRefused),
  the mark the translated route and passthrough effort fitting already
  read) and sends that model's later requests with thinking left to the
  model at once, so the classifier, which asks on every action, doesn't
  pay a 400 round trip each time.
- Chat/Responses routes already retried this wording through
  effortLevelsNamed (reasoning_effort none -> lowest level); unchanged.
- docs/subsystems/gateway-routing.md says so.

Tests: TestSub2APIThinkingOffRefused (the reporter's literal message;
the classifier relayed is refused once, asked again, then sent without
thinking at once, title asks included; a 400 for another level stays the
agent's), TestRefusesThinkingOff, and the Sub2API wording added to
TestTranslatedThinkingOffRefused. Broke the fix by hand twice (the new
clause off; the remembered send off) and watched each go red.

Run: gofmt -l internal/ (clean); go vet ./internal/gateway on darwin,
GOOS=linux -tags nogui, GOOS=windows; go test -tags nogui
./internal/gateway -count=1 -timeout 40m: ok in 669s (load ~40 from
other sessions; an earlier run hit the default 10m timeout under that
load). Not tried against a real Sub2API.
2026-10-13 10:06:12 +08:00

74 lines
3.1 KiB
Go

package gateway
import (
"bytes"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// sub2apiNoneRefused is Sub2API's answer, on its Messages API, to a
// request for gpt-6.1-sol with thinking turned off, which it reads as
// reasoning effort none (anthropicReasoningEffort) and turns away (#1465,
// MengXiangxi's message, from ValidateGPT61SolReasoningEffort).
const sub2apiNoneRefused = `{"type":"error","error":{"type":"invalid_request_error","message":"gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max"}}`
// #1465: Claude Code's auto mode classifier, relayed as it was sent to a
// Sub2API's gpt-6.1-sol, turns thinking off; Sub2API refused every one,
// and Claude Code blocked every action. Asked again with thinking left to
// the model, and so from the start from then on, its title asks (which
// magpie turns thinking off for) included. Another 400 naming the levels,
// for another level, is the agent's to see.
func TestSub2APIThinkingOffRefused(t *testing.T) {
var sent []string
f := &fake{t: t, reply: anthropicHi}
f.refuse = func(b []byte) (int, string) {
th := gjson.GetBytes(b, "thinking.type").String()
sent = append(sent, th)
if th == "disabled" {
return 400, sub2apiNoneRefused
}
return 0, ""
}
setup(t, provider.Anthropic, f)
srv := New()
for turn, want := range [][]string{{"disabled", ""}, {""}} {
sent = nil
code, body := postTo(t, srv, "/v1/messages", autoModeAsk("m1"))
if code != 200 || len(sent) != len(want) || sent[len(sent)-1] != "" {
t.Fatalf("turn %d: %d %s, thinking sent %q, want %q", turn+1, code, body, sent, want)
}
if !bytes.Contains(f.got, []byte("tidy up the build")) {
t.Errorf("turn %d: sent %s", turn+1, f.got)
}
}
sent = nil
if code, body := postTo(t, srv, "/v1/messages", `{"model":"m1","max_tokens":32,"stream":true,"messages":[{"role":"user","content":"title?"}]}`); code != 200 || len(sent) != 1 || sent[0] != "" {
t.Errorf("title: %d %s, thinking sent %q", code, body, sent)
}
f.calls = 0
f.refuse = func([]byte) (int, string) {
return 400, `{"type":"error","error":{"type":"invalid_request_error","message":"reasoning effort \"xhigh\" exceeds this group's limit; use low, medium or high"}}`
}
if code, _ := post(t, "/v1/messages", autoModeAsk("m1")); code != 400 || f.calls != 1 {
t.Errorf("another level refused: %d after %d calls", code, f.calls)
}
}
func TestRefusesThinkingOff(t *testing.T) {
for b, want := range map[string]bool{
sub2apiNoneRefused: true,
`{"error":{"message":"Sub2API: gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max","type":"invalid_request_error"},"type":"error"}`: true,
`GLM-5.3 always engages in thinking and cannot be disabled`: true,
`{"error":{"message":"reasoning effort \"xhigh\" exceeds this group's limit; use low, medium or high"}}`: false,
`{"error":{"message":"None of the tools match; use low, medium or high"}}`: false,
} {
if got := refusesThinkingOff([]byte(b)); got != want {
t.Errorf("%s: %v, want %v", b, got, want)
}
}
}