gateway: a request its vendor's risk control refused isn't sent on to the provider's other accounts (_teamtaylor_)

_teamtaylor_ on Discord: WorkBuddy's risk control spreads across accounts
that send the same request, and magpie sent one request on from account to
account until its tries ran out, so each account was restricted in turn.

The gateway already read the refusal (WorkBuddy's 403 11140 as the plugin
words it, provider.WBRestrictedHint; ZCode's 3012, ZCodeRestrictedHint) as
failRestricted and rested the account 2h, but the candidate loop then went
on to the next candidate, which is usually a sibling account. Now such a
refusal (riskRefused: failRestricted and not a remote magpie's) walls the
provider off for the rest of the request: its other accounts are dropped
from the candidates (elsewhere), from busyNow's replan rounds and from
roomy's left-out list (elsewhereWeighed). A group's other provider is
still asked; with none left the agent gets the refusal. The last
candidate's restricted refusal now rests the account too (before, only a
non-last one did). Generic: any plugin ending its error in either hint
gets it. The workbuddy plugin needs no change (it does no cross-account
retry of its own).

Run (on mote, through mote-run):
- go test -count=1 -timeout 45m ./internal/gateway: ok (727s), before
  the rebase onto origin/main 43849d51c
- at the pushed head (rebased on 43849d51c): go test -run
  'Risk|Restricted|ZCode|Plain403|PolicyRefusal|Protection|ZDR|Overflow|Live'
  ./internal/gateway: ok; GOOS=linux and GOOS=windows go vet
  ./internal/gateway: ok; gofmt -l: empty
- plugins repo, HOME=$(mktemp -d) bun test packages/workbuddy (0.2.20,
  unchanged): 318 pass, 0 fail

Broken by hand:
- the walling block made `if false && ...`: TestRiskRefusalNotSentTo-
  TheProvidersOtherAccounts (accounts asked [acct-1 acct-2], the other
  provider never), TestRiskRefusalAloneIsToldAndRests/two_accounts and
  TestPluginRestrictedAccountSkipped (asked a,b) go red
- the last candidate's rest back to failVerify only: TestRiskRefusal-
  AloneIsToldAndRests (no rest) and TestPluginRestrictedAccountSkipped
  (a's rest nil) go red

TestPluginRestrictedAccountSkipped changed meaning on purpose: the
restricted account's request is now answered 403 with the hint, b
unasked; the next request goes to b. Another vendor's 11140 still fails
over to b.

Not tried: a real WorkBuddy account (no restricted account to hand);
the agent's own retry, or the conversation's next request, still goes to
the provider's next account, so only the within-request walk is stopped.
This commit is contained in:
yetone
2026-11-04 20:00:38 +08:00
parent 43849d51c3
commit 2f726b1f95
5 changed files with 177 additions and 12 deletions
File diff suppressed because one or more lines are too long
+35
View File
@@ -1757,6 +1757,41 @@ func elsewhere(left []candidate, c candidate) []candidate {
return out
}
// riskRefused says c's vendor refused it for the account's safety — its
// risk control restricted or blocked the account (failRestricted:
// WorkBuddy's 11140, ZCode's 3012, or any plugin's refusal ending in
// provider.WBRestrictedHint or provider.ZCodeRestrictedHint) — rather
// than for quota or a passing fault. A vendor's risk control reads the
// request as well as the account, so the same request sent on to another
// of its accounts trips it there too. Another computer's magpie that
// says so (remoteModelRest) speaks for one account of its own, and its
// other models may be other vendors: it walls off nothing here.
func riskRefused(c candidate, status int, header http.Header, body []byte) bool {
return failureOf(c, status, body) == failRestricted && !remoteModelRest(c, failRestricted, header)
}
// elsewhereWeighed is elsewhere on a list of candidates beside their
// weights (roomy's left out as too small), the two kept paired: weights
// that don't pair with the list are kept as they are.
func elsewhereWeighed(left []candidate, w []Weighed, c candidate) ([]candidate, []Weighed) {
paired := len(w) == len(left)
var out []candidate
var outW []Weighed
for k, x := range left {
if x.p.ID == c.p.ID {
continue
}
out = append(out, x)
if paired {
outW = append(outW, w[k])
}
}
if !paired {
outW = w
}
return out, outW
}
// retryable says whether another provider may do better with a request
// that failed this way: the vendor was busy, out of quota or failing, or
// this key or provider can't serve it — not the request itself at fault.
+17 -1
View File
@@ -4276,6 +4276,20 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
}
// nor is it asked again after a moment
protected = protected || zdr || noRoom
if !hw.passing && !hw.refused && hw.failed() && riskRefused(c, hw.code(), hw.header, hw.errBody()) {
// the vendor's risk control restricted the account (WorkBuddy's
// 403 11140, ZCode's 3012): its risk control reads the same
// request on whichever account it comes from, so none of the
// provider's other accounts is sent it, now or in a later
// round of this request (_teamtaylor_ on Discord: one request
// sent on from account to account got each restricted in
// turn, until its tries ran out). It rests as restricted;
// another provider left is asked, else the agent is told
cands = append(cands[:i+1:i+1], elsewhere(cands[i+1:], c)...)
busyNow = slices.DeleteFunc(busyNow, func(b busyTry) bool { return b.c.p.ID == c.p.ID })
small, smallW = elsewhereWeighed(small, smallW, c)
last = i == len(cands)-1
}
if wait, ok := passing(hw.code(), hw.header, hw.errBody(), again); ok && !protected && !last && hw.failed() && spentAfter(cands[i+1:]) {
// the others left are out of their allowance (Discord, waroy: a
// Codex account run out, Grok busy a moment): this one is the
@@ -4798,7 +4812,9 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// (offline.go): told so, and it doesn't rest
try.Fail = failOffline
}
if try.Fail == failVerify && !held || strictAlone {
// and one its vendor restricted (riskRefused), for the next
// request not to be sent to it again
if (try.Fail == failVerify || try.Fail == failRestricted) && !held || strictAlone {
// the last one left rests too, for the app to show and the
// next requests to be held
rest := s.restAfter(c, call.Status, hw.header, []byte(call.Error))
+106
View File
@@ -0,0 +1,106 @@
package gateway
import (
"io"
"net/http"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// riskBlocked answers as an account whose vendor's risk control blocked it,
// in magpie's words (provider.ZCodeRestrictedHint, as the ZCode plugin
// puts its 3012).
func riskBlocked(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
io.WriteString(w, `{"error":{"message":"blocked — `+provider.ZCodeRestrictedHint+`","type":"permission_error"}}`)
}
// A vendor's risk control reads the request as well as the account: one
// request sent on from account to account got each of them restricted in
// turn, until its tries ran out (_teamtaylor_ on Discord, WorkBuddy). An
// account its vendor restricted walls its provider off for the rest of the
// request: none of the provider's other accounts is sent it, and a group's
// other provider answers. The restricted account rests; the provider's
// other accounts don't, and answer the next request.
func TestRiskRefusalNotSentToTheProvidersOtherAccounts(t *testing.T) {
codexSignedIn(t, "b@example.com", "c@example.com")
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
t.Fatal(err)
}
var tried []string
chatgptRefusing(t, &tried, riskBlocked)
other := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "b", provider.Chat, other)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"codex/gpt-5.5", "b/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || other.n != 1 {
t.Fatalf("%d %s; the other provider asked %d times; accounts asked %v", code, body, other.n, tried)
}
if strings.Join(tried, ",") != "acct-1" {
t.Fatalf("the restricted request was sent on to %v", tried)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Fail != failRestricted || r.Tries[0].Rest == nil || r.Tries[0].Rest.By != "restricted" {
t.Fatalf("tries %+v", r.Tries)
}
for _, who := range []string{"codex@b@example.com", "codex@c@example.com"} {
if _, ok := restOf(who); ok {
t.Fatalf("%s rests, never having been asked", who)
}
}
// the next request goes to the provider's next account
tried = nil
if code, body := postAs(t, s, "", `{"model":"codex/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`); code != 200 || strings.Join(tried, ",") != "acct-2" {
t.Fatalf("next request: %d %s; asked %v", code, body, tried)
}
}
// With no other provider, the agent is told at once and the account
// rests, whether it was the only account or the provider had more; a
// refusal that isn't the vendor's risk control (a plain 403 alone) still
// goes on to the next account as before.
func TestRiskRefusalAloneIsToldAndRests(t *testing.T) {
for _, x := range []struct {
name string
spares []string
}{{"lone account", nil}, {"two accounts", []string{"b@example.com"}}} {
t.Run(x.name, func(t *testing.T) {
codexSignedIn(t, x.spares...)
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
t.Fatal(err)
}
var tried []string
chatgptRefusing(t, &tried, riskBlocked)
s := New()
code, body := postAs(t, s, "", `{"model":"codex/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`)
if code != http.StatusForbidden || !strings.Contains(body, provider.ZCodeRestrictedHint) || strings.Join(tried, ",") != "acct-1" {
t.Fatalf("%d %s; asked %v", code, body, tried)
}
r, ok := restOf("codex@me@example.com")
if !ok || r.Why != failRestricted || r.By != "restricted" {
t.Fatalf("the restricted account rests %+v (%v)", r, ok)
}
})
}
t.Run("a plain 403 goes on", func(t *testing.T) {
codexSignedIn(t, "b@example.com")
if err := provider.SetRouting("codex", provider.Ordered); err != nil {
t.Fatal(err)
}
var tried []string
chatgptRefusing(t, &tried, func(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
io.WriteString(w, `{"error":{"message":"Forbidden"}}`)
})
if code, body := postAs(t, New(), "", `{"model":"codex/gpt-5.5","messages":[{"role":"user","content":"hi"}]}`); code != 200 || strings.Join(tried, ",") != "acct-1,acct-2" {
t.Fatalf("%d %s; asked %v", code, body, tried)
}
})
}
+18 -10
View File
@@ -119,11 +119,14 @@ func TestOnlyWorkBuddyRestrictedRestsAsRestricted(t *testing.T) {
}
// Through a plugin, as the moved WorkBuddy is served: an account whose
// answer is the plugin's restricted refusal rests two hours and the next
// account answers, on this request and on the ones after it, which don't
// ask the restricted one again (before #1849 it was back within minutes
// and asked by every request). The same vendor's raw 11140 from a plugin
// that isn't WorkBuddy's (fakeco) rests as any failure does.
// answer is the plugin's restricted refusal rests two hours, and the
// request it refused isn't sent on to the provider's other account: the
// vendor's risk control reads the request too, and restricted each account
// it was sent on to in turn (_teamtaylor_ on Discord). The agent is told;
// the next request goes to the other account, never the restricted one
// (before #1849 it was back within minutes and asked by every request).
// The same vendor's raw 11140 from a plugin that isn't WorkBuddy's
// (fakeco) rests as any failure does, and the next account answers it.
func TestPluginRestrictedAccountSkipped(t *testing.T) {
bun := testenv.Bun(t)
for _, tc := range []struct {
@@ -182,12 +185,17 @@ func TestPluginRestrictedAccountSkipped(t *testing.T) {
}
s := New()
code, body := postAs(t, s, "", `{"model":"fakeco/fake-1","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from b") {
t.Fatalf("%d %s", code, body)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 {
t.Fatalf("tries %+v", r.Tries)
mu.Lock()
first := strings.Join(tried, ",")
mu.Unlock()
if tc.why == failRestricted {
// the agent is told, and b never sent the request
if code != http.StatusForbidden || !strings.Contains(body, provider.WBRestrictedHint) || first != "a" || len(r.Tries) != 1 {
t.Fatalf("%d %s; asked %s; tries %+v", code, body, first, r.Tries)
}
} else if code != 200 || !strings.Contains(body, "from b") || len(r.Tries) != 2 {
t.Fatalf("%d %s; tries %+v", code, body, r.Tries)
}
f := r.Tries[0]
if f.Fail != tc.why || f.Rest == nil || f.Rest.Why != tc.why {