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(skillhub): exclude internal rename backups from packages

xd-boboe08381973565
diff --git a/apps/desktop/src/main/skillhub/__tests__/activationPreferences.test.ts b/apps/desktop/src/main/skillhub/__tests__/activationPreferences.test.ts
index 149bb1a8e3..1d73aabdcb 100644
--- a/apps/desktop/src/main/skillhub/__tests__/activationPreferences.test.ts
+++ b/apps/desktop/src/main/skillhub/__tests__/activationPreferences.test.ts
@@ -65,7 +65,7 @@ describe('Skill activation preferences', () => {
expect(reloaded.isCindySkillEnabled(source)).toBe(true);
});
- it.each(['success', 'enabled-source', 'content-failure', 'preference-failure', 'owner-changed'])('migrates disabled state with a local rename (%s)', async (scenario) => {
+ it.each(['success', 'enabled-source', 'backup-cleanup-failure', 'content-failure', 'preference-failure', 'owner-changed'])('migrates disabled state with a local rename (%s)', async (scenario) => {
const { renameLocalSkill } = await import('../scanner');
const { setCindySkillEnabled, readDisabledSkillPaths, skillActivationKey } = await import('../activationPreferences');
const source = path.join(root, scenario, '.agents', 'skills', 'old-name');
@@ -77,6 +77,13 @@ describe('Skill activation preferences', () => {
const before = [...readDisabledSkillPaths()];
const oldKey = skillActivationKey(source);
const realRename = fs.renameSync;
+ const realUnlink = fs.unlinkSync;
+ const unlinkSpy = vi.spyOn(fs, 'unlinkSync').mockImplementation((file) => {
+ if (scenario === 'backup-cleanup-failure' && String(file).startsWith(path.join(destination, 'SKILL.md.xdt-rename-'))) {
+ throw Object.assign(new Error('simulated locked backup'), { code: 'EPERM' });
+ }
+ return realUnlink(file);
+ });
let injected = false;
const spy = vi.spyOn(fs, 'renameSync').mockImplementation((from, to) => {
if (!injected && ((scenario === 'content-failure' && String(to) === path.join(destination, 'SKILL.md'))
@@ -88,12 +95,37 @@ describe('Skill activation preferences', () => {
});
try {
const result = await renameLocalSkill({ absolutePath: source, newName: 'new-name' }, () => scenario !== 'owner-changed');
- if (scenario === 'success' || scenario === 'enabled-source') {
+ if (scenario === 'success' || scenario === 'enabled-source' || scenario === 'backup-cleanup-failure') {
expect(result).toEqual({ success: true, newAbsolutePath: destination });
expect(readDisabledSkillPaths()).not.toContain(oldKey);
- if (scenario === 'success') expect(readDisabledSkillPaths()).toContain(skillActivationKey(destination));
+ if (scenario !== 'enabled-source') expect(readDisabledSkillPaths()).toContain(skillActivationKey(destination));
else expect(readDisabledSkillPaths()).not.toContain(skillActivationKey(destination));
expect(fs.readFileSync(path.join(destination, 'SKILL.md'), 'utf8')).toContain('name: new-name');
+ if (scenario === 'backup-cleanup-failure') {
+ const backup = fs.readdirSync(destination).find((name) => name.startsWith('SKILL.md.xdt-rename-'))!;
+ expect(backup).toBeTruthy();
+ expect(fs.readFileSync(path.join(destination, backup), 'utf8')).toBe(content);
+ const { listSkillFolderChildren, readSkillRawFile } = await import('../scanner');
+ const { computeFolderHashDetailed } = await import('../folderHash');
+ const { pack } = await import('../zipPacker');
+ const { writeSnapshot, getSnapshotPath } = await import('../snapshot');
+ const { default: JSZip } = await import('jszip');
+ const visible = await listSkillFolderChildren({ dirPath: destination });
+ expect(visible).toEqual({ success: true, entries: [{ name: 'SKILL.md', kind: 'file' }] });
+ expect((await readSkillRawFile({ filePath: path.join(destination, backup) })).success).toBe(false);
+ const hash = await computeFolderHashDetailed(destination);
+ expect(hash.manifest.map((file) => file.path)).toEqual(['SKILL.md']);
+ const packed = await pack(destination);
+ expect(packed.manifest.files.map((file) => file.relPath)).toEqual(['SKILL.md']);
+ const zip = await JSZip.loadAsync(packed.buffer);
+ expect(Object.keys(zip.files)).toEqual(['SKILL.md']);
+ expect(await zip.file('SKILL.md')!.async('string')).toContain('name: new-name');
+ await writeSnapshot(destination, 'cleanup-failure');
+ expect(fs.readdirSync(getSnapshotPath('cleanup-failure'))).toEqual(['SKILL.md']);
+ realUnlink(path.join(destination, backup));
+ expect((await computeFolderHashDetailed(destination)).hash).toBe(hash.hash);
+ expect((await pack(destination)).sha256).toBe(packed.sha256);
+ }
} else {
expect(injected).toBe(scenario !== 'owner-changed');
expect(result.success).toBe(false);
@@ -101,7 +133,7 @@ describe('Skill activation preferences', () => {
expect(fs.readFileSync(path.join(source, 'SKILL.md'), 'utf8')).toBe(content);
expect(readDisabledSkillPaths()).toEqual(before);
}
- } finally { spy.mockRestore(); }
+ } finally { spy.mockRestore(); unlinkSpy.mockRestore(); }
});
});
diff --git a/apps/desktop/src/main/skillhub/__tests__/packageIgnore.test.ts b/apps/desktop/src/main/skillhub/__tests__/packageIgnore.test.ts
index cfbbc9820f..2b1ac550c5 100644
--- a/apps/desktop/src/main/skillhub/__tests__/packageIgnore.test.ts
+++ b/apps/desktop/src/main/skillhub/__tests__/packageIgnore.test.ts
@@ -2,6 +2,17 @@ import { describe, expect, it } from 'vitest';
import { isIgnoredSkillPackagePath } from '../packageIgnore';
describe('skillhub/packageIgnore', () => {
+ it('excludes internal rename backups and staging files without hiding ordinary backup fixtures', () => {
+ const backup = 'SKILL.md.xdt-rename-94a2109f-0344-4c8e-b61e-372140d20848';
+ // Package paths accept both separators, independently of the host OS.
+ for (const file of [backup, `nested/${backup}`, `nested\\${backup.toUpperCase()}`, 'SKILL.md.xdt-tmp', 'docs/example.json.xdt-tmp']) {
+ expect(isIgnoredSkillPackagePath(file), file).toBe(true);
+ }
+ for (const file of ['SKILL.md', 'SKILL.md.bak', 'SKILL.md.xdt-rename-example', `${backup}.md`, 'docs/.backup/example.md']) {
+ expect(isIgnoredSkillPackagePath(file), file).toBe(false);
+ }
+ });
+
it('keeps declared dotfile fixtures and dot directories', () => {
expect(isIgnoredSkillPackagePath('.cca-bindings.json')).toBe(false);
expect(isIgnoredSkillPackagePath('.cca-state/task/current-goal.md')).toBe(false);
diff --git a/apps/desktop/src/main/skillhub/packageIgnore.ts b/apps/desktop/src/main/skillhub/packageIgnore.ts
index 82f7a34046..0fdc3671d1 100644
--- a/apps/desktop/src/main/skillhub/packageIgnore.ts
+++ b/apps/desktop/src/main/skillhub/packageIgnore.ts
@@ -18,6 +18,11 @@ const EXCLUDED_BASENAMES = new Set([
const EXCLUDED_RELATIVE_PATH_RE =
/(^|\/)\.m2\/settings(?:-security)?\.xml$/;
+// Reserved by scanner's atomic writes/renames. A locked backup can outlive a
+// successful transaction, but must never enter browsing, hashing or publishing.
+const RENAME_BACKUP_BASENAME_RE =
+ /^skill\.md\.xdt-rename-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
+
function normalizePackagePath(relativePath: string): string {
return relativePath.replace(/\\/g, '/').replace(/^\/+/, '');
}
@@ -41,6 +46,7 @@ export function isIgnoredSkillPackagePath(relativePath: string): boolean {
if (lowerParts.some((part) => EXCLUDED_DIR_SEGMENTS.has(part))) return true;
const basename = lowerParts[lowerParts.length - 1] ?? '';
+ if (basename.endsWith('.xdt-tmp') || RENAME_BACKUP_BASENAME_RE.test(basename)) return true;
if (EXCLUDED_BASENAMES.has(basename)) return true;
if (basename === '.ds_store') return true;
if (basename.startsWith('._')) return true;
diff --git a/apps/desktop/src/main/skillhub/scanner.ts b/apps/desktop/src/main/skillhub/scanner.ts
index 44ffe2600f..b162d23bd0 100644
--- a/apps/desktop/src/main/skillhub/scanner.ts
+++ b/apps/desktop/src/main/skillhub/scanner.ts
@@ -910,7 +910,9 @@ export async function renameLocalSkill(params: {
backedUp = true;
fs.renameSync(tmpPath, newSkillMd);
});
- try { fs.unlinkSync(backupPath); } catch { /* A leftover backup must not roll back committed preferences. */ }
+ // packageIgnore excludes this reserved backup from browsing, hashes,
+ // snapshots and ZIPs even if Windows keeps it locked after commit.
+ try { fs.unlinkSync(backupPath); } catch { /* Do not roll back committed preferences. */ }
return { success: true, newAbsolutePath };
} catch (err) {
if (renamed) {
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 17a42125bd..784d7d3656 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -58,6 +58,9 @@
停用偏好。安装、学习、导入、重命名及卸载共用小写归一的名称锁,一次操作的多个锁名
按相同规则去重;大小写敏感卷上的同名大小写变体也保守互斥。卸载遇到目录大小写
变体时按实际文件身份匹配清单,并使用清单原始名称清理记录及自动同步偏好。
+ 原子编辑的 `*.xdt-tmp` 和重命名的 `SKILL.md.xdt-rename-<UUID>` 是内部保留文件,
+ 共用 `packageIgnore` 从浏览、哈希、发布快照与 ZIP 中排除;即使备份清理被文件锁阻止,
+ 也不得上传旧内容,不因清理失败回滚已经提交的重命名与偏好。
- `scope=market|team` 是公开与组织目录的通用读取上下文;列表得到的 scope 必须贯穿详情、
文件、版本、扫描、下载、Learn 和批量同步。同 slug 在不同 scope 下是两条独立远端记录;
但全局 Skill 发现路径仍按 slug 只有一个安装槽,自动同步配置重复同一 slug 时由首个有效项