Files
magpie/internal/gateway/thinking_back.go
T
yetone 6c08ee13b0 gateway: a model asking for earlier reasoning back goes on to the group's next member
EDwinpzy (Discord #feedback, magpie 0.1.2717): Command Code's
reflection/beam:free answered a second turn with 400 "Missing required
parameter: 'messages[N].reasoning_content'". The 400 wasn't retryable,
so a routing group of Beam then Glyph ended at Beam and never asked
Glyph. The client's request is whole; it is that model's demand for
history it doesn't have, and another member's model may not make it.

reasoningBackRefused (thinking_back.go) reads the demand: DeepSeek's
"must be passed back" (thinkingBackRefusal) and words that name
reasoning_content as missing or required. shapeRefused and retryable
take it, so the member fails as a shape refusal (failShape): no rest,
the next member is asked once. refuseThinkingBack's refill still runs
first, inside forwardOnce. A 400 for a missing 'messages' or 'model'
still stops at the first member. The plugin side (commandcode 0.1.22
fills Beam's earlier replies with reasoning_content) ships separately.

Tests: TestGroupFailsOverAMissingReasoning (chat and responses routes,
the reporter's two-turn shape, his words in the Provider API's error
JSON) and TestMissingReasoningIsNotABadRequest.

Run on mote: go test -count=1 -timeout 45m ./internal/gateway: every
test passes except TestCodexPoolSaysTheClientsVersion, red in a full
package run and green alone, already filed as #4807 (another test's
Codex identity reaches it); go test ./internal/provider ok;
GOOS=linux and GOOS=windows go vet ./internal/gateway ok; gofmt -l clean.
After a rebase on 253aee08b (conflict with #4732's zhipuImageRefusal in
shapeRefused, both kept): the same packages and vets again, provider ok,
gateway red only for #4807 and TestCompactionOverByteCapEveryTime, which
is #4862 (red in a full run, green alone). At the pushed head, rebased on
d76c4b23a: TestGroupFailsOver*, the two new tests, ThinkingBack, Zhipu,
Webp, #4807's and #4862's tests ok in gateway; Mover/Moved/CommandCode ok
in provider.

Broken by hand: with reasoningBackRefused returning false (the old
classification), both new tests are red (400 from Beam, Glyph never
asked; shapeRefused and retryable false for every demand).

Not tried: a real Beam request (the Command Code key here answers 401),
so the error JSON around the reporter's words is constructed, not saved.
2026-11-03 05:10:38 +08:00

85 lines
3.8 KiB
Go

package gateway
import (
"bytes"
"io"
"net/http"
"regexp"
"github.com/yetone/magpie/internal/provider"
)
// thinkingBackRefusal is DeepSeek's model asking for an earlier turn's
// thinking, wherever it is served: on its Messages API "The
// `content[].thinking` in the thinking mode must be passed back to the
// API", on its Chat API the same of `reasoning_content` (Kimi's says it
// without the backticks). Checked against api.deepseek.com on 2026-11-02:
// an assistant turn of the tool loop being continued with no thinking is
// refused, one with a space is taken, as are finished turns and plain
// replies without.
var thinkingBackRefusal = regexp.MustCompile("(?i)`?(?:content\\[\\]\\.thinking|reasoning_content)`? in the thinking mode must be passed back")
// reasoningBackWords are a model asking for an earlier assistant
// message's reasoning in other words than thinkingBackRefusal's:
// Reflection's Beam on Command Code, 400 "Missing required parameter:
// 'messages[1].reasoning_content'" for a second turn whose first reply
// has none (EDwinpzy on Discord, magpie 0.1.2717), and the like that name
// reasoning_content as missing or required. A request missing what every
// API requires ("Missing required parameter: 'messages'") isn't one.
var reasoningBackWords = regexp.MustCompile(`(?i)\breasoning_content\b[^"]{0,40}\b(?:is (?:missing|required)|must be (?:passed|provided|included))|\b(?:missing|required)\b[^"]{0,80}\breasoning_content\b`)
// reasoningBackRefused says a model turned the request away for an
// earlier turn's reasoning it didn't have: a demand of that model's on the
// history, which another member's model, that asks nothing of the kind,
// may well take as it is (shapeRefused).
func reasoningBackRefused(body []byte) bool {
return thinkingBackRefusal.Match(body) || reasoningBackWords.Match(body)
}
// thinkingBackNeeded is how unfit remembers a provider's model asking
// for its thinking back on every tool turn (thinkingBackRefusal).
func thinkingBackNeeded(model string) string { return "thinking back\x00" + model }
// withThinkingBack is body with each tool turn's thinking filled in
// (withThinkingOnToolTurns, withReasoningOnToolTurns), for a provider's
// model that asked for it before though neither its host nor its name
// says DeepSeek: a relay, or a model the user named otherwise. Those
// two are filled when the body is built (anthropicReplaysThinking,
// chatReplaysReasoning), so this is the learned rest of the class.
func (s *Server) withThinkingBack(p provider.Provider, to provider.Protocol, body []byte) []byte {
if to != provider.Anthropic && to != provider.Chat || s.fits(p.ID, thinkingBackNeeded(modelOf(body)), to) {
return body
}
return fillThinkingBack(to, body)
}
func fillThinkingBack(to provider.Protocol, body []byte) []byte {
if to == provider.Anthropic {
return withThinkingOnToolTurns(body)
}
return withReasoningOnToolTurns(body)
}
// refuseThinkingBack reads a 400 from a Messages or Chat upstream: when
// it is thinkingBackRefusal and filling body's tool turns changes it,
// the provider's model is remembered as asking for it, and true says
// to ask once more (forwardOnce fills them from then on). res's body
// is left readable either way.
func (s *Server) refuseThinkingBack(p provider.Provider, to provider.Protocol, body []byte, res *http.Response) bool {
if to != provider.Anthropic && to != provider.Chat || !badRequest(res.StatusCode) {
return false
}
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
res.Body.Close()
res.Body = io.NopCloser(bytes.NewReader(b))
model := modelOf(body)
if !thinkingBackRefusal.Match(wordsOf(b)) || !s.fits(p.ID, thinkingBackNeeded(model), to) {
return false
}
if bytes.Equal(fillThinkingBack(to, body), body) {
return false
}
s.markUnfit(p.ID, thinkingBackNeeded(model), to)
return true
}