Files
magpie/internal/gateway/risk_refused_test.go
T
yetone 2f726b1f95 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.
2026-11-04 20:00:38 +08:00

107 lines
4.3 KiB
Go

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)
}
})
}