Files
magpie/internal/gateway/refusal_handover_test.go
T
yetone 645eb92f3f gateway: a refusal after shown thinking goes to the next fallback
yhwangl on X: a request whose content trips a vendor's safety filter
after the model streamed its thinking, before any answer, stopped there;
the group's next member was never asked, as it is when credit runs out.

When a model's reasoning is shown, a try passes on its first thinking
event, so a refusal after it was the agent's. Now, for a streamed
Anthropic, Chat or Responses request with another member still to ask
(handsOver), the try's events go out one by one (passOver) and a refusal
or refusal error before any text or tool call hands the request on
(handOver): Anthropic's open blocks are closed and the next member's
stream is spliced into the same reply (spliceAfter): its message_start
or response.created/in_progress/queued are dropped, block indexes or
output_index go after those sent, sequence_number goes on. Chat chunks
go as they come. Once text or a call has gone, a refusal stays the
agent's as before. With no other member nothing changes. A refusal is
about the request, so nobody is rested for it.

Zhipu's finish_reason "sensitive" and error 1301 (敏感), DashScope's
data_inspection_failed and Volcengine Ark's *SensitiveContentDetected
are now read as refusals too.

Not covered (#4604): Gemini clients; a Responses reasoning item handed
over gets no output_item.done and the next completed lists only its own
output; A's thinking signature stays in the reply; non-streamed requests.

Run (mote, worktree at base 3baf3b6df):
- go test -count=1 -timeout 45m ./internal/gateway/...: ok (597s)
- targeted -run 'TestRefusal|TestSensitive|TestThinking|TestDeepSeek|
  TestStreamEvent|TestHighRisk|Refus': ok
- gofmt -l internal/: clean; go vet ./internal/gateway; GOOS=linux go vet
  -tags nogui ./...; GOOS=windows go vet ./...: ok
Rebased on 8a410f35a (5 commits, Claude TUI files in internal/gateway):
go vet ./internal/gateway and the targeted run plus ClaudeTUI|
ClaudeSubscription tests ok at the pushed head.
Broken by hand: handsOver forced to false (the old behaviour):
TestRefusalAfterShownThinkingGoesToTheNext (every upstream x client
subtest: b never asked), TestRefusalHandedOverTwice and
TestDeepSeekRefusalHoldsATrustedMemberAgain red; reverted.
Not tried: a real GLM, DashScope or Ark account tripping its filter, and
a real Claude Code or Codex client reading the spliced stream; the tests
use fake upstreams with GLM's, DeepSeek's and Anthropic's event shapes.
2026-11-01 00:40:07 +08:00

277 lines
11 KiB
Go

package gateway
import (
"bufio"
"encoding/json"
"strings"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// GLM on a Chat API reasoning, its filter then ending the reply with
// nothing said: Zhipu's finish_reason "sensitive" (yhwangl on X: the
// thinking was shown, the answer never came, and the next model wasn't
// asked).
var glmThoughtThenSensitive = sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"first thought"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{"reasoning_content":" second thought"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"sensitive"}],"usage":{"prompt_tokens":3,"completion_tokens":5,"total_tokens":8}}`,
`data: [DONE]`)
// The same, ended by OpenAI's content_filter.
var chatThoughtThenFiltered = strings.Replace(glmThoughtThenSensitive, `"sensitive"`, `"content_filter"`, 1)
// The same, ended by Zhipu's 1301 error mid-stream.
var glmThoughtThen1301 = sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"first thought"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{"reasoning_content":" second thought"}}]}`,
`data: {"error":{"code":"1301","message":"系统检测到输入或生成内容可能包含不安全或敏感内容,请您避免输入易产生敏感内容的提示语,感谢您的配合。"}}`)
// An Anthropic API's reasoning, its thinking block still open when the
// filter's stop_reason "refusal" ends it.
var glmAnthropicThoughtThenRefused = sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"glm-5.1","content":[],"usage":{"input_tokens":3}}}`,
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"first thought"}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":" second thought"}}`,
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"refusal","stop_sequence":null},"usage":{"output_tokens":5}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
const chatStreamAsk = `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`
// dataOf reads an SSE body's data, one per event, [DONE] as it is.
func dataOf(t *testing.T, body string) []string {
t.Helper()
var out []string
sc := bufio.NewScanner(strings.NewReader(body))
sc.Buffer(nil, 1<<20)
for sc.Scan() {
if d, ok := strings.CutPrefix(sc.Text(), "data:"); ok {
out = append(out, strings.TrimSpace(d))
}
}
return out
}
// A group's first member that reasons as it comes (a vendor whose filter
// isn't known to refuse after reasoning, GLM's way) and is refused by its
// filter before a word of its answer is said hands the request to the
// next member, whose answer follows the reasoning in the same stream, as
// one reply: Claude Code's, Codex's and a Chat client's. Nobody rests for
// it. Before, the agent had the reasoning, then the refusal, and the next
// member was never asked (yhwangl on X).
func TestRefusalAfterShownThinkingGoesToTheNext(t *testing.T) {
for _, up := range []struct {
name string
proto provider.Protocol
body string
}{
{"chat sensitive", provider.Chat, glmThoughtThenSensitive},
{"chat content_filter", provider.Chat, chatThoughtThenFiltered},
{"chat 1301", provider.Chat, glmThoughtThen1301},
{"anthropic refusal", provider.Anthropic, glmAnthropicThoughtThenRefused},
} {
for _, client := range []struct {
name, path, ask string
check func(t *testing.T, body string)
}{
{"messages", "/v1/messages", claudeCodeAsk, checkAnthropicSpliced},
{"chat", "/v1/chat/completions", chatStreamAsk, checkChatSpliced},
{"responses", "/v1/responses", codexAsk, checkResponsesSpliced},
} {
t.Run(up.name+" to "+client.name, func(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", up.body}}}
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", up.proto, a)
scriptedOn(t, "b", provider.Anthropic, b)
refusalGroup(t, "a/m", "b/m")
s := New()
code, body := sendTo(s, client.path, client.ask)
if code != 200 || a.n != 1 || b.n != 1 {
t.Fatalf("%d (a %d, b %d): %s", code, a.n, b.n, body)
}
if !strings.Contains(body, "first thought") || !strings.Contains(body, " second thought") || !strings.Contains(body, "from b") {
t.Fatalf("not the reasoning and then the next member's answer: %s", body)
}
for _, w := range []string{"sensitive", "content_filter", "refusal", "1301", "safety filter"} {
if strings.Contains(body, w) {
t.Fatalf("the refusal (%s) reached the agent: %s", w, body)
}
}
client.check(t, body)
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Tries[1].Fail != "" {
t.Fatalf("tries: %+v", r.Tries)
}
if s.resting("a") {
t.Fatal("the refused member was set aside")
}
})
}
}
}
// One message, the reasoning's block stopped before the next member's
// text block, numbered after it.
func checkAnthropicSpliced(t *testing.T, body string) {
t.Helper()
starts, blocks, open := 0, 0, map[int64]bool{}
for _, d := range dataOf(t, body) {
switch gjson.Get(d, "type").String() {
case "message_start":
starts++
case "content_block_start":
i := gjson.Get(d, "index").Int()
if i != int64(blocks) || len(open) > 0 {
t.Fatalf("block %d begun as block %d, %v still open: %s", blocks, i, open, body)
}
blocks++
open[i] = true
case "content_block_delta":
if !open[gjson.Get(d, "index").Int()] {
t.Fatalf("a delta for a block not open: %s", d)
}
case "content_block_stop":
delete(open, gjson.Get(d, "index").Int())
}
}
if starts != 1 || blocks != 2 || len(open) != 0 || strings.Count(body, `"type":"message_stop"`) != 1 {
t.Fatalf("message_start %d, blocks %d, open %v: %s", starts, blocks, open, body)
}
}
// One stream, one [DONE].
func checkChatSpliced(t *testing.T, body string) {
t.Helper()
ev := dataOf(t, body)
if strings.Count(body, "[DONE]") != 1 || ev[len(ev)-1] != "[DONE]" {
t.Fatalf("not one stream: %s", body)
}
for _, d := range ev[:len(ev)-1] {
if !json.Valid([]byte(d)) {
t.Fatalf("a chunk that isn't JSON: %q", d)
}
}
}
// One response begun, the next member's message after the reasoning's
// item, the sequence numbers going on from the reasoning's.
func checkResponsesSpliced(t *testing.T, body string) {
t.Helper()
created, last, items := 0, int64(-1), map[string]int64{}
for _, d := range dataOf(t, body) {
typ := gjson.Get(d, "type").String()
if typ == "response.created" {
created++
}
if sq := gjson.Get(d, "sequence_number"); sq.Exists() {
if sq.Int() != last+1 {
t.Fatalf("sequence_number %d after %d: %s", sq.Int(), last, body)
}
last = sq.Int()
}
if typ == "response.output_item.added" {
items[gjson.Get(d, "item.type").String()] = gjson.Get(d, "output_index").Int()
}
}
if created != 1 || strings.Count(body, `"type":"response.completed"`) != 1 {
t.Fatalf("response.created %d: %s", created, body)
}
if r, ok := items["reasoning"]; !ok || items["message"] != r+1 {
t.Fatalf("output items %v: %s", items, body)
}
}
// With nobody behind it, the refusal after shown reasoning is the
// agent's, as before.
func TestRefusalAfterShownThinkingWithNoOneElse(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", glmThoughtThenSensitive}}}
scriptedOn(t, "a", provider.Chat, a)
refusalGroup(t, "a/m")
s := New()
code, body := sendTo(s, "/v1/chat/completions", chatStreamAsk)
if code != 200 || a.n != 1 || !strings.Contains(body, "first thought") || !strings.Contains(body, `"finish_reason":"sensitive"`) {
t.Fatalf("%d (a %d): %s", code, a.n, body)
}
if r := lastRoute(s); len(r.Tries) != 1 || r.Tries[0].Rest != nil {
t.Fatalf("tries: %+v", r.Tries)
}
}
// A refusal once the answer's text began is the agent's: what was said
// can't be taken back, and nobody else is asked.
func TestRefusalAfterThinkingAndTextStays(t *testing.T) {
fresh(t)
late := sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"first thought"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"half"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"sensitive"}]}`,
`data: [DONE]`)
a := &scripted{replies: []reply{{200, "text/event-stream", late}}}
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Anthropic, b)
refusalGroup(t, "a/m", "b/m")
s := New()
code, body := sendTo(s, "/v1/chat/completions", chatStreamAsk)
if code != 200 || body != late || b.n != 0 {
t.Fatalf("%d (b %d): %s", code, b.n, body)
}
if r := lastRoute(s); len(r.Tries) != 1 || r.Tries[0].Rest != nil {
t.Fatalf("tries: %+v", r.Tries)
}
}
// Two members in a row refused after their reasoning: the third answers,
// its block numbered after both.
func TestRefusalHandedOverTwice(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "text/event-stream", glmAnthropicThoughtThenRefused}}}
b := &scripted{replies: []reply{{200, "text/event-stream", glmAnthropicThoughtThenRefused}}}
c := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
scriptedOn(t, "a", provider.Anthropic, a)
scriptedOn(t, "b", provider.Anthropic, b)
scriptedOn(t, "c", provider.Anthropic, c)
refusalGroup(t, "a/m", "b/m", "c/m")
s := New()
code, body := sendTo(s, "/v1/messages", claudeCodeAsk)
if code != 200 || a.n != 1 || b.n != 1 || c.n != 1 || !strings.Contains(body, "from b") || strings.Contains(body, "refusal") {
t.Fatalf("%d (a %d, b %d, c %d): %s", code, a.n, b.n, c.n, body)
}
if strings.Count(body, `"type":"message_start"`) != 1 || !strings.Contains(body, `{"type":"content_block_start","index":2,"content_block":{"type":"text"`) {
t.Fatalf("not one message of three blocks: %s", body)
}
}
// The vendors' words for their filter refusing, beside OpenAI's and
// Anthropic's: Zhipu's, DashScope's and Volcengine Ark's.
func TestSensitiveContentRefusals(t *testing.T) {
for _, raw := range []string{
`{"error":{"code":"1301","message":"系统检测到输入或生成内容可能包含不安全或敏感内容"}}`,
`{"error":{"code":"data_inspection_failed","type":"data_inspection_failed","message":"Input data may contain inappropriate content."}}`,
`{"code":"DataInspectionFailed","message":"Output data may contain inappropriate content."}`,
`{"error":{"code":"OutputTextSensitiveContentDetected","message":"The request failed because the output text may contain sensitive information."}}`,
`{"error":{"code":"InputTextSensitiveContentDetected","message":"x"}}`,
} {
if _, ok := policyRefusal([]byte(raw)); !ok {
t.Errorf("not a refusal: %s", raw)
}
}
for _, raw := range []string{
`{"error":{"code":"1301","message":"rate limited"}}`,
`{"error":{"code":"1113","message":"余额不足"}}`,
} {
if _, ok := policyRefusal([]byte(raw)); ok {
t.Errorf("a refusal: %s", raw)
}
}
if stopFromChat("sensitive") != "filter" {
t.Error("Zhipu's sensitive is not the filter's stop")
}
}