From 65d6b1dee98046c68e40d56fbcf7fa2b4265d369 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 11:12:52 +0800 Subject: [PATCH] provider: ZCode's 3012 risk control is not told as an IP firewall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit windy_66454 (Discord, 10-31, v0.1.2297) had every request to a ZCode group answered "ZCode: 405 Method Not Allowed — the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy", and both members rested together on the account as "why other". The plugin tells ZCode's Start Plan 3012 ("request has been blocked due to unusual activity", the account's risk control) apart from Alibaba Cloud's block page: 3012 carries ZCodeRestrictedHint. APIError matched "request has been blocked" with edgeBlocked first and rewrote the whole body to " — BlockedHint", so the restricted hint was cut, the user was told to change networks, and the gateway rested the account with failOther's backoff instead of failRestricted. A body that already carries ZCodeRestrictedHint or WBRestrictedHint now keeps its words. The reporter's text is what both cases read as before this, so which one they met is not known from the text. Run (mote, at this tree): - TestZCodeRiskControlNotToldAsFirewall: red on the old provider/test.go (told "ZCode: 405 Method Not Allowed — the provider's network firewall ..."), green now; its block-page case still says BlockedHint and rests as failOther. - go test ./internal/gateway -run 'ZCode|Edge|Relay|Restricted|WorkBuddy|Blocked|Refusal' and go test ./internal/provider: ok. - gofmt -l clean; go vet darwin/linux/windows of gateway and provider ok. Not tried: the reporter's real account; no ZCode account answers 3012 here. --- internal/gateway/zcode_blocked_test.go | 51 ++++++++++++++++++++++++++ internal/provider/test.go | 15 +++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/internal/gateway/zcode_blocked_test.go b/internal/gateway/zcode_blocked_test.go index 0cef940e6..308aa42f1 100644 --- a/internal/gateway/zcode_blocked_test.go +++ b/internal/gateway/zcode_blocked_test.go @@ -284,3 +284,54 @@ func TestZCodeStartPlanBlockRestsRestricted(t *testing.T) { t.Fatal("a 200 read as restricted") } } + +// The ZCode plugin's 3012 answer keeps ZCode's own words, "request has +// been blocked due to unusual activity", in front of ZCodeRestrictedHint. +// Served through the plugin, it reaches the agent and the trace with the +// hint, never as a firewall blocking the address (BlockedHint), and the +// account rests as restricted. Before, the gateway read ZCode's words as +// Alibaba Cloud's block page, told the agent to change networks and +// rested the account as any failure (windy_66454 on Discord, 10-31). The +// plugin's words for the page itself are still the firewall's. +func TestZCodeRiskControlNotToldAsFirewall(t *testing.T) { + for _, tc := range []struct { + name, body, says, notSays, why string + }{ + {"risk control", `{"type":"error","error":{"type":"permission_error","message":"request has been blocked due to unusual activity. (405, code 3012) — ` + provider.ZCodeRestrictedHint + `"}}`, provider.ZCodeRestrictedHint, provider.BlockedHint, failRestricted}, + {"block page", `{"type":"error","error":{"type":"permission_error","message":"405 Method Not Allowed — ` + provider.BlockedHint + `"}}`, provider.BlockedHint, provider.ZCodeRestrictedHint, failOther}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("FAKE_ZCODE", "1") + movedFake(t, "zcode", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusMethodNotAllowed) + io.WriteString(w, tc.body) + })) + p, err := provider.Find("zcode") + if err != nil { + t.Fatal(err) + } + c := candidate{p: *p, rest: "zcode", model: "GLM-5.3"} + b := &scripted{replies: []reply{{200, "", chatOK}}} + scriptedOn(t, "b", provider.Chat, b) + if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"zcode/GLM-5.3", "b/m"}, Routing: provider.Ordered}); err != nil { + t.Fatal(err) + } + s := New() + if code, body := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`); code != 200 { + t.Fatalf("group request: %d %s", code, body) + } + tries := s.trace.routes[len(s.trace.routes)-1].Tries + if len(tries) != 2 || tries[0].Rest == nil { + t.Fatalf("tries: %+v", tries) + } + tr := tries[0] + if !strings.Contains(tr.Error, tc.says) || strings.Contains(tr.Error, tc.notSays) { + t.Fatalf("told %q", tr.Error) + } + if tr.Fail != tc.why || tr.Rest.Why != tc.why || tr.Rest.Key != c.restKey() { + t.Fatalf("failed %q, rested %+v", tr.Fail, *tr.Rest) + } + }) + } +} diff --git a/internal/provider/test.go b/internal/provider/test.go index 40fcb060a..ea8ce4674 100644 --- a/internal/provider/test.go +++ b/internal/provider/test.go @@ -880,12 +880,25 @@ func EdgeBlocked(b []byte) bool { return edgeBlocked.Match(b) || bytes.Contains(b, []byte(BlockedHint)) } +// restrictedHinted says an error body is one a plugin already put in +// magpie's words as an account its vendor restricted: ZCode's Start Plan +// 3012 (ZCodeRestrictedHint) keeps ZCode's own "request has been blocked +// due to unusual activity" in front of the hint, which edgeBlocked reads +// as a firewall's page. Told as that, the hint was cut, the agent was told +// to change networks, and the gateway rested the account as any failure, +// never as restricted: the 405 windy_66454 met (Discord, 10-31) read +// "405 Method Not Allowed — " BlockedHint, the same words ZCode's 3012 +// and Alibaba Cloud's page were both told in. +func restrictedHinted(b []byte) bool { + return bytes.Contains(b, []byte(ZCodeRestrictedHint)) || bytes.Contains(b, []byte(WBRestrictedHint)) +} + // APIError pulls the human message out of an error body when there is one. // A firewall's block page is put in plain words (BlockedHint); Google's // "verify your account" refusal also says what to do about it, with the // link it gave. func APIError(b []byte, fallback string) string { - if edgeBlocked.Match(b) { + if edgeBlocked.Match(b) && !restrictedHinted(b) { if said, ok := WebPage(b); ok && fallback != "" { return fallback + ": " + said + " — " + BlockedHint }