| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { describe, expect, it } from "vitest";
- import {
- loadPluginAgentTemplates,
- resolvePluginAgentProfiles,
- loadZCodeAgentProfiles,
- } from "../src/subagents.js";
- import { loadPluginAgentProfiles } from "./helpers/subagents.js";
- function pluginMetadata(input: { enabled?: boolean; id: string; name: string; rootPath: string }) {
- return {
- commandRootCount: 0,
- components: [{ kind: "agent" as const, items: [{ name: "code-architect" }] }],
- dataPath: join(input.rootPath, ".data"),
- declaredMcpServerNames: [],
- enabled: input.enabled ?? true,
- id: input.id,
- manifestPath: join(input.rootPath, ".claude-plugin", "plugin.json"),
- marketplace: "claude-plugins-official",
- mcpServerNames: [],
- name: input.name,
- hookDetails: [],
- rootPath: input.rootPath,
- skillCount: 0,
- skillRootCount: 0,
- source: "cache" as const,
- version: "0.0.0",
- };
- }
- describe("loadZCodeAgentProfiles", () => {
- it("一次读取迁移后的插件覆盖注入规范名和别名,不修改插件文件或串用另一插件覆盖", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-selection-"));
- try {
- const storageRoot = join(root, "state");
- const pluginRoot = join(root, "plugin");
- const agentId = "plugin:fixture@personal:code-architect";
- await mkdir(join(storageRoot, "v2"), { recursive: true });
- await mkdir(join(pluginRoot, "agents"), { recursive: true });
- const path = join(pluginRoot, "agents", "code-architect.md");
- const markdown =
- "---\nname: code-architect\ndescription: Architect\nmodel: custom:p:old\nthoughtLevel: low\n---\nPrompt";
- await writeFile(path, markdown);
- await writeFile(
- join(storageRoot, "v2", "agents-state.json"),
- JSON.stringify({
- pluginAgentModelOverrides: { [agentId]: "custom:builtin%3Azai-coding-plan:GLM" },
- pluginAgentThoughtLevelOverrides: { [agentId]: "high" },
- }),
- );
- const state = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
- const selection = {
- providerId: "account:zai-individual-coding-plan",
- modelId: "GLM",
- options: { reasoningLevel: "high" },
- };
- expect(state.pluginAgentModelSelectionOverrides).toEqual({ [agentId]: selection });
- const plugins = [
- pluginMetadata({ id: "fixture@personal", name: "fixture", rootPath: pluginRoot }),
- ];
- const templates = loadPluginAgentTemplates({ plugins });
- const result = resolvePluginAgentProfiles(
- { plugins, modelSelectionOverrides: state.pluginAgentModelSelectionOverrides },
- templates,
- );
- // 保存后只组合已读取模板;清除覆盖恢复插件原值,新增自定义同名定义移除裸名别名。
- await rm(path);
- const restored = resolvePluginAgentProfiles(
- { plugins, reservedProfileNames: ["code-architect"] },
- templates,
- );
- expect(restored.profiles).toHaveLength(1);
- expect(restored.profiles[0]?.modelSelection).toEqual({
- providerId: "p",
- modelId: "old",
- options: { reasoningLevel: "low" },
- });
- await writeFile(path, markdown);
- expect(result.profiles.map((p) => [p.name, p.modelSelection])).toEqual([
- ["fixture:code-architect", selection],
- ["code-architect", selection],
- ]);
- const modelOnly = { providerId: "p", modelId: "new" };
- expect(
- loadPluginAgentProfiles({ plugins, modelSelectionOverrides: { [agentId]: modelOnly } })
- .profiles[0]?.modelSelection,
- ).toEqual(modelOnly);
- expect(
- loadPluginAgentProfiles({
- plugins,
- modelSelectionOverrides: { "plugin:other@personal:code-architect": selection },
- }).profiles[0]?.modelSelection,
- ).toEqual({
- providerId: "p",
- modelId: "old",
- options: { reasoningLevel: "low" },
- });
- expect(await readFile(path, "utf8")).toBe(markdown);
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- });
- it("loads only direct user and project Markdown files from beta-compatible roots", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagents-"));
- const storageRoot = join(root, "home");
- const workingDirectory = join(root, "workspace");
- try {
- await mkdir(join(storageRoot, "agents"), { recursive: true });
- await mkdir(join(workingDirectory, ".zcode", "agents"), { recursive: true });
- for (const agentsRoot of [
- join(storageRoot, "agents"),
- join(workingDirectory, ".zcode", "agents"),
- ]) {
- await mkdir(join(agentsRoot, "nested"));
- await writeFile(
- join(agentsRoot, "nested", "ignored.markdown"),
- "---\nname: nested-reviewer\ndescription: Nested\n---\nNested prompt",
- );
- }
- await writeFile(
- join(storageRoot, "agents", "reviewer.md"),
- `---
- name: zcode-reviewer
- description: 用户级中文审查 agent
- ---
- 用户级 prompt`,
- "utf8",
- );
- await writeFile(
- join(workingDirectory, ".zcode", "agents", "reviewer.md"),
- `---
- name: zcode-reviewer
- description: 项目级中文审查 agent
- ---
- 项目级 prompt`,
- "utf8",
- );
- const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- expect(result.diagnostics).toEqual([]);
- expect(result.profiles.map((profile) => profile.source)).toEqual(["user", "project"]);
- expect(result.profiles.map((profile) => profile.description)).toEqual([
- "用户级中文审查 agent",
- "项目级中文审查 agent",
- ]);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("ignores project Markdown permissionMode because project profiles are untrusted", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagents-project-permission-"));
- const storageRoot = join(root, "home");
- const workingDirectory = join(root, "workspace");
- try {
- await mkdir(join(storageRoot, "agents"), { recursive: true });
- await mkdir(join(workingDirectory, ".zcode", "agents"), { recursive: true });
- await writeFile(
- join(storageRoot, "agents", "trusted.md"),
- `---
- name: trusted-runner
- description: 用户级运行 agent
- permissionMode: bypassPermissions
- ---
- 用户级 prompt`,
- "utf8",
- );
- await writeFile(
- join(workingDirectory, ".zcode", "agents", "attacker.md"),
- `---
- name: attacker-runner
- description: 项目级运行 agent
- permissionMode: bypassPermissions
- ---
- 项目级 prompt`,
- "utf8",
- );
- const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- const userProfile = result.profiles.find((profile) => profile.name === "trusted-runner");
- const projectProfile = result.profiles.find((profile) => profile.name === "attacker-runner");
- expect(userProfile?.permissionMode).toBe("bypassPermissions");
- expect(projectProfile?.permissionMode).toBeUndefined();
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("reports diagnostics for invalid agent Markdown without failing the load", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagents-invalid-"));
- const storageRoot = join(root, "home");
- const workingDirectory = join(root, "workspace");
- try {
- await mkdir(join(storageRoot, "agents"), { recursive: true });
- await writeFile(
- join(storageRoot, "agents", "invalid.md"),
- `---
- name: missing-description
- ---
- prompt`,
- "utf8",
- );
- const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- expect(result.profiles).toEqual([]);
- expect(result.diagnostics).toHaveLength(1);
- expect(result.diagnostics[0]?.code).toBe("agent_missing_required_frontmatter");
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("skips disabled user Markdown profiles from shared subagent state", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagents-disabled-"));
- const storageRoot = join(root, "home");
- const workingDirectory = join(root, "workspace");
- try {
- await mkdir(join(storageRoot, "agents"), { recursive: true });
- await mkdir(join(storageRoot, "v2"), { recursive: true });
- await writeFile(
- join(storageRoot, "agents", "reviewer.md"),
- `---
- name: zcode-reviewer
- description: 用户级中文审查 agent
- ---
- 用户级 prompt`,
- "utf8",
- );
- await writeFile(
- join(storageRoot, "agents", "runner.md"),
- `---
- name: zcode-runner
- description: 用户级运行 agent
- ---
- 用户级 prompt`,
- "utf8",
- );
- await writeFile(
- join(storageRoot, "v2", "agents-state.json"),
- JSON.stringify({ disabledAgentIds: ["user:user:zcode-reviewer"] }),
- "utf8",
- );
- const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- expect(result.diagnostics).toEqual([]);
- expect(result.profiles.map((profile) => profile.name)).toEqual(["zcode-runner"]);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("loads structured built-in model selections from shared subagent state", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagents-built-in-models-"));
- const storageRoot = join(root, "home");
- const workingDirectory = join(root, "workspace");
- try {
- await mkdir(join(storageRoot, "v2"), { recursive: true });
- await writeFile(
- join(storageRoot, "v2", "agents-state.json"),
- JSON.stringify({
- builtInModelSelectionOverrides: {
- Explore: {
- providerId: "custom-openai",
- modelId: "glm-5.2",
- options: { reasoningLevel: "max" },
- },
- "general-purpose": {
- providerId: "custom-openai",
- modelId: "gpt-5.4",
- options: { reasoningLevel: "high" },
- },
- ignored: {
- providerId: "custom-openai",
- modelId: "ignored",
- },
- },
- disabledAgentIds: [],
- }),
- "utf8",
- );
- const result = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- expect(result.diagnostics).toEqual([]);
- expect(result.profiles).toEqual([]);
- expect(result.builtInModelSelectionOverrides).toEqual({
- Explore: {
- providerId: "custom-openai",
- modelId: "glm-5.2",
- options: { reasoningLevel: "max" },
- },
- "general-purpose": {
- providerId: "custom-openai",
- modelId: "gpt-5.4",
- options: { reasoningLevel: "high" },
- },
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- });
- describe("loadPluginAgentProfiles", () => {
- it("SA97-09:用户迁移和重新加载不改项目/插件文件或其显式旧身份", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-subagent-scope-migration-"));
- const storageRoot = join(root, "storage");
- const workingDirectory = join(root, "workspace");
- const pluginRoot = join(root, "plugin");
- const paths = [
- join(storageRoot, "agents", "code-architect.md"),
- join(workingDirectory, ".zcode", "agents", "code-architect.md"),
- join(pluginRoot, "agents", "code-architect.md"),
- ];
- const markdown =
- "---\r\nname: code-architect\r\ndescription: review\r\n# 保留备注\r\nmodel: builtin:zai-coding-plan/GLM\r\nthoughtLevel: high\r\n---\r\n正文 builtin:zai-coding-plan/GLM\r\n";
- try {
- for (const directory of [
- join(storageRoot, "agents"),
- join(workingDirectory, ".zcode", "agents"),
- join(pluginRoot, "agents"),
- ]) {
- await mkdir(directory, { recursive: true });
- }
- for (const path of paths) await writeFile(path, markdown);
- for (let load = 0; load < 2; load++) {
- const local = await loadZCodeAgentProfiles({ storageRoot, workingDirectory });
- const plugins = loadPluginAgentProfiles({
- plugins: [pluginMetadata({ id: "scope-test", name: "scope-test", rootPath: pluginRoot })],
- });
- expect(local.diagnostics).toEqual([]);
- expect(plugins.diagnostics).toEqual([]);
- expect(
- local.profiles.find((profile) => profile.source === "user")?.modelSelection?.providerId,
- ).toBe("account:zai-individual-coding-plan");
- expect(
- local.profiles.find((profile) => profile.source === "project")?.modelSelection
- ?.providerId,
- ).toBe("builtin:zai-coding-plan");
- for (const profile of plugins.profiles)
- expect(profile.modelSelection).toEqual({
- providerId: "builtin:zai-coding-plan",
- modelId: "GLM",
- options: { reasoningLevel: "high" },
- });
- expect(plugins.profiles.length).toBeGreaterThan(0);
- expect(await readFile(paths[0]!, "utf8")).toBe(
- markdown.replace(
- "model: builtin:zai-coding-plan",
- "model: account:zai-individual-coding-plan",
- ),
- );
- for (const path of paths.slice(1)) expect(await readFile(path, "utf8")).toBe(markdown);
- }
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("loads enabled plugin agents as runtime profiles", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-"));
- const pluginRoot = join(root, "feature-dev");
- try {
- await mkdir(join(pluginRoot, "agents"), { recursive: true });
- await writeFile(
- join(pluginRoot, "agents", "code-architect.md"),
- `---
- name: code-architect
- description: 设计功能架构
- tools:
- - Read
- ---
- 架构 prompt`,
- "utf8",
- );
- const result = loadPluginAgentProfiles({
- plugins: [
- pluginMetadata({
- id: "feature-dev@claude-plugins-official",
- name: "feature-dev",
- rootPath: pluginRoot,
- }),
- ],
- });
- expect(result.diagnostics).toEqual([]);
- expect(result.profiles.map((profile) => profile.name)).toEqual([
- "feature-dev:code-architect",
- "code-architect",
- ]);
- expect(result.profiles[0]).toMatchObject({
- source: "user",
- tools: ["Read"],
- systemPrompt: "架构 prompt",
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("applies plugin agent model overrides from shared subagent state to canonical and alias profiles", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-overrides-"));
- const storageRoot = join(root, "home");
- const pluginRoot = join(root, "document-skills");
- try {
- await mkdir(join(pluginRoot, "agents"), { recursive: true });
- await mkdir(join(storageRoot, "v2"), { recursive: true });
- await writeFile(
- join(pluginRoot, "agents", "code-architect.md"),
- `---
- name: code-architect
- description: 插件默认模型
- model: custom:custom-openai:gpt-5.4
- thoughtLevel: low
- ---
- 架构 prompt`,
- "utf8",
- );
- await writeFile(
- join(storageRoot, "v2", "agents-state.json"),
- JSON.stringify({
- pluginAgentModelOverrides: {
- "plugin:document-skills@zcode-plugins-official:code-architect":
- "custom:custom-openai:glm-5.2",
- "plugin:other@zcode-plugins-official:code-architect": "custom:custom-openai:ignored",
- },
- pluginAgentThoughtLevelOverrides: {
- "plugin:document-skills@zcode-plugins-official:code-architect": "high",
- },
- }),
- "utf8",
- );
- const plugins = [
- pluginMetadata({
- id: "document-skills@zcode-plugins-official",
- name: "document-skills",
- rootPath: pluginRoot,
- }),
- ];
- const state = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
- const overridden = loadPluginAgentProfiles({
- plugins,
- modelSelectionOverrides: state.pluginAgentModelSelectionOverrides,
- });
- const selection = {
- providerId: "custom-openai",
- modelId: "glm-5.2",
- options: { reasoningLevel: "high" },
- };
- // 规范名与裸名别名是同一文件的两个调用入口,必须共享同一覆盖。
- expect(overridden.profiles.map((profile) => [profile.name, profile.modelSelection])).toEqual([
- ["document-skills:code-architect", selection],
- ["code-architect", selection],
- ]);
- // 未传入覆盖时保留 md 声明;插件 loader 不单独读取 state 文件。
- const untouched = loadPluginAgentProfiles({ plugins });
- expect(untouched.profiles[0]).toMatchObject({
- modelSelection: {
- providerId: "custom-openai",
- modelId: "gpt-5.4",
- options: { reasoningLevel: "low" },
- },
- });
- // 覆盖 model 但未指定 thoughtLevel 时,不能沿用 md 里另一模型的档位。
- await writeFile(
- join(storageRoot, "v2", "agents-state.json"),
- JSON.stringify({
- pluginAgentModelOverrides: {
- "plugin:document-skills@zcode-plugins-official:code-architect":
- "custom:custom-openai:glm-5.2",
- },
- }),
- "utf8",
- );
- const modelOnlyState = await loadZCodeAgentProfiles({ storageRoot, workingDirectory: root });
- const modelOnly = loadPluginAgentProfiles({
- plugins,
- modelSelectionOverrides: modelOnlyState.pluginAgentModelSelectionOverrides,
- });
- expect(modelOnly.profiles[0]?.modelSelection).toEqual({
- providerId: "custom-openai",
- modelId: "glm-5.2",
- });
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- it("skips disabled plugin agents and ambiguous bare aliases", async () => {
- const root = await mkdtemp(join(tmpdir(), "zcode-plugin-subagents-ambiguous-"));
- const firstRoot = join(root, "feature-dev");
- const secondRoot = join(root, "pr-review-toolkit");
- try {
- for (const pluginRoot of [firstRoot, secondRoot]) {
- await mkdir(join(pluginRoot, "agents"), { recursive: true });
- await writeFile(
- join(pluginRoot, "agents", "code-architect.md"),
- `---
- name: code-architect
- description: 设计功能架构
- ---
- architect prompt`,
- "utf8",
- );
- }
- const result = loadPluginAgentProfiles({
- plugins: [
- pluginMetadata({
- id: "feature-dev@claude-plugins-official",
- name: "feature-dev",
- rootPath: firstRoot,
- }),
- pluginMetadata({
- id: "pr-review-toolkit@claude-plugins-official",
- name: "pr-review-toolkit",
- rootPath: secondRoot,
- }),
- pluginMetadata({
- enabled: false,
- id: "disabled@claude-plugins-official",
- name: "disabled",
- rootPath: join(root, "disabled"),
- }),
- ],
- });
- expect(result.profiles.map((profile) => profile.name)).toEqual([
- "feature-dev:code-architect",
- "pr-review-toolkit:code-architect",
- ]);
- expect(result.diagnostics.map((diagnostic) => diagnostic.code)).toEqual([
- "agent_ambiguous_name",
- "agent_ambiguous_name",
- ]);
- } finally {
- await rm(root, { force: true, recursive: true });
- }
- });
- });
|