zcode 0.1.57: the busy hold's answer arms no quota rest (#859)
A busy hold (0.1.54) armed while a Start Plan request was in flight
answers that request with the hold's own 429 from askBusy. It carries no
3008, so busy() does not claim it and spentUp counts it as a spent gift.
On a -Trial pick that rested the model in `blocked` for a minute, and
the next -Trial asks were answered locally "over its quota ... exhausted"
with quota left. Under free first it rested the model the same way
before the replay on the plan.
busyHeldAnswer now sets `x-magpie-busy-hold: 1`, and the -Trial and
free-first gift paths read it before spentUp: the hold's answer passes on
as the rate limit it is and arms no rest. Free first still replays a
held answer on the plan, through the plain >= 400 branch. The gate's
queue answer and the hold's early returns never reach those paths.
Run at the reviewed head e5858d7b merged with origin/main 03ab6d66, on
mote (Bun 1.4.2, HOME and TMPDIR inside the run folder):
- bun test packages/zcode: 236 pass, 0 fail.
- bun scripts/check.mjs zcode: ok.
- The new test with main's index.mjs: red at
blocked.get(...glm-5.3-flash) toBeUndefined (received a timestamp).
- Broken by hand: the free-first guard removed alone leaves all 236
green, so only the -Trial guard has a test.
npm had 0.1.56 before the merge; this is 0.1.57.
This commit is contained in:
@@ -404,6 +404,55 @@ test("a rested -Trial entry's local 429 ends with the words magpie reads", async
|
||||
expect(said).toContain("not asked again for a minute")
|
||||
})
|
||||
|
||||
// 11-08: a busy hold armed while a -Trial request was in flight answers it
|
||||
// with the hold's own 429 (askBusy's in-flight stop), and that answer has
|
||||
// no 3008 in it. spentUp counts every 429 that busy() does not claim as a
|
||||
// spent bucket, so the hold's answer armed the blocked map — the model then
|
||||
// answered "over its quota" for a minute with half its free quota left.
|
||||
// The hold's answer must pass through the -Trial path untouched and arm
|
||||
// nothing.
|
||||
test("a busy hold's answer through a -Trial entry is passed on and arms no quota rest", async () => {
|
||||
planAndBalance()
|
||||
const waits = _internal.BUSY_WAITS.splice(0)
|
||||
_internal.BUSY_WAITS.push(1)
|
||||
try {
|
||||
let caps = 0
|
||||
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = () => {
|
||||
caps++
|
||||
return new Response(JSON.stringify({ code: "3008", msg: "user concurrency limit exceeded" }), { status: 429, headers: { "content-type": "application/json" } })
|
||||
}
|
||||
const opts = await freePlugin({ freeFirst: true }).then((h) => h.loader(async () => coding, { id: "zcode" }))
|
||||
const ask = (model) => opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body(model) })
|
||||
// five asks in flight: the first ask of each caps once (one signal per
|
||||
// request), the re-ask of each caps again — the fifth distinct cap hit
|
||||
// arms the hold, and every ask that wakes after it comes home the
|
||||
// hold's 429
|
||||
const all = await Promise.all([1, 2, 3, 4, 5].map(() => ask("GLM-5.3-Flash-Trial")))
|
||||
expect(all.map((r) => r.status)).toEqual([429, 429, 429, 429, 429])
|
||||
expect(_internal.busyHold.until).toBeGreaterThan(Date.now())
|
||||
for (const r of all) {
|
||||
const said = JSON.parse(await r.text())
|
||||
const text = said.error?.message ?? said.msg ?? ""
|
||||
// every answer is the hold's own, or the vendor's cap as it came —
|
||||
// never a quota exhaustion the gift never said
|
||||
expect(text.includes("turned away 5 requests") || said.code === "3008").toBe(true)
|
||||
expect(text).not.toContain("over its quota")
|
||||
expect(text).not.toContain("exhausted or temporarily unavailable")
|
||||
}
|
||||
// the hold's answer is no spent answer: the gift stays usable
|
||||
expect(_internal.blocked.get(KEY + "\0" + jwt + "\0glm-5.3-flash")).toBeUndefined()
|
||||
// and the next -Trial ask really goes to the gift once the hold is off
|
||||
_internal.busyHold.until = 0
|
||||
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] })
|
||||
sent.length = 0
|
||||
expect((await ask("GLM-5.3-Flash-Trial")).status).toBe(200)
|
||||
expect(sent.filter((c) => c.origin === "https://zcode.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1)
|
||||
} finally {
|
||||
_internal.BUSY_WAITS.length = 0
|
||||
_internal.BUSY_WAITS.push(...waits)
|
||||
}
|
||||
})
|
||||
|
||||
test("magpie's free-first switch is declared on, and off keeps the plain model on the plan", async () => {
|
||||
planAndBalance()
|
||||
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] })
|
||||
|
||||
@@ -2158,6 +2158,15 @@ function busyCap() {
|
||||
}
|
||||
}
|
||||
|
||||
// BUSY_HOLD_MARK marks an answer the hold made up: a request the gate or the
|
||||
// hold stopped locally never reached ZCode, and it says nothing about the
|
||||
// gift's quota. The gift paths (a -Trial pick, free first) read the mark
|
||||
// before they judge the answer spent, so a hold's 429 passes through as the
|
||||
// rate limit it is and arms no minute's rest (11-08: the hold's answer
|
||||
// through a -Trial entry read as "over its quota" with half the free quota
|
||||
// left).
|
||||
const BUSY_HOLD_MARK = "x-magpie-busy-hold"
|
||||
|
||||
function busyHeldAnswer() {
|
||||
const wait = Math.max(1, Math.ceil((busyHold.until - busyHold.now()) / 1000))
|
||||
const error = {
|
||||
@@ -2165,7 +2174,8 @@ function busyHeldAnswer() {
|
||||
message: `ZCode turned away ${BUSY_N} requests in the last minute as too many requests at once, each again when asked a second later. So that ZCode's firewall does not block this network, magpie sends no free Start Plan requests for the next ${wait}s. Wait, run fewer agents or subagents on ZCode at once, or use a GLM Coding Plan model`,
|
||||
}
|
||||
return new Response(JSON.stringify({ type: "error", error }), {
|
||||
status: 429, statusText: STATUS_TEXT[429], headers: { "content-type": "application/json", "retry-after": String(wait) },
|
||||
status: 429, statusText: STATUS_TEXT[429],
|
||||
headers: { "content-type": "application/json", "retry-after": String(wait), [BUSY_HOLD_MARK]: "1" },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3067,7 +3077,11 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
||||
const json = (res.headers.get("content-type") ?? "").includes("application/json")
|
||||
if (res.status >= 400 || json) {
|
||||
const text = await res.text()
|
||||
const spent = spentUp(text, res.status)
|
||||
// the hold's own answer (BUSY_HOLD_MARK) is a rate limit the
|
||||
// gift never said: it passes through as it came and arms
|
||||
// no rest
|
||||
const held = res.headers.get(BUSY_HOLD_MARK) === "1"
|
||||
const spent = !held && spentUp(text, res.status)
|
||||
if (!spent && res.status < 400) return done(kept(res, text))
|
||||
if (spent) rest(blocked, s.key + "\0" + s.jwt + "\0" + modelOf(orig).toLowerCase())
|
||||
return spent && res.status < 400 ? kept(new Response(text, { status: 429, headers: res.headers })) : done(kept(res, text))
|
||||
@@ -3087,7 +3101,9 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
|
||||
if (res.status >= 400 || json) {
|
||||
const text = await res.text()
|
||||
const cap = busy(res.status, text)
|
||||
const spent = !cap && spentUp(text, res.status)
|
||||
// the hold's own answer is no spent answer either (see the
|
||||
// -Trial path above)
|
||||
const spent = !cap && res.headers.get(BUSY_HOLD_MARK) !== "1" && spentUp(text, res.status)
|
||||
if (spent) {
|
||||
restModel(true)
|
||||
return done(kept(await spentSaid(await replayOnPlan())))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-zcode-auth",
|
||||
"version": "0.1.56",
|
||||
"version": "0.1.57",
|
||||
"description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
Reference in New Issue
Block a user