Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: thinking pi sends back in reasoning goes to DeepSeek once, as reasoning_content
yetone1bb8143b374e
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex f07978386..5837fd6f6 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -187,9 +187,9 @@ recorded as a prediction failure (`noPrediction`).- A tool a Chat request's message carries joins the request's `tools` (`liftMessageTools` in [`message_tools.go`](../../internal/gateway/message_tools.go), #2322). Pi with `supportsMidConvoToolAdditions` gives a tool it adds mid-conversation as `{"role":"system","tools":[…]}` with no content, and keeps top-level `tools` as they were at the start; no Chat API reads tools there, and `parseChat` read only the top-level ones, so the tool could be called on no route. `handle` lifts them for every Chat body before `serveAgent`, so the Chat relay, the schema fixers, redaction (the tool is a tool definition, not message text), the prompt shown and routing all see them; `parseChat` lifts too, for a body that never came through `handle` (the Claude subscription bridge's direct callers). Each tool joins once by name and the first definition wins: a top-level tool keeps its own, then the earliest message's, as `liftAdditionalTools` and a `tool_addition` (`withoutDeferredTools`) do. A system or developer message that carried only tools is left out, so it is neither relayed as an empty system message nor joined into the system text as an empty part; one with content keeps its place without `tools`. Messages stay where they are: the in-place system message the issue also asks for is not this change. `TestPiAddedToolReachesEveryRoute` sends deephbz's Pi 1.2.0 bytes through the Chat relay, Messages, Responses, Gemini and Codex, and `TestPiAddedToolOfferedToClaudeCode` through the Claude subscription bridge.- A Chat request's tool in another API's shape is put in the Chat shape, `{"type":"function","function":{name, description, parameters, strict}}` (`shapeChatTools` in [`chat_tool_shape.go`](../../internal/gateway/chat_tool_shape.go), #2622, jinyue806): MCP's `{"name","description","inputSchema"}` (ZCode), Anthropic's `input_schema`, a bare `{"name","parameters"}` and Responses' flat `{"type":"function","name",…}`. No Chat API takes those (SenseNova: "field Tools[0].Type invalid, should be set" and "function cannot be empty"), and `parseChat` read no name from them, so a translated request offered a tool named "". It runs right after `liftMessageTools`, in `handle` for every Chat body and in `parseChat`, so it holds for every Chat upstream, not SenseNova's alone. A tool with its `function`, a hosted tool (any other `type`) and one with no name are left as they came, and a request with none to shape is relayed byte for byte. `TestChatToolInAnotherShapeIsShaped` sends the report's request with each of its four tools through the Chat relay, Messages and Responses; `TestShapeChatToolsLeavesChatShape`.- Every request to a key+URL provider goes with magpie's User-Agent (`magpie/<Version>`, `userAgentFor` in [`user_agent.go`](../../internal/gateway/user_agent.go)), except a provider set `passUserAgent` (#1851, `magpie provider set <id> user-agent=agent`, the editor's "Send the agent's own User-Agent"): it gets the User-Agent the inbound request carried, unchanged, for a coding plan that serves only the agents it names. One the agent didn't send, and magpie's own (`magpiesOwn`: another magpie's `magpie/…`, `magpie-router/1`, `magpie-vision/1`, `magpie-search/1`, and the image tool's `<client> magpie-image/1`, built from MCP's clientInfo, not a User-Agent), go as magpie; magpie never makes up another client's. `forwardOnce` sets it for chat, Anthropic, Responses, Gemini and count_tokens; embeddings, images and videos (`retrieveFrom`, `sendWith`, `remoteVideoContent`) set it with `stampUA` from the caller the request came with (`caller.ua`, `withCaller`), and before #1851 went with Go's default. Claude Code's and Codex's own headers are copied after, as before, and the user's `header.User-Agent` is put on when the request is signed, so it wins. `provider.PassesUserAgent` refuses it for a signed-in account, a plugin's, and a vendor whose own client magpie goes as (`OwnClient`: OpenCode, Cline, Kilo, another magpie); their signing sets their own. `TestUserAgentPassedOn`, `TestUserAgentAccountUntouched`, `TestUserAgentFor`, `TestPassesUserAgent`, `provider-user-agent.test.cjs`.-- A Chat upstream that wants a turn's reasoning back (`chatReplaysReasoning` in [`chat.go`](../../internal/gateway/chat.go): a host or a model, as magpie or the provider names it, with `deepseek` in it, such as OpenCode Go's `deepseek-v4.1-flash`, or Command Code's plugin) gets every assistant tool call with `reasoning_content`, relayed (`withReasoningOnToolTurns` in `passthrough`) or translated (`buildChat`). DeepSeek turns the whole request away ("The `reasoning_content` in the thinking mode must be passed back to the API") when a tool call made since the user's last message has none, which a client sends for a turn it saw no thinking on: another model's, a translated one (#1462). The turn's own thinking goes back as it came; one sent in `reasoning` or `reasoning_details` is copied in; otherwise it goes with a space (`noReasoning`: DeepSeek's Chat API takes an empty one, but a relay that leaves empty fields out would drop it). An assistant message right after a tool result, a reply with no call of its own, gets one too: DeepSeek refuses it the same way (checked 2026-10-12, #1822). A reply to the user, any other model and Mistral (#494) are sent as before.+- A Chat upstream that wants a turn's reasoning back (`chatReplaysReasoning` in [`chat.go`](../../internal/gateway/chat.go): a host or a model, as magpie or the provider names it, with `deepseek` in it, such as OpenCode Go's `deepseek-v4.1-flash`, or Command Code's plugin) gets every assistant tool call with `reasoning_content`, relayed (`withReasoningOnToolTurns` in `passthrough`) or translated (`buildChat`). DeepSeek turns the whole request away ("The `reasoning_content` in the thinking mode must be passed back to the API") when a tool call made since the user's last message has none, which a client sends for a turn it saw no thinking on: another model's, a translated one (#1462). The turn's own thinking goes back as it came; one sent in `reasoning` or `reasoning_details` is copied in, and `reasoning` is then left out (`asReasoningContent`): a self-hosted DeepSeek server reads the two as one field and refused pi's tool turn, which carried its thinking in `reasoning`, as "duplicate field reasoning" (ArvinCharl on X, v0.1.1560), while `reasoning_details`, a list of typed entries that can hold an encrypted one, stays; otherwise it goes with a space (`noReasoning`: DeepSeek's Chat API takes an empty one, but a relay that leaves empty fields out would drop it). An assistant message right after a tool result, a reply with no call of its own, gets one too: DeepSeek refuses it the same way (checked 2026-10-12, #1822). A reply to the user, any other model and Mistral (#494) are sent as before.- An assistant turn the user cut off before it said anything (only its thinking, an empty text, nothing; an isolated Codex reasoning item; a Gemini thought alone) stays in the agent's history and is sent on every later turn. DeepSeek and Kimi turn the whole request away over an assistant message with neither content nor tool calls, `reasoning_content` alone included ("Invalid assistant message: content or tool_calls must be set", #2599). Every Chat upstream gets such a turn left out, translated (`buildChat`, before `pairToolMessages`) or relayed (`silentAssistantsOut` in `passthrough`), by one rule (`withoutSilentAssistants` in [`chat.go`](../../internal/gateway/chat.go)): the two user messages it stood between join into one, and after a tool result the user's message follows the tool message. A prefill to go on from (`prefix`, `partial`, a Resume), a legacy `function_call`, a refusal and an audio reply stay, and a relayed body without such a turn goes byte for byte. `TestChatEmptyAssistantLeftOutFromAnthropic`, `…FromResponses`, `…FromGemini`, `TestChatRelayLeavesOutEmptyAssistant`, `TestChatRelaySilentAssistantsOnly`.-- Kimi's Chat API (`isKimi`: the Kimi platform's and a Kimi Code membership's hosts or presets) turns `reasoning_details` in a message away in any shape: an entry with a stream's `index` ("the reasoning_details at position 1 entry 0 must not contain streaming index") and any other entry ("… has an invalid type"), checked against api.kimi.com/coding on 2026-10-12. pi keeps another vendor's thinking there (MiMo's, through Command Code, with `index: 0`), so a conversation routed on to Kimi was refused on every turn (#1765). `passthrough` sends it to Kimi as `reasoning_content` (`reasoningDetailsAsContent`), which Kimi takes; a message with `reasoning_content` of its own keeps that. Another upstream that refuses a thinking field "at position N", as Kimi words it (a relay in front of it), is asked once more the same way and marked (`refusedInMessages`, `withoutMessageReasoning`).+- Kimi's Chat API (`isKimi`: the Kimi platform's and a Kimi Code membership's hosts or presets) turns `reasoning_details` in a message away in any shape: an entry with a stream's `index` ("the reasoning_details at position 1 entry 0 must not contain streaming index") and any other entry ("… has an invalid type"), checked against api.kimi.com/coding on 2026-10-12. pi keeps another vendor's thinking there (MiMo's, through Command Code, with `index: 0`), so a conversation routed on to Kimi was refused on every turn (#1765). `passthrough` sends it to Kimi as `reasoning_content` (`reasoningDetailsAsContent`, with the message's `reasoning` left out once its text is there), which Kimi takes; a message with `reasoning_content` of its own keeps that. Another upstream that refuses a thinking field "at position N", as Kimi words it (a relay in front of it), is asked once more the same way and marked (`refusedInMessages`, `withoutMessageReasoning`).- A Responses request's top-level `access_programs`, the Cyber program Codex asks the model under (`{"cyber":"standard"}` on a subagent it inherits, OpenAI's Daybreak programs, magpie-releases#18), goes only where Codex itself would send it: codex-rs sends it with a ChatGPT sign-in, or an API key on its provider named `openai`, and to no other provider (`cyber_access_program.rs`, `for_auth`, `for_provider`). In `forwardOnce`, relayed or built (from Chat too), it goes as it came to a ChatGPT (Codex) account, a host ending in `openai.com`, another magpie (`IsRemoteMagpie`, whose own upstream decides) and a relay set `codexAsSent` when Codex sent the request; every other Responses upstream is sent the request without it (`keepsAccessPrograms`, `withoutTopMember`, the rest byte for byte). OpenCode Go answered it, for DeepSeek and GLM, with 503 "Upstream request failed: Endpoint is unavailable." and 200 without it or with null; its Haiku took it (#3172, three reporters' real A/B runs). It is taken out before the user's extraBody is merged, so an `access_programs` written there still goes. Chat and Messages builds never carry it; a Chat request relayed as it came keeps whatever its client wrote. `TestAccessProgramsGoToOpenAIAlone`, `TestWithoutTopMember`, `TestCodexAccessProgramsReachBackend`.- Claude Code's deferred tools (ToolSearch, MCP tools that come and go) put blocks of their own in a message: a `tool_addition` (`tool` a `tool_reference` naming the tool, or a `tool_definition`), a `tool_removal`, and a `tool_reference`. Kimi's Anthropic endpoint refuses any of them with a whole-request 400 "Invalid request Error" that names nothing, so once a session had loaded a tool every turn failed (#2042); DeepSeek's takes a `tool_addition` naming a tool but refuses one defining it, a `tool_removal` and a message's `tool_reference` with 422 "unknown variant `tool_removal`" (both checked against the real endpoints on 2026-10-14). OpenCode Go also rejects DeepSeek inline definitions with a generic 422 "Upstream request failed: Endpoint is unavailable." (checked on 2026-10-15). Kimi and DeepSeek models on OpenCode are never sent these blocks (`forwardOnce`); a provider that refuses one in those words is asked again at once without them and not sent them again while magpie runs (`refuseShapes`, `shapeDeferred`; the retry loop reads a 422 as well as a 400). `withoutDeferredTools` drops the blocks and a message they leave empty; definitions present only in a `tool_addition.tool.definition` join the request's `tools` once by name (an existing top-level definition wins), and a `tool_reference` inside a `tool_result` (ToolSearch's answer, which both take) stays. Any other provider is sent them as Claude Code wrote them ([`anthropic_shapes.go`](../../internal/gateway/anthropic_shapes.go), `TestKimiIsSentNoDeferredToolBlocks`, `TestDeferredToolBlocksRefusedOnceLeftOut`, `TestOpenCodeDeepSeekInlineTools`, `TestDeferredInlineDefinitionPreserved`).- ToolSearch's answer is a `tool_result` whose content is `tool_reference` blocks alone. OpenDesign's Anthropic endpoint can't read one: 422 "Failed to deserialize the JSON body into the target type: messages[4]: missing field `content`" (#2124, the reporter's direct request without magpie fails the same way; the same result with a text block beside the references is taken). A provider that refuses in those words while the request holds such a result is asked again at once with a text block first in each, "Tool <name> is loaded and can be called now." (`toolLoadedText`, the words a translated route already gives a model in `stringOrText`), the references and the `tool_use`/`tool_result` pairing kept, and is sent it that way while magpie runs (`refuseShapes`, `shapeRefsOnly`, `withRefsSaid`). This is in `forward`, so `/v1/messages`, `count_tokens` and a group's member all get it; a request translated to another API already carries the references as that text. Any other provider is sent the bare references (`TestRefsOnlyToolResultRefusedGetsText`, `TestRefsSaidLeavesOtherResults`).diff --git a/internal/gateway/chat_tool_reasoning_test.go b/internal/gateway/chat_tool_reasoning_test.goindex 4eb00eea1..0f785aa72 100644--- a/internal/gateway/chat_tool_reasoning_test.go+++ b/internal/gateway/chat_tool_reasoning_test.go@@ -2,6 +2,7 @@ package gatewayimport ("encoding/json"+ "fmt""strings""testing"@@ -62,7 +63,7 @@ func chatProvider(t *testing.T, up string, p provider.Provider) {// A Chat request relayed as it came to a DeepSeek model, here behind// OpenCode Go whose host doesn't say DeepSeek, gives every tool call// without reasoning_content one, so DeepSeek takes it (#1462). The-// thinking a turn has keeps it, one sent in another field goes there too,+// thinking a turn has keeps it, one sent in reasoning goes there instead,// and every other message goes as it was sent.func TestChatToolTurnWithoutReasoningRelayed(t *testing.T) {f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: deepseekRefuseToolTurns}@@ -84,7 +85,7 @@ func TestChatToolTurnWithoutReasoningRelayed(t *testing.T) {sameJSON(t, ms[1], `{"role":"assistant","content":"Hello!"}`)sameJSON(t, ms[3], `{"role":"assistant","content":null,"tool_calls":[`+timeCall+`],"reasoning_content":" "}`)sameJSON(t, ms[5], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_2","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning_content":"Once more."}`)- sameJSON(t, ms[7], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_3","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning":"OpenRouter's field.","reasoning_content":"OpenRouter's field."}`)+ sameJSON(t, ms[7], `{"role":"assistant","content":null,"tool_calls":[{"id":"call_3","type":"function","function":{"name":"get_time","arguments":"{}"}}],"reasoning_content":"OpenRouter's field."}`)sameJSON(t, ms[4], `{"role":"tool","tool_call_id":"call_1","content":"12:00"}`)}@@ -153,3 +154,62 @@ func TestTranslatedToolTurnWithoutReasoning(t *testing.T) {t.Errorf("the reply was given reasoning: %s", f.got)}}++// selfHostedRefuseDuplicate answers as the reporter's self-hosted DeepSeek+// server does: it reads reasoning and reasoning_content as one field, so a+// message carrying both is turned away, and DeepSeek's own rule that a+// tool turn needs its thinking back holds too.+func selfHostedRefuseDuplicate(body []byte) (int, string) {+ var q struct {+ Messages []map[string]json.RawMessage `json:"messages"`+ }+ json.Unmarshal(body, &q)+ for i, m := range q.Messages {+ if m["reasoning"] != nil && m["reasoning_content"] != nil {+ return 400, fmt.Sprintf(`{"error":{"message":"Failed to deserialize the JSON body into the target type: messages[%d]: duplicate field `+"`reasoning`"+`","type":"invalid_request_error"}}`, i)+ }+ }+ return deepseekRefuseToolTurns(body)+}++// ArvinCharl on X (v0.1.1560): pi on a self-hosted DeepSeek through the+// Chat path keeps the thinking the server gave in reasoning and sends it+// back there. magpie gave the tool turn reasoning_content with the same+// text and left reasoning in, and the server refused "duplicate field+// reasoning". The thinking goes once, in reasoning_content; a message with+// no tool turn to mend keeps its reasoning, and a body with no tool calls+// goes byte for byte.+func TestSelfHostedDeepSeekGetsReasoningOnce(t *testing.T) {+ f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: selfHostedRefuseDuplicate}+ up := setup(t, provider.Chat, f)+ chatProvider(t, up.URL, provider.Provider{ID: "ds-local", Models: []string{"deepseek-v4"}})+ srv := New()+ code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"ds-local/deepseek-v4","stream":false,"tools":[{"type":"function","function":{"name":"read","parameters":{"type":"object","properties":{}}}}],"messages":[+ {"role":"user","content":"Read note.txt."},+ {"role":"assistant","content":"","reasoning":"I should read the file first.","tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{\"path\":\"note.txt\"}"}}]},+ {"role":"tool","tool_call_id":"call_1","content":"hello"},+ {"role":"assistant","content":"It says hello.","reasoning":"The file says hello.","reasoning_details":[{"type":"reasoning.encrypted","data":"gAAAA"}]},+ {"role":"user","content":"Thanks."}]}`)+ if code != 200 || f.calls != 1 {+ t.Fatalf("%d after %d calls, sent %s: %s", code, f.calls, f.got, body)+ }+ ms := sentMessages(t, f.got)+ sameJSON(t, ms[1], `{"role":"assistant","content":"","reasoning_content":"I should read the file first.","tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{\"path\":\"note.txt\"}"}}]}`)+ // a reply to a tool result is given it as well (#1822); its+ // reasoning_details, not another name for the text, stays+ sameJSON(t, ms[3], `{"role":"assistant","content":"It says hello.","reasoning_content":"The file says hello.","reasoning_details":[{"type":"reasoning.encrypted","data":"gAAAA"}]}`)++ // with no tool calls, the body goes as it was sent, reasoning and all+ f.refuse, f.calls = nil, 0+ plain := `{"model":"deepseek-v4","stream":false,"messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"Hello!","reasoning":"Greet back."},{"role":"user","content":"bye"}]}`+ if got := withReasoningOnToolTurns([]byte(plain)); string(got) != plain {+ t.Errorf("a body with no tool calls changed:\n%s", got)+ }+}++// Kimi, given reasoning_details' thinking as reasoning_content (#1765),+// is not also sent the message's reasoning with the same text.+func TestKimiReasoningDetailsWithReasoningGoOnce(t *testing.T) {+ m := `{"messages":[{"role":"assistant","content":"91 = 7 x 13.","reasoning":"7*13=91.","reasoning_details":[{"type":"reasoning.text","text":"7*13=91.","index":0}]}]}`+ sameJSON(t, sentMessages(t, reasoningDetailsAsContent([]byte(m)))[0], `{"role":"assistant","content":"91 = 7 x 13.","reasoning_content":"7*13=91."}`)+}diff --git a/internal/gateway/chattidy.go b/internal/gateway/chattidy.goindex 82ed7d0f5..f5bbcab94 100644--- a/internal/gateway/chattidy.go+++ b/internal/gateway/chattidy.go@@ -592,10 +592,11 @@ const noReasoning = " "// reasoning_content one: the thinking it sent back in another field// (reasoning, reasoning_details), else noReasoning. DeepSeek turns away a// reply after a tool result with none as it does a call (checked against-// api.deepseek.com on 2026-10-12, #1822). A message with reasoning_content-// of its own keeps it, any other reply is left as it is (DeepSeek doesn't-// ask for its thinking), and a body with no tool calls comes back byte for-// byte.+// api.deepseek.com on 2026-10-12, #1822). Thinking taken from reasoning+// goes in reasoning_content alone (asReasoningContent). A message with+// reasoning_content of its own keeps it, any other reply is left as it is+// (DeepSeek doesn't ask for its thinking), and a body with no tool calls+// comes back byte for byte.func withReasoningOnToolTurns(body []byte) []byte {afterTool := falsereturn editMessages(body, []string{"tool_calls"}, func(m map[string]json.RawMessage) bool {@@ -622,11 +623,24 @@ func withReasoningOnToolTurns(body []byte) []byte {if think == "" {think = noReasoning}- m["reasoning_content"], _ = marshalPlain(think)+ asReasoningContent(m, think)return true})}+// asReasoningContent puts think in a message's reasoning_content and+// leaves its reasoning out: a server that reads the two as one field (a+// self-hosted DeepSeek, as pi sends its thinking back in reasoning) turns+// a message with both away as "duplicate field reasoning", so the thinking+// goes once. reasoning_details stays: it is a list of typed entries, not+// another name for the text, and can hold what the text can't (an+// encrypted entry, a signature); an upstream that refuses it has it taken+// out (reasoningDetailsAsContent, withoutMessageReasoning).+func asReasoningContent(m map[string]json.RawMessage, think string) {+ m["reasoning_content"], _ = marshalPlain(think)+ delete(m, "reasoning")+}+// silentAssistantsOut is withoutSilentAssistants for a Chat request// relayed as the client sent it (#2599): an agent that speaks Chat keeps// the turn it cut off as {"role":"assistant","content":""} (or null, with@@ -747,7 +761,7 @@ func reasoningDetailsAsContent(body []byte) []byte {var own stringif decodeJSON(m["reasoning_content"], &own) != nil || own == "" {if think := reasoningText(m); think != "" {- m["reasoning_content"], _ = marshalPlain(think)+ asReasoningContent(m, think)}}delete(m, "reasoning_details")