Files
magpie/internal/gateway/reasoning_back_fallback_test.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

101 lines
4.8 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// beamReasoning400 is Command Code's reflection/beam:free turning a second
// turn away (EDwinpzy on Discord, magpie 0.1.2717): the reporter's words,
// "Missing required parameter: 'messages[N].reasoning_content'", in the
// OpenAI error shape the Provider API answers in.
const beamReasoning400 = `{"error":{"message":"Missing required parameter: 'messages[1].reasoning_content'.","type":"invalid_request_error","param":"messages[1].reasoning_content","code":"missing_required_parameter"}}`
// A model that wants an earlier reply's reasoning back the history doesn't
// have is the model's demand, not a bad request: a group of Beam then
// Glyph goes on to Glyph, on the Chat route and on Responses alike, and
// Beam doesn't rest for it. Before, the 400 wasn't retryable and the route
// ended at Beam.
func TestGroupFailsOverAMissingReasoning(t *testing.T) {
second := []struct {
route string
post func(*Server) (int, string)
}{
{"chat", func(s *Server) (int, string) {
return postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"Hello! How can I help?"},{"role":"user","content":"what model are you?"}]}`)
}},
{"responses", func(s *Server) (int, string) {
return postResponses(t, s, `{"model":"group/g","stream":true,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Hello! How can I help?"}]},{"type":"message","role":"user","content":[{"type":"input_text","text":"what model are you?"}]}]}`)
}},
}
for _, x := range second {
t.Run(x.route, func(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{400, "", beamReasoning400}}}
b := &scripted{replies: []reply{{200, "text/event-stream", chatStreamOK}}}
scriptedOn(t, "beam", provider.Chat, a)
scriptedOn(t, "glyph", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"beam/m", "glyph/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := x.post(s)
if code != 200 || !strings.Contains(body, "from b") || a.n != 1 || b.n != 1 {
t.Fatalf("%d %s (beam %d, glyph %d)", code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Status != 400 || r.Tries[0].Fail != failShape || r.Tries[0].Rest != nil {
t.Fatalf("tries %+v", r.Tries)
}
})
}
}
// A request that is itself bad still stops at the first member, and the
// words are read as a demand for reasoning only where they name it.
func TestMissingReasoningIsNotABadRequest(t *testing.T) {
fresh(t)
bad := `{"error":{"message":"Missing required parameter: 'messages'.","type":"invalid_request_error","param":"messages","code":"missing_required_parameter"}}`
a := &scripted{replies: []reply{{400, "", bad}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
if code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`); code != 400 || a.n != 1 || b.n != 0 {
t.Fatalf("%d %s (a %d, b %d)", code, body, a.n, b.n)
}
for _, x := range []struct {
status int
body string
want bool
}{
{400, beamReasoning400, true},
{400, `Command Code: Missing required parameter: 'messages[3].reasoning_content'.`, true},
// DeepSeek's and Kimi's words for their thinking on a tool turn
{400, "{\"error\":{\"message\":\"The `reasoning_content` in the thinking mode must be passed back to the API.\",\"type\":\"invalid_request_error\"}}", true},
{400, "{\"error\":{\"message\":\"Missing `reasoning_content` field in the assistant message at message index 1.\",\"type\":\"invalid_request_error\"}}", true},
{400, `{"error":{"message":"thinking is enabled but reasoning_content is missing in assistant tool call message at index 2","type":"invalid_request_error"}}`, true},
{422, beamReasoning400, true},
{200, beamReasoning400, false},
{500, beamReasoning400, false},
// the request's own fault
{400, bad, false},
{400, `{"error":{"message":"Missing required parameter: 'model'.","type":"invalid_request_error"}}`, false},
{400, `{"error":{"message":"messages[1].content: field required"}}`, false},
} {
if got := shapeRefused(x.status, []byte(x.body)); got != x.want {
t.Errorf("shapeRefused(%d, %s) = %v", x.status, x.body, got)
}
// (a bare 'model' was retryable before, read as a model unserved)
if got := retryable(x.status, []byte(x.body)); x.want && !got {
t.Errorf("retryable(%d, %s) = %v", x.status, x.body, got)
}
}
}