Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/prompt-model-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ async function readCatalog(path: string, missingIsEmpty = false): Promise<Map<st
}

/**
* Reject a CLI override before the interactive runtime starts when both local
* Reject a CLI override before the runtime starts when both local
* registries are readable and conclusively show that the selection is absent.
* If either registry is unavailable, defer to the runtime to avoid rejecting
* models supplied by a remote or not-yet-materialized catalog.
* Account catalogs can add models and providers beyond the local registries,
* so the runtime owns their validation. Also defer if either registry is unavailable.
*/
export async function invalidConfiguredPromptModel(
model: string | undefined,
env: NodeJS.ProcessEnv = process.env
): Promise<string | undefined> {
if (!model) return undefined;
const separator = model.indexOf("/");
const providerId = model.slice(0, separator);
if (providerId.startsWith("account:")) return undefined;

let personal: Map<string, Set<string>> | undefined;
let builtin: Map<string, Set<string>> | undefined;
try {
Expand All @@ -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);
Expand Down
24 changes: 22 additions & 2 deletions test/prompt-model-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand Down
36 changes: 36 additions & 0 deletions test/runtime/launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
40 changes: 21 additions & 19 deletions test/runtime/session-model-recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Loading