From 3f37b858db8074959447c16ed06ac51d444c7fcf Mon Sep 17 00:00:00 2001 From: Lucentio-Huang <48576858+Lucentio-Huang@users.noreply.github.com> Date: Sat, 14 Nov 2026 17:02:35 +0800 Subject: [PATCH] zcode 0.1.63: the app's China sign-in keeps its identity, and the active provider's profile names the account (#1135) ZCode's app sign-in reads oauth:active_provider and includes zai_cn, so a China-channel account keeps its name and user id, follows renewal for the same user, and stays on its own session when the app switches to another user with the same display name. Unreadable active_provider falls back to the old order with zai_cn added. Reviewed at 5d6e923e3. Run (mica, sandbox HOME): bun test packages/zcode 260 pass; scripts/check.mjs zcode ok. Broken by hand: main's index.mjs with this PR's tests fails 3 of own.test.mjs (China identity, active zai_cn, active bigmodel). Co-authored-by: Lucentio-Huang --- packages/zcode/index.mjs | 6 +++++- packages/zcode/own.test.mjs | 40 +++++++++++++++++++++++++++++++++++++ packages/zcode/package.json | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index e3ea6e8..29063cc 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -1804,7 +1804,11 @@ function ownSignIn() { else s = { ...s, jwt, base: s.base || ZAI_BASE, site: s.base === BIGMODEL_BASE ? "bigmodel" : "zai" } if (!s.key && !s.jwt && !isTeam(s)) return null let info = {} - for (const name of ["oauth:zai:user_info", "oauth:bigmodel:user_info"]) { + // The JWT belongs to the app's active sign-in. Cached profiles from + // another provider must not name it, and 3.18's China sign-in is zai_cn. + const active = decrypt(key, store["oauth:active_provider"]) + const families = [...new Set([active, "zai", "zai_cn", "bigmodel"].filter((x) => ["zai", "zai_cn", "bigmodel"].includes(x)))] + for (const name of families.map((x) => `oauth:${x}:user_info`)) { const v = decrypt(key, store[name]) if (v && !first(info.email, info.name, info.user_id)) { try { diff --git a/packages/zcode/own.test.mjs b/packages/zcode/own.test.mjs index 551476e..c244298 100644 --- a/packages/zcode/own.test.mjs +++ b/packages/zcode/own.test.mjs @@ -315,3 +315,43 @@ test("ZCode 3.16's key location lists the coding plan's models", async () => { _internal.routes.clear() await expect(hooks.provider.models({ models: {} }, { auth: { ...z, type: "oauth" } })).rejects.toThrow("coding plan key") }) + +// ZCode 3.18's China sign-in keeps its identity under zai_cn. Losing it +// makes every gift-only account "ZCode" and lets it follow a different +// account's session when the app switches users. +test("the app's China sign-in keeps its identity and follows only that user", async () => { + const as = (id) => JSON.stringify({ email: "ada@phone.local", user_id: id }) + zcodeHas({ + zcodejwttoken: "Bearer china-a", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-a"), + }) + const auth = await appSignIn() + expect(auth.accountId).toBe("ada") + expect(auth.uid).toBe("china-user-a") + + zcodeHas({ + zcodejwttoken: "Bearer china-renewed", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-a"), + }) + expect((await _internal.stateOf(auth)).jwt).toBe("china-renewed") + + // Two accounts may share the same display name; the uid guards them. + zcodeHas({ + zcodejwttoken: "Bearer china-b", + "oauth:active_provider": "zai_cn", + "oauth:zai_cn:user_info": as("china-user-b"), + }) + expect((await _internal.stateOf(auth)).jwt).toBe("china-a") +}) + +test.each(["zai_cn", "zai", "bigmodel"])("the app's active %s identity wins over another provider's cached profile", async (active) => { + const store = { zcodejwttoken: "Bearer active-session", "oauth:active_provider": active } + for (const family of ["zai", "zai_cn", "bigmodel"]) + store[`oauth:${family}:user_info`] = JSON.stringify({ email: `${family}@example.com`, user_id: `user-${family}` }) + zcodeHas(store) + const auth = await appSignIn() + expect(auth.accountId).toBe(`${active}@example.com`) + expect(auth.uid).toBe(`user-${active}`) +}) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index b669eb1..7edc2cd 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.61", + "version": "0.1.63", "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",