zcode 0.1.17: ZCode app's sign-in follows a ZCode whose data was moved to another folder

What changed: ZCode 3.14 has a 数据存储路径 (data storage path) setting.
Changing it copies ~/.zcode/v2 to <folder>/.zcode/v2 (all but setting.json,
copyDataDirectory's filter) and from the next start ZCode reads
credentials.json and runtime/provider from there. The choice stays in the
home folder's .zcode/v2/setting.json as dataBaseDir
(readBootstrapDataBaseDirFromDisk), and setting.json itself is always read
from the home folder (getSettingsFile: ZCODE_DESKTOP_HOME_DIR, HOME,
USERPROFILE). The plugin read credentials.json and runtime/provider from
~/.zcode/v2 only, so for such a user "ZCode app's sign-in" read the stale
copy left at home (or nothing). It now reads dataBaseDir as magpie's built-in
does (magpie 41972b25f, internal/appdir ZCodeDataIn: trimmed, ignored when
empty, not a string or not absolute) for credentials.json and the
zcode-builtin.json walk, and keeps reading setting.json (the team plan)
from the home folder. The credential key's seed is unchanged: ZCode builds
it from os.homedir(), not the data folder.

Read from ZCode 3.14.3's app.asar (a copy): out/main/index.js (bootstrap),
out/main/chunk-*.js and out/host/chunk-*.js (getDataBaseDir,
getAppConfigDir, copyDataDirectory, getCredentialsFile, the settings file
under the home folder, defaultCredentialSecret).

What was run:
- moved.test.mjs (new): with dataBaseDir set, ownSignIn takes the moved
  folder's key and session over the stale home copy, and a team plan named
  in the home setting.json; "", blank, relative, number and null keep the
  home folder. With zcodeDir put back on homedir() it fails (old key read).
- HOME=$(mktemp -d) bun test at the repo root: 1185 pass, 0 fail.
- bun scripts/check.mjs zcode.
- Not run: a real ZCode with a moved folder, Windows.
This commit is contained in:
yetone
2026-10-13 16:33:29 +08:00
parent c3bcb5cdf3
commit 65a76769d6
3 changed files with 83 additions and 5 deletions
+24 -4
View File
@@ -19,7 +19,7 @@
import { createDecipheriv, createHash } from "node:crypto"
import { existsSync, readFileSync, readdirSync } from "node:fs"
import { arch, homedir, release, userInfo } from "node:os"
import { join } from "node:path"
import { isAbsolute, join } from "node:path"
import PROMPT from "./prompt.mjs"
const PROVIDER = "zcode"
@@ -933,8 +933,28 @@ async function authorize(site, log) {
// team's plan. An account signed in "as ZCode" reads them for every
// request, so it follows ZCode's own sign-in, its renewed session and its
// switches between plans, as magpie's built-in did (zcode.go zcodeOwn).
//
// ZCode 3.14 can move its data (数据存储路径, "data storage path"): it copies
// ~/.zcode/v2 to <folder>/.zcode/v2, all but setting.json, and from its next
// start reads credentials.json and runtime/provider from there. The choice
// stays in the home folder's setting.json as dataBaseDir, which ZCode's main
// process reads before anything else (readBootstrapDataBaseDirFromDisk);
// setting.json itself, and the team plan it names, stay in the home folder.
const zcodeDir = () => join(homedir(), ".zcode", "v2")
// settingDir is where ZCode keeps setting.json, moved or not.
const settingDir = () => join(homedir(), ".zcode", "v2")
// dataIn is the folder ZCode keeps its .zcode data in: the home folder,
// unless dataBaseDir moved it. A value that isn't an absolute path is
// ignored, as magpie's built-in does (internal/appdir ZCodeDataIn).
function dataIn() {
const d = readJSON(join(settingDir(), "setting.json"))?.dataBaseDir
const t = typeof d === "string" ? d.trim() : ""
return t && isAbsolute(t) ? t : homedir()
}
// zcodeDir is where ZCode keeps credentials.json and runtime/provider.
const zcodeDir = () => join(dataIn(), ".zcode", "v2")
// secret is the key ZCode encrypts its credentials with.
function secret() {
@@ -974,7 +994,7 @@ function readJSON(path) {
// ownTeam is the team plan ZCode switched to, when it did.
function ownTeam(store, key) {
const set = readJSON(join(zcodeDir(), "setting.json"))
const set = readJSON(join(settingDir(), "setting.json"))
if (!set) return null
const families = set.providerFamilyDomain === "zai" || set.providerFamilyDomain === "bigmodel" ? [set.providerFamilyDomain] : ["zai", "bigmodel"]
for (const fam of families) {
@@ -1139,7 +1159,7 @@ function modelsOf(cfg, plan) {
// localConfig is the latest provider config an installed ZCode has.
function localConfig() {
const files = []
const rt = join(homedir(), ".zcode", "v2", "runtime", "provider")
const rt = join(zcodeDir(), "runtime", "provider")
const walk = (dir, depth) => {
let es = []
try {
+58
View File
@@ -0,0 +1,58 @@
// ZCode 3.14 can move its data to another folder (数据存储路径): it copies
// ~/.zcode/v2 there, all but setting.json, records the folder in the home
// folder's setting.json as dataBaseDir, and from then on reads
// credentials.json from the new folder. "ZCode app's sign-in" follows it, as
// magpie's built-in does (internal/appdir ZCodeDataIn), and still takes the
// team plan from the home folder's setting.json, where ZCode keeps it.
import { test, expect, beforeAll } from "bun:test"
import { homedir, tmpdir } from "node:os"
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"
import { createCipheriv, createHash, randomBytes } from "node:crypto"
import { join } from "node:path"
let _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"
;({ _internal } = await import("./index.mjs"))
})
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(".")
}
const writeStore = (v2, store) => {
mkdirSync(v2, { recursive: true })
writeFileSync(join(v2, "credentials.json"), JSON.stringify(Object.fromEntries(Object.entries(store).map(([k, v]) => [k, enc(v)]))))
}
const KEY = "account-provider:coding-plan:account:zai-individual-coding-plan:account:u1:api-key"
test("ZCode's sign-in is read from the folder its data was moved to", () => {
const homeV2 = join(homedir(), ".zcode", "v2")
const moved = mkdtempSync(join(tmpdir(), "zcode-moved-"))
const movedV2 = join(moved, ".zcode", "v2")
const setting = (s) => writeFileSync(join(homeV2, "setting.json"), JSON.stringify(s))
try {
// what ZCode leaves after the move: the old copy at home, the live one moved
writeStore(homeV2, { [KEY]: "old.stale", zcodejwttoken: "j-old" })
writeStore(movedV2, { [KEY]: "new.live", zcodejwttoken: "j-new", "oauth:zai:access_token": "tok-team" })
setting({ locale: "zh-CN", dataBaseDir: moved })
expect(_internal.ownSignIn()).toMatchObject({ key: "new.live", jwt: "j-new" })
// the team plan ZCode switched to stays in the home folder's setting.json
setting({ dataBaseDir: " " + moved + " ", providerFamilyConnectionSelections: { zai: { kind: "team-coding-plan", organizationId: "org1", projectId: "p1" } } })
expect(_internal.ownSignIn()).toMatchObject({ token: "tok-team", org: "org1", project: "p1", jwt: "j-new" })
// an empty, blank, relative or non-string dataBaseDir is no move
for (const d of ["", " ", "ZCodeData", 7, null]) {
setting({ dataBaseDir: d })
expect(_internal.ownSignIn()).toMatchObject({ key: "old.stale", jwt: "j-old" })
}
} finally {
rmSync(moved, { recursive: true, force: true })
rmSync(join(homeV2, "setting.json"), { force: true })
}
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.16",
"version": "0.1.17",
"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",
"type": "module",
"main": "./index.mjs",