Files
magpie/internal/gateway/censorship_451_test.go
T
ggbdpq c2dd693dac gateway: a 451 censorship_blocked refusal asks the group's next member (#5089)
StepFun's safety filter refuses a request with HTTP 451 and an error
object saying {"type":"censorship_blocked"}. filterReasons, the map of
the words a vendor says its filter stopped the reply with, didn't know
the type, so policyRefusal didn't read the 451 as a refusal and
retryable has no 451 of its own: the status went to the agent as it
came, the group's next member unasked, and the try stood as a plain
failure of the account.

The same refusal said in a 200 stream's finish_reason would have been
one, as content_filter is. Now the type is in the map, and the refusal
goes to the next member the way any refusal the vendor names does:
nobody rests, as the filter refusing is about the request, not the
account.

Reviewed and ported: one filterReasons entry, vendor-neutral, and
nobody rests as before. On mote, -run 'Censorship': with the map entry
reverted, red: "TestCensorshipBlocked451FailsOver ... whole: 451" and
"TestCensorshipBlockedIsAPolicyRefusal ... policyRefusal: "", false";
green with it. Whole package: ok (906.6s). Not tried: a real 451 from
StepFun.

Generated-by: GLM-5.3-Flash (ZCode)
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-05 18:31:16 +08:00

57 lines
2.2 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// StepFun's safety filter refuses with HTTP 451 and the type
// "censorship_blocked" (#5089): the vendor names the refusal in its error
// object, machine-readable, as the ones policyRefusal reads — the group's
// next member is asked, and nobody rests, as for any refusal of the same
// family. Before, the 451 went to the agent as it came, the next member
// unasked, and the try stood as a plain failure.
func TestCensorshipBlocked451FailsOver(t *testing.T) {
const blocked = `{"error":{"type":"censorship_blocked","message":"The content you provided or machine outputted is blocked."}}`
for _, ask := range []struct {
name string
body string
}{
{"whole", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`},
{"stream", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`},
} {
t.Run(ask.name, func(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{451, "", blocked}}}
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()
code, body := sendTo(s, "/v1/chat/completions", ask.body)
if code != 200 || !strings.Contains(body, "hello") || strings.Contains(body, "censorship") || a.n != 1 || b.n != 1 {
t.Fatalf("%s: %d %s (a %d, b %d)", ask.name, code, body, a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Status != 200 {
t.Fatalf("%s: tries: %+v", ask.name, r.Tries)
}
if s.resting("a") {
t.Fatal("a censorship refusal set the member aside")
}
})
}
}
// The refusal read from the error object itself, as settle reads it.
func TestCensorshipBlockedIsAPolicyRefusal(t *testing.T) {
note, ok := policyRefusal([]byte(`{"error":{"type":"censorship_blocked","message":"The content you provided or machine outputted is blocked."}}`))
if !ok || !strings.Contains(note, "censorship_blocked") {
t.Fatalf("policyRefusal: %q, %v", note, ok)
}
}