DD on Discord: through magpie, Claude's safety filter refusing a turn never makes Claude Code move to its fallback model, as it does on Anthropic's API. magpie asked every other account and group member (#248), then told Claude Code a 400 "<model> refused this request", which it takes as an API error. Claude Code 2.1.296 switches on stop_reason "refusal" in a 200 reply (the stream's message_delta or a whole message, stop_details giving the category), and only with a fallback model set; a 400, Anthropic's "Output blocked by content filtering policy" or magpie's, is never retried. What changed: - When nobody is left after a refusal, an agent on Anthropic's Messages API gets the held refusal itself, the vendor's 200 with stop_reason and stop_details, streamed or whole (holdWriter.handBack): keyed Anthropic providers, groups, the Claude subscription. Codex and Chat clients keep the 400, since an empty-looking whole reply makes Codex ask again. - Settings' refusalRetries counts how many more candidates (accounts, then group members, another model among them) a refusal is asked of. Unset is every one, #248's behaviour (Koohoko asked for it); 0 hands it straight back. GUI: Settings > Models > Replies > "Ask again after a refusal" (Never ask again, 1, 2, 3, All), en/zh/zh-TW/ja/de. Global, no CLI: magpie has no general settings CLI (as noLoopGuard). Other 400s are still never asked again; refusals still rest nobody. - disabled-models.test.cjs wheels to Enable, which the new row put past the window's foot at webkit de 420. - Docs: reference.md "When the safety filter refuses", gateway-routing.md. Run: - go test -tags nogui ./internal/gateway (full, -timeout 40m: a first run hit the 10m default at load ~70 from other sessions), ./internal/gui, ./internal/settings; gofmt -l clean; GOOS=linux and windows go vet. - GUI: refusal-retries-setting (en, zh, zh-TW, ja, de at 1100 and 420) with gui-zh-tw, gui-ja, gui-de, i18n-keys and every tab=models suite (disabled-models, images-*, loop-guard-setting, member-model-setting, search-*, searcher-pick, vision-mcp), chromium and webkit: 176 pass. - refusal-retries-setting under load (8 runs at once, load ~200): first 4 of 8 failed at en/ja 420, a press landing on the first save's "Saved" note over the row, then WebKit's wheel still moving the view as the press was measured. It now reaches each button clear of anything drawn over it (reader.cjs reach) and waits for the scroll to rest: 16 of 16. Broken by hand: - handBack off: TestRefusalRetriesNoneHandsClaudeCodeTheRefusal, ...CountsTheTries, ...Group, ...ClaudeSubscription and TestRefusalWithNobodyLeftIsAnError fail (400 "refused this request"). - count ignored: those plus ...CodexStillTold400 fail (the next key answers). - GUI keep-on-omit dropped: TestSettingsRefusalRetries fails; the GUI test fails with All sent as undefined, and with the row not appended. Not tried: a real Claude account's refusal, or Claude Code's fallback switch end to end; the refusal shapes are from Claude Code's source and Anthropic's docs.
198 lines
8.0 KiB
Go
198 lines
8.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"cmp"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Claude's refusal with what its classifier said of it, streamed and
|
|
// whole, as Anthropic ends a turn its safety filter stopped before
|
|
// anything was said (DD on Discord).
|
|
var (
|
|
anthropicRefusalDetailed = strings.Replace(anthropicRefusal, `"stop_reason":"refusal","stop_sequence":null}`,
|
|
`"stop_reason":"refusal","stop_sequence":null,"stop_details":{"type":"refusal","category":"cyber","explanation":"flagged"}}`, 1)
|
|
anthropicRefusalWhole = `{"id":"m1","type":"message","role":"assistant","model":"m","content":[],"stop_reason":"refusal","stop_sequence":null,"stop_details":{"type":"refusal","category":"cyber","explanation":"flagged"},"usage":{"input_tokens":3,"output_tokens":2}}`
|
|
anthropicAnswerWhole = `{"id":"m2","type":"message","role":"assistant","model":"m","content":[{"type":"text","text":"from b"}],"stop_reason":"end_turn","usage":{"input_tokens":3,"output_tokens":4}}`
|
|
)
|
|
|
|
// refusalRetries sets Settings' RefusalRetries for the test.
|
|
func refusalRetries(t *testing.T, n int) {
|
|
t.Helper()
|
|
s := settings.Load()
|
|
s.RefusalRetries = &n
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// keyedClaude is a keyed Anthropic provider "a" with keys k1, k2, k3 in
|
|
// order, the ones in refusing answering every turn with Claude's refusal,
|
|
// the others with "from b"; it returns the keys asked, in order.
|
|
func keyedClaude(t *testing.T, refusing ...string) *[]string {
|
|
t.Helper()
|
|
var keys []string
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
k := cmp.Or(r.Header.Get("x-api-key"), strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))
|
|
keys = append(keys, k)
|
|
refuse := false
|
|
for _, x := range refusing {
|
|
refuse = refuse || x == k
|
|
}
|
|
if !strings.Contains(string(b), `"stream":true`) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, map[bool]string{true: anthropicRefusalWhole, false: anthropicAnswerWhole}[refuse])
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, map[bool]string{true: anthropicRefusalDetailed, false: anthropicAnswer}[refuse])
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "a", Name: "A", Models: []string{"m"}, Anthropic: up.URL, Routing: provider.Ordered,
|
|
Key: "k1", Keys: []provider.KeyAccount{{Key: "k2"}, {Key: "k3"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &keys
|
|
}
|
|
|
|
// handedBack says body is Claude's refusal as the vendor said it, to an
|
|
// agent on Anthropic's Messages API: what Claude Code switches to its
|
|
// fallback model on, not magpie's 400.
|
|
func handedBack(code int, body string) bool {
|
|
return code == 200 && strings.Contains(body, `"stop_reason":"refusal"`) && strings.Contains(body, `"category":"cyber"`) &&
|
|
!strings.Contains(body, "refused this request")
|
|
}
|
|
|
|
const messagesAsk = `{"model":"a/m","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`
|
|
|
|
// With RefusalRetries 0, a keyed Anthropic provider's refusal goes to
|
|
// Claude Code as Anthropic said it, streamed or whole, with its other keys
|
|
// never asked and the key that refused not set aside (DD on Discord: magpie
|
|
// moved on through the accounts and answered 400, so Claude Code never
|
|
// switched to its fallback model as it does on Anthropic's own API).
|
|
func TestRefusalRetriesNoneHandsClaudeCodeTheRefusal(t *testing.T) {
|
|
for _, stream := range []bool{true, false} {
|
|
fresh(t)
|
|
refusalRetries(t, 0)
|
|
keys := keyedClaude(t, "k1", "k2", "k3")
|
|
s := New()
|
|
ask := messagesAsk
|
|
if !stream {
|
|
ask = strings.Replace(ask, `"stream":true,`, "", 1)
|
|
}
|
|
code, body := sendTo(s, "/v1/messages", ask)
|
|
if !handedBack(code, body) || strings.Join(*keys, ",") != "k1" {
|
|
t.Fatalf("stream %v: status %d: %s (keys %v)", stream, code, body, *keys)
|
|
}
|
|
r := lastRoute(s)
|
|
if len(r.Tries) != 1 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil {
|
|
t.Fatalf("stream %v: tries: %+v", stream, r.Tries)
|
|
}
|
|
if _, ok := restOf("a"); ok {
|
|
t.Fatalf("stream %v: a set aside by a refusal", stream)
|
|
}
|
|
}
|
|
}
|
|
|
|
// RefusalRetries counts the tries after the first refusal: at 1, two keys
|
|
// of three are asked; unset, as by default, every one is (#248), and the
|
|
// one that answers is what the agent gets.
|
|
func TestRefusalRetriesCountsTheTries(t *testing.T) {
|
|
fresh(t)
|
|
refusalRetries(t, 1)
|
|
keys := keyedClaude(t, "k1", "k2", "k3")
|
|
code, body := sendTo(New(), "/v1/messages", messagesAsk)
|
|
if !handedBack(code, body) || strings.Join(*keys, ",") != "k1,k2" {
|
|
t.Fatalf("status %d: %s (keys %v)", code, body, *keys)
|
|
}
|
|
|
|
fresh(t)
|
|
keys = keyedClaude(t, "k1", "k2")
|
|
code, body = sendTo(New(), "/v1/messages", messagesAsk)
|
|
if code != 200 || !strings.Contains(body, "from b") || strings.Join(*keys, ",") != "k1,k2,k3" {
|
|
t.Fatalf("unset: status %d: %s (keys %v)", code, body, *keys)
|
|
}
|
|
|
|
// nobody left by default either: the vendor's refusal, every key asked
|
|
fresh(t)
|
|
keys = keyedClaude(t, "k1", "k2", "k3")
|
|
code, body = sendTo(New(), "/v1/messages", messagesAsk)
|
|
if !handedBack(code, body) || strings.Join(*keys, ",") != "k1,k2,k3" {
|
|
t.Fatalf("unset, all refusing: status %d: %s (keys %v)", code, body, *keys)
|
|
}
|
|
}
|
|
|
|
// Codex, on the Responses API, is still told a refusal with nobody left as
|
|
// a 400 it doesn't ask again for (#248): an empty reply that looks whole
|
|
// it would ask for again and again. RefusalRetries holds for it as well.
|
|
func TestRefusalRetriesCodexStillTold400(t *testing.T) {
|
|
fresh(t)
|
|
refusalRetries(t, 0)
|
|
keys := keyedClaude(t, "k1")
|
|
code, body := sendTo(New(), "/v1/responses", `{"model":"a/m","stream":true,"input":[{"role":"user","content":[{"type":"input_text","text":"hi"}]}]}`)
|
|
if code != 400 || !strings.Contains(body, "refused this request") || strings.Join(*keys, ",") != "k1" {
|
|
t.Fatalf("status %d: %s (keys %v)", code, body, *keys)
|
|
}
|
|
}
|
|
|
|
// A routing group whose first member refuses: at RefusalRetries 0 the next
|
|
// member, another model, isn't asked and Claude Code has the refusal, to
|
|
// switch to the fallback model it was set up with; unset, the next member
|
|
// answers (#248).
|
|
func TestRefusalRetriesGroup(t *testing.T) {
|
|
for _, none := range []bool{true, false} {
|
|
fresh(t)
|
|
if none {
|
|
refusalRetries(t, 0)
|
|
}
|
|
a := &scripted{replies: []reply{{200, "text/event-stream", anthropicRefusalDetailed}}}
|
|
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
|
scriptedOn(t, "a", provider.Anthropic, a)
|
|
scriptedOn(t, "b", provider.Anthropic, b)
|
|
refusalGroup(t, "a/m", "b/m")
|
|
s := New()
|
|
code, body := sendTo(s, "/v1/messages", strings.Replace(messagesAsk, "a/m", "group/g", 1))
|
|
if none && (!handedBack(code, body) || a.n != 1 || b.n != 0) {
|
|
t.Fatalf("none: status %d: %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
if !none && (code != 200 || !strings.Contains(body, "from b") || a.n != 1 || b.n != 1) {
|
|
t.Fatalf("unset: status %d: %s (a %d, b %d)", code, body, a.n, b.n)
|
|
}
|
|
if r := lastRoute(s); r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil {
|
|
t.Fatalf("none %v: tries: %+v", none, r.Tries)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A Claude subscription served through the claude CLI: at RefusalRetries 0
|
|
// the spare account isn't asked, and Claude Code has the refusal with its
|
|
// classifier's category, streamed and whole.
|
|
func TestRefusalRetriesClaudeSubscription(t *testing.T) {
|
|
log := claudeSubscriptionRefusing(t)
|
|
refusalRetries(t, 0)
|
|
s := New()
|
|
for _, ask := range []string{
|
|
`{"model":"claude/claude-sonnet-5","max_tokens":50,"stream":true,"messages":[{"role":"user","content":"ping"}]}`,
|
|
`{"model":"claude/claude-sonnet-5","max_tokens":50,"messages":[{"role":"user","content":"ping again"}]}`,
|
|
} {
|
|
os.Remove(log)
|
|
code, body := sendTo(s, "/v1/messages", ask)
|
|
b, _ := os.ReadFile(log)
|
|
if !handedBack(code, body) || strings.Join(strings.Fields(string(b)), ",") != "own" {
|
|
t.Fatalf("%s: status %d: %s (runs %q)", ask, code, body, b)
|
|
}
|
|
r := lastRoute(s)
|
|
if len(r.Tries) != 1 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil {
|
|
t.Fatalf("tries: %+v", r.Tries)
|
|
}
|
|
}
|
|
}
|