01huadalang on Discord (#feedback): three ZCode accounts used at once from one address got the address refused with a 405; two were served. They asked for the first account to be used until it runs out, then the next. The plugin now declares maxAccounts 2, on the auth hook and as magpie.maxAccounts in package.json, beside maxConcurrency. magpie (the release after this, its provider setting Accounts at once) sends requests to the first two accounts of the user's list only and holds the others in reserve; one that runs out or fails rests and the first in reserve takes its place. The user's own value on the provider wins, 0 for every account. A magpie that doesn't read the field uses every account, as before. README: "Two accounts at once". Run on mote: HOME=$(mktemp -d) bun test in packages/zcode (183 pass, 0 fail) and bun scripts/check.mjs (exit 0). Broken by hand: package.json's maxAccounts set to 3 turns "two accounts at once are declared, the same number twice" red. Not tried: real ZCode accounts (none used, per the task), and whether two at once is always under ZCode's limit; that is the reporter's finding, and the user can lower it to 1.
195 lines
9.1 KiB
JavaScript
195 lines
9.1 KiB
JavaScript
// ZCode serves one account a few requests at a time, and past that answers
|
|
// 429 code 3008 "user concurrency limit exceeded" or 3009 "model concurrency
|
|
// limit exceeded". magpie sends every agent's and every session's requests of
|
|
// this computer through this one plugin process and queues them by the number
|
|
// the plugin declares, so a request the vendor refuses anyway is asked again a
|
|
// moment later. The requests go to a local Bun.serve; nothing reaches z.ai or
|
|
// bigmodel.
|
|
import { afterAll, afterEach, beforeAll, beforeEach, expect, test } from "bun:test"
|
|
import { homedir, tmpdir } from "node:os"
|
|
import { readFileSync, realpathSync } from "node:fs"
|
|
import { ZCodeAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const { busy, spentUp, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS } = _internal
|
|
const WAITS = [...BUSY_WAITS]
|
|
|
|
let server
|
|
const got = []
|
|
let answer = null // what the fake answers, when set
|
|
beforeAll(() => {
|
|
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
|
|
server = Bun.serve({
|
|
port: 0,
|
|
async fetch(req) {
|
|
got.push({ path: new URL(req.url).pathname, headers: Object.fromEntries(req.headers), body: await req.text() })
|
|
if (answer) return answer()
|
|
return Response.json({ type: "message", content: [] })
|
|
},
|
|
})
|
|
})
|
|
afterAll(() => server?.stop(true))
|
|
|
|
// the plugin's requests to the plans go to the fake; anything else fails
|
|
const real = globalThis.fetch
|
|
const HOSTS = ["https://zcode.z.ai", "https://api.z.ai", "https://open.bigmodel.cn"]
|
|
function routeToFake() {
|
|
globalThis.fetch = (input, init) => {
|
|
const url = input instanceof Request ? input.url : String(input)
|
|
const host = HOSTS.find((h) => url.startsWith(h + "/"))
|
|
if (!host) throw new Error("no network in tests: " + url)
|
|
return real(`http://127.0.0.1:${server.port}/${new URL(host).host}${url.slice(host.length)}`, init)
|
|
}
|
|
}
|
|
beforeEach(() => {
|
|
routeToFake()
|
|
got.length = 0
|
|
answer = null
|
|
// the plans read is this process's: each test starts from none, as the
|
|
// other suites do (dual.test.mjs)
|
|
_internal.routes.clear()
|
|
_internal.blocked.clear()
|
|
})
|
|
afterEach(() => {
|
|
globalThis.fetch = real
|
|
answer = null
|
|
BUSY_WAITS.splice(0, BUSY_WAITS.length, ...WAITS)
|
|
})
|
|
|
|
const jwt = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig"
|
|
const DEVICE = "11111111-2222-4333-8444-555555555555"
|
|
const auth = () => ({ type: "oauth", access: jwt, refresh: JSON.stringify({ site: "https://zcode.z.ai", device: DEVICE, jwt, plan: "Start Plan" }), expires: 0 })
|
|
const BODY = JSON.stringify({ model: "GLM-5.3", max_tokens: 16, messages: [{ role: "user", content: "hi" }] })
|
|
|
|
// ask sends one request through the plugin and returns its answer unread
|
|
async function ask() {
|
|
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
const opts = await hooks.auth.loader(async () => auth(), { id: "zcode" })
|
|
const headers = { "content-type": "application/json", "x-api-key": "zcode", "anthropic-version": "2023-06-01", "user-agent": "ai-sdk/anthropic/3.0.81" }
|
|
return opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", headers, body: BODY })
|
|
}
|
|
|
|
// one is a whole request, read out, as an agent's client does
|
|
async function one() {
|
|
const res = await ask()
|
|
return { status: res.status, text: await res.text() }
|
|
}
|
|
|
|
const CAP = { code: 3008, msg: "user concurrency limit exceeded" }
|
|
|
|
test("a cap refusal is asked again and the answer served", async () => {
|
|
let n = 0
|
|
answer = () => (++n === 1 ? Response.json(CAP, { status: 429 }) : Response.json({ type: "message", content: [] }))
|
|
const res = await one()
|
|
expect(res.status).toBe(200)
|
|
expect(JSON.parse(res.text)).toEqual({ type: "message", content: [] })
|
|
// the same request, byte for byte, and no third ask
|
|
expect(got.length).toBe(2)
|
|
expect(got[1].path).toBe(got[0].path)
|
|
expect(got[1].body).toBe(got[0].body)
|
|
expect(got[1].headers["authorization"]).toBe(got[0].headers["authorization"])
|
|
expect(got[1].headers["x-zcode-app-version"]).toBe(got[0].headers["x-zcode-app-version"])
|
|
})
|
|
|
|
test("the cap's own codes are not a gift spent", () => {
|
|
expect(spentUp(JSON.stringify(CAP), 429)).toBe(false)
|
|
expect(spentUp('{"code":3009,"msg":"model concurrency limit exceeded"}', 429)).toBe(false)
|
|
expect(spentUp('{"error":{"code":"3008","message":"user concurrency limit exceeded"}}', 429)).toBe(false)
|
|
// a 429 that is a rate limit or a spent plan is as it was
|
|
expect(spentUp('{"code":1302,"msg":"rate limit reached"}', 429)).toBe(true)
|
|
expect(spentUp('{"error":{"code":"1113"}}', 429)).toBe(true)
|
|
expect(spentUp("", 429)).toBe(true)
|
|
expect(spentUp('{"code":1005}', 200)).toBe(true)
|
|
})
|
|
|
|
test("a rate limit is not asked again", async () => {
|
|
answer = () => Response.json({ code: 1302, msg: "rate limit reached" }, { status: 429 })
|
|
const res = await one()
|
|
expect(res.status).toBe(429)
|
|
expect(got.length).toBe(1)
|
|
expect(res.text).toContain("rate limit reached")
|
|
expect(res.text).not.toContain("ZCode serves one account")
|
|
})
|
|
|
|
test("a cap that outlasts the waits is said plainly, still a 429", async () => {
|
|
BUSY_WAITS.splice(0, BUSY_WAITS.length, 1, 1)
|
|
answer = () => Response.json(CAP, { status: 429 })
|
|
const res = await one()
|
|
expect(res.status).toBe(429)
|
|
expect(got.length).toBe(3)
|
|
const said = JSON.parse(res.text).msg
|
|
expect(said).toContain("user concurrency limit exceeded (429, code 3008)")
|
|
expect(said).toContain("ZCode serves one account a few requests at a time")
|
|
})
|
|
|
|
// bodyThatFails is this plugin's fetch answered with a head and one chunk,
|
|
// then a body that fails while it is being read: a connection the vendor
|
|
// dropped mid-body, or the abort magpie sends when an agent's request goes
|
|
// away
|
|
function bodyThatFails(status) {
|
|
let sent = 0
|
|
return new Response(
|
|
new ReadableStream({
|
|
pull(c) {
|
|
if (sent++ === 0) return c.enqueue(new TextEncoder().encode('{"msg":"half a body'))
|
|
c.error(new Error("the connection dropped mid-body"))
|
|
},
|
|
}),
|
|
{ status, headers: { "content-type": "application/json" } },
|
|
)
|
|
}
|
|
|
|
test("a body read that fails is the agent's error, and the account goes on", async () => {
|
|
// magpie aborts this fetch when the agent's request goes away, and the
|
|
// vendor's connection can drop mid-body: the failure goes back to the agent
|
|
// as the error it is, and the next request of the same account is served
|
|
const routed = globalThis.fetch
|
|
globalThis.fetch = async () => bodyThatFails(429)
|
|
expect(await ask().then(() => "served", () => "threw")).toBe("threw")
|
|
globalThis.fetch = routed
|
|
expect((await one()).status).toBe(200)
|
|
})
|
|
|
|
test("a failed read of the no-plan answer is the agent's error too", async () => {
|
|
// the other read a request takes: a key with no plan behind it reads what
|
|
// Z.ai said, and that read can fail the same way
|
|
const key = async () => {
|
|
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "sk-probe", metadata: { site: "zai" } }), { id: "zcode" })
|
|
return opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode" }, body: BODY })
|
|
}
|
|
const routed = globalThis.fetch
|
|
globalThis.fetch = async () => bodyThatFails(400)
|
|
expect(await key().then(() => "served", () => "threw")).toBe("threw")
|
|
globalThis.fetch = routed
|
|
expect((await key()).status).toBe(200)
|
|
})
|
|
|
|
test("the cap is read by its words when the envelope carries no code", () => {
|
|
expect(busy(429, '{"error":{"type":"rate_limit_error","message":"model concurrency limit exceeded"}}')).toBe(true)
|
|
expect(busy(200, JSON.stringify(CAP))).toBe(false)
|
|
expect(busy(429, "<html>blocked</html>")).toBe(false)
|
|
expect(busy(429, '{"code":1302,"msg":"rate limit reached"}')).toBe(false)
|
|
})
|
|
|
|
test("the account's cap is declared, and declared as the same number twice", async () => {
|
|
// the field magpie reads off the auth hook, and the one its package.json
|
|
// declares for a host that reads nothing else: magpie's queue spaces the
|
|
// requests by it. It is a default, not a cap the plugin enforces — a plan's
|
|
// own cap follows its tier, so a user raises it on the account instead
|
|
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"))
|
|
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
expect(hooks.auth.maxConcurrency).toBe(DEFAULT_CONCURRENCY)
|
|
expect(pkg.magpie.maxConcurrency).toBe(DEFAULT_CONCURRENCY)
|
|
})
|
|
|
|
test("two accounts at once are declared, the same number twice", async () => {
|
|
// 01huadalang on Discord: three ZCode accounts used at once from one
|
|
// address got it refused with a 405, two didn't. magpie keeps the first
|
|
// two of the user's list taking requests and the others in reserve
|
|
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"))
|
|
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
expect(DEFAULT_ACCOUNTS).toBe(2)
|
|
expect(hooks.auth.maxAccounts).toBe(DEFAULT_ACCOUNTS)
|
|
expect(pkg.magpie.maxAccounts).toBe(DEFAULT_ACCOUNTS)
|
|
})
|