gateway: Kimi Code's refusal of thinking turned off is asked again with it left to the model (#3739)

thinkingOffUnlessAsked turns thinking off on a Messages request for a
non-Claude model that doesn't mention it. Kimi Code (China)'s K3.1
(k3.1-pro, k3.1-pro-highspeed) refuses that with 400, "K3.1 Pro does not
support disabled thinking mode. Please enable thinking and try again, or
choose another model.", in a wording neither alwaysThinks,
mandatoryReasoning nor thinkingOffRefused matched, so the 400 went back
to the client on every request that carried no thinking — ZCode's, which
asks for a level as output_config.effort and sends no thinking — and the
session could not start. withoutThinkingOff would have handled it; only
the match missed, as in #1141.

alwaysThinks now matches Kimi's literal sentence. Both retry sites use
it: the relayed Messages path (withoutThinkingOff, gateway.go) and the
translated Chat/Responses path (thinkingBackOn).

Tests: TestAnthropicPassthroughKimiThinkingOffRefused (the reporter's
error body: omitted thinking succeeds after one retry without thinking;
adaptive goes once, as sent; Kimi's own max_tokens 400 and a near wording
"tool use is not supported by this model" are not retried), and
TestTranslatedThinkingOffRefused now runs Kimi's wording. With the regex
change reverted both fail (400 handed back).

Review and run record (landed by the maintainer's agent; gua2100 brought
both fixes from Discord, rebased onto main):
- External code, read line by line: no network, dependency, workflow or
  credential change; Codex base_instructions/identity untouched.
- Base origin/main d99e213f5. Full checks on mote (macOS, Go 1.26.8,
  sandbox HOME) at 31f025634, whose tree is the one pushed (only messages reworded since): gofmt, go vet darwin, linux,
  windows, builds for all three: rc=0. go test -tags nogui -count=1
  -timeout 45m ./...: 79 packages ok, internal/gateway ok (855s); two reds
  in untouched packages, both filed before: internal/plugin
  TestGatewayHookMixedPiPackage (#2865, "starting plugins: context deadline
  exceeded") and internal/update TestRelaunchOpenTriedAgain (#3881). Run
  alone at the same tree both pass (plugin 97s; update -count=3).
- Broken by hand: the #3739 regex reverted, so both Kimi tests fail (400
  handed back). The #2532 product files reverted (six files), so all three
  tests fail. The mergeTurns role guard removed fails (5 input messages,
  want 7).
- Real binary on mote, sandbox HOME, a fake upstream that records requests:
  at d99e213f5, wrke1990's literal #2532 body sends Plan in instructions
  and Default as a user <system-reminder>; at HEAD both developer items
  keep their role and place. #3739's curl body (and zfsdream96's ZCode
  shape, output_config effort max, no thinking) is a 400 at d99e213f5; at
  HEAD it is a 200 after one retry without thinking, and the next
  request goes without disabled.
- Rebased onto fa427216a (responses.go stream deltas, landed meanwhile;
  no conflict). At the rebased pushed tree: gofmt, go vet and
  go test -count=1 ./internal/gateway ok (580s) on mote. The full suite
  was not rerun after that rebase.
- Not covered: the search reroute's re-parse when a fallback changes
  whether the target is Responses (breaking it leaves the tests green;
  no route found that reaches it). Not tried: the real Kimi Code
  endpoint and its kimi-code-cn preset (a custom Anthropic provider with
  Kimi's literal 400 was used), a real Codex session against a paid
  upstream, Windows/Linux runtime. #3739's second symptom (k3 losing
  thinking when output_config.effort is asked) is not addressed.

Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
ggbdpq
2026-10-30 21:38:00 +08:00
committed by yetone
parent fa427216a4
commit 72e8d9fa6c
3 changed files with 41 additions and 2 deletions
+3 -2
View File
@@ -430,8 +430,9 @@ var anthropicModel = regexp.MustCompile(`(?i)(?:^|[/.:-])claude-`)
// "…requires adaptive thinking; thinking.type="disabled" (including
// reasoning.effort=none) is not allowed (2013)", and Volcengine Ark's
// glm-5.3 "thinking.type `disabled` is not supported by this model"
// (#1141).
var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking (?:can ?not|can't) be (?:disabled|turned off)|enable_thinking[^"]{0,60}restricted to true|始终(?:支持|开启|启用)?思考|思考[^"]{0,20}(?:不可|无法|不能)关闭|requires adaptive thinking\W+thinking\.type\W+disabled\W+\(including reasoning\.effort=none\) is not allowed|thinking\.type \x60disabled\x60 is not supported by this model`)
// (#1141). Kimi Code (China)'s K3.1 answers "…does not support disabled
// thinking mode" (#3739).
var alwaysThinks = regexp.MustCompile(`(?i)always engages in thinking|thinking (?:can ?not|can't) be (?:disabled|turned off)|enable_thinking[^"]{0,60}restricted to true|始终(?:支持|开启|启用)?思考|思考[^"]{0,20}(?:不可|无法|不能)关闭|requires adaptive thinking\W+thinking\.type\W+disabled\W+\(including reasoning\.effort=none\) is not allowed|thinking\.type \x60disabled\x60 is not supported by this model|does not support disabled thinking mode`)
// refusesThinkingOff is an Anthropic endpoint turning away a request
// with thinking turned off (thinking.type disabled): a model that always
+36
View File
@@ -770,6 +770,42 @@ func TestAnthropicPassthroughMiniMaxArkThinkingOffRefused(t *testing.T) {
}
}
// #3739: Kimi Code (China)'s K3.1 models refuse thinking turned off in
// their own words, the reporter's literal message. The request magpie
// turned it off for (ZCode sends none; it asks for a level as
// output_config.effort) is asked again with thinking left to the model,
// once; thinking the caller asked for goes as it came, and another 400 on
// the same request, or a near wording, is the caller's to see.
func TestAnthropicPassthroughKimiThinkingOffRefused(t *testing.T) {
const refusal = `{"type":"error","error":{"type":"invalid_request_error","message":"Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model."}}`
f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`}
f.refuse = func(b []byte) (int, string) {
if bytes.Contains(b, []byte(`"disabled"`)) {
return 400, refusal
}
return 0, ""
}
setup(t, provider.Anthropic, f)
code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":[{"type":"text","text":"Hi","cache_control":{"type":"ephemeral"}}]}]}`)
if code != 200 || f.calls != 2 || bytes.Contains(f.got, []byte("thinking")) || !bytes.Contains(f.got, []byte(`"Hi"`)) {
t.Fatalf("omitted: %d %s after %d calls, last sent %s", code, body, f.calls, f.got)
}
f.calls = 0
if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"."}]}`); code != 200 || f.calls != 1 || !bytes.Contains(f.got, []byte(`"thinking":{"type":"adaptive"}`)) {
t.Errorf("adaptive: %d %s after %d calls, sent %s", code, body, f.calls, f.got)
}
for _, other := range []string{
`{"type":"error","error":{"type":"invalid_request_error","message":"Kimi Code (China): max_tokens is too large"}}`,
`{"type":"error","error":{"type":"invalid_request_error","message":"Kimi Code (China): tool use is not supported by this model"}}`,
} {
f.calls = 0
f.refuse = func([]byte) (int, string) { return 400, other }
if code, _ := post(t, "/v1/messages", `{"model":"m1","max_tokens":1,"messages":[{"role":"user","content":"."}]}`); code != 400 || f.calls != 1 {
t.Errorf("%s: %d after %d calls", other, code, f.calls)
}
}
}
// The same refusal, escaped as \uXXXX the way a relay whose encoder
// escapes what isn't ASCII sends it (#1669): asked again all the same.
func TestAnthropicPassthroughAlwaysThinksInEscapedChinese(t *testing.T) {
+2
View File
@@ -107,6 +107,8 @@ func TestTranslatedThinkingOffRefused(t *testing.T) {
// MiniMax's and Volcengine Ark's wordings (#1141)
`invalid params, model \"MiniMax-M3.1-Flash-Preview\" requires adaptive thinking; thinking.type=\"disabled\" (including reasoning.effort=none) is not allowed (2013)`,
"thinking.type `disabled` is not supported by this model Request id: 0217",
// Kimi Code's K3.1 (ZCode sends no thinking at all, #3739)
`Kimi Code (China): K3.1 Pro does not support disabled thinking mode. Please enable thinking and try again, or choose another model.`,
// Sub2API's gpt-6.1-sol reads disabled as reasoning effort none (#1465)
`gpt-6.1-sol does not support reasoning effort \"none\"; use low, medium, high, xhigh or max`,
// #699's Chinese, escaped as \uXXXX the way a relay's