From 7b072d798b750fbc34927758b934020a330640e6 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 06:33:51 +0800 Subject: [PATCH] =?UTF-8?q?zed=200.1.6:=20a=20model=20token=20Zed=20refuse?= =?UTF-8?q?s=20while=20listing=20the=20models=20marks=20the=20account,=20a?= =?UTF-8?q?s=20the=20built-in's=20zedLapse=20did=20there=20=E2=80=94=20the?= =?UTF-8?q?=20models=20hook=20throws=20with=20signIn:=20expired=20on=20the?= =?UTF-8?q?=20refusal=20(magpie=2078c3229=20reads=20it),=20and=20falls=20b?= =?UTF-8?q?ack=20to=20the=20listed=20models=20on=20any=20other=20failure?= =?UTF-8?q?=20as=20before.=20models.test.mjs=20fails=20without=20the=20cha?= =?UTF-8?q?nge.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/zed/index.mjs | 7 +++- packages/zed/models.test.mjs | 80 ++++++++++++++++++++++++++++++++++++ packages/zed/package.json | 2 +- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 packages/zed/models.test.mjs diff --git a/packages/zed/index.mjs b/packages/zed/index.mjs index 481ff72..cfa0e4b 100644 --- a/packages/zed/index.mjs +++ b/packages/zed/index.mjs @@ -961,7 +961,12 @@ export async function ZedAuthPlugin({ client } = {}) { try { l = { at: Date.now(), models: await fetchModels(s) } lists.set(s.userId, l) - } catch { + } catch (e) { + // Zed refusing the account's own sign-in while its list was + // read: the built-in marked the account then (zedFetchModels → + // ZedToken → zedLapse), so magpie is told as an answer's + // X-Magpie-Sign-In would tell it + if (e instanceof SignInExpired) throw Object.assign(e, { signIn: "expired" }) if (!l) return provider.models } } diff --git a/packages/zed/models.test.mjs b/packages/zed/models.test.mjs new file mode 100644 index 0000000..19341a8 --- /dev/null +++ b/packages/zed/models.test.mjs @@ -0,0 +1,80 @@ +// provider.models says what the built-in's model list did to the account's +// mark (internal/provider/zed.go zedFetchModels): Zed refusing the sign-in +// while the list is read marked it lapsed; anything else left it alone. +import { afterEach, expect, test } from "bun:test" +import { ZedAuthPlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.tokens.clear() + _internal.lists.clear() +}) + +let n = 0 +const auth = () => ({ + type: "oauth", + access: "plain-access-" + ++n, + refresh: JSON.stringify({ userId: String(5000 + n), systemId: "", org: "org-me", plan: "zed_pro" }), + expires: 0, + accountId: "octo", +}) + +const list = JSON.stringify({ models: [{ provider: "anthropic", id: "claude-sonnet-5", display_name: "Claude Sonnet 5", max_token_count: 1000, max_output_tokens: 100 }] }) + +async function models(a, route) { + const seen = [] + globalThis.fetch = async (url) => { + const u = new URL(String(url)) + seen.push(u.pathname) + return route(u.pathname) + } + const hooks = await ZedAuthPlugin({}) + return { run: () => hooks.provider.models({ models: { declared: {} } }, { auth: a }), seen } +} + +test("the model token refused: the sign-in expired, as zedLapse marked it", async () => { + const { run, seen } = await models(auth(), () => new Response("unauthorized", { status: 401 })) + const err = await run().catch((e) => e) + expect(err).toBeInstanceOf(Error) + expect(err.signIn).toBe("expired") + expect(err.message).toContain("the Zed sign-in has expired") + expect(seen).toEqual(["/client/llm_tokens"]) +}) + +test("the list refused with the token just minted: kept, the declared models", async () => { + // a stale token is minted again once; /models refusing both is no word on + // the account's own sign-in, which the built-in left unmarked + const { run, seen } = await models(auth(), (p) => + p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response("nope", { status: 401 }), + ) + expect(await run()).toEqual({ declared: {} }) + expect(seen).toEqual(["/client/llm_tokens", "/models", "/client/llm_tokens", "/models"]) +}) + +test("Zed down: no word on the sign-in, the declared models", async () => { + const { run } = await models(auth(), () => new Response("bad gateway", { status: 502 })) + expect(await run()).toEqual({ declared: {} }) +}) + +test("a list read: the account's models", async () => { + const { run } = await models(auth(), (p) => (p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list))) + expect(Object.keys(await run())).toEqual(["claude-sonnet-5"]) +}) + +test("refused after a list was read: still says expired", async () => { + const a = auth() + let refuse = false + const { run } = await models(a, (p) => { + if (refuse) return new Response("unauthorized", { status: 401 }) + return p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list) + }) + expect(Object.keys(await run())).toEqual(["claude-sonnet-5"]) + // the list goes stale and the token with it + const uid = JSON.parse(a.refresh).userId + _internal.lists.set(uid, { at: 0, models: _internal.lists.get(uid).models }) + _internal.tokens.clear() + refuse = true + const err = await run().catch((e) => e) + expect(err.signIn).toBe("expired") +}) diff --git a/packages/zed/package.json b/packages/zed/package.json index 1c2c68e..13161e3 100644 --- a/packages/zed/package.json +++ b/packages/zed/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zed-auth", - "version": "0.1.5", + "version": "0.1.6", "description": "Sign in to a Zed subscription (Zed Pro, trial, student, business) from OpenCode or magpie", "type": "module", "main": "./index.mjs",