gateway: a routing group's reply has its DSML close and finish mended as a member's own does

喜欢一个人 on Discord later said the model alone works and only their
routing group "DeepSeek-V4.1 flash" (several entries of the model, in
order, fallback last) breaks from Alma. TestDSMLThroughGroup sends the
reporter's bytes through such a group: a native call followed by
"\n</||DSML|| calls>" cut inside the full-width pipes, and the whole
text-written block, answered by the first member and by the second after
the first's 500, on Chat streamed and whole, Responses streamed and whole
and Anthropic streamed and whole. Each must give the exec_command call,
no DSML, no stray "\n" message, finish tool_calls / stop_reason tool_use,
nothing of the refused try, and the tries counted.

Alone vs group, read in the code: the tidy is the same. textCallTidy,
chatTidy and chatWhole are set up in passthrough, textCallSee in
translate and streamTranslated, per try inside attempt; none of them is
keyed on the model, provider or group (chatToolNames reads the request's
tools), and holdWriter drops a refused try's held bytes and switches on
FirstToken only before anything was let through (started()). So the
group path leaked exactly what the model path leaked, which the two
commits before this fix. What a group does change is what the upstream
is asked: a member the catalog marks as not reasoning (quietTo) is sent
no reasoning ask in a group only, and a FirstToken group moves on from a
slow member. Either can change how the model writes its reply (thinking
off), which is a likely reason the reporter saw the residue only through
the group; neither bypasses the tidy.

Ran: the test at this head, ok. At origin/main 9b0f48612 (this test and
textcalls_close_test.go copied in), red on its first case: the group's
Chat stream carried "\n</|" + "|DSML|| calls>" and finish "stop".
gofmt -l clean.
Run at the tree pushed (go test -timeout 45m ./... in a sandbox HOME at
308f693df, tree cfd91bbea, on origin/main c358e7660; only messages
changed since): 68 packages ok, exit 0. The one red at an earlier run
(770c5fc48), TestGeminiCompatRelayEndedShort, is answered in the finish
commit. gofmt -l clean; go vet ./internal/gateway for darwin, linux
-tags nogui and windows clean.

Not tried: the reporter's real group and relay, and Alma itself; the
catalog's reasoning flags for their relay's entry are not known here.

Reported by 喜欢一个人 on Discord.
This commit is contained in:
yetone
2026-10-22 04:54:05 +08:00
parent 9a0df964e4
commit b0d46ca7fe
+64
View File
@@ -0,0 +1,64 @@
package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// 喜欢一个人 on Discord called the model through a routing group of several
// of its entries, in order, from Alma, and only the group went wrong. A
// group's reply is mended as a model's own is: by the member that answers,
// the first or one after a refusal, on every route, with nothing the
// refused try sent before it.
func TestDSMLThroughGroup(t *testing.T) {
native := sse(nativeExec, chunkOf(reportedClose[:6]), chunkOf(reportedClose[6:]), stopChunk, `data: [DONE]`)
written := sse(chunkOf("Let me look.\n\n"+reportedBlock[:40]), chunkOf(reportedBlock[40:]), stopChunk, `data: [DONE]`)
refused := reply{500, "application/json", `{"error":{"message":"upstream overloaded"}}`}
for _, up := range []struct{ name, body string }{{"native", native}, {"written", written}} {
for _, first := range []bool{true, false} {
for _, route := range []struct{ path, body string }{
{"/v1/chat/completions", `{"model":"group/flash","stream":true,"messages":[{"role":"user","content":"go"}],` + execChatTool + `}`},
{"/v1/chat/completions", `{"model":"group/flash","messages":[{"role":"user","content":"go"}],` + execChatTool + `}`},
{"/v1/responses", `{"model":"group/flash","stream":true,"input":"go",` + execResponsesTool + `}`},
{"/v1/responses", `{"model":"group/flash","stream":false,"input":"go",` + execResponsesTool + `}`},
{"/v1/messages", `{"model":"group/flash","max_tokens":100,"stream":false,"messages":[{"role":"user","content":"go"}],"tools":[{"name":"exec_command","input_schema":{"type":"object"}}]}`},
{"/v1/messages", `{"model":"group/flash","max_tokens":100,"stream":true,"messages":[{"role":"user","content":"go"}],"tools":[{"name":"exec_command","input_schema":{"type":"object"}}]}`},
} {
fresh(t)
ok := reply{200, "text/event-stream", up.body}
a, b := &scripted{replies: []reply{ok}}, &scripted{replies: []reply{ok}}
if !first {
a.replies = []reply{refused}
}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
if err := provider.SaveGroup(provider.Group{ID: "flash", Name: "DeepSeek-V4.1 flash", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", route.path, strings.NewReader(route.body)))
body := rec.Body.String()
served := "a"
if !first {
served = "b"
}
bad := rec.Code != 200 || strings.Contains(body, "DSML") || !strings.Contains(body, "exec_command") || strings.Contains(body, "overloaded")
switch route.path {
case "/v1/chat/completions":
bad = bad || !strings.Contains(body, `"finish_reason":"tool_calls"`) || strings.Contains(body, `"finish_reason":"stop"`)
case "/v1/messages":
bad = bad || !strings.Contains(body, `"stop_reason":"tool_use"`)
}
if up.name == "native" {
bad = bad || strings.Contains(body, `"content":"\n"`) || strings.Contains(body, `"text":"\n"`)
}
if bad || (first && b.n != 0) || (!first && (a.n != 1 || b.n != 1)) {
t.Fatalf("%s, served by %s, %s %s: %d (a %d, b %d tries)\n%s", up.name, served, route.path, route.body, rec.Code, a.n, b.n, body)
}
}
}
}
}