Files
magpie/internal/gateway/thinking_needed.go
T
yetone 25df3187bf gateway: a model whose upstream says thinking.type must be "enabled" is asked again thinking
cybercaius on Discord (#help), v0.1.1862: Claude Desktop's connection
check failed with 'Gateway rejected model
"magpie-0958175816.anthropic.claude-haiku-5-5[1m]" (HTTP 400)', the body
'天翼云: {"error":{"message":"{\"code\":\"400\",\"message\":\"thinking.type
must be \\\"enabled\\\" …","type":"upstream_error",…}}'. 天翼云 is his own
provider, a relay whose message is its own upstream's whole body.

What was sent: Desktop's check ({"max_tokens":1,"messages":[{"role":
"user","content":"."}]}, raised to 16) asks for no thinking. Relayed to
a Messages provider, a vendor's model gets thinking {"type":"disabled"}
(thinkingOffUnlessAsked) and a Claude-named one none; translated to Chat
it gets no reasoning_effort. Nothing today changed that: no gateway
commit since yesterday touches what thinking is sent. What changed is
where the check goes: ee6f8866f sends it to Desktop's default (his
天翼云 group), and c30e2a645 (v0.1.1861) has it go by the group's order
instead of a classifier's pick, so the group's first member now gets
it. Why the refusal wasn't read: refusesThinkingOff knows a vendor
refusing thinking *off* (alwaysThinks, thinkingOffRefused, …), and its
retry only takes "disabled" out, which leaves thinking absent, still
not "enabled". Nothing knew an upstream that wants thinking enabled.

Now (thinking_needed.go): a 400 saying thinking.type must be enabled
(thinkingNeededRefusal, read off the reply, not the provider) is asked
again once thinking at Anthropic's least budget, 1024, max_tokens raised
past it by what was asked (16 -> 1040) and temperature/top_p/top_k
left out (withThinkingEnabled); markUnfit(thinkingNeeded) keeps the
model so until restart, so the next check or title goes thinking in
one call. Both routes: passthrough (relayed Messages), forwardTranslated
(Messages body once built; Chat/Responses at the lowest effort via
thinkingBackOn), and translate for a non-streaming client whose refusal
came in a 200 stream. forwardOnce doesn't turn such a model's enabled
into adaptive (a Claude 5.5 name would otherwise send adaptive and be
refused again). docs/subsystems/gateway-routing.md says so.

Test: TestClaudeDesktopProbeThinkingNeeded, Desktop's literal check on
the literal id through a 天翼云 group to a fake that answers the
reported body (wrapped as the relay wraps it) whenever thinking.type
isn't "enabled": a vendor model and a Claude name on Messages, Chat
with the refusal as a 400 and inside a 200 stream. Each: 200 in two
calls, the retry carrying enabled/1024 under max_tokens > 1024, then the
next check and a title in one call. TestWithThinkingEnabled.

Run (mote, at this tree): the tests above; go test -tags nogui
./internal/gateway/ (whole package): ok (537s); gofmt -l clean;
go vet ./internal/gateway/ for darwin, linux, windows: ok.
The package run was at d6df72a2b plus this change; rebased on
51247a55b (0b99b8fbe touched internal/gateway), the new tests with
-run 'Desktop|Thinking|ThinkOff|Translated|LoopGuard': ok.

Broken by hand (each red on mote):
- gateway.go as on origin/main: all four subtests 400 with the reported
  message, byte for byte in its shape;
- adaptiveThinking not skipped: the Claude-name subtest red (sent
  thinking adaptive);
- the in-stream retry in translate off: the stream subtest red;
- the memory off (upfront thinking in passthrough and
  forwardTranslated): every subtest red ("after: 200 in 2 calls", or
  400 once the retry's own fits check stops it).

Not tried: the reporter's real 天翼云 relay, or which API it is on
(Messages or Chat) — the reply shape fits both, both covered; a relay
that turns reasoning_effort into thinking (the Chat cases assume it);
a streaming client's refusal inside a 200 stream (only the
non-streaming one, Desktop's check, is asked again); a real Claude
Desktop. Not run: the full ./... suite.
2026-10-28 17:53:52 +08:00

65 lines
2.7 KiB
Go

package gateway
import (
"regexp"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// thinkingNeededRefusal is an upstream turning away a Messages request
// whose thinking isn't "enabled": thinking left out, or turned off as
// magpie does for a vendor's model asked without it
// (thinkingOffUnlessAsked). A relay in front of such a model answered
// Claude Desktop's connection check ({"max_tokens":1,"messages":[…"."]},
// no thinking) with `thinking.type must be "enabled"` (cybercaius on
// Discord, v0.1.1862, his own 天翼云 provider). Read off the reply, not
// the provider: any upstream that says it is asked again thinking.
var thinkingNeededRefusal = regexp.MustCompile(`(?i)thinking[._ ]type\W+(?:must|should|has to) be\W+enabled\b`)
// thinkingNeeded is how unfit remembers an upstream that takes model only
// with thinking enabled (thinkingNeededRefusal).
func thinkingNeeded(model string) string { return "thinking needed\x00" + model }
// neededBudget is the thinking budget a request that asked for none is
// sent at, for a model that needs thinking: Anthropic's least, which the
// reply limit is raised past (withThinkingEnabled).
const neededBudget = 1024
// withThinkingEnabled is a Messages body that asked for no thinking, or
// turned it off, sent thinking at the least budget instead, for a model
// that takes no other: max_tokens is raised past the budget by what was
// asked (a budget must be below it), and the sampling a thinking request
// is refused with (temperature, top_p, top_k) left out. false for a body
// that asks to think already, or isn't JSON.
func withThinkingEnabled(body []byte) ([]byte, bool) {
if !gjson.ValidBytes(body) {
return nil, false
}
switch gjson.GetBytes(body, "thinking.type").String() {
case "enabled", "adaptive":
return nil, false
}
maxTokens := gjson.GetBytes(body, "max_tokens").Int()
if maxTokens <= neededBudget {
maxTokens = neededBudget + max(maxTokens, 1)
}
out := withoutFields(body, "temperature", "top_p", "top_k")
out = withFields(out, map[string]any{
"thinking": map[string]any{"type": "enabled", "budget_tokens": neededBudget},
"max_tokens": maxTokens,
})
return out, true
}
// markThinkingNeeded remembers that p takes model only thinking, by the
// name magpie knows it and the vendor's (wire), which forwardOnce reads
// off the body: thinking enabled isn't made adaptive for it there, as
// "enabled" is the one type it said it takes.
func (s *Server) markThinkingNeeded(p provider.Provider, to provider.Protocol, model, wire string) {
s.markUnfit(p.ID, thinkingNeeded(model), to)
if wire != "" && wire != model {
s.markUnfit(p.ID, thinkingNeeded(wire), to)
}
}