Files
magpie/internal/gateway/verify_test.go
T
yetone eb1da15722 gateway: a vendor's error reaches the agent in its own words first (@gptchan404)
@gptchan404 on X: magpie wrapped a vendor's error in its own words, e.g.
Factory's refusal arrived as "claude-opus-5-5 refused this request
(safety filter: content_policy_rejected — <Factory's words>) via
Factory", so there was no error of the vendor's own to take back to it.

Now the message the agent reads starts with the vendor:

  Factory: This request was blocked by Factory's usage policy check.
  (upstream request id …, code content_policy_rejected) (HTTP 400)
  magpie: claude-opus-5-5 refused this request (safety filter:
  content_policy_rejected) via Factory — <the plugin's advice>

Line 1 is the provider's name, the vendor's words verbatim
(provider.VendorWords), then (HTTP status, code, request id), each
left out when the words already hold it. magpie's own notes go on a
second line that starts "magpie: ". A plugin's advice after " — " goes
on magpie's line (pluginWords). The vendor's JSON error fields
(type/code/param/request_id) still pass through as before
(writeVendorErrorFrom).

Covered: Anthropic messages, OpenAI chat, Responses and Gemini, each
whole and streamed, for any vendor error (writeVendorErrorFrom ->
upstreamTold) and for a safety-filter refusal no one else answered
(failTo -> refusalTold).

Only the client sees the new text. A try's held bytes and call.Error
keep magpie's reading, because the rest scope, the rate hold, the
retired model, the verify hold and failover all classify them. The
holdWriter retells them on output only (retold, in release and in
passing writes). codex_backend's summaryEndedShort now reads the
vendor line only.

Tests whose pinned text changed on purpose: kept_error_test,
zcode_blocked_test, verify_test (sameVerify: a held verify answer is
magpie's own and keeps its one-line form),
remote_magpie_restricted_test. Doc: docs/subsystems/gateway-routing.md.

Run (mote, at this tree):
- go test ./internal/gateway -run
  'Told|Refus|Factory|Kept|Relay|Verify|Codex|Plugin|Retired|Rate|Zcode|ZCode|Restricted':
  ok (346s).
- Red on old code: TestRefusalToldWithTheVendorsWordsFirst and
  TestVendorErrorToldWithItsOwnWordsFirst, 32 of 32 subtests red at
  origin/main 76868bad6. TestPluginAdviceToldOnMagpiesLine red with
  pluginWords broken by hand.
- Earlier gateway run: only TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes
  red, also red on clean origin/main (#7117).
- gofmt -l clean. GOOS=windows go vet ./... ok. GOOS=linux go vet of
  ./internal/gateway and ./internal/provider ok (a cross ./... vet stops
  in Wails' cgo files).
- Full suite: queued on mote at this tree, pushed before it finishes
  per the owner's rule (10-22); any red gets an issue.

Not tried: a real Factory account (the case comes from the plugin's
own message format, packages/factory index.mjs); the GUI request view
shows the client's body, so it shows this text, and has no separate
vendor-raw view; a remote magpie chained in front of another would
show the inner magpie's first line inside the outer's.
2026-11-20 15:28:24 +08:00

148 lines
7.4 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
const verifyLinked = `{"error":{"code":403,"message":"Verify your account to continue.","status":"PERMISSION_DENIED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"VALIDATION_REQUIRED","metadata":{"validation_url":"https://accounts.google.com/signin/continue?sarp=1&x=2"}}]}}`
// An account Google wants verified (#152) is refused until someone does:
// the agent is told how, with the link; its reconnects for the next moment
// are told the same without asking again; the app can lift the rest.
func TestVerifyRefusalRestsTheLastOne(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{403, "application/json", verifyLinked}}}
scriptedOn(t, "a", provider.Chat, a)
s := New()
req := `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`
code, body := postAs(t, s, "", req)
msg := provider.APIError([]byte(body), "")
if code != 403 || a.n != 1 || !strings.Contains(msg, "Verify your account to continue.") ||
!strings.Contains(msg, "open https://accounts.google.com/signin/continue?sarp=1&x=2 in a browser") {
t.Fatalf("%d after %d tries: %s", code, a.n, msg)
}
r := s.trace.routes[len(s.trace.routes)-1]
try := r.Tries[len(r.Tries)-1]
if try.Fail != failVerify || try.Rest == nil || try.Rest.By != "verify" || try.Rest.Key != "a" ||
try.Rest.Link != "https://accounts.google.com/signin/continue?sarp=1&x=2" {
t.Fatalf("try %+v rest %+v", try, try.Rest)
}
// the agent reconnects at once: held, not sent on, and told the same:
// the vendor's words and where to verify (the first reply puts the
// vendor's words first, upstreamTold; a held one is magpie's own)
for range 3 {
if code, again := postAs(t, s, "", req); code != 403 || !sameVerify(provider.APIError([]byte(again), ""), msg) {
t.Fatalf("%d %s", code, again)
}
}
if a.n != 1 {
t.Fatalf("asked again %d times while held", a.n-1)
}
if rest, ok := restOf("a"); !ok || rest.Why != failVerify {
t.Fatalf("%+v %v", rest, ok)
}
// verified, and said so in the app: asked again
if !s.Unrest("a") || s.resting("a") {
t.Fatal("rest not lifted")
}
a.replies = []reply{{200, "", chatOK}}
if code, body := postAs(t, s, "", req); code != 200 || a.n != 2 {
t.Fatalf("%d after %d: %s", code, a.n, body)
}
}
// Without a link, the agent is told to verify it in the vendor's app; with
// another to ask, the request goes on to it and the refused one rests.
func TestVerifyRefusalFallsBack(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{403, "application/json", `{"error":{"code":403,"message":"Verify your account to continue.","status":"PERMISSION_DENIED"}}`}}}
b := &scripted{replies: []reply{{200, "", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
scriptedOn(t, "b", provider.Chat, b)
provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered})
s := New()
code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || a.n != 1 || b.n != 1 {
t.Fatalf("%d (a %d, b %d): %s", code, a.n, b.n, body)
}
r := s.trace.routes[len(s.trace.routes)-1]
if rest := r.Tries[0].Rest; r.Tries[0].Fail != failVerify || rest == nil || rest.By != "verify" || rest.Link != "" ||
!strings.Contains(rest.said, "open the Antigravity app") {
t.Fatalf("tries %+v", r.Tries)
}
if failure(403, []byte(`{"error":{"message":"The caller does not have permission"}}`)) == failVerify {
t.Fatal("any 403 read as a verification")
}
}
// Google's link reaches the agent and the Routing page's Verify whole: every
// query parameter, its & unescaped, whether the body wrote & or &.
func TestVerifyLinkWhole(t *testing.T) {
const link = "https://accounts.google.com/signin/continue?sarp=1&scc=1&continue=https://developers.google.com/gemini-code-assist/auth/auth_success_gemini%3Fsrc%3Dag&plt=AKgnsbvK-3x_Yq9Zt0lPq2vN7m8w&flowName=GlifWebSignIn&authuser=0"
for _, amp := range []string{"&", `&`} {
fresh(t)
u := strings.ReplaceAll(link, "&", amp)
body := `{"error":{"code":403,"message":"Verify your account to continue.","status":"PERMISSION_DENIED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"VALIDATION_REQUIRED","domain":"cloudcode-pa.googleapis.com","metadata":{"validation_error_message":"Verify your account to continue.","validation_url_link_text":"Verify your account","validation_url":"` + u + `"}},{"@type":"type.googleapis.com/google.rpc.Help","links":[{"description":"Further action is required to use this service. Navigate to the following URL to complete verification:\n\n` + u + `\n\nLearn more:\n\nhttps://support.google.com/accounts?p=al_alert\n","url":"` + u + `"},{"description":"Learn more","url":"https://support.google.com/accounts?p=al_alert"}]}]}}`
a := &scripted{replies: []reply{{403, "application/json", body}}}
scriptedOn(t, "a", provider.Chat, a)
s := New()
_, got := postAs(t, s, "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
if msg := provider.APIError([]byte(got), ""); !strings.Contains(msg, " open "+link+" in a browser") {
t.Fatalf("%s: %s", amp, msg)
}
if l, _ := provider.Verification([]byte(got)); l != link {
t.Fatalf("%s: read back %q", amp, l)
}
r := s.trace.routes[len(s.trace.routes)-1]
if rest := r.Tries[len(r.Tries)-1].Rest; rest == nil || rest.Link != link {
t.Fatalf("%s: rest %+v", amp, rest)
}
}
}
// The agy-cli plugin (plugins packages/agy-cli, usage.mjs verifyRefused)
// turns agy's bare "Verification Required" ending (kidgpt on X, 11-16) into
// a 403 in these words: the gateway reads them as Google's verification
// refusal, rests the account as "verify" with the link when there is one,
// and passes the words on once, not with its own advice added again.
func TestAgyCLIVerifyWords(t *testing.T) {
const link = "https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv"
const said = `agy bridge: Google wants this account verified before Antigravity serves it again (agy: \"Verification Required\") — this Google account needs to be verified: `
for _, c := range []struct{ tail, link string }{
{"open " + link + " in a browser signed in to it, verify it, then try again", link},
{"open the Antigravity app, or run `agy` in a terminal, signed in to it and do what it asks, then try again", ""},
} {
fresh(t)
body := `{"type":"error","error":{"type":"permission_error","message":"` + said + c.tail + `"}}`
a := &scripted{replies: []reply{{403, "application/json", body}}}
scriptedOn(t, "a", provider.Chat, a)
s := New()
code, got := postAs(t, s, "", `{"model":"a/m","messages":[{"role":"user","content":"hi"}]}`)
msg := provider.APIError([]byte(got), "")
if code != 403 || strings.Count(msg, "needs to be verified") != 1 || !strings.Contains(msg, c.tail) {
t.Fatalf("%d: %s", code, msg)
}
r := s.trace.routes[len(s.trace.routes)-1]
try := r.Tries[len(r.Tries)-1]
if try.Fail != failVerify || try.Rest == nil || try.Rest.By != "verify" || try.Rest.Link != c.link {
t.Fatalf("try %+v rest %+v", try, try.Rest)
}
}
}
// sameVerify says whether two answers to an account Google wants verified
// tell the same: Google's words, and the link to verify it at.
func sameVerify(a, b string) bool {
for _, w := range []string{"Verify your account to continue.", "open https://accounts.google.com/signin/continue?sarp=1&x=2 in a browser"} {
if !strings.Contains(a, w) || !strings.Contains(b, w) {
return false
}
}
return true
}