Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): deduplicate overlapping skill discovery sources

xdc34ef3976047
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-skills.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-skills.test.ts
index b43afa3af4..211a218d80 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-skills.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-skills.test.ts
@@ -5,8 +5,8 @@
* SKILL.md,与 CC/Codex 的技能可见性对齐;发现层零基线上下文(仅 name/description)。
*/
-import { mkdtempSync, mkdirSync, realpathSync, writeFileSync, rmSync } from 'node:fs';
-import { tmpdir } from 'node:os';
+import { mkdtempSync, mkdirSync, realpathSync, symlinkSync, writeFileSync, rmSync } from 'node:fs';
+import os, { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -43,8 +43,10 @@ describe('PiAgent.listAgentSkills (filesystem discovery, no binary spawn)', () =
let workingDir = '';
beforeEach(() => {
workingDir = mkdtempSync(path.join(tmpdir(), 'pi-skills-'));
+ vi.spyOn(os, 'homedir').mockReturnValue(workingDir);
});
afterEach(() => {
+ vi.restoreAllMocks();
rmSync(workingDir, { recursive: true, force: true });
});
@@ -73,8 +75,66 @@ describe('PiAgent.listAgentSkills (filesystem discovery, no binary spawn)', () =
it('returns a skills array without throwing when the project has no skill dirs', async () => {
const agent = new PiAgent(buildDeps());
const result = await agent.listAgentSkills({ workingDir });
- // 真实环境可能有用户级 ~/.agents/skills,故只断言"不抛 + 是数组",不断言空。
- expect(Array.isArray(result.skills)).toBe(true);
+ expect(result.skills).toEqual([]);
+ });
+
+ it.each([true, false])('keeps one managed entry and its enabled=%s state when discovery follows a symlink to it', async (enabled) => {
+ const source = path.join(workingDir, 'managed', 'shared');
+ const sharedRoot = path.join(workingDir, '.agents', 'skills');
+ mkdirSync(source, { recursive: true });
+ mkdirSync(sharedRoot, { recursive: true });
+ const entry = path.join(source, 'SKILL.md');
+ writeFileSync(entry, '---\nname: shared\ndescription: scanned description\n---\n');
+ symlinkSync(source, path.join(sharedRoot, 'shared'), process.platform === 'win32' ? 'junction' : 'dir');
+ const managed = {
+ kind: 'agent-skill' as const, source: 'skill' as const, scope: 'user' as const,
+ name: 'shared', description: 'managed description', path: entry, enabled,
+ claudeCommandName: 'managed-shared',
+ };
+ const agent = new PiAgent({ ...buildDeps(), getManagedSkills: async () => [managed] });
+
+ const result = await agent.listAgentSkills({ workingDir });
+
+ expect(result.skills).toEqual([{
+ kind: 'agent-skill', source: 'skill', scope: 'user',
+ name: 'shared', description: 'managed description', path: entry, enabled,
+ runtimeCommandName: 'skill:shared',
+ }]);
+ });
+
+ it('deduplicates overlapping package paths without hiding same-name skills from different files', async () => {
+ const source = path.join(workingDir, '.pi', 'skills', 'shared');
+ mkdirSync(source, { recursive: true });
+ const entry = path.join(source, 'SKILL.md');
+ writeFileSync(entry, '---\nname: shared\ndescription: project skill\n---\n');
+ const managedEntry = path.join(workingDir, 'managed', 'SKILL.md');
+ const uniquePackage = path.join(workingDir, 'package', 'SKILL.md');
+ const agent = new PiAgent({
+ ...buildDeps(),
+ getManagedSkills: async () => [{
+ kind: 'agent-skill', source: 'skill', scope: 'user',
+ name: 'shared', path: managedEntry, enabled: true, claudeCommandName: 'managed-shared',
+ }],
+ resolvePiManagedPackageResources: async () => ({
+ extensions: [], promptTemplates: [], packageRoots: [workingDir],
+ skills: [
+ { name: 'shared', path: managedEntry },
+ { name: 'shared', path: entry },
+ { name: 'package-only', path: uniquePackage },
+ ],
+ }),
+ });
+
+ const result = await agent.listAgentSkills({ workingDir, includeManagedPiPackages: true });
+
+ expect(result.skills.map((skill) => skill.path)).toEqual([uniquePackage, managedEntry, entry]);
+ expect(result.skills.filter((skill) => skill.name === 'shared')).toHaveLength(2);
+ expect(result.skills.find((skill) => skill.path === entry)).toMatchObject({
+ origin: 'package', runtimeStatus: 'approved', runtimeCommandName: 'skill:shared',
+ });
+ expect(result.skills.find((skill) => skill.path === uniquePackage)).toMatchObject({
+ origin: 'package', runtimeStatus: 'approved', runtimeCommandName: 'skill:package-only',
+ });
});
it('includes Cindy-managed package skills only when the host explicitly allows them', async () => {
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 29c68483dd..95277301fd 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -8560,6 +8560,8 @@ export class PiAgent extends BaseAgent {
}),
opts.includeManagedPiPackages ? this.deps.resolvePiManagedPackageResources?.().catch(() => undefined) : undefined,
]);
+ const packageSkillPaths = new Set((managedPackages?.skills ?? []).map((skill) => canonicalSkillPath(skill.path)));
+ const seenSkillPaths = new Set<string>();
const out: ListAgentSkillsResult = {
skills: [
...(await this.deps.getManagedSkills?.() ?? []).map(({ claudeCommandName: _command, ...skill }) =>
@@ -8578,7 +8580,8 @@ export class PiAgent extends BaseAgent {
enabled: it.enabled ?? true,
runtimeStatus: it.runtimeStatus,
runtimeCommandName: `skill:${it.name}`,
- })),
+ }))
+ .filter((skill) => !packageSkillPaths.has(canonicalSkillPath(skill.path))),
...(managedPackages?.skills ?? []).map((skill) => ({
kind: 'agent-skill' as const,
name: skill.name,
@@ -8591,7 +8594,15 @@ export class PiAgent extends BaseAgent {
runtimeStatus: 'approved' as const,
runtimeCommandName: `skill:${skill.name}`,
})),
- ].sort((a, b) => a.name.localeCompare(b.name)),
+ ].filter((skill) => {
+ if (!skill.path) return true;
+ // Host-managed and package metadata win over filesystem aliases.
+ // Names alone cannot identify distinct project and user skills.
+ const identity = canonicalSkillPath(skill.path);
+ if (seenSkillPaths.has(identity)) return false;
+ seenSkillPaths.add(identity);
+ return true;
+ }).sort((a, b) => a.name.localeCompare(b.name)),
};
if (errors.length > 0) out.errors = errors;
return out;