From 986a4474feea2e422e121702e09b5a3cb0fd34ea Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 20:39:41 +0800 Subject: [PATCH] =?UTF-8?q?zcode:=20the=20Start=20Plan's=20405=20block=20i?= =?UTF-8?q?s=20said=20for=20what=20it=20is,=20not=20only=20as=20a=20firewa?= =?UTF-8?q?ll=20(#425:=20ZCode=20405=20Method=20Not=20Allowed=20on=20GLM-5?= =?UTF-8?q?.3-Flash=20from=20DeepSeek=20Harness,=20while=20zcode2api=20wor?= =?UTF-8?q?ks=20with=20the=20same=20account)=20=E2=80=94=20zcode.z.ai=20an?= =?UTF-8?q?swers=20a=20Start=20Plan=20request=20it=20turns=20away=20with?= =?UTF-8?q?=20405=20"request=20has=20been=20blocked=20due=20to=20unusual?= =?UTF-8?q?=20activity"=20(code=203012=20"method=20not=20allowed"),=20whic?= =?UTF-8?q?h=20magpie=20read=20as=20Alibaba=20Cloud's=20firewall=20blockin?= =?UTF-8?q?g=20the=20address=20and=20told=20the=20user=20to=20wait=20or=20?= =?UTF-8?q?switch=20network.=20zcode2api's=20own=20write-ups=20(a137460387?= =?UTF-8?q?/zcode2api=20"3012=20=E7=9A=84=E6=A0=B9=E5=9B=A0=E4=B8=8E?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D",=20gakiyukr/zcode2api-plus=20request.go)=20?= =?UTF-8?q?find=20the=20endpoint=20checks=20what=20a=20request=20carries?= =?UTF-8?q?=20and=20takes=20only=20one=20with=20ZCode's=20own=20system=20p?= =?UTF-8?q?rompt,=20which=20zcode2api=20injects;=20magpie's=20endpoint,=20?= =?UTF-8?q?method=20and=20protocol=20are=20the=20same=20as=20theirs,=20and?= =?UTF-8?q?=20magpie=20doesn't=20pretend=20to=20be=20ZCode.=20On=20a=20Sta?= =?UTF-8?q?rt=20Plan=20account=20the=20405=20/=203012=20/=20block=20page?= =?UTF-8?q?=20now=20reads=20"ZCode's=20Start=20Plan=20turns=20away=20reque?= =?UTF-8?q?sts=20that=20don't=20come=20from=20the=20ZCode=20app=20itself,?= =?UTF-8?q?=20and=20magpie=20doesn't=20pretend=20to=20be=20it;=20it=20can?= =?UTF-8?q?=20also=20be=20a=20network=20block=20of=20this=20IP.=20Use=20an?= =?UTF-8?q?=20account=20with=20a=20GLM=20Coding=20Plan,=20or=20add=20anoth?= =?UTF-8?q?er=20provider=20to=20this=20group",=20in=20place=20of=20the=20g?= =?UTF-8?q?eneric=20hint,=20to=20the=20agent=20and=20in=20the=20trace,=20w?= =?UTF-8?q?here=20the=20Routing=20page=20gives=20it=20on=20its=20own=20lin?= =?UTF-8?q?e=20in=20English=20or=20Chinese;=20the=20group=20still=20goes?= =?UTF-8?q?=20on=20to=20its=20next=20member.=20A=20Coding=20Plan=20account?= =?UTF-8?q?=20and=20every=20other=20provider=20keep=20the=20generic=20hint?= =?UTF-8?q?.=20Requests,=20headers=20and=20prompts=20are=20unchanged.=20Te?= =?UTF-8?q?stZCodeStartBlockedSaid=20and=20routing-zcode-blocked.test.cjs?= =?UTF-8?q?=20fail=20without=20the=20change.=20go=20vet,=20go=20build=20-t?= =?UTF-8?q?ags=20nogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go?= =?UTF-8?q?=20build,=20go=20test=20-tags=20nogui=20./...=20pass=20(TestUse?= =?UTF-8?q?rPath,=20a=20load=20flake,=20passes=20alone);=20routing-zcode-b?= =?UTF-8?q?locked,=20routing-blocked,=20routing-wb-refused,=20click-scroll?= =?UTF-8?q?,=20old-webkit=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/i18n.js | 1 + internal/gui/assets/routing.js | 5 +- internal/gui/tests/README.md | 11 +- .../gui/tests/routing-zcode-blocked.test.cjs | 104 ++++++++++++++++++ internal/provider/account.go | 5 + internal/provider/test.go | 2 +- internal/provider/zcode.go | 6 + internal/provider/zcode_blocked_test.go | 45 ++++++++ internal/provider/zcode_start.go | 20 ++++ 9 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 internal/gui/tests/routing-zcode-blocked.test.cjs create mode 100644 internal/provider/zcode_blocked_test.go diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 8bd412e97..3b7a3a702 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -1512,6 +1512,7 @@ const I18N = { "It said: {error}": "原话:{error}", "WorkBuddy refuses chats from Codex and Claude Code (their system prompt); use it from Hermes, OpenCode or Pi, or add another provider to this group": "WorkBuddy 会拒绝来自 Codex 和 Claude Code 的对话(因为它们的系统提示词);请在 Hermes、OpenCode 或 Pi 里使用它,或在这个分组里再加一个供应商", "the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy": "供应商的网络防火墙拦截了来自这个 IP 的请求;请稍等一会儿,或换个网络或代理", + "ZCode's Start Plan turns away requests that don't come from the ZCode app itself, and magpie doesn't pretend to be it; it can also be a network block of this IP. Use an account with a GLM Coding Plan, or add another provider to this group": "ZCode 体验套餐会拒绝不是来自 ZCode 客户端本身的请求,magpie 不会冒充它;也可能是网络拦截了这个 IP。请改用有 GLM Coding Plan 的账号,或在这个分组里加上其他供应商", "Show the latest request that failed": "查看最近一个失败的请求", "rate limited": "限流", "out of credit": "余额不足", diff --git a/internal/gui/assets/routing.js b/internal/gui/assets/routing.js index 7c96e152f..05967593a 100644 --- a/internal/gui/assets/routing.js +++ b/internal/gui/assets/routing.js @@ -486,7 +486,10 @@ const WB_REFUSED = "WorkBuddy refuses chats from Codex and Claude Code (their system prompt); use it from Hermes, OpenCode or Pi, or add another provider to this group"; // what it adds to a vendor's edge firewall's block page (provider.BlockedHint) const BLOCKED = "the provider's network firewall blocked requests from this IP; wait a while, or switch to another network or proxy"; - const HINTS = [WB_REFUSED, BLOCKED]; + // what it says of ZCode's Start Plan turning a request away (#425, + // provider.ZCodeStartBlockedHint), in place of BLOCKED + const ZCODE_BLOCKED = "ZCode's Start Plan turns away requests that don't come from the ZCode app itself, and magpie doesn't pretend to be it; it can also be a network block of this IP. Use an account with a GLM Coding Plan, or add another provider to this group"; + const HINTS = [WB_REFUSED, BLOCKED, ZCODE_BLOCKED]; function trySaid(r, i) { const tr = r.tries[i], w = tried(r, tr), agent = agentName(r.agent); diff --git a/internal/gui/tests/README.md b/internal/gui/tests/README.md index ecd711bb5..73c1e0ade 100644 --- a/internal/gui/tests/README.md +++ b/internal/gui/tests/README.md @@ -549,6 +549,15 @@ firewall blocked this IP; wait, or switch network or proxy) is on its own line in English and Chinese; another 405 gets none, and picking the request leaves the page where it is. Chromium and WebKit, API faked. +`routing-zcode-blocked.test.cjs` opens a request ZCode's Start Plan turned +away (#425: 405 "request has been blocked due to unusual activity") on the +Routing page: what the gateway made of it is given without the hint it adds, +and the hint (ZCode takes only its own app's requests and magpie doesn't +pretend to be it, or the network blocked this IP; use a GLM Coding Plan or +another provider in the group) is on its own line in English and Chinese; +another 405 gets none, and picking the request leaves the page where it is. +Chromium and WebKit, API faked. + `routing-shape.test.cjs` opens a group request whose first member answered 422 because its API couldn't read the request (#350: SuperGrok and Codex's additional_tools item) and whose next member answered: the Routing page says @@ -869,7 +878,7 @@ reads shows none of it. English and Chinese, Chromium and WebKit, with With Node.js and Playwright available: ```sh -node --test internal/gui/tests/menu-scroll.test.cjs internal/gui/tests/click-scroll.test.cjs internal/gui/tests/panel-fold.test.cjs internal/gui/tests/rollup-short.test.cjs internal/gui/tests/panel-routing.test.cjs internal/gui/tests/gateway-fold.test.cjs internal/gui/tests/routing-kind.test.cjs internal/gui/tests/balance-fix.test.cjs internal/gui/tests/cli-update.test.cjs internal/gui/tests/login-import.test.cjs internal/gui/tests/agy-launch.test.cjs internal/gui/tests/currency.test.cjs internal/gui/tests/usage-ledger.test.cjs internal/gui/tests/update-check.test.cjs internal/gui/tests/omarchy.test.cjs internal/gui/tests/brand.test.cjs internal/gui/tests/library-loading.test.cjs internal/gui/tests/provider-typed.test.cjs node --test internal/gui/tests/signin-callback.test.cjs internal/gui/tests/routing-steady.test.cjs internal/gui/tests/old-webkit.test.cjs internal/gui/tests/session-terminal.test.cjs internal/gui/tests/text-size.test.cjs internal/gui/tests/zcode-site.test.cjs internal/gui/tests/rail-tip.test.cjs internal/gui/tests/sync-update.test.cjs internal/gui/tests/plugin-signin.test.cjs internal/gui/tests/plugin-market.test.cjs internal/gui/tests/s3-sync.test.cjs internal/gui/tests/routing-wb-refused.test.cjs internal/gui/tests/routing-blocked.test.cjs internal/gui/tests/routing-shape.test.cjs internal/gui/tests/market-have.test.cjs internal/gui/tests/sessions-today.test.cjs internal/gui/tests/routing-side-calls.test.cjs internal/gui/tests/import-all-skills.test.cjs internal/gui/tests/routing-flood.test.cjs internal/gui/tests/routing-busy.test.cjs internal/gui/tests/own-skill.test.cjs internal/gui/tests/routing-manual.test.cjs internal/gui/tests/tray-usages.test.cjs internal/gui/tests/provider-remove.test.cjs internal/gui/tests/signin-link.test.cjs internal/gui/tests/tray-inuse.test.cjs internal/gui/tests/codex-auto-reset.test.cjs internal/gui/tests/routing-auto-reset.test.cjs internal/gui/tests/claude-usage-asked.test.cjs internal/gui/tests/claude-risk.test.cjs internal/gui/tests/library-toggle-status.test.cjs internal/gui/tests/library-all.test.cjs internal/gui/tests/routing-reroute-title.test.cjs internal/gui/tests/plain-names.test.cjs internal/gui/tests/panel-effort.test.cjs internal/gui/tests/agent-models-all-hidden.test.cjs internal/gui/tests/omp-roles.test.cjs internal/gui/tests/claude-effort.test.cjs internal/gui/tests/remote-magpie.test.cjs internal/gui/tests/qoder-cn-tile.test.cjs internal/gui/tests/provider-levels-scroll.test.cjs internal/gui/tests/signin-paste-codex.test.cjs internal/gui/tests/usage-alert.test.cjs internal/gui/tests/quota-families.test.cjs internal/gui/tests/provider-search.test.cjs internal/gui/tests/webdav-settings-refresh.test.cjs internal/gui/tests/lan-docker.test.cjs internal/gui/tests/plugin-key-hint.test.cjs internal/gui/tests/update-hide.test.cjs internal/gui/tests/tray-logos.test.cjs internal/gui/tests/routing-proxy.test.cjs internal/gui/tests/sse-body.test.cjs internal/gui/tests/remote-group-served.test.cjs internal/gui/tests/plugin-move-overflow.test.cjs internal/gui/tests/routing-time.test.cjs internal/gui/tests/unlisted-models.test.cjs internal/gui/tests/plugin-updates.test.cjs internal/gui/tests/request-archive.test.cjs internal/gui/tests/group-member-fast.test.cjs internal/gui/tests/providers-off-fold.test.cjs internal/gui/tests/account-return.test.cjs internal/gui/tests/usage-ledger-detail.test.cjs internal/gui/tests/usage-ledger-chart.test.cjs internal/gui/tests/panel-usage.test.cjs internal/gui/tests/usage-refresh.test.cjs internal/gui/tests/usage-ledger-content.test.cjs internal/gui/tests/claude-direct.test.cjs internal/gui/tests/signin-again.test.cjs internal/gui/tests/sessions-calendar-fill.test.cjs internal/gui/tests/editor-short-window.test.cjs internal/gui/tests/group-from-model-add.test.cjs internal/gui/tests/sync-other-kind.test.cjs internal/gui/tests/web-page.test.cjs internal/gui/tests/balance-parts.test.cjs internal/gui/tests/search-api.test.cjs internal/gui/tests/provider-file-error.test.cjs +node --test internal/gui/tests/menu-scroll.test.cjs internal/gui/tests/click-scroll.test.cjs internal/gui/tests/panel-fold.test.cjs internal/gui/tests/rollup-short.test.cjs internal/gui/tests/panel-routing.test.cjs internal/gui/tests/gateway-fold.test.cjs internal/gui/tests/routing-kind.test.cjs internal/gui/tests/balance-fix.test.cjs internal/gui/tests/cli-update.test.cjs internal/gui/tests/login-import.test.cjs internal/gui/tests/agy-launch.test.cjs internal/gui/tests/currency.test.cjs internal/gui/tests/usage-ledger.test.cjs internal/gui/tests/update-check.test.cjs internal/gui/tests/omarchy.test.cjs internal/gui/tests/brand.test.cjs internal/gui/tests/library-loading.test.cjs internal/gui/tests/provider-typed.test.cjs node --test internal/gui/tests/signin-callback.test.cjs internal/gui/tests/routing-steady.test.cjs internal/gui/tests/old-webkit.test.cjs internal/gui/tests/session-terminal.test.cjs internal/gui/tests/text-size.test.cjs internal/gui/tests/zcode-site.test.cjs internal/gui/tests/rail-tip.test.cjs internal/gui/tests/sync-update.test.cjs internal/gui/tests/plugin-signin.test.cjs internal/gui/tests/plugin-market.test.cjs internal/gui/tests/s3-sync.test.cjs internal/gui/tests/routing-wb-refused.test.cjs internal/gui/tests/routing-blocked.test.cjs internal/gui/tests/routing-zcode-blocked.test.cjs internal/gui/tests/routing-shape.test.cjs internal/gui/tests/market-have.test.cjs internal/gui/tests/sessions-today.test.cjs internal/gui/tests/routing-side-calls.test.cjs internal/gui/tests/import-all-skills.test.cjs internal/gui/tests/routing-flood.test.cjs internal/gui/tests/routing-busy.test.cjs internal/gui/tests/own-skill.test.cjs internal/gui/tests/routing-manual.test.cjs internal/gui/tests/tray-usages.test.cjs internal/gui/tests/provider-remove.test.cjs internal/gui/tests/signin-link.test.cjs internal/gui/tests/tray-inuse.test.cjs internal/gui/tests/codex-auto-reset.test.cjs internal/gui/tests/routing-auto-reset.test.cjs internal/gui/tests/claude-usage-asked.test.cjs internal/gui/tests/claude-risk.test.cjs internal/gui/tests/library-toggle-status.test.cjs internal/gui/tests/library-all.test.cjs internal/gui/tests/routing-reroute-title.test.cjs internal/gui/tests/plain-names.test.cjs internal/gui/tests/panel-effort.test.cjs internal/gui/tests/agent-models-all-hidden.test.cjs internal/gui/tests/omp-roles.test.cjs internal/gui/tests/claude-effort.test.cjs internal/gui/tests/remote-magpie.test.cjs internal/gui/tests/qoder-cn-tile.test.cjs internal/gui/tests/provider-levels-scroll.test.cjs internal/gui/tests/signin-paste-codex.test.cjs internal/gui/tests/usage-alert.test.cjs internal/gui/tests/quota-families.test.cjs internal/gui/tests/provider-search.test.cjs internal/gui/tests/webdav-settings-refresh.test.cjs internal/gui/tests/lan-docker.test.cjs internal/gui/tests/plugin-key-hint.test.cjs internal/gui/tests/update-hide.test.cjs internal/gui/tests/tray-logos.test.cjs internal/gui/tests/routing-proxy.test.cjs internal/gui/tests/sse-body.test.cjs internal/gui/tests/remote-group-served.test.cjs internal/gui/tests/plugin-move-overflow.test.cjs internal/gui/tests/routing-time.test.cjs internal/gui/tests/unlisted-models.test.cjs internal/gui/tests/plugin-updates.test.cjs internal/gui/tests/request-archive.test.cjs internal/gui/tests/group-member-fast.test.cjs internal/gui/tests/providers-off-fold.test.cjs internal/gui/tests/account-return.test.cjs internal/gui/tests/usage-ledger-detail.test.cjs internal/gui/tests/usage-ledger-chart.test.cjs internal/gui/tests/panel-usage.test.cjs internal/gui/tests/usage-refresh.test.cjs internal/gui/tests/usage-ledger-content.test.cjs internal/gui/tests/claude-direct.test.cjs internal/gui/tests/signin-again.test.cjs internal/gui/tests/sessions-calendar-fill.test.cjs internal/gui/tests/editor-short-window.test.cjs internal/gui/tests/group-from-model-add.test.cjs internal/gui/tests/sync-other-kind.test.cjs internal/gui/tests/web-page.test.cjs internal/gui/tests/balance-parts.test.cjs internal/gui/tests/search-api.test.cjs internal/gui/tests/provider-file-error.test.cjs ``` If Playwright is installed outside the repository, set `NODE_PATH` to the diff --git a/internal/gui/tests/routing-zcode-blocked.test.cjs b/internal/gui/tests/routing-zcode-blocked.test.cjs new file mode 100644 index 000000000..38ea177e9 --- /dev/null +++ b/internal/gui/tests/routing-zcode-blocked.test.cjs @@ -0,0 +1,104 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// ZCode's Start Plan turning a request away (#425: 405 "request has been +// blocked due to unusual activity", code 3012): the Routing page gives +// what the gateway made of it, and apart from that what it means (ZCode +// takes only its own app's requests, or the network blocked the address, +// and what to use instead), in the page's language. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); + +const assets = path.resolve(__dirname, "../assets"); +const now = new Date(); +const day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-"); +const at = (i) => new Date(now.getTime() - (i + 1) * 60e3).toISOString(); +const hint = "ZCode's Start Plan turns away requests that don't come from the ZCode app itself, and magpie doesn't pretend to be it; it can also be a network block of this IP. Use an account with a GLM Coding Plan, or add another provider to this group"; +const vendor = "ZCode: 405 Method Not Allowed"; +const key = { id: "zcode", provider: "zcode", name: "ZCode", kind: "provider", model: "GLM-5.3-Flash" }; +// newest first: the block, then another 405 with nothing to add +const errs = [vendor + " — " + hint, "ZCode: 405 Method Not Allowed", "", "", "", ""]; +const routes = errs.map((error, i) => { + const status = error ? 405 : 200; + return { + id: 100 - i, seq: 100 - i, time: at(i), agent: "claude", model: "zcode/GLM-5.3-Flash", provider: "zcode", + order: [key], tries: [{ id: key.id, model: key.model, start: at(i), done: true, status, ms: 400, ...(error ? { error } : {}) }], + done: true, status, ms: 400, ...(error ? { error } : {}), + }; +}); + +function serve(lang) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/settings.json", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + return async (route) => { + const url = new URL(route.request().url()); + const json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json(state); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3)); + return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + if (url.pathname === "/api/gateway/history") { + const d = url.searchParams.get("day"); + return json({ cut: false, days: [{ day, requests: routes.length }], routes: d ? routes : [] }); + } + if (url.pathname === "/api/groups") return json({ groups: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } }); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + await route.fulfill({ body: await fs.readFile(file), contentType }); + }; +} + +const want = { + en: { said: "It said: " + vendor, hint }, + zh: { said: "原话:" + vendor, hint: "ZCode 体验套餐会拒绝不是来自 ZCode 客户端本身的请求,magpie 不会冒充它;也可能是网络拦截了这个 IP。请改用有 GLM Coding Plan 的账号,或在这个分组里加上其他供应商" }, +}; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: ZCode's Start Plan block says what it means`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const context = await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang)); + t.after(async () => { + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-zcode-blocked.png`), fullPage: true }); + } + await browser.close(); + }); + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-day").nth(1).click(); + await page.locator(".rt-req").nth(errs.length - 1).waitFor(); + + const steps = async () => page.locator(".rt-steps li").evaluateAll((ls) => ls.map((l) => [l.className, l.textContent])); + const row = page.locator(".rt-req").nth(0); + const was = await row.evaluate((e) => e.getBoundingClientRect().top); + await row.click(); + await page.waitForTimeout(400); + assert(Math.abs((await row.evaluate((e) => e.getBoundingClientRect().top)) - was) <= 1, "picking the request moved the page"); + let got = await steps(); + // the vendor's words as they were, without magpie's hint in them + assert.deepEqual(got.filter(([c]) => c === "aside said").map(([, s]) => s), [want[lang].said]); + // and the hint on its own line, in the page's language + assert.deepEqual(got.filter(([, s]) => s === want[lang].hint).map(([c]) => c), ["aside"]); + + // another 405 gets no hint + await page.locator(".rt-req").nth(1).click(); + await page.waitForTimeout(300); + got = await steps(); + assert.equal(got.filter(([c]) => c === "aside said").length, 1); + assert(!got.some(([, s]) => s.includes("Start Plan") || s.includes("体验套餐")), JSON.stringify(got)); + assert.deepEqual(errors, []); + }); + } +} diff --git a/internal/provider/account.go b/internal/provider/account.go index b9b841c47..221dcd958 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -194,6 +194,11 @@ func (p Provider) Explain(msg string, status int, body []byte) string { return msg } if more := p.Account.explain(status, body); more != "" { + // an account's reading of a block page that names the network + // block itself (ZCodeStartBlockedHint) takes the generic one's place + if more == ZCodeStartBlockedHint { + msg = strings.TrimSuffix(msg, " — "+BlockedHint) + } return msg + " — " + more } return msg diff --git a/internal/provider/test.go b/internal/provider/test.go index 2aeb99a5c..4dc75e633 100644 --- a/internal/provider/test.go +++ b/internal/provider/test.go @@ -328,7 +328,7 @@ var edgeBlocked = regexp.MustCompile(`(?i)request has been blocked|errors\.aliyu // EdgeBlocked says whether an error body is a firewall's block page rather // than the vendor's API answering, or one already put in plain words. func EdgeBlocked(b []byte) bool { - return edgeBlocked.Match(b) || bytes.Contains(b, []byte(BlockedHint)) + return edgeBlocked.Match(b) || bytes.Contains(b, []byte(BlockedHint)) || bytes.Contains(b, []byte(ZCodeStartBlockedHint)) } // APIError pulls the human message out of an error body when there is one. diff --git a/internal/provider/zcode.go b/internal/provider/zcode.go index 2c92bed9c..dde8fb3b6 100644 --- a/internal/provider/zcode.go +++ b/internal/provider/zcode.go @@ -320,6 +320,12 @@ func zcodeProvider(who, plan string, k zcodeKey) Provider { req.Header.Set("Authorization", "Bearer "+key) return nil } + acct.explain = func(status int, body []byte) string { + if zcodeOnStart(nil, k) { + return zcodeStartExplain(status, body) + } + return "" + } acct.models = func() []catalog.Model { if zcodeOnStart(nil, k) { return zcodeStartModels diff --git a/internal/provider/zcode_blocked_test.go b/internal/provider/zcode_blocked_test.go new file mode 100644 index 000000000..df69cf051 --- /dev/null +++ b/internal/provider/zcode_blocked_test.go @@ -0,0 +1,45 @@ +package provider + +import ( + "net/http" + "strings" + "testing" +) + +// zcodeBlockPage is the page zcode.z.ai answers a Start Plan request it +// turns away with (#425). +const zcodeBlockPage = `405 +

Sorry, your request has been blocked due to unusual activity.

+errors.aliyun.com` + +// The Start Plan's block is said for what it is (#425): ZCode turns away +// requests that aren't its app's own, or the network blocked the address, +// and what to use instead; not only the firewall. A Coding Plan account, +// and any other provider, keep the generic hint. +func TestZCodeStartBlockedSaid(t *testing.T) { + say := func(p Provider, status int, body string) string { + return p.Explain(p.Name+": "+APIError([]byte(body), "405 Method Not Allowed"), status, []byte(body)) + } + start := zcodeProvider("trial@example.com", "Start Plan", zcodeKey{JWT: "a.b.c"}) + if got, want := say(start, http.StatusMethodNotAllowed, zcodeBlockPage), "ZCode: 405 Method Not Allowed — "+ZCodeStartBlockedHint; got != want { + t.Fatalf("Start Plan block:\n got %q\nwant %q", got, want) + } + if got := say(start, http.StatusMethodNotAllowed, `{"code":3012,"msg":"method not allowed"}`); !strings.HasSuffix(got, " — "+ZCodeStartBlockedHint) || strings.Contains(got, BlockedHint) { + t.Fatalf("Start Plan 3012: %q", got) + } + if got := say(start, http.StatusTooManyRequests, `{"code":1302,"msg":"rate limited"}`); strings.Contains(got, ZCodeStartBlockedHint) { + t.Fatalf("a rate limit got the block hint: %q", got) + } + if !EdgeBlocked([]byte(say(start, http.StatusMethodNotAllowed, zcodeBlockPage))) { + t.Fatal("the Start Plan's hint, relayed, isn't taken as a block") + } + + plan := zcodeProvider("pro@example.com", "GLM Coding Plan", zcodeKey{Key: "id.secret", Base: "https://api.z.ai/api/anthropic"}) + if got, want := say(plan, http.StatusMethodNotAllowed, zcodeBlockPage), "ZCode: 405 Method Not Allowed — "+BlockedHint; got != want { + t.Fatalf("Coding Plan block:\n got %q\nwant %q", got, want) + } + other := Provider{ID: "za", Name: "ZA"} + if got, want := say(other, http.StatusMethodNotAllowed, zcodeBlockPage), "ZA: 405 Method Not Allowed — "+BlockedHint; got != want { + t.Fatalf("another provider:\n got %q\nwant %q", got, want) + } +} diff --git a/internal/provider/zcode_start.go b/internal/provider/zcode_start.go index f885143e6..b4fecf8a2 100644 --- a/internal/provider/zcode_start.go +++ b/internal/provider/zcode_start.go @@ -23,6 +23,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "runtime" "strconv" "strings" @@ -47,6 +48,25 @@ var zcodeStartModels = func() []catalog.Model { return out }() +// ZCodeStartBlockedHint is what the Start Plan's "request has been blocked +// due to unusual activity" (HTTP 405, code 3012 "method not allowed") +// means (#425): zcode.z.ai looks at what a request carries and turns away +// one that isn't the ZCode app's own, its system prompt and all, as +// zcode2api found; it can still be Alibaba Cloud's firewall blocking the +// address (BlockedHint). magpie sends the agent's request as it is. +const ZCodeStartBlockedHint = "ZCode's Start Plan turns away requests that don't come from the ZCode app itself, and magpie doesn't pretend to be it; it can also be a network block of this IP. Use an account with a GLM Coding Plan, or add another provider to this group" + +// zcodeStartRefused matches that refusal: the block page, or its code. +var zcodeStartRefused = regexp.MustCompile(`(?i)unusual activity|"code"\s*:\s*"?3012\b`) + +// zcodeStartExplain gives ZCodeStartBlockedHint for the Start Plan's block. +func zcodeStartExplain(status int, body []byte) string { + if status >= 400 && (status == http.StatusMethodNotAllowed || EdgeBlocked(body) || zcodeStartRefused.Match(body)) { + return ZCodeStartBlockedHint + } + return "" +} + // ---- which plan ----------------------------------------------------------------- var zcodeRoutes = struct {