diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index 35affe1..2cce5e9 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -510,6 +510,7 @@ async function codingUsage(s) { const level = typeof d.level === "string" ? d.level : "" if (level) out.plan = "GLM Coding " + level[0].toUpperCase() + level.slice(1) out.windows = limitWindows(d) + notePlan(s.key, out.windows) const resets = personalResets(root, s.key) let subs = null try { @@ -1211,6 +1212,7 @@ async function teamUsage(s, key) { const headers = teamHeaders(s.base, s.org, s.project) const d = (await call("GET", root + "/api/monitor/usage/quota/limit?type=2", { auth: key, headers })) ?? {} const out = { windows: limitWindows(d) } + notePlan(key, out.windows) if (!s.token) return out const resets = teamResets(root, s.token, headers) try { @@ -1675,29 +1677,101 @@ function ownLocation(store, key) { // its key: the secret of the key it names, read as ZCode lists the // project's keys (BigModel's token bare, Z.ai's as a Bearer). Nothing is // made: a location whose key is gone (404) leaves the sign-in keyless, as -// before; any other failure is said. A key read is kept for the token. +// before; any other failure is said. A key read is kept for the token, and +// the last key read for the location (lastKeys) serves while a read can't +// reach Z.ai, so a network that drops one read keeps the plan's key; an +// answer that refused the read (a 401, a 500) is said as before. The read +// is bounded (KEY_READ_MS): an ask that hung for minutes +// held the request with it (#1020, takomitkm). A read that never reached +// Z.ai — the socket (ECONNRESET), a timeout, the name — with no key read +// before is thrown marked unreached, which serve answers as a wall +// (wallAnswer): it is the network, not the sign-in, so it says nothing of +// signing in again. const locatedKeys = new Map() +const lastKeys = new Map() +const KEY_READ_MS = 20_000 async function located(s) { if (!s || s.key || !s.loc || !s.token) return s const l = s.loc - const id = [s.token, l.origin, l.org, l.project, l.apiKeyId].join("\0") + const at = [s.site, l.origin, l.org, l.project, l.apiKeyId].join("\0") + const id = s.token + "\0" + at let got = locatedKeys.get(id) if (!got || (got.err && Date.now() - got.at >= 60_000)) { const auth = s.site === "zai" ? "Bearer " + s.token : s.token try { - const secret = first((await call("GET", `${keysURL(l.origin, l.org, l.project)}/copy/${encodeURIComponent(l.apiKeyId)}`, { auth }))?.secretKey) + const secret = first((await call("GET", `${keysURL(l.origin, l.org, l.project)}/copy/${encodeURIComponent(l.apiKeyId)}`, { auth, signal: AbortSignal.timeout(KEY_READ_MS) }))?.secretKey) got = secret ? { key: `${l.apiKeyId}.${secret}` } : { err: new Error("no secret for the key") } } catch (e) { got = e?.status === 404 ? { key: "" } : { err: e } } + if (got.err && unreached(got.err) && lastKeys.get(at)) got.key = lastKeys.get(at) + else if (!got.err) lastKeys.set(at, got.key) got.at = Date.now() locatedKeys.set(id, got) } - if (got.err) throw new Error(`ZCode's ${SITES[s.site].name} coding plan key: ${got.err.message} — sign in to ZCode again`) + if (got.err && !got.key) { + const name = SITES[s.site].name + if (unreached(got.err)) { + const e = new Error(`ZCode's ${name} coding plan key: ${got.err.message} — ${name} could not be reached, so the request was not sent`) + e.unreached = true + throw e + } + throw new Error(`ZCode's ${name} coding plan key: ${got.err.message} — sign in to ZCode again`) + } if (got.key) s.key = got.key return s } +// unreached says whether a fetch failed before any answer: the socket +// closed or refused, a timeout, a name that didn't resolve. An answer with +// a status (ZError) never is, nor is a caller's abort. +const UNREACHED = /ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE|ENOTFOUND|EAI_AGAIN|ECONNABORTED|EHOSTUNREACH|ENETUNREACH|socket connection was closed|ConnectionRefused|ConnectionClosed|FailedToOpenSocket|fetch failed|timed out/i +function unreached(e) { + if (!e || e instanceof ZError || e.name === "AbortError") return false + if (e.name === "TimeoutError") return true + return [e.code, e.message, e.cause?.code, e.cause?.message].some((x) => typeof x === "string" && UNREACHED.test(x)) +} + +// wallAnswer is a lane that can't serve the request now — not reached, or +// its plan known full — told to magpie as a wall: a 429 whose error.code +// is one of magpie's wallCodes (yetone/magpie@067e4c484), which it sends +// once, never again in place, and rests as a rate limit, a minute at +// first, while the next account is asked. A thrown error was a 502 that +// magpie asked again eight times (#1020). The words are kept off magpie's +// quota and credit words, so the rest stays a brief one. +function wallAnswer(message) { + const error = { type: "rate_limit_error", code: "user_model_rate_limited", message } + return new Response(JSON.stringify({ type: "error", error }), { status: 429, statusText: STATUS_TEXT[429], headers: { "content-type": "application/json" } }) +} + +// planFull is a coding plan key whose last usage read showed a window +// counted full with its reset ahead: until then (and for PLAN_FULL_TRUST +// after the read at most) free first's replay doesn't send to it. +const planFull = new Map() +const PLAN_FULL_TRUST = 30 * 60_000 +function notePlan(key, windows) { + if (!key) return + let until = 0 + for (const w of windows ?? []) { + if (w.aside) continue + const full = (isNum(w.amount) && isNum(w.limit) && w.limit > 0 && w.amount >= w.limit) || (isNum(w.used) && w.used >= 100) + const t = Date.parse(w.resetsAt ?? "") + if (full && t > Date.now()) until = Math.max(until, t) + } + if (until) planFull.set(key, { until, at: Date.now() }) + else planFull.delete(key) +} +function planFullUntil(key) { + const f = key && planFull.get(key) + if (!f) return 0 + const now = Date.now() + if (now >= f.until || now - f.at >= PLAN_FULL_TRUST) { + planFull.delete(key) + return 0 + } + return f.until +} + // ownSignIn is what ZCode is signed in to now: the account, its coding // plan key, ZCode's session token (the Start Plan's key) and the team plan // it is switched to; null when it has none of them. @@ -2959,7 +3033,15 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { } async function serve(input, init, own) { const auth = await getAuth() - const s = await located(stateOf(auth)) + let s + try { + s = await located(stateOf(auth)) + } catch (e) { + // the plan's key couldn't be read for the network: a wall, + // sent once (wallAnswer), not a 502 asked again (#1020) + if (e?.unreached) return kept(wallAnswer(e.message)) + throw e + } if (!s) throw new Error("not signed in to ZCode") // the account a Start Plan slot belongs to: its user id, which a // renewed session keeps, before the key or session itself @@ -3082,7 +3164,18 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { h2.delete("content-length") opts.body = marked(JSON.stringify({ ...JSON.parse(orig), model: asked }), s.device, sid) delete opts.duplex - return askBusy(u, opts, h2) + // a plan the last usage read showed full isn't sent to: it + // would only refuse (#1020) + const full = planFullUntil(key) + if (full) return wallAnswer(`ZCode's ${SITES[s.site].name} GLM Coding Plan was full at its last read, until ${new Date(full).toISOString()}, so the request was not sent to it`) + try { + return await askBusy(u, opts, h2) + } catch (e) { + // a plan that can't be reached is a wall too, sent once; a + // caller's abort stays an abort + if (opts.signal?.aborted || !unreached(e)) throw e + return wallAnswer(`ZCode's ${SITES[s.site].name} GLM Coding Plan: ${e.message} — it could not be reached, so the request was not sent`) + } } // a block page held this computer off zcode.z.ai (edge): the // Start Plan and a picked gift are answered here; free first @@ -3358,4 +3451,4 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { // config sets what zcodeConfig has read, for tests: null is ZCode's config // out of reach, undefined forgets it const config = (cfg) => (cfgCache = cfg === undefined ? null : { cfg, at: Date.now() }) -export const _internal = { edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS } +export const _internal = { wallAnswer, unreached, planFull, notePlan, planFullUntil, locatedKeys, lastKeys, edge, EDGE_HOLD, busyHold, startGate, NARROW_FOR, limitOf, BUSY_N, BUSY_WINDOW, BUSY_HOLD, busyCap, busyHeld, config, entry, limitWindows, termOf, startUsage, routes, blocked, sidelined, plansOf, giftOf, dualUsage, claimHint, claimDaily, spentUp, giftServes, modelOf, teamKeys, ownSignIn, stateOf, located, dress, queryOf, PROMPT, marked, keyDevice, sessionOf, busy, BUSY_WAITS, DEFAULT_CONCURRENCY, DEFAULT_ACCOUNTS } diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 05cd3d2..3fcea0b 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.59", + "version": "0.1.60", "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", diff --git a/packages/zcode/wall.test.mjs b/packages/zcode/wall.test.mjs new file mode 100644 index 0000000..db18ad4 --- /dev/null +++ b/packages/zcode/wall.test.mjs @@ -0,0 +1,272 @@ +// A lane that can't serve the request now is told to magpie as a wall: a +// 429 with error.code user_model_rate_limited, one of magpie's wallCodes +// (yetone/magpie@067e4c484), sent once and rested briefly while the next +// account is asked. takomitkm (#1020) saw one request tried eight times over +// six minutes: the ZCode 3.16 key read threw "ZCode's Z.ai coding plan key: +// ECONNRESET: The socket connection was closed unexpectedly", a 502 magpie +// asked again, and free first's replay sent to a coding plan whose usage +// read already showed it full. Nothing leaves the machine: fetch is a fake. +import { afterEach, beforeAll, beforeEach, expect, test } from "bun:test" +import { homedir, tmpdir } from "node:os" +import { mkdirSync, realpathSync, writeFileSync } from "node:fs" +import { createCipheriv, createHash, randomBytes } from "node:crypto" +import { join } from "node:path" + +let ZCodeAuthPlugin, _internal +beforeAll(async () => { + if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test") + process.env.ZCODE_CREDENTIAL_SECRET = "test-seed" + ;({ ZCodeAuthPlugin, _internal } = await import("./index.mjs")) +}) + +// the reporter's words, as Bun's fetch gave them +const RESET = "ECONNRESET: The socket connection was closed unexpectedly" +const reset = () => Object.assign(new Error(RESET), { code: "ECONNRESET" }) + +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) +beforeEach(() => { + _internal.routes.clear() + _internal.blocked.clear() + _internal.sidelined.clear() + _internal.planFull.clear() + _internal.locatedKeys.clear() + _internal.lastKeys.clear() + _internal.edge.until = 0 + _internal.busyHold.until = 0 + _internal.busyHold.hits.length = 0 + _internal.startGate.lanes.clear() + _internal.startGate.narrow.clear() +}) +const ok = (data) => new Response(JSON.stringify({ code: 0, data })) +const body = (model) => JSON.stringify({ model, max_tokens: 100, messages: [{ role: "user", content: "hi" }] }) +const MSG = "https://api.z.ai/api/anthropic/v1/messages" +const post = (opts, model = "GLM-5.3-Flash") => opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body(model) }) +const walled = async (res) => { + expect(res.status).toBe(429) + const j = JSON.parse(await res.text()) + expect(j.error.code).toBe("user_model_rate_limited") + expect(j.error.type).toBe("rate_limit_error") + // magpie's quota and credit words would rest it far longer than a wall + expect(j.error.message).not.toMatch(/quota|usage.?limit|balance|credit|billing|daily|weekly|monthly|resets? at|limit.?reached|exceeded/i) + return j.error.message +} + +// ---- the key read (ZCode 3.16's key location) --------------------------------- + +const dir = () => join(homedir(), ".zcode", "v2") +function enc(text) { + const key = createHash("sha256").update("test-seed").digest() + const iv = randomBytes(12) + const c = createCipheriv("aes-256-gcm", key, iv) + const data = Buffer.concat([c.update(text, "utf8"), c.final()]) + return "enc:v1:" + [iv, c.getAuthTag(), data].map((b) => b.toString("base64url")).join(".") +} +function zcodeHas(token) { + const loc = "project-key-location:v1:" + encodeURIComponent(JSON.stringify(["https://api.z.ai", "zai", "u7", null, 1, "non-team"])) + const store = { + zcodejwttoken: "Bearer j1", + "oauth:zai:access_token": token, + "oauth:zai:user_info": JSON.stringify({ id: "u7", email: "q@example.com" }), + [loc]: JSON.stringify({ organizationId: "org7", projectId: "proj7", apiKeyId: "k7" }), + } + mkdirSync(dir(), { recursive: true }) + writeFileSync(join(dir(), "credentials.json"), JSON.stringify(Object.fromEntries(Object.entries(store).map(([k, v]) => [k, enc(v)])))) + writeFileSync(join(dir(), "setting.json"), JSON.stringify({})) +} +// the key read answers as keyRead says; the plan's messages are counted +function located(keyRead) { + const tries = { key: 0, plan: 0 } + globalThis.fetch = async (input, init = {}) => { + const url = new URL(input instanceof Request ? input.url : String(input)) + if (url.pathname.endsWith("/api_keys/copy/k7")) { + tries.key++ + return keyRead(init) + } + if (url.pathname === "/api/anthropic/v1/messages") { + tries.plan++ + return new Response(JSON.stringify({ type: "message", content: [] }), { headers: { "content-type": "application/json" } }) + } + if (url.pathname.endsWith("/api/biz/subscription/list")) return ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + if (url.pathname.endsWith("/billing/balance")) return ok({ plans: [] }) + return new Response("{}", { status: 404 }) + } + return tries +} +async function signedIn(token = "tok-z") { + zcodeHas(token) + const hooks = await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }, { freeFirst: false }) + const m = hooks.auth.methods.find((x) => x.label === "ZCode app's sign-in") + const got = await (await m.authorize()).callback() + const auth = { ...got, type: "oauth" } + return { auth, opts: await hooks.auth.loader(async () => auth, { id: "zcode" }) } +} + +test("a key read the socket dropped is a wall sent once, not a sign-in to redo", async () => { + const tries = located(() => { + throw reset() + }) + const { opts } = await signedIn() + const res = await post(opts) + expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") + const said = await walled(res) + expect(said).toContain("ZCode's Z.ai coding plan key: " + RESET) + expect(said).not.toContain("sign in") + expect(tries).toEqual({ key: 1, plan: 0 }) + // magpie asks again within the minute: the read's failure is kept, so + // nothing hangs on the network again + await walled(await post(opts)) + expect(tries.key).toBe(1) +}) + +test("a key read that times out is a wall too", async () => { + located(() => { + throw new DOMException("The operation timed out.", "TimeoutError") + }) + const { opts } = await signedIn() + expect(await walled(await post(opts))).toContain("could not be reached") +}) + +test("the key read is bounded", async () => { + let signal + located((init) => { + signal = init.signal + return ok({ secretKey: "sec7" }) + }) + const { auth } = await signedIn() + await _internal.located(_internal.stateOf(auth)) + expect(signal).toBeInstanceOf(AbortSignal) +}) + +test("a key read Z.ai refused still says to sign in again", async () => { + located(() => new Response(JSON.stringify({ code: 401, msg: "token expired" }), { status: 401 })) + const { opts } = await signedIn() + await expect(post(opts)).rejects.toThrow("sign in to ZCode again") +}) + +test("the last key read serves while a read fails", async () => { + let fail = false + const tries = located(() => { + if (fail) throw reset() + return ok({ secretKey: "sec7" }) + }) + const first = await signedIn("tok-a") + expect((await post(first.opts)).status).toBe(200) + // ZCode renewed its token, and the read with the new one is dropped + fail = true + const next = await signedIn("tok-b") + const s = await _internal.located(_internal.stateOf(next.auth)) + expect(s.key).toBe("k7.sec7") + expect((await post(next.opts)).status).toBe(200) + expect(tries.key).toBe(2) + expect(tries.plan).toBe(2) +}) + +test("a socket error is told from an answer and a caller's abort", () => { + expect(_internal.unreached(reset())).toBe(true) + expect(_internal.unreached(new Error(RESET))).toBe(true) + expect(_internal.unreached(new TypeError("fetch failed"))).toBe(true) + expect(_internal.unreached(new DOMException("The operation timed out.", "TimeoutError"))).toBe(true) + expect(_internal.unreached(new DOMException("This operation was aborted", "AbortError"))).toBe(false) + expect(_internal.unreached(new Error("no secret for the key"))).toBe(false) +}) + +// ---- free first's replay onto the plan ---------------------------------------- + +const jwt = ["{}", JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 3600 })].map((s) => Buffer.from(s).toString("base64url")).join(".") + ".sig" +const KEY = "dual.secret" +const now = Math.trunc(Date.now() / 1000) +const balance = { + server_time: now, + plans: [{ plan_id: "zai-start-plan", user_plan_id: "up1", name: "Start Plan", status: "active", ends_at: now + 7 * 86400, entitlements: [{ entitlement_id: "e1", period: "daily" }] }], + balances: [{ plan_id: "zai-start-plan", user_plan_id: "up1", entitlement_id: "e1", show_name: "Trust Build", capabilities: ["model:glm-5.3-flash"], + total_units: 1000, used_units: 250, remaining_units: 750, expires_at: now + 3600 }], +} +const coding = { type: "oauth", access: KEY, refresh: JSON.stringify({ site: "zai", base: "https://api.z.ai/api/anthropic", key: KEY, jwt, device: "11111111-2222-4333-8444-555555555555", plan: "GLM Coding Pro" }), expires: 0 } +// the gift answers 503 (so free first replays on the plan), the plan as +// plan() says, the week's window as limit() reads it +function dual({ plan, week }) { + const tries = { gift: 0, plan: 0 } + globalThis.fetch = async (input, init = {}) => { + const url = new URL(input instanceof Request ? input.url : String(input)) + if (url.origin === "https://zcode.z.ai" && url.pathname.endsWith("/messages")) { + tries.gift++ + return new Response(JSON.stringify({ type: "error", error: { type: "api_error", message: "upstream unavailable" } }), { status: 503, headers: { "content-type": "application/json" } }) + } + if (url.pathname === "/api/anthropic/v1/messages") { + tries.plan++ + return plan() + } + if (url.pathname.endsWith("/billing/balance")) return ok(balance) + if (url.pathname.endsWith("/api/biz/subscription/list")) return ok([{ productName: "GLM Coding Pro", status: "VALID" }]) + if (url.pathname === "/api/monitor/usage/quota/limit") + return ok({ level: "pro", limits: [{ type: "CREDIT_LIMIT", unit: 6, number: 1, usage: 5000, currentValue: week, remaining: 5000 - week, percentage: Math.round((100 * week) / 5000), nextResetTime: (now + 3 * 86400) * 1000 }] }) + return ok([]) + } + return tries +} +const planOK = () => new Response(JSON.stringify({ type: "message", content: [] }), { headers: { "content-type": "application/json" } }) +const freeHooks = async () => (await ZCodeAuthPlugin({ client: { auth: { set: async () => {} } } }, { freeFirst: true })).auth +const freeFirst = async () => (await freeHooks()).loader(async () => coding, { id: "zcode" }) +const readUsage = async () => (await freeHooks()).usage(async () => coding, { id: "zcode" }) + +test("a plan the usage read showed full is not replayed onto", async () => { + const tries = dual({ plan: planOK, week: 5000 }) + const u = await readUsage() + expect(u.windows.find((w) => w.display === "5000 / 5000")).toBeTruthy() + const res = await post(await freeFirst()) + const said = await walled(res) + expect(said).toContain("not sent") + expect(tries).toEqual({ gift: 1, plan: 0 }) +}) + +test("a plan the usage read showed room on is replayed onto", async () => { + const tries = dual({ plan: planOK, week: 1200 }) + await readUsage() + expect((await post(await freeFirst())).status).toBe(200) + expect(tries).toEqual({ gift: 1, plan: 1 }) +}) + +test("a full mark is let go once the read shows room again, or once it is old", async () => { + _internal.notePlan(KEY, [{ name: "Week", used: 100, amount: 5000, limit: 5000, resetsAt: new Date(Date.now() + 86400_000).toISOString() }]) + expect(_internal.planFullUntil(KEY)).toBeGreaterThan(Date.now()) + _internal.notePlan(KEY, [{ name: "Week", used: 20, amount: 1000, limit: 5000, resetsAt: new Date(Date.now() + 86400_000).toISOString() }]) + expect(_internal.planFullUntil(KEY)).toBe(0) + // a window set aside (MCP calls) or one whose reset has passed never is + _internal.notePlan(KEY, [{ name: "MCP · Month", used: 100, aside: true, resetsAt: new Date(Date.now() + 86400_000).toISOString() }]) + _internal.notePlan(KEY, [{ name: "Week", used: 100, resetsAt: new Date(Date.now() - 1000).toISOString() }]) + expect(_internal.planFullUntil(KEY)).toBe(0) + _internal.planFull.set(KEY, { until: Date.now() + 86400_000, at: Date.now() - 31 * 60_000 }) + expect(_internal.planFullUntil(KEY)).toBe(0) +}) + +test("a replay the socket dropped is a wall sent once", async () => { + const tries = dual({ + plan: () => { + throw reset() + }, + week: 1200, + }) + const said = await walled(await post(await freeFirst())) + expect(said).toContain(RESET) + expect(tries).toEqual({ gift: 1, plan: 1 }) +}) + +test("a replay the caller aborted stays an abort", async () => { + const ctl = new AbortController() + dual({ + plan: () => { + ctl.abort() + throw new TypeError("fetch failed") + }, + week: 1200, + }) + const opts = await freeFirst() + await expect(opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash"), signal: ctl.signal })).rejects.toThrow() +}) + +test("the wall's body is what magpie reads as one", async () => { + const res = _internal.wallAnswer("x") + expect(res.headers.get("content-type")).toBe("application/json") + await walled(res) +})