Files
plugins/packages/zcode/concurrency.test.mjs
yetone 36dc4007fc zcode 0.1.68: every account takes requests, the Start Plan held to six in flight from this address
mathfournity on X: with several ZCode accounts in magpie, requests went to
two of them only. 0.1.40 declared maxAccounts 2 after 01huadalang's address
was refused with a 405 while three accounts took requests at once, so
magpie kept the first two of the list taking requests and held every other
account in reserve until one rested. A user with five gift accounts saw
two used.

What zcode.z.ai's edge firewall counts is the address's Start Plan
requests in flight (busyHold's logs: ~10 in flight against a cap of three
before the IP's 405), not how many accounts sent them. So the plugin no
longer declares maxAccounts, and holds that instead: besides each
account's three (startGate), every account's Start Plan requests share
one address lane of six, what two accounts of three each sent while the
two-account default stood. A request takes its account's slot first, then
the address's, within the one 120s wait; a wait that runs out on the
address's is answered here with a 429 that says so, nothing sent, and
lets the account's slot go. Free first counts the address lane full as
it does the account's. The coding plan, on api.z.ai, is held to none of
it. A user's own Accounts at once is kept as they set it.

Run:
- HOME=$(mktemp -d) bun test in packages/zcode on the Linux box
  (wsl-run, Bun 1.4.2): 275 pass, 0 fail.
- Broken by hand: the new tests against origin/main's index.mjs and
  package.json: 3 red (maxAccounts 2 declared; peak 12 in flight over
  four accounts where six are allowed; the address wait never runs out).
- magpie's gateway with this plugin under bun and four accounts on a fake
  Z.ai (a throwaway test, not committed, on mote): 12 requests at once
  went 3/3/3/3 to the four accounts, where 0.1.67 sent 6/6 to two and
  none to the others; 1302, 1308, 3008 and 500 on one account each went
  to the next and rested the first, as before.
- start.test's in-flight hold test parks twelve accounts' requests at
  once; it raises the address lane to twelve, since the hold's handling
  of such a run is what it tests.

Not tried: a real ZCode account; whether zcode.z.ai refuses an address
for the number of accounts as well as for requests in flight.
2026-11-17 01:24:45 +08:00

198 lines
9.2 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 } = _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)
// a reply a test leaves unread keeps its Start Plan slot (startGate)
_internal.startGate.lanes.clear()
_internal.startGate.narrow.clear()
_internal.startGate.now = () => Date.now()
})
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("no accounts at once are declared, so magpie uses every account", async () => {
// mathfournity on X: with maxAccounts two, a user's other gift accounts
// sat in reserve. The address's Start Plan requests in flight are held
// instead (inflight.test.mjs), over every account
const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8"))
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
expect(hooks.auth.maxAccounts).toBeUndefined()
expect(pkg.magpie.maxAccounts).toBeUndefined()
})