| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- // PLG02-PLG05/PLG10 机制单测(docs/plugin-reference-mention.md、
- // docs/conversation-session-case-catalog.md PLG 组):
- // 严格 URI 解析、label 欺骗、未知/禁用/冲突 fail closed、live 交集、去重与上限、模板序列化。
- import { describe, expect, it } from "vitest";
- import type { PluginMetadata, PluginReferenceCatalog } from "@zcode/contracts";
- import {
- buildPluginReferenceCatalog,
- buildPluginReferenceReminderBody,
- extractPluginReferences,
- isValidPluginStableId,
- MAX_PLUGIN_REFERENCE_REMINDER_BYTES,
- MAX_PLUGIN_REFERENCES_PER_TURN,
- type LivePluginMcpServer,
- type LivePluginSkill,
- type LivePluginSubagent,
- } from "../src/plugin-reference/index.js";
- function pluginMetadata(
- overrides: Partial<PluginMetadata> & { id: string; name: string; marketplace: string },
- ): PluginMetadata {
- return {
- commandRootCount: 0,
- components: [],
- dataPath: `/data/${overrides.name}`,
- declaredMcpServerNames: [],
- enabled: true,
- hookDetails: [],
- manifestPath: `/plugins/${overrides.name}/plugin.json`,
- mcpServerNames: [],
- rootPath: `/plugins/${overrides.name}`,
- skillCount: 0,
- skillRootCount: 0,
- source: "cache",
- ...overrides,
- };
- }
- function catalogWith(
- entries: Partial<PluginReferenceCatalog["plugins"][number]>[],
- ): PluginReferenceCatalog {
- return {
- plugins: entries.map((entry) => ({
- pluginId: "demo@mkt-a",
- name: "demo",
- marketplace: "mkt-a",
- enabled: true,
- conflictingPluginIds: [],
- skillQualifiedNames: [],
- mcpServerNames: [],
- subagentNames: [],
- rootPath: "/plugins/demo",
- ...entry,
- })),
- };
- }
- const DEMO_SKILL: LivePluginSkill = {
- qualifiedName: "demo:search",
- pluginName: "demo",
- rootPath: "/plugins/demo/skills",
- source: "plugin",
- };
- const DEMO_MCP: LivePluginMcpServer = {
- serverName: "plugin:demo:main",
- connected: true,
- providerVisibleToolCount: 2,
- };
- const DEMO_SUBAGENT: LivePluginSubagent = {
- name: "demo:reviewer",
- path: "/plugins/demo/agents/reviewer.md",
- };
- describe("plugin reference strict URI parser", () => {
- it("extracts stable ids from canonical markdown links in first-appearance order", () => {
- const { references, invalidCount, truncatedCount } = extractPluginReferences(
- "check [@B](plugin://b@mkt) then [@A](plugin://a@mkt) and [@B again](plugin://b@mkt)",
- );
- expect(references).toEqual(["b@mkt", "a@mkt"]);
- expect(invalidCount).toBe(0);
- expect(truncatedCount).toBe(0);
- });
- it("takes identity only from the destination, never from the label", () => {
- const { references } = extractPluginReferences(
- "[@innocent-name](plugin://evil@mkt) and [@evil](./README.md)",
- );
- expect(references).toEqual(["evil@mkt"]);
- });
- it("rejects scheme case variants, query, fragment, credentials, percent and control characters", () => {
- const invalidInputs = [
- "[@x](Plugin://demo@mkt)",
- "[@x](PLUGIN://demo@mkt)",
- "[@x](plugin://demo@mkt?inject=1)",
- "[@x](plugin://demo@mkt#frag)",
- "[@x](plugin://user:pass@demo@mkt)",
- "[@x](plugin://demo%40mkt)",
- "[@x](<plugin://demo@mkt>)",
- "[@x](plugin://demo)",
- "[@x](plugin://@mkt)",
- "[@x](plugin://demo@)",
- ];
- for (const input of invalidInputs) {
- const { references } = extractPluginReferences(input);
- expect(references, input).toEqual([]);
- }
- });
- it("caps per-turn references and reports truncation", () => {
- const text = Array.from(
- { length: MAX_PLUGIN_REFERENCES_PER_TURN + 3 },
- (_, index) => `[@p${index}](plugin://p${index}@mkt)`,
- ).join(" ");
- const { references, truncatedCount } = extractPluginReferences(text);
- expect(references).toHaveLength(MAX_PLUGIN_REFERENCES_PER_TURN);
- expect(truncatedCount).toBe(3);
- });
- it("validates stable id charset and single separator strictly", () => {
- expect(isValidPluginStableId("demo@mkt-a")).toBe(true);
- expect(isValidPluginStableId("demo.plugin_1@zcode-plugins-official")).toBe(true);
- expect(isValidPluginStableId("demo@mkt@extra")).toBe(false);
- expect(isValidPluginStableId("demo mkt@a")).toBe(false);
- expect(isValidPluginStableId(`a@${"b".repeat(300)}`)).toBe(false);
- expect(isValidPluginStableId("-demo@mkt")).toBe(false);
- });
- });
- describe("plugin reference catalog builder", () => {
- it("marks enabled same-manifest-name plugins as mutually conflicting (fail closed)", () => {
- const catalog = buildPluginReferenceCatalog([
- pluginMetadata({ id: "demo@mkt-a", name: "demo", marketplace: "mkt-a" }),
- pluginMetadata({ id: "demo@mkt-b", name: "demo", marketplace: "mkt-b" }),
- pluginMetadata({ id: "other@mkt-a", name: "other", marketplace: "mkt-a" }),
- ]);
- const byId = new Map(catalog.plugins.map((entry) => [entry.pluginId, entry]));
- expect(byId.get("demo@mkt-a")?.conflictingPluginIds).toEqual(["demo@mkt-b"]);
- expect(byId.get("demo@mkt-b")?.conflictingPluginIds).toEqual(["demo@mkt-a"]);
- expect(byId.get("other@mkt-a")?.conflictingPluginIds).toEqual([]);
- });
- it("does not treat a disabled duplicate as a conflict and keeps disabled entries for diagnostics", () => {
- const catalog = buildPluginReferenceCatalog([
- pluginMetadata({ id: "demo@mkt-a", name: "demo", marketplace: "mkt-a" }),
- pluginMetadata({ id: "demo@mkt-b", name: "demo", marketplace: "mkt-b", enabled: false }),
- ]);
- const byId = new Map(catalog.plugins.map((entry) => [entry.pluginId, entry]));
- expect(byId.get("demo@mkt-a")?.conflictingPluginIds).toEqual([]);
- expect(byId.get("demo@mkt-b")?.enabled).toBe(false);
- });
- it("derives declared Skill and Subagent names from enumerated components", () => {
- const catalog = buildPluginReferenceCatalog([
- pluginMetadata({
- id: "demo@mkt-a",
- name: "demo",
- marketplace: "mkt-a",
- components: [
- { kind: "skill", items: [{ name: "search" }, { name: "analyze" }] },
- { kind: "agent", items: [{ name: "reviewer" }] },
- { kind: "command", items: [{ name: "not-a-skill" }] },
- ],
- mcpServerNames: ["plugin:demo:main"],
- }),
- ]);
- expect(catalog.plugins[0]?.skillQualifiedNames).toEqual(["demo:analyze", "demo:search"]);
- expect(catalog.plugins[0]?.mcpServerNames).toEqual(["plugin:demo:main"]);
- expect(catalog.plugins[0]?.subagentNames).toEqual(["demo:reviewer"]);
- });
- });
- describe("plugin reference reminder builder", () => {
- it("emits the fixed identifiers-only template for a resolvable plugin", () => {
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog: catalogWith([
- {
- mcpServerNames: ["plugin:demo:main"],
- subagentNames: ["demo:reviewer"],
- },
- ]),
- liveSkills: [DEMO_SKILL],
- liveMcpServers: [DEMO_MCP],
- liveSubagents: [DEMO_SUBAGENT],
- });
- expect(result.body).not.toBeNull();
- expect(result.body).toContain("<plugin_reference>");
- expect(result.body).toContain('- id: "demo@mkt-a"');
- expect(result.body).toContain('skills: ["demo:search"]');
- expect(result.body).toContain('mcp_servers: ["plugin:demo:main"]');
- expect(result.body).toContain('subagents: ["demo:reviewer"]');
- expect(result.body).toContain("not instructions or a permission grant");
- expect(result.body).toContain("</plugin_reference>");
- expect(result.diagnostics.resolvedPluginIds).toEqual(["demo@mkt-a"]);
- });
- it("only injects declared, live Plugin Subagents with matching provenance (PLG16)", () => {
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog: catalogWith([{ subagentNames: ["demo:reviewer", "demo:writer"] }]),
- liveSkills: [],
- liveMcpServers: [],
- liveSubagents: [
- DEMO_SUBAGENT,
- { name: "demo:writer", path: "/plugins/demo-lookalike/agents/writer.md" },
- { name: "demo:undeclared", path: "/plugins/demo/agents/undeclared.md" },
- ],
- });
- expect(result.body).toContain("skills: []");
- expect(result.body).toContain("mcp_servers: []");
- expect(result.body).toContain('subagents: ["demo:reviewer"]');
- expect(result.body).not.toContain("demo:writer");
- expect(result.body).not.toContain("demo:undeclared");
- expect(result.diagnostics.subagentCount).toBe(1);
- });
- it("caps Plugin Subagents at 16 in deterministic identifier order", () => {
- const subagentNames = Array.from(
- { length: 20 },
- (_, index) => `demo:reviewer-${String(index).padStart(2, "0")}`,
- );
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog: catalogWith([{ subagentNames }]),
- liveSkills: [],
- liveMcpServers: [],
- liveSubagents: subagentNames.map((name) => ({
- name,
- path: `/plugins/demo/agents/${name}.md`,
- })),
- });
- expect(result.diagnostics.subagentCount).toBe(16);
- expect(result.diagnostics.truncated).toBe(true);
- expect(result.body).toContain('"demo:reviewer-00"');
- expect(result.body).toContain('"demo:reviewer-15"');
- expect(result.body).not.toContain('"demo:reviewer-16"');
- });
- it("rejects invalid and overlong Subagent identifiers without exposing profile data", () => {
- const tooLongName = `demo:${"x".repeat(124)}`;
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog: catalogWith([
- {
- subagentNames: ["demo:bad\nidentifier", tooLongName],
- },
- ]),
- liveSkills: [],
- liveMcpServers: [],
- liveSubagents: [
- { name: "demo:bad\nidentifier", path: "/plugins/demo/agents/bad.md" },
- { name: tooLongName, path: "/plugins/demo/agents/too-long.md" },
- ],
- });
- expect(result.body).toBeNull();
- expect(JSON.stringify(result)).not.toContain("/plugins/demo/agents");
- expect(result.diagnostics.skipped).toEqual(
- expect.arrayContaining([
- { pluginId: "demo@mkt-a", reason: "invalid_identifier" },
- { pluginId: "demo@mkt-a", reason: "no_live_capabilities" },
- ]),
- );
- });
- it("skips unknown, disabled-in-session and ambiguous references without failing the turn", () => {
- const catalog: PluginReferenceCatalog = {
- plugins: [
- {
- pluginId: "conflict@mkt-a",
- name: "conflict",
- marketplace: "mkt-a",
- enabled: true,
- conflictingPluginIds: ["conflict@mkt-b"],
- skillQualifiedNames: ["conflict:tool"],
- mcpServerNames: [],
- subagentNames: [],
- rootPath: "/plugins/conflict",
- },
- {
- pluginId: "off@mkt-a",
- name: "off",
- marketplace: "mkt-a",
- enabled: false,
- conflictingPluginIds: [],
- skillQualifiedNames: [],
- mcpServerNames: [],
- subagentNames: [],
- rootPath: "/plugins/off",
- },
- ],
- };
- const result = buildPluginReferenceReminderBody({
- references: ["nope@mkt-a", "off@mkt-a", "conflict@mkt-a"],
- catalog,
- liveSkills: [],
- liveMcpServers: [],
- });
- expect(result.body).toBeNull();
- expect(result.diagnostics.skipped).toEqual([
- { pluginId: "nope@mkt-a", reason: "unknown" },
- { pluginId: "off@mkt-a", reason: "disabled_in_session" },
- { pluginId: "conflict@mkt-a", reason: "ambiguous" },
- ]);
- });
- it("intersects with live inventory: disconnected or tool-less MCP servers are excluded", () => {
- const catalog = catalogWith([{ mcpServerNames: ["plugin:demo:main", "plugin:demo:aux"] }]);
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: [DEMO_SKILL],
- liveMcpServers: [
- { serverName: "plugin:demo:main", connected: false, providerVisibleToolCount: 2 },
- { serverName: "plugin:demo:aux", connected: true, providerVisibleToolCount: 0 },
- ],
- });
- expect(result.body).toContain("mcp_servers: []");
- expect(result.body).toContain('skills: ["demo:search"]');
- });
- it("enforces skill provenance by plugin name and rootPath prefix", () => {
- const catalog = catalogWith([{}]);
- const foreignSkills: LivePluginSkill[] = [
- { ...DEMO_SKILL, pluginName: "other" },
- { ...DEMO_SKILL, rootPath: "/plugins/demo-lookalike/skills" },
- { ...DEMO_SKILL, source: "user" },
- ];
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: foreignSkills,
- liveMcpServers: [],
- });
- expect(result.body).toBeNull();
- expect(result.diagnostics.skipped).toEqual([
- { pluginId: "demo@mkt-a", reason: "no_live_capabilities" },
- ]);
- });
- it("only counts MCP servers declared in the frozen catalog entry", () => {
- const catalog = catalogWith([{ mcpServerNames: [] }]);
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: [],
- liveMcpServers: [DEMO_MCP],
- });
- expect(result.body).toBeNull();
- });
- it("omits the entire reminder when every referenced plugin has no live capability", () => {
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog: catalogWith([{}]),
- liveSkills: [],
- liveMcpServers: [],
- });
- expect(result.body).toBeNull();
- expect(result.diagnostics.resolvedPluginIds).toEqual([]);
- });
- it("dedupes and sorts capability identifiers and JSON-escapes dynamic values", () => {
- const catalog = catalogWith([{ mcpServerNames: ["plugin:demo:main"] }]);
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: [
- { ...DEMO_SKILL, qualifiedName: "demo:zeta" },
- { ...DEMO_SKILL, qualifiedName: "demo:alpha" },
- { ...DEMO_SKILL, qualifiedName: "demo:alpha" },
- ],
- liveMcpServers: [DEMO_MCP],
- });
- expect(result.body).toContain('skills: ["demo:alpha", "demo:zeta"]');
- // JSON.stringify 转义由固定引用格式承载:标识符本身经过字符集校验,不含引号。
- expect(result.body).not.toContain("skills: [demo:alpha");
- });
- it("rejects capability identifiers outside the allowed charset (fail closed per entry)", () => {
- const catalog = catalogWith([{}]);
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: [{ ...DEMO_SKILL, qualifiedName: "demo:evil\ninjection" }],
- liveMcpServers: [],
- });
- expect(result.body).toBeNull();
- expect(result.diagnostics.skipped).toEqual(
- expect.arrayContaining([
- { pluginId: "demo@mkt-a", reason: "invalid_identifier" },
- { pluginId: "demo@mkt-a", reason: "no_live_capabilities" },
- ]),
- );
- });
- it("keeps plugins in reference order and truncates over per-turn capability budgets", () => {
- const catalog: PluginReferenceCatalog = {
- plugins: Array.from({ length: 3 }, (_, index) => ({
- pluginId: `p${index}@mkt`,
- name: `p${index}`,
- marketplace: "mkt",
- enabled: true,
- conflictingPluginIds: [],
- skillQualifiedNames: [],
- mcpServerNames: [],
- subagentNames: [],
- rootPath: `/plugins/p${index}`,
- })),
- };
- const liveSkills: LivePluginSkill[] = catalog.plugins.flatMap((entry) =>
- Array.from({ length: 20 }, (_, skillIndex) => ({
- qualifiedName: `${entry.name}:s${String(skillIndex).padStart(2, "0")}`,
- pluginName: entry.name,
- rootPath: `${entry.rootPath}/skills`,
- source: "plugin",
- })),
- );
- const result = buildPluginReferenceReminderBody({
- references: ["p0@mkt", "p1@mkt", "p2@mkt"],
- catalog,
- liveSkills,
- liveMcpServers: [],
- });
- // 32 个 Skill 预算:p0 占 20、p1 占 12(截断)、p2 无预算整条跳过。
- expect(result.diagnostics.resolvedPluginIds).toEqual(["p0@mkt", "p1@mkt"]);
- expect(result.diagnostics.skillCount).toBe(32);
- expect(result.diagnostics.truncated).toBe(true);
- expect(result.diagnostics.skipped).toEqual(
- expect.arrayContaining([{ pluginId: "p2@mkt", reason: "no_live_capabilities" }]),
- );
- });
- it("stays under the reminder byte budget by dropping trailing plugins", () => {
- // 逼近上限:长 stable ID(≤256)+ 长 capability 标识(≤128),总渲染量超过 8 KiB,
- // 验证按用户引用顺序保留前项、从尾部整条丢弃的截断路径。
- const idSuffix = "m".repeat(240);
- const catalog: PluginReferenceCatalog = {
- plugins: Array.from({ length: 8 }, (_, index) => ({
- pluginId: `p${index}@${idSuffix}`,
- name: `p${index}`,
- marketplace: idSuffix,
- enabled: true,
- conflictingPluginIds: [],
- skillQualifiedNames: [],
- mcpServerNames: Array.from(
- { length: 2 },
- (_, serverIndex) => `plugin:p${index}:${"x".repeat(100)}${serverIndex}`,
- ),
- subagentNames: [],
- rootPath: `/plugins/p${index}`,
- })),
- };
- const liveSkills: LivePluginSkill[] = catalog.plugins.flatMap((entry) =>
- Array.from({ length: 4 }, (_, skillIndex) => ({
- qualifiedName: `${entry.name}:${"s".repeat(120)}${skillIndex}`,
- pluginName: entry.name,
- rootPath: `${entry.rootPath}/skills`,
- source: "plugin",
- })),
- );
- const liveMcpServers: LivePluginMcpServer[] = catalog.plugins.flatMap((entry) =>
- entry.mcpServerNames.map((serverName) => ({
- serverName,
- connected: true,
- providerVisibleToolCount: 1,
- })),
- );
- const result = buildPluginReferenceReminderBody({
- references: catalog.plugins.map((entry) => entry.pluginId),
- catalog,
- liveSkills,
- liveMcpServers,
- });
- expect(result.body).not.toBeNull();
- expect(Buffer.byteLength(result.body ?? "", "utf8")).toBeLessThanOrEqual(
- MAX_PLUGIN_REFERENCE_REMINDER_BYTES,
- );
- expect(result.diagnostics.truncated).toBe(true);
- expect(result.diagnostics.resolvedPluginIds.length).toBeLessThan(catalog.plugins.length);
- expect(result.diagnostics.resolvedPluginIds[0]).toBe(catalog.plugins[0]?.pluginId);
- });
- it("never invents capabilities from tools filtered out by allow/disallow policy", () => {
- // provider-visible tool 计数由调用侧按注册表(策略过滤后)给出;计数为 0 时 fail closed。
- const catalog = catalogWith([{ mcpServerNames: ["plugin:demo:main"] }]);
- const result = buildPluginReferenceReminderBody({
- references: ["demo@mkt-a"],
- catalog,
- liveSkills: [],
- liveMcpServers: [
- { serverName: "plugin:demo:main", connected: true, providerVisibleToolCount: 0 },
- ],
- });
- expect(result.body).toBeNull();
- });
- });
|