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>
This commit is contained in:
ggbdpq
2026-11-05 18:31:16 +08:00
committed by yetone
parent 2027bb5a6d
commit c2dd693dac
2 changed files with 60 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
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)
}
}
+4
View File
@@ -3591,6 +3591,10 @@ var filterReasons = map[string]string{
// agent took it for a failed sign-in (sweanng on Discord, ZCode:
// "Provider authentication failed", reason=auth_failed status=403)
"content_policy_rejected": "content_policy_rejected",
// StepFun's error type, its filter refusing with an HTTP 451 (#5089):
// read from the error object, the refusal is the request's, and the
// group's next member is asked rather than the 451 handed on
"censorship_blocked": "censorship_blocked",
// Zhipu's finish_reason when its filter stops GLM's reply, and
// DashScope's error code when Alibaba's stops Qwen's: both after the
// reasoning went to the agent, which had it end there (yhwangl on X)