Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): reclaim escaped managed skill links
Yuewei-LLc7f66b2bb527
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.tsindex f51e5b3efd..a262ab701c 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts@@ -343,6 +343,76 @@ describe('skillSlot · reconcileGhostSkillLinks', () => {expect(fs.existsSync(managedLink)).toBe(false);});+ it('reclaims a live managed projection after its snapshot root is replaced by an outside link', async () => {+ const outsideRoot = path.join(workDir, 'outside-snapshot-root');+ const snapshotRoot = path.join(approvalStateRoot, 'skill-snapshots');+ const managedTarget = path.join(snapshotRoot, 'managed', 'rev', 'skills', 'demo');+ await fs.promises.mkdir(managedTarget, { recursive: true });+ await fs.promises.mkdir(path.join(outsideRoot, 'managed', 'rev', 'skills', 'demo'), {+ recursive: true,+ });+ await fs.promises.mkdir(sharedDir(), { recursive: true });++ const link = path.join(sharedDir(), 'managed--demo');+ await fs.promises.symlink(+ managedTarget,+ link,+ process.platform === 'win32' ? 'junction' : 'dir',+ );+ await fs.promises.rm(snapshotRoot, { recursive: true, force: true });+ await fs.promises.symlink(+ outsideRoot,+ snapshotRoot,+ process.platform === 'win32' ? 'junction' : 'dir',+ );++ const result = await reconcileGhostSkillLinks({ ghosts: [], brainRoot, approvalStateRoot, homeDir });+ expect(result.changed).toBe(true);+ expect(fs.existsSync(link)).toBe(false);+ });++ it('does not remove a foreign link that replaces a managed projection before cleanup', async () => {+ const managedTarget = path.join(+ approvalStateRoot,+ 'skill-snapshots',+ 'managed',+ 'rev',+ 'skills',+ 'demo',+ );+ const foreignTarget = path.join(workDir, 'foreign-skill');+ await fs.promises.mkdir(managedTarget, { recursive: true });+ await fs.promises.mkdir(foreignTarget, { recursive: true });+ await fs.promises.mkdir(sharedDir(), { recursive: true });++ const link = path.join(sharedDir(), 'managed--demo');+ await fs.promises.symlink(+ managedTarget,+ link,+ process.platform === 'win32' ? 'junction' : 'dir',+ );++ const actualLstat = fs.promises.lstat.bind(fs.promises);+ let replaced = false;+ vi.spyOn(fs.promises, 'lstat').mockImplementation(async (target, options) => {+ if (!replaced && path.resolve(String(target)) === path.resolve(link)) {+ replaced = true;+ await fs.promises.unlink(link);+ await fs.promises.symlink(+ foreignTarget,+ link,+ process.platform === 'win32' ? 'junction' : 'dir',+ );+ }+ return actualLstat(target, options as never);+ });++ const result = await reconcileGhostSkillLinks({ ghosts: [], brainRoot, approvalStateRoot, homeDir });+ expect(result.changed).toBe(false);+ expect(result.warnings).toContain('技能链接 managed--demo 在回收前已变化,留待下一轮对账');+ expect(sameRealPath(link, foreignTarget)).toBe(true);+ });+it('用户自建悬空链接不因目标路径里恰有 cindy-brain 段而被删(布局 id 必须对上链接名)', async () => {await fs.promises.mkdir(sharedDir(), { recursive: true });// 用户把自己项目目录(路径里恰好有一段叫 cindy-brain)链进技能根,随后目标没了。diff --git a/apps/desktop/src/main/cindy-brain/skillSlot.ts b/apps/desktop/src/main/cindy-brain/skillSlot.tsindex 70f6387232..ae06480491 100644--- a/apps/desktop/src/main/cindy-brain/skillSlot.ts+++ b/apps/desktop/src/main/cindy-brain/skillSlot.ts@@ -383,29 +383,43 @@ export async function reconcileGhostSkillLinks(}const managedLive = new Map<string, string>(); // linkName → realpath(compare 形态)- const toRemove: string[] = [];+ const toRemove = new Map<string, string>();for (const entName of linkNames) {const linkPath = path.join(sharedSkillsDir, entName);+ let rawTarget: string | null = null;+ try {+ rawTarget = await fsp.readlink(linkPath);+ } catch {+ // A concurrent unlink/replacement is handled by the later lstat guard.+ }const real = await realPathOrNull(linkPath);if (real !== null) {// 活链接:目标在当前 owner 的受管根内才归我们管;他 owner / 外来链接不碰。- if (!managedRootCompares.some((root) => isSameOrInside(real, root))) continue;+ const lexicalTarget = rawTarget === null+ ? null+ : path.resolve(path.dirname(linkPath), rawTarget);+ const rawManaged = lexicalTarget !== null && targetLooksGhostManaged(+ lexicalTarget,+ entName,+ approvalStateDirName,+ danglingManagedRootCompares,+ );+ // If the managed snapshot root was replaced by a link, resolved ownership+ // can escape. Keep the lexical managed proof so our projection is revoked.+ if (!rawManaged && !managedRootCompares.some((root) => isSameOrInside(real, root))) continue;const want = desired.get(entName);const wantCompare = want? ((await realPathOrNull(want.target)) ?? normalizeForCompare(want.target)): null;if (want !== undefined && real === wantCompare) {managedLive.set(entName, real);- } else {- toRemove.push(entName);+ } else if (rawTarget !== null) {+ toRemove.set(entName, rawTarget);}continue;}// 断链:目标命中受管结构即回收(含他 owner 与登出态临时根的残留)。- let rawTarget: string;- try {- rawTarget = await fsp.readlink(linkPath);- } catch {+ if (rawTarget === null) {continue;}const absTarget = path.isAbsolute(rawTarget)@@ -419,16 +433,21 @@ export async function reconcileGhostSkillLinks(danglingManagedRootCompares,)) {- toRemove.push(entName);+ toRemove.set(entName, rawTarget);}}// —— 删除步:先撤旧再建新,防"改目标"落进冲突分支。- for (const linkName of toRemove) {+ for (const [linkName, expectedRawTarget] of toRemove) {const linkPath = path.join(sharedSkillsDir, linkName);try {const stat = await fsp.lstat(linkPath);if (!stat.isSymbolicLink()) continue; // TOCTOU 防御:再确认一次才动手+ const currentRawTarget = await fsp.readlink(linkPath);+ if (currentRawTarget !== expectedRawTarget) {+ warnings.push(`技能链接 ${linkName} 在回收前已变化,留待下一轮对账`);+ continue;+ }await fsp.unlink(linkPath);actions.push({ linkName, op: 'removed' });changed = true;