From 8f370ba4329517c6bafc6427f5cd74a6d5c6f319 Mon Sep 17 00:00:00 2001 From: Kingsword Date: Wed, 25 Nov 2026 16:45:32 +0800 Subject: [PATCH] fix(cli): defer account model validation to runtime --- src/prompt-model-validation.ts | 12 ++++--- test/prompt-model-validation.test.ts | 24 +++++++++++-- test/runtime/launcher.test.ts | 36 +++++++++++++++++++ test/runtime/session-model-recovery.test.ts | 40 +++++++++++---------- 4 files changed, 86 insertions(+), 26 deletions(-) diff --git a/src/prompt-model-validation.ts b/src/prompt-model-validation.ts index 0b53b9ac..6cf75207 100644 --- a/src/prompt-model-validation.ts +++ b/src/prompt-model-validation.ts @@ -61,16 +61,20 @@ async function readCatalog(path: string, missingIsEmpty = false): Promise { if (!model) return undefined; + const separator = model.indexOf("/"); + const providerId = model.slice(0, separator); + if (providerId.startsWith("account:")) return undefined; + let personal: Map> | undefined; let builtin: Map> | undefined; try { @@ -88,8 +92,6 @@ export async function invalidConfiguredPromptModel( } if (!personal || !builtin) return undefined; - const separator = model.indexOf("/"); - const providerId = model.slice(0, separator); const modelId = model.slice(separator + 1); const personalModels = personal.get(providerId); const builtinModels = builtin.get(providerId); diff --git a/test/prompt-model-validation.test.ts b/test/prompt-model-validation.test.ts index 2465ba22..94022ed3 100644 --- a/test/prompt-model-validation.test.ts +++ b/test/prompt-model-validation.test.ts @@ -53,9 +53,9 @@ describe("explicit startup model validation", () => { const personalPath = join(directory, "personal.json"); const builtinPath = join(directory, "builtin.json"); await writeFile(personalPath, JSON.stringify(catalog("custom", "known-model"))); - await writeFile(builtinPath, JSON.stringify(catalog("account:zai-plan", "GLM-5.3", "builtinModelIds"))); + await writeFile(builtinPath, JSON.stringify(catalog("builtin:fixture", "GLM-5.3", "builtinModelIds"))); try { - expect(await invalidConfiguredPromptModel("account:zai-plan/GLM-5.3", { + expect(await invalidConfiguredPromptModel("builtin:fixture/GLM-5.3", { ZCODE_PERSONAL_PROVIDER_CONFIG_FILE: personalPath, ZCODE_BUILTIN_PROVIDER_CONFIG_FILE: builtinPath })).toBeUndefined(); @@ -64,6 +64,26 @@ describe("explicit startup model validation", () => { } }); + test.each(["account:zai-individual-coding-plan", "account:bigmodel-individual-coding-plan"])( + "defers account models absent from readable local catalogs to the runtime (%s)", async providerId => { + const directory = await mkdtemp(join(tmpdir(), "zcode-model-validation-")); + const personalPath = join(directory, "personal.json"); + const builtinPath = join(directory, "builtin.json"); + await writeFile(personalPath, JSON.stringify(catalog("custom", "known-model"))); + await writeFile(builtinPath, JSON.stringify(catalog(providerId, "GLM-5.3", "builtinModelIds"))); + try { + const env = { + ZCODE_PERSONAL_PROVIDER_CONFIG_FILE: personalPath, + ZCODE_BUILTIN_PROVIDER_CONFIG_FILE: builtinPath + }; + expect(await invalidConfiguredPromptModel(`${providerId}/GLM-5.4`, env)).toBeUndefined(); + expect(await invalidConfiguredPromptModel("account:new-plan/GLM-5.4", env)).toBeUndefined(); + } finally { + await rm(directory, { recursive: true, force: true }); + } + } + ); + test("defers validation if either local registry is unavailable", async () => { const directory = await mkdtemp(join(tmpdir(), "zcode-model-validation-")); const personalPath = join(directory, "personal.json"); diff --git a/test/runtime/launcher.test.ts b/test/runtime/launcher.test.ts index 895ed5c6..cfdd97aa 100644 --- a/test/runtime/launcher.test.ts +++ b/test/runtime/launcher.test.ts @@ -212,6 +212,42 @@ describe("launcher/runtime integration", () => { } }); + test.skipIf(process.platform === "win32").each([{ args: [] }, { args: ["-p", "Answer exactly: pong"] }])( + "forwards an account model missing from local catalogs to the runtime (%j)", async ({ args }) => { + const directory = await mkdtemp(join(tmpdir(), "zcode-launcher-account-model-")); + const fakeNode = join(directory, "fake-node"); + const personalPath = join(directory, "personal.json"); + const builtinPath = join(directory, "builtin.json"); + const providerId = "account:zai-individual-coding-plan"; + const model = `${providerId}/GLM-5.4`; + await writeFile(fakeNode, [ + `#!${node}`, + "console.log(JSON.stringify({ model: process.env.ZCODE_CLI_PROMPT_MODEL, args: process.argv.slice(3) }));", + "" + ].join("\n")); + await chmod(fakeNode, 0o755); + await writeFile(personalPath, JSON.stringify(providerFixture({ providerId: "custom", modelId: "known" }))); + await writeFile(builtinPath, JSON.stringify(providerFixture({ providerId, modelId: "GLM-5.3" }))); + try { + const result = await run(["--model", model, ...args], "", { + HOME: directory, + USERPROFILE: directory, + ZCODE_NODE: fakeNode, + ZCODE_DATA_BASE_DIR: directory, + ZCODE_PERSONAL_PROVIDER_CONFIG_FILE: personalPath, + ZCODE_BUILTIN_PROVIDER_CONFIG_FILE: builtinPath + }); + expect(result.code, result.stderr).toBe(0); + const forwarded = JSON.parse(result.stdout); + expect(forwarded.model).toBe(model); + expect(forwarded.args).not.toContain("--model"); + for (const arg of args) expect(forwarded.args).toContain(arg); + } finally { + await rm(directory, { recursive: true, force: true }); + } + } + ); + test("adds a local marketplace and installs its Plugin end to end", async () => { const marketplace = join(home, "fixture-marketplace"); const plugin = join(marketplace, "plugin"); diff --git a/test/runtime/session-model-recovery.test.ts b/test/runtime/session-model-recovery.test.ts index 5e92af6a..3bc92a47 100644 --- a/test/runtime/session-model-recovery.test.ts +++ b/test/runtime/session-model-recovery.test.ts @@ -99,22 +99,24 @@ test.each(["missing-model"] as SessionModelCase[])( }, 25_000 ); -test("headless cold resume rejects an unavailable explicit model without falling back", async () => { - await using fixture = await createSessionModelFixture("valid"); - const before = fixture.readSelection(); - const child = Bun.spawn([...fixture.command, "--resume", fixture.sessionId, "--model", "zai/unavailable", "--prompt", "Do not fall back."], { - cwd: fixture.directory, env: fixture.env, stdin: "ignore", stdout: "pipe", stderr: "pipe" - }); - const timeout = setTimeout(() => child.kill("SIGKILL"), 15_000); - try { - const [code, stdout, stderr] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); - expect(code).toBe(1); - expect(`${stdout}\n${stderr}`).toContain("zai/unavailable"); - expect(fixture.requests).toEqual([]); - expect(fixture.readSelection()).toEqual(before); - } finally { - if (child.exitCode === null) child.kill("SIGKILL"); - await child.exited; - clearTimeout(timeout); - } -}, 30_000); +test.each(["zai/unavailable", "account:zai-individual-coding-plan/unavailable"])( + "headless cold resume rejects an unavailable explicit model without falling back (%s)", async model => { + await using fixture = await createSessionModelFixture("valid"); + const before = fixture.readSelection(); + const child = Bun.spawn([...fixture.command, "--resume", fixture.sessionId, "--model", model, "--prompt", "Do not fall back."], { + cwd: fixture.directory, env: fixture.env, stdin: "ignore", stdout: "pipe", stderr: "pipe" + }); + const timeout = setTimeout(() => child.kill("SIGKILL"), 15_000); + try { + const [code, stdout, stderr] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]); + expect(code).toBe(1); + expect(`${stdout}\n${stderr}`).toContain(model); + expect(fixture.requests).toEqual([]); + expect(fixture.readSelection()).toEqual(before); + } finally { + if (child.exitCode === null) child.kill("SIGKILL"); + await child.exited; + clearTimeout(timeout); + } + }, 30_000 +);