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(desktop): recover invalid built-in Skill pointer

xd-bobo13662ed318b9
diff --git a/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts b/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
index 9a8955ef32..9b1fb8a74a 100644
--- a/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/built-in-skills.test.ts
@@ -443,6 +443,59 @@ describe('built-in Skills', () => {
expect(fs.realpathSync(sharedLink)).toBe(committedCreatorBytes);
});
+ it('repairs a committed active pointer redirected outside the managed versions directory', async () => {
+ const input = fixture();
+ const initial = await prepareBuiltInSkills(input);
+ const root = path.join(input.appDataDir, 'Cindy', 'shared-system-skills');
+ const activePath = path.join(root, '.active');
+ const manifest = JSON.parse(
+ fs.readFileSync(path.join(root, '.cindy-system-skills.json'), 'utf8'),
+ );
+ const committedRoot = path.join(root, '.versions', manifest.activeBundle);
+ const redirectedRoot = path.join(input.appDataDir, 'redirected-system-skills');
+ const redirectedCreator = path.join(redirectedRoot, 'cindy-skill-creator');
+ fs.mkdirSync(redirectedCreator, { recursive: true });
+ fs.writeFileSync(path.join(redirectedCreator, 'SKILL.md'), '# Redirected\n');
+ fs.unlinkSync(activePath);
+ fs.symlinkSync(redirectedRoot, activePath, process.platform === 'win32' ? 'junction' : 'dir');
+ const sharedLink = path.join(input.homeDir, '.agents', 'skills', 'cindy-skill-creator');
+ expect(fs.realpathSync(sharedLink)).toBe(fs.realpathSync(redirectedCreator));
+
+ const recovered = await prepareBuiltInSkills(input);
+
+ expect(recovered.changed).toBe(true);
+ expect(recovered.projectionSafe).toBe(true);
+ expect(recovered.warnings).toEqual([]);
+ expect(fs.realpathSync(activePath)).toBe(fs.realpathSync(committedRoot));
+ expect(fs.realpathSync(sharedLink)).toBe(fs.realpathSync(initial.descriptors[0]!.absolutePath));
+ expect(fs.readFileSync(path.join(redirectedCreator, 'SKILL.md'), 'utf8')).toBe(
+ '# Redirected\n',
+ );
+ });
+
+ it('does not overwrite a non-link active pointer placeholder', async () => {
+ const input = fixture();
+ await prepareBuiltInSkills(input);
+ const root = path.join(input.appDataDir, 'Cindy', 'shared-system-skills');
+ const activePath = path.join(root, '.active');
+ fs.unlinkSync(activePath);
+ fs.mkdirSync(path.join(activePath, 'cindy-skill-creator'), { recursive: true });
+ fs.writeFileSync(
+ path.join(activePath, 'cindy-skill-creator', 'SKILL.md'),
+ '# User placeholder\n',
+ );
+
+ const failed = await prepareBuiltInSkills(input);
+
+ expect(failed.changed).toBe(false);
+ expect(failed.projectionSafe).toBe(false);
+ expect(failed.warnings.join('\n')).toContain('active pointer is not a link');
+ expect(fs.lstatSync(activePath).isDirectory()).toBe(true);
+ expect(fs.readFileSync(path.join(activePath, 'cindy-skill-creator', 'SKILL.md'), 'utf8')).toBe(
+ '# User placeholder\n',
+ );
+ });
+
it('restores the previous manifest and active pointer when publication fails', async () => {
const input = fixture();
const initial = await prepareBuiltInSkills(input);
diff --git a/apps/desktop/src/main/maker-host/built-in-skills.ts b/apps/desktop/src/main/maker-host/built-in-skills.ts
index 9cbce1471f..7ab9b1f495 100644
--- a/apps/desktop/src/main/maker-host/built-in-skills.ts
+++ b/apps/desktop/src/main/maker-host/built-in-skills.ts
@@ -536,13 +536,18 @@ function activeBundleTarget(root: string, bundle: string): string {
return path.join(root, VERSIONS_DIRECTORY, bundle);
}
-async function readActiveBundle(root: string): Promise<string | null> {
+type ActiveBundlePointer =
+ | { kind: 'missing' }
+ | { kind: 'valid'; bundle: string }
+ | { kind: 'stale' };
+
+async function inspectActiveBundlePointer(root: string): Promise<ActiveBundlePointer> {
const activePath = path.join(root, ACTIVE_BUNDLE_LINK);
let stat: fs.Stats;
try {
stat = await fsp.lstat(activePath);
} catch (error) {
- if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { kind: 'missing' };
throw error;
}
if (!stat.isSymbolicLink()) {
@@ -558,9 +563,12 @@ async function readActiveBundle(root: string): Promise<string | null> {
);
const segments = relative.split(path.sep);
if (segments.length !== 1 || !validActiveBundle(segments[0])) {
- throw new Error(`built-in Skill active pointer has an invalid target: ${target}`);
+ // The entry is still Cindy's pointer shape, so recover it like any other
+ // stale publication. A non-link entry remains user-owned and is rejected
+ // above rather than overwritten.
+ return { kind: 'stale' };
}
- return segments[0];
+ return { kind: 'valid', bundle: segments[0] };
}
async function switchActiveBundle(
@@ -572,7 +580,8 @@ async function switchActiveBundle(
if (!(await fsp.stat(target).catch(() => null))?.isDirectory()) {
throw new Error(`built-in Skill bundle is unavailable: ${target}`);
}
- if (await readActiveBundle(root) === bundle) return false;
+ const activePointer = await inspectActiveBundlePointer(root);
+ if (activePointer.kind === 'valid' && activePointer.bundle === bundle) return false;
const activePath = path.join(root, ACTIVE_BUNDLE_LINK);
const replacement = `${activePath}.next-${randomUUID()}`;
@@ -592,8 +601,8 @@ async function switchActiveBundle(
async function removeActiveBundlePointer(root: string): Promise<boolean> {
const activePath = path.join(root, ACTIVE_BUNDLE_LINK);
- const activeBundle = await readActiveBundle(root);
- if (activeBundle === null) return false;
+ const activePointer = await inspectActiveBundlePointer(root);
+ if (activePointer.kind === 'missing') return false;
await fsp.unlink(activePath);
return true;
}