Files
magpie/internal/gateway/thinking_back_test.go
T
yetone 4bd0d6f389 gateway: a provider whose model asks for a tool turn's thinking back, under a name that doesn't say DeepSeek, is asked again with it and sent it from then on (qisishiqi)
qisishiqi (Discord #help): a routing group fell from a rate-limited Codex
subscription to "My Claude", an Anthropic-format endpoint, and the request
failed 400 "My Claude: The `content[].thinking` in the thinking mode must
be passed back to the API."

That is DeepSeek's model refusing a tool loop continued without the
thinking of its tool turn. magpie fills those turns (a space, or the
turn's readable reasoning) only when the host or the model names
DeepSeek (anthropicReplaysThinking, chatReplaysReasoning, #1462/#1611).
A relay serving DeepSeek under another name, or a model the user named
otherwise, got nothing, and Codex's sealed reasoning has no text to give.

Now forward reads a 400 on Messages or Chat whose words are DeepSeek's
(thinkingBackRefusal: `content[].thinking` / `reasoning_content` ... in
the thinking mode must be passed back), and when filling the tool turns
(withThinkingOnToolTurns / withReasoningOnToolTurns) changes the body,
remembers the provider's model (unfit, thinkingBackNeeded) and asks once
more; forwardOnce fills them for it from then on, every route alike. A
provider that never asked is sent the conversation as before. Scoped to
the vendor's refusal, not to every Anthropic endpoint.

Checked against api.deepseek.com with the owner's key, 2026-11-02
(deepseek-flash, Messages and Chat): a tool turn of the loop being
continued with no thinking is refused with exactly these words (the
test fixtures are those bytes); a space thinking block or
reasoning_content " " is taken; readable unsigned thinking is taken;
finished tool turns and plain replies without thinking are taken, with
or without tools; thinking disabled takes it all. So DeepSeek's docs'
"even for turns without a tool call" is not enforced today, and the
existing tool-turn rule stands.

Sibling: the Chat side had the same name-only gate, and gets the same
learned fill (TestReasoningBackLearnedBehindAnotherName). Responses
(reasoning_text) is filled by model name on any host already.

Run at this head on mote:
- go test ./internal/gateway -count=1 -timeout 45m: every test ok but
  TestCodexPoolSaysTheClientsVersion, #4807 (red in a full run, passes
  alone; passed 5 of 5 alone here). Not touched by this change.
- GOOS=linux and GOOS=windows go vet ./internal/gateway: ok. gofmt -l: clean.
- New: TestThinkingBackRefusalRead (the live bytes and qisishiqi's
  literal error read; Responses' reasoning_text and a bound-signature
  refusal not), TestThinkingBackLearnedBehindAnotherName (Codex request
  mid loop, group: 429 on the Responses member, then "My Claude" with
  claude-sonnet-4-6 and relay-model: 200 on the second ask with a space
  thinking block first, next turn 200 on the first ask, another provider
  sent no thinking), TestReasoningBackLearnedBehindAnotherName.

Broken by hand: with the retry in forward off, all three cases go red
with qisishiqi's error ("400 after 1 calls ... My Claude: The
`content[].thinking` ..."); with the learned fill in forwardOnce off,
the second turn is refused again ("400 after 2 calls").

Not tried: qisishiqi's own provider (its host and model are unknown); a
real relay in front of DeepSeek; a plugin provider's fetch path.
2026-11-02 17:03:34 +08:00

148 lines
7.2 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// What api.deepseek.com answered on 2026-11-02, byte for byte, to a tool
// loop continued with a tool turn that has no thinking: on its Messages
// API and on its Chat API.
const (
deepseekThinkingBackLive = `{"error":{"message":"The ` + "`content[].thinking`" + ` in the thinking mode must be passed back to the API. (request_id: cae816b6-d668-4a55-935d-99d1b8ad3686)","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}`
deepseekReasoningBackLive = `{"error":{"message":"The ` + "`reasoning_content`" + ` in the thinking mode must be passed back to the API. (request_id: 6360375a-b044-4a13-b51e-548de94677ea)","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}`
// qisishiqi's error on Discord (#help, 2026-11-02), as magpie passed it on
qisishiqiError = `{"code":null,"message":"My Claude: The ` + "`content[].thinking`" + ` in the thinking mode must be passed back to the API.","param":null,"type":"invalid_request_error"}`
)
// thinkingBackStream is the Messages stream the fakes answer with.
var thinkingBackStream = sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","model":"m1","usage":{"input_tokens":7}}}`,
`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Done."}}`,
`data: {"type":"content_block_stop","index":0}`,
`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}`,
`data: {"type":"message_stop"}`)
// codexMidLoop is Codex continuing a tool loop its own model began: the
// reasoning it has for the call is OpenAI's, sealed, with no text.
const codexMidLoop = `{"model":"%s","stream":false,"reasoning":{"effort":"high"},
"tools":[{"type":"function","name":"shell","parameters":{"type":"object","properties":{"command":{"type":"string"}}}}],
"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the login bug"}]},
{"type":"reasoning","id":"rs_1","summary":[],"encrypted_content":"gAAAA-codex-sealed"},
{"type":"function_call","call_id":"call_1","name":"shell","arguments":"{\"command\":\"ls\"}"},
{"type":"function_call_output","call_id":"call_1","output":"main.go"}]}`
func TestThinkingBackRefusalRead(t *testing.T) {
for _, s := range []string{deepseekThinkingBackLive, deepseekReasoningBackLive, qisishiqiError,
`{"error":{"message":"The reasoning_content in the thinking mode must be passed back to the API."}}`} {
if !thinkingBackRefusal.Match(wordsOf([]byte(s))) {
t.Errorf("not read as asking for thinking back: %s", s)
}
}
for _, s := range []string{
"The `reasoning_text` in the thinking mode must be passed back", // DeepSeek's Responses, its own path
"messages.1.content.0: Invalid signature in thinking block",
"prompt is too long",
} {
if thinkingBackRefusal.MatchString(s) {
t.Errorf("%q read as asking for thinking back", s)
}
}
}
// qisishiqi (Discord): a routing group fell from a rate-limited Codex
// subscription to "My Claude", an Anthropic endpoint whose host and
// model don't say DeepSeek, mid tool loop. DeepSeek's model behind it
// asked for the call's thinking, which Codex's sealed reasoning can't
// give. magpie asks once more with a space for it, and from then on
// sends it at once; a provider that never asked is sent none.
func TestThinkingBackLearnedBehindAnotherName(t *testing.T) {
for _, model := range []string{"claude-sonnet-4-6", "relay-model"} {
t.Run(model, func(t *testing.T) {
plan := &fake{t: t, ctype: "application/json", code: 429, reply: `{"error":{"message":"rate limited"}}`}
up := setup(t, provider.Responses, plan)
mine := &fake{t: t, reply: thinkingBackStream, refuse: func(b []byte) (int, string) {
if code, _ := deepseekRefuseAnthropic(b); code == 400 {
return 400, deepseekThinkingBackLive
}
return 0, ""
}}
mineSrv := httptest.NewServer(mine)
t.Cleanup(mineSrv.Close)
plain := &fake{t: t, reply: thinkingBackStream}
plainSrv := httptest.NewServer(plain)
t.Cleanup(plainSrv.Close)
for _, p := range []provider.Provider{
{ID: "plan", Name: "Plan", Key: "k", Models: []string{"gpt-5.5"}, Responses: up.URL + "/v1"},
{ID: "myclaude", Name: "My Claude", Key: "k", Models: []string{model}, Anthropic: mineSrv.URL},
{ID: "plain", Name: "Plain", Key: "k", Models: []string{model}, Anthropic: plainSrv.URL},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
if err := provider.SaveGroup(provider.Group{ID: "cc", Name: "cc", Members: []string{"plan/gpt-5.5", "myclaude/" + model}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
srv := New()
req := strings.Replace(codexMidLoop, "%s", "group/cc", 1)
code, body := postTo(t, srv, "/v1/responses", req)
if code != 200 || mine.calls != 2 {
t.Fatalf("%d after %d calls to My Claude, want 200 on the second: %s", code, mine.calls, body)
}
turn := gjson.GetBytes(mine.got, `messages.#(role=="assistant")`)
if th := turn.Get(`content.#(type=="thinking")`); th.Get("thinking").String() != noReasoning || turn.Get("content.0.type").String() != "thinking" {
t.Errorf("the tool turn went as %s, want a thinking block first", turn.Raw)
}
// learned: the next request is taken the first time
mine.calls = 0
if code, body = postTo(t, srv, "/v1/responses", req); code != 200 || mine.calls != 1 {
t.Fatalf("%d after %d calls, want 200 on the first: %s", code, mine.calls, body)
}
// a provider that never asked gets the turn as before
if code, body = postTo(t, srv, "/v1/responses", strings.Replace(codexMidLoop, "%s", "plain/"+model, 1)); code != 200 {
t.Fatalf("plain: %d %s", code, body)
}
if strings.Contains(string(plain.got), `"thinking":" "`) {
t.Errorf("a provider that never asked was given thinking: %s", plain.got)
}
})
}
}
// The sibling on Chat: DeepSeek's model behind a Chat provider whose host
// and model don't say so, given Codex's loop, asks for reasoning_content
// back; magpie asks once more with it, and from then on.
func TestReasoningBackLearnedBehindAnotherName(t *testing.T) {
f := &fake{t: t, reply: chatStreamOK, refuse: func(b []byte) (int, string) {
if code, _ := deepseekRefuseToolTurns(b); code == 400 {
return 400, deepseekReasoningBackLive
}
return 0, ""
}}
up := setup(t, provider.Chat, f)
chatProvider(t, up.URL, provider.Provider{ID: "relay", Models: []string{"relay-model"}})
srv := New()
req := strings.Replace(codexMidLoop, "%s", "relay/relay-model", 1)
code, body := postTo(t, srv, "/v1/responses", req)
if code != 200 || f.calls != 2 {
t.Fatalf("%d after %d calls, want 200 on the second: %s", code, f.calls, body)
}
if rc := gjson.GetBytes(f.got, `messages.#(role=="assistant").reasoning_content`); !rc.Exists() || rc.String() != noReasoning {
t.Errorf("the tool turn's reasoning_content = %s, want a space: %s", rc.Raw, f.got)
}
f.calls = 0
if code, body = postTo(t, srv, "/v1/responses", req); code != 200 || f.calls != 1 {
t.Fatalf("%d after %d calls, want 200 on the first: %s", code, f.calls, body)
}
}