@magpie-community/opencode-zcode-auth 0.1.63 → 0.1.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +13 -0
  2. package/index.mjs +53 -11
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -275,6 +275,19 @@ the slots. An account whose slots are taken elsewhere is narrowed to one
275
275
  within a few refusals and then refused alone, so the firewall hold still
276
276
  comes before the 405.
277
277
 
278
+ Since 0.1.64 the request that brought about such a narrowing is not
279
+ failed either. Up to 0.1.63 it went back to magpie as a 429 after its
280
+ re-asks, so two sessions on an account served one at a time showed a 429
281
+ every time the narrowing ran out (JJLIN070414 on X), and with one account
282
+ the agent was told it. Now it gives its slot back at once, without the 2s
283
+ re-ask, and waits at the head of the queue until one of the account's
284
+ other requests has ended, then is sent again, as ZCode's own client
285
+ (its dynamic-workflow governor) cuts its cap and queues a capped request
286
+ rather than failing it. The waits share the gate's two minutes, after
287
+ which it is answered here with the queue's 429. A request refused alone,
288
+ or free first's gift (which goes to the coding plan), is answered as
289
+ before.
290
+
278
291
  ## Two accounts at once
279
292
 
280
293
  Three ZCode accounts used at once from one address can get the address
package/index.mjs CHANGED
@@ -2044,7 +2044,11 @@ function kept(res, text) {
2044
2044
  // this computer through this one process, so the plugin does what that
2045
2045
  // client does: the Start Plan gets a few requests of an account in flight at
2046
2046
  // once (startGate), the rest wait their turn here instead of being sent to be
2047
- // refused, and a refusal anyway is asked again twice (BUSY_WAITS).
2047
+ // refused, and a refusal anyway is asked again twice (BUSY_WAITS). A refusal
2048
+ // of a request sent beside others of the same account lowers that account's
2049
+ // gate and waits its turn again (askBusy's requeue), as ZCode's own
2050
+ // dynamic-workflow governor cuts its cap and queues a capped subagent
2051
+ // request rather than failing it.
2048
2052
  //
2049
2053
  // The coding plan keeps no gate of the plugin's: its cap depends on its tier
2050
2054
  // (Z.ai serves Max more at once than Pro, and Lite fewer) and is higher
@@ -2068,6 +2072,10 @@ const BUSY_CODES = new Set(["3008", "3009", "3010"])
2068
2072
  // requests a capped account could serve on the wire, and the agents' own
2069
2073
  // retries came on top.
2070
2074
  const BUSY_WAITS = [1000, 2000]
2075
+ // REQUEUE_MAX bounds how often one request is handed back to the gate by a
2076
+ // narrowing (askBusy): each narrowing lowers the gate, three to one at most
2077
+ // takes two, so more is a fault of the count, not ZCode.
2078
+ const REQUEUE_MAX = 4
2071
2079
  // The hint names neither "plan" nor "limit": after the vendor's "…limit
2072
2080
  // exceeded" either would read to magpie as a plan used up.
2073
2081
  const BUSY_HINT =
@@ -2129,8 +2137,10 @@ const startFull = (key) => {
2129
2137
  }
2130
2138
 
2131
2139
  // startSlot waits for one of the account's Start Plan slots: { release },
2132
- // null when startGate.wait ran out first, or the agent's abort thrown.
2133
- function startSlot(key, signal) {
2140
+ // null when the wait (startGate.wait unless given) ran out first, or the
2141
+ // agent's abort thrown. A request handed back by a narrowing (requeued) was
2142
+ // first in line already, so it waits at the head of the queue.
2143
+ function startSlot(key, signal, { wait = startGate.wait, front = false } = {}) {
2134
2144
  const lanes = startGate.lanes
2135
2145
  let l = lanes.get(key)
2136
2146
  if (!l) lanes.set(key, (l = { busy: 0, queue: [] }))
@@ -2172,9 +2182,10 @@ function startSlot(key, signal) {
2172
2182
  leave()
2173
2183
  next()
2174
2184
  resolve(null)
2175
- }, startGate.wait)
2185
+ }, Math.max(0, wait))
2176
2186
  signal?.addEventListener?.("abort", aborted, { once: true })
2177
- l.queue.push(go)
2187
+ if (front) l.queue.unshift(go)
2188
+ else l.queue.push(go)
2178
2189
  })
2179
2190
  }
2180
2191
 
@@ -2270,10 +2281,12 @@ const busyHeld = () => busyHold.now() < busyHold.until
2270
2281
  // busyCap records one sustained concurrency cap of a Start Plan request.
2271
2282
  // Only a run arms a hold; the window is cleared on arming, and a cap that
2272
2283
  // lands while the hold is on counts toward nothing — the hold's own 429s and
2273
- // the first caps after it cannot keep re-arming it.
2284
+ // the first caps after it cannot keep re-arming it. It says true when the
2285
+ // cap narrowed the account instead: the request is then requeued, not
2286
+ // failed (see askBusy).
2274
2287
  function busyCap(key) {
2275
- if (key != null && narrowed(key)) return
2276
- if (busyHeld()) return
2288
+ if (key != null && narrowed(key)) return true
2289
+ if (busyHeld()) return false
2277
2290
  const now = busyHold.now()
2278
2291
  busyHold.hits.push(now)
2279
2292
  while (busyHold.hits.length && now - busyHold.hits[0] > BUSY_WINDOW) busyHold.hits.shift()
@@ -2281,6 +2294,7 @@ function busyCap(key) {
2281
2294
  busyHold.until = now + BUSY_HOLD
2282
2295
  busyHold.hits.length = 0
2283
2296
  }
2297
+ return false
2284
2298
  }
2285
2299
 
2286
2300
  // BUSY_HOLD_MARK marks an answer the hold made up: a request the gate or the
@@ -2349,8 +2363,13 @@ function busyAnswer(res, text) {
2349
2363
  // 1s and then 2s, as ZCode's own client does. Past them the cap goes back as
2350
2364
  // busyAnswer's 429. A request refused again at its 1s re-ask is a sustained
2351
2365
  // cap, told to onCap once (the firewall hold's count); one the re-ask got
2352
- // past is not. The plan path's patience, shared with free first's replay,
2353
- // whose plan has a cap of its own.
2366
+ // past is not. When onCap says the cap narrowed the account (others of this
2367
+ // computer were in flight on it), asking again in 2s would only be refused
2368
+ // beside them again: the busyAnswer goes back at once marked `requeue`, and
2369
+ // the Start Plan path waits for one of those others to end and sends it
2370
+ // again (JJLIN070414 on X: two sessions on one account, each pair's second
2371
+ // request failed 429 where ZCode's own client queues it). The plan path's
2372
+ // patience, shared with free first's replay, whose plan has a cap of its own.
2354
2373
  async function askBusy(u, o, hs, onCap) {
2355
2374
  let res = await fetch(u, { ...o, headers: hs })
2356
2375
  let caps = 0
@@ -2358,7 +2377,11 @@ async function askBusy(u, o, hs, onCap) {
2358
2377
  const text = await res.text()
2359
2378
  const cap = busy(429, text)
2360
2379
  // one request's refusals are one saturation signal, not one per ask
2361
- if (cap && ++caps === 2) onCap?.()
2380
+ if (cap && ++caps === 2 && onCap?.() === true) {
2381
+ res = busyAnswer(res, text)
2382
+ res.requeue = true
2383
+ break
2384
+ }
2362
2385
  // the hold armed: a request already in flight stops asking too. The
2363
2386
  // second check catches the asks that capped before the hold armed and
2364
2387
  // woke from their wait after it. Keying on onCap keeps the coding plan
@@ -3250,6 +3273,25 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
3250
3273
  // coding plan is served by another host and was never refused
3251
3274
  // with the IP's 405.
3252
3275
  res = await askBusy(url, opts, h, start ? () => busyCap(gateKey) : undefined)
3276
+ // a cap this computer brought on itself, with others of the
3277
+ // account in flight beside it, narrowed the account's gate:
3278
+ // the request gives its slot back and waits, first in line,
3279
+ // until one of those others has ended, then is sent again —
3280
+ // queued, as ZCode's own client queues its subagents' caps,
3281
+ // not failed. Free first's gift goes to the plan instead,
3282
+ // which serves it now. The waits share one startGate.wait;
3283
+ // each turn the gate is lower or the request is alone, where
3284
+ // a cap is ZCode's or another client's and goes back as it
3285
+ // did.
3286
+ const until = Date.now() + startGate.wait
3287
+ for (let turn = 0; res.requeue && !freeGift && turn < REQUEUE_MAX; turn++) {
3288
+ own.slot?.release()
3289
+ own.slot = await startSlot(gateKey, opts.signal, { wait: until - Date.now(), front: true })
3290
+ if (!own.slot) return kept(startQueueAnswer(gateKey))
3291
+ if (edgeHeld()) return kept(edgeHeldAnswer())
3292
+ if (busyHeld()) return kept(busyHeldAnswer())
3293
+ res = await askBusy(url, opts, h, () => busyCap(gateKey))
3294
+ }
3253
3295
  } catch (e) {
3254
3296
  // free first moved the request onto the gift, so a gift that
3255
3297
  // cannot be asked at all — the network, the host — is answered
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-zcode-auth",
3
- "version": "0.1.63",
3
+ "version": "0.1.64",
4
4
  "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",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",