From 6e32942a240f940f4d6df15afa79a85508c00e0e Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 23 Oct 2026 00:05:52 +0800 Subject: [PATCH] gateway: a safety refusal that also says the model is retired retires nothing (#3274, CartethyiaQwQ) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CartethyiaQwQ (#3274) found one more way a safety refusal punished a provider: serve's retirement check runs on any failed try, and modelRetired accepts a 400 whose body says the model "is retired", so a cyber_policy refusal whose message also said so took the model out of the provider's lists (retired.json) until an answer brought it back. No real refusal seen so far says it; a boundary case. - serve: a try settled as a refusal (hw.refused) retires nothing. By then its body is magpie's note "safety filter: cyber_policy — ", text, so policyRefusal can't tell it there. - modelRetired: a body that is the safety filter's error object (policyRefusal) is never a retirement, for its other callers too (modelRefused, which rests the model, and retryable). The note cuts the vendor's message at 300 characters, so the test's refusal is short enough to keep the retirement words in it, as a real one would. Tests: TestRefusalSayingRetiredRetiresNothing (an OpenAI-compatible provider answering a 400 cyber_policy refusal saying "The model m1 is retired." retires nothing; the same words under model_deprecated still retire), and TestRetiredWords gets three cases. Broken by hand: without the serve guard, TestRefusalSayingRetiredRetiresNothing is red ("retired true, want false"); without the modelRetired guard, TestRetiredWords is red on both refusal cases. A Codex account's refusal never reached Retire on main, so it was tried only through the OpenAI-compatible path. Full suite at 91f777e93 (base 8278fd9b0, origin/main at push): go test -tags nogui -timeout 45m ./... — 69 packages ok, none failed (internal/gateway 566.8s). gofmt clean; vet darwin, linux nogui, windows. Co-authored-by: CartethyiaQwQ --- internal/gateway/fallback.go | 5 +++++ internal/gateway/gateway.go | 5 +++-- internal/gateway/retired_test.go | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go index 87bd727e8..43b34db31 100644 --- a/internal/gateway/fallback.go +++ b/internal/gateway/fallback.go @@ -1106,6 +1106,11 @@ func modelRetired(status int, body []byte) bool { default: return false } + // the safety filter turning the request away (#3274) is about the + // request, whatever its message says of the model + if _, refused := policyRefusal(body); refused { + return false + } // the words are matched decoded, too (#1669) body = wordsOf(body) return retiredWords.Match(body) && !quotaWords.Match(body) diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f47632a91..12d2998d9 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -2786,9 +2786,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro if hw.passing { clientResponseID = call.Usage.ResponseID } - if !hw.passing && modelRetired(hw.code(), hw.errBody()) { + if !hw.passing && !hw.refused && modelRetired(hw.code(), hw.errBody()) { // the vendor's list may still name it (OpenCode Zen's - // exo-free): it leaves the lists of this provider's models + // exo-free): it leaves the lists of this provider's models; a + // refusal is about the request, whatever it says (#3274) provider.Retire(c.p.ID, c.model) } outgrew := false // a failure that didn't say so was the request's length diff --git a/internal/gateway/retired_test.go b/internal/gateway/retired_test.go index a1ddbd46d..7297cbb8d 100644 --- a/internal/gateway/retired_test.go +++ b/internal/gateway/retired_test.go @@ -143,6 +143,11 @@ func TestRetiredWords(t *testing.T) { {401, `{"error":{"code":null,"message":"OpenCode Zen Free: Model glm-5-free is not supported","param":null,"type":"authentication_error"}}`, true}, {401, `Model grok-4.7 is not supported for format anthropic`, false}, {401, `Invalid API key.`, false}, + // the safety filter's refusal, whatever its message says of the + // model, is about the request (#3274) + {400, `{"error":{"type":"invalid_request_error","code":"cyber_policy","message":"This request was flagged by our safety filter. The model m1 is retired.","param":null}}`, false}, + {400, `{"error":{"code":"content_filter","message":"The model m1 has been deprecated."}}`, false}, + {400, `{"error":{"type":"invalid_request_error","code":"model_deprecated","message":"The model m1 is retired."}}`, true}, } { if got := modelRetired(c.status, []byte(c.body)); got != c.want { t.Errorf("modelRetired(%d, %s) = %v", c.status, c.body, got) @@ -174,3 +179,31 @@ func TestZenUnsupportedModelLeavesTheListAlone(t *testing.T) { t.Fatalf("the account's other model sat out with it: %d %s", code, body) } } + +// A safety filter's refusal whose message also says the model is retired +// is a refusal of the request: the model stays in the provider's lists +// (#3274, CartethyiaQwQ). A retirement said without one still retires. +func TestRefusalSayingRetiredRetiresNothing(t *testing.T) { + for _, x := range []struct { + name, body string + retire bool + }{ + {"refusal", `{"error":{"type":"invalid_request_error","code":"cyber_policy","message":"This content was flagged for possible cybersecurity risk. The model m1 is retired.","param":null}}`, false}, + {"retired alone", `{"error":{"type":"invalid_request_error","code":"model_deprecated","message":"The model m1 is retired."}}`, true}, + } { + t.Run(x.name, func(t *testing.T) { + fresh(t) + t.Cleanup(func() { provider.Unretire("a", "m1") }) + serveOn(t, "a", "k", []string{"m1"}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + io.ReadAll(r.Body) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusBadRequest) + io.WriteString(w, x.body) + })) + code, body := postAs(t, New(), "", `{"model":"a/m1","messages":[{"role":"user","content":"hi"}]}`) + if got := provider.Retired("a", "m1"); got != x.retire { + t.Fatalf("retired %v, want %v (%d %s)", got, x.retire, code, body) + } + }) + } +}