Since 10-24 many ZCode Start Plan accounts get 405 {"code":3012,"msg":
"request has been blocked due to unusual activity."} on every request.
On several of them ZCode's own app (3.16.3) gets the same 405, and
ZCode's support bot calls 3012 risk control on the account. magpie said
"though magpie sends it as the ZCode app does; it can be a network block
of this IP", rested the account as an unknown failure, and asked it again
soon after.
- provider.ZCodeRestrictedHint: the words @magpie-community/opencode-
zcode-auth 0.1.27 puts after ZCode's own ("ZCode's risk control blocked
this account for unusual activity (code 3012); it rests, and the
group's other providers are used. If the ZCode app fails on this
account too, only ZCode support can lift the block").
- gateway: restrictedSaid reads those words as failRestricted, as
WorkBuddy's 11140: the account rests restrictedRest (2h), the next
member is asked, and the Routing page's lift button brings it back.
- Routing page: the hint replaces the old one, in zh, zh-TW, ja and de.
- zcode mover min 0.1.27 (npm view --prefer-online: 0.1.27).
Not proven: why ZCode flags these accounts. Diffed against ZCode 3.16.3's
glm/zcode.cjs: the Start Plan goes unsigned there too (client request
signing is off for zhipu-account start-plan), and the headers match the
plugin's. Not tried with a real blocked account.
Run (sandbox HOME, at this tree): go test ./internal/provider ok;
go test -tags nogui ./internal/gui/... ok; go test ./internal/gateway
-run 'ZCode|Restricted|Blocked|EdgeBlocked|Moved' ok, and the new
TestZCodeStartPlanBlockRestsRestricted is red with routing.go reverted;
go vet darwin, linux, windows on provider and gateway ok; gofmt clean.
GUI (Playwright 1.62.0, chromium + webkit): routing-zcode-blocked,
i18n-keys, routing-blocked, routing-wb-restricted, gui-de, gui-ja,
gui-zh-tw: 41 pass, 0 fail. Not run: the full ./... suite (the machine
had a queue of full suites at load ~120).
166 lines
6.7 KiB
Go
166 lines
6.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// countAsked is an upstream that counts tokens, noting each time it's asked.
|
|
type countAsked struct {
|
|
mu sync.Mutex
|
|
paths []string
|
|
}
|
|
|
|
func (c *countAsked) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
c.mu.Lock()
|
|
c.paths = append(c.paths, r.URL.Path)
|
|
c.mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"input_tokens":999}`)
|
|
}
|
|
|
|
func (c *countAsked) asked() []string {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return append([]string(nil), c.paths...)
|
|
}
|
|
|
|
func estimated(t *testing.T, rec *httptest.ResponseRecorder) int {
|
|
t.Helper()
|
|
var got struct {
|
|
InputTokens int `json:"input_tokens"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &got); rec.Code != 200 || err != nil || got.InputTokens == 0 {
|
|
t.Fatalf("count_tokens: %d %s", rec.Code, rec.Body)
|
|
}
|
|
return got.InputTokens
|
|
}
|
|
|
|
// Claude Code asks count_tokens before each turn; ZCode itself never does,
|
|
// and zcode.z.ai answers it with an error every time. A ZCode account's
|
|
// count is estimated, the vendor not asked.
|
|
func TestZCodeCountTokensEstimated(t *testing.T) {
|
|
fresh(t)
|
|
up := &countAsked{}
|
|
srv := httptest.NewServer(up)
|
|
t.Cleanup(srv.Close)
|
|
p := provider.Provider{ID: "zcode", Name: "ZCode", Anthropic: srv.URL + "/api/v1/zcode-plan/anthropic", Models: []string{"glm-5.1"},
|
|
Account: &provider.Account{Agent: "zcode", User: "trial@example.com", Plan: "Start Plan"}}
|
|
body := []byte(`{"model":"glm-5.1","messages":[{"role":"user","content":"hello there"}]}`)
|
|
rec := httptest.NewRecorder()
|
|
New().countOn(rec, httptest.NewRequest("POST", "/v1/messages/count_tokens", nil), p, "glm-5.1", true, body)
|
|
if n := estimated(t, rec); n == 999 || len(up.asked()) > 0 {
|
|
t.Fatalf("zcode.z.ai was asked to count: %v, %d", up.asked(), n)
|
|
}
|
|
}
|
|
|
|
// ZCode moved onto its plugin estimates as the built-in does.
|
|
func TestMovedZCodeCountTokensEstimated(t *testing.T) {
|
|
up := &countAsked{}
|
|
movedFake(t, "zcode", up)
|
|
rec := httptest.NewRecorder()
|
|
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages/count_tokens",
|
|
strings.NewReader(`{"model":"zcode/fake-claude","messages":[{"role":"user","content":"hello there"}]}`)))
|
|
if n := estimated(t, rec); n == 999 || len(up.asked()) > 0 {
|
|
t.Fatalf("the vendor was asked to count: %v, %d", up.asked(), n)
|
|
}
|
|
}
|
|
|
|
// edgeBlockPage is Alibaba Cloud's ESA block page, as zcode.z.ai served it.
|
|
const edgeBlockPage = `<!DOCTYPE html>
|
|
<html><head><meta charset="utf-8"><title>405</title></head>
|
|
<body><div class="main"><h1>Sorry, your request has been blocked due to unusual activity.</h1>
|
|
<p>Request ID: 0bd6a2b417592xxxx</p>
|
|
<a href="https://errors.aliyun.com/error/405?code=blocked">errors.aliyun.com</a></div></body></html>`
|
|
|
|
// A vendor's edge firewall blocking this address answers an HTML page: the
|
|
// agent and the trace are told so in plain words, not the page, and the
|
|
// group's next member is asked, as for another provider's failure.
|
|
func TestEdgeBlockedSaidPlainly(t *testing.T) {
|
|
fresh(t)
|
|
blocked := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
io.WriteString(w, edgeBlockPage)
|
|
}))
|
|
t.Cleanup(blocked.Close)
|
|
var other int
|
|
fine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
other++
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"ok","choices":[{"message":{"role":"assistant","content":"from b"}}]}`)
|
|
}))
|
|
t.Cleanup(fine.Close)
|
|
for _, p := range []provider.Provider{
|
|
{ID: "za", Name: "ZA", Key: "k", Models: []string{"m"}, Chat: blocked.URL + "/v1"},
|
|
{ID: "zb", Name: "ZB", Key: "k", Models: []string{"m"}, Chat: fine.URL + "/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// alone: the agent is told what happened
|
|
s := New()
|
|
code, reply := postAs(t, s, "", `{"model":"za/m","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != http.StatusMethodNotAllowed || !strings.Contains(reply, provider.BlockedHint) || strings.Contains(reply, "<") {
|
|
t.Fatalf("blocked reply: %d %s", code, reply)
|
|
}
|
|
if tr := s.trace.routes[len(s.trace.routes)-1].Tries[0]; !strings.HasSuffix(tr.Error, " — "+provider.BlockedHint) || strings.Contains(tr.Error, "<") {
|
|
t.Fatalf("trace: %+v", tr)
|
|
}
|
|
|
|
// in a group: the next member answers
|
|
if err := provider.SaveGroup(provider.Group{Name: "Z", Members: []string{"za/m", "zb/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s = New()
|
|
code, reply = postAs(t, s, "", `{"model":"group/z","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != http.StatusOK || !strings.Contains(reply, "from b") || other != 1 {
|
|
t.Fatalf("group reply: %d %s (next asked %d)", code, reply, other)
|
|
}
|
|
tries := s.trace.routes[len(s.trace.routes)-1].Tries
|
|
if len(tries) != 2 || tries[0].Rest == nil || !strings.Contains(tries[0].Error, provider.BlockedHint) {
|
|
t.Fatalf("tries: %+v", tries)
|
|
}
|
|
}
|
|
|
|
// The ZCode plugin's answer to the Start Plan's 405 code 3012 (0.1.27 on,
|
|
// plugins#348) is an account ZCode's risk control blocked: it rests two
|
|
// hours as restricted, the account rather than one model, and is lifted
|
|
// by hand like WorkBuddy's. The words 0.1.26 gave the same 405 read as any
|
|
// failure does.
|
|
func TestZCodeStartPlanBlockRestsRestricted(t *testing.T) {
|
|
fresh(t)
|
|
s := New()
|
|
p := provider.Provider{ID: "zcode", Name: "ZCode", Account: &provider.Account{Agent: "plugin", User: "u1"}}
|
|
c := candidate{p: p, model: "GLM-5.3-Flash", rest: p.ID + "#u1"}
|
|
body := []byte(`{"type":"error","error":{"type":"permission_error","message":"request has been blocked due to unusual activity. (405, code 3012) — ` + provider.ZCodeRestrictedHint + `"}}`)
|
|
now := time.Now()
|
|
rest := s.restAfter(c, 405, nil, body)
|
|
if rest.Why != failRestricted || rest.By != "restricted" || rest.Key != c.restKey() {
|
|
t.Fatalf("rests %+v", rest)
|
|
}
|
|
if d := restedFor(rest, now); d < restrictedRest-time.Minute || d > restrictedRest+time.Minute {
|
|
t.Fatalf("rests %v", d)
|
|
}
|
|
if !s.Unrest(rest.Key) {
|
|
t.Fatal("its rest can't be lifted")
|
|
}
|
|
old := []byte(`{"type":"error","error":{"type":"permission_error","message":"405 Method Not Allowed — ZCode's Start Plan still turned this request away, though magpie sends it as the ZCode app does; it can be a network block of this IP, or ZCode checking for something new. Use an account with a GLM Coding Plan, or add another provider to this group"}}`)
|
|
if f := failureOf(c, 405, old); f == failRestricted {
|
|
t.Fatal("0.1.26's words read as restricted")
|
|
}
|
|
if f := failureOf(c, 200, body); f == failRestricted {
|
|
t.Fatal("a 200 read as restricted")
|
|
}
|
|
}
|