zcode 0.1.58: an account served fewer than three is narrowed, not held; trial entries are free (#438)

takomitkm (plugins#438, 0.1.57, default settings) had the busy hold arm
with one account, one agent and at most two in flight, at about five
asks a minute, and arm again within a minute of its end. A coding plan
model's weekly or monthly limit (1310) also rested the whole account, so
its -Trial models got the cached 1310 without being asked.

Busy hold. ZCode serves that account one Start Plan request at a time,
so every second request was refused again at its 1s re-ask and counted
toward the process-wide hold. A sustained cap with others of the same
account in flight (the gate's lane busy >= 2) now narrows that account's
gate to busy-1, at least one, for NARROW_FOR (5 minutes; startGate.now
is its injectable clock) and counts toward no hold. A lone sustained
cap still counts, so the hold stays process-wide and keeps the firewall
protection: #801's caps came with up to ~10 in flight, which the gate
never sends, and an account whose slots are taken elsewhere narrows
3 -> 2 -> 1 within a few caps and is then refused alone, which arms the
hold as before. The queue answer says the account's current number.

Rest. The models hook marks each -Trial entry free: true, so magpie's
PaidBesideFree makes a coding plan model's quota refusal rest that
model, not the account. Gift-only accounts' plain names stay unmarked.
Side effects: the Free mark in pickers, free-preferring groups pick the
trials first, and they price at 0, which they are.

Run (mote, bun 1.4.2, HOME=$(mktemp -d)): packages/zcode bun test,
240 pass, 0 fail, 17 files, at this head rebased on 60acd62b;
bun scripts/check.mjs exit 0.

Broken by hand:
- narrowing off (if (false && ...)): the three new inflight tests fail;
  the reporter's case shows the hold armed (until=1000000240000).
- lone caps narrow too (busy < 1): seven hold tests fail (the
  saturated-elsewhere replay, refused-even-one-at-a-time, start.test's
  hold tests, dual's hold test).
- free: true removed: the new gift test fails.

Tests: inflight.test's reporter's case (fake caps at >= 2 in flight,
injected clocks), the narrowing's expiry, and a lone refusal still
arming the hold; gift.test's free trial entries. start.test's in-flight
guard now uses twelve accounts of one request each, and dual.test's hold
test a gate of one, so their caps stay lone ones.

Not tried: a real ZCode account served one at a time; the core side
(magpie's TestZCodeCodingPlanLimitLeavesTrials) is in magpie with the
mover min.

Reported-by: takomitkm
This commit is contained in:
yetone
2026-11-11 03:26:57 +08:00
parent 60acd62bee
commit ea3573bfe9
8 changed files with 211 additions and 22 deletions
+8 -4
View File
@@ -53,6 +53,8 @@ afterEach(() => {
_internal.busyHold.now = () => Date.now()
// 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 sha = (s) => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16)
@@ -689,8 +691,10 @@ test("the busy hold reads its clock: a spread run holds nothing, caps during the
// The hold's guard is the one the serial tests cannot see: requests already
// in flight when the hold arms get their counted cap after it. Hold the
// clock still, put 12 Start Plan requests in flight at once — four accounts,
// three each, as startGate lets them — and answer them all 3008 after they
// clock still, put 12 Start Plan requests in flight at once — twelve
// accounts, one each, so each cap is a lone request's, the kind that counts
// (two or more of one account's narrow its gate instead, plugins#438) — and
// answer them all 3008 after they
// have all reached the fake. No first cap counts; the 1s re-asks are
// refused again and the fifth of those arms the hold. With the guard gone,
// the seven after it count toward a second hold.
@@ -702,7 +706,7 @@ test("caps that land after the hold armed change neither hits nor until (request
let clock = 1_000_000_000_000
_internal.busyHold.now = () => clock
const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } })
const accounts = [1, 2, 3, 4].map((n) => {
const accounts = Array.from({ length: 12 }, (_, i) => i + 1).map((n) => {
const own = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600, n })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig"
const s = { site: "zai", device: DEVICE, jwt: own, plan: "Start Plan" }
return { type: "oauth", access: own, refresh: JSON.stringify(s), expires: 0 }
@@ -716,7 +720,7 @@ test("caps that land after the hold armed change neither hits nor until (request
return Response.json({ code: "3008", msg: "user concurrency limit exceeded" }, { status: 429 })
}
const all = loaders.flatMap((opts) =>
Array.from({ length: 3 }, () =>
Array.from({ length: 1 }, () =>
opts.fetch("https://api.z.ai/api/anthropic/v1/messages", { method: "POST", headers: { "content-type": "application/json", "x-api-key": "zcode" }, body: agentText })))
// every request is in flight (reached the fake) before any answer comes
while (got.filter((r) => r.path.endsWith("/messages")).length < 12) await Bun.sleep(5)