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(auth): 私有账号清理不再等待无关实例的共享技能锁 (#6021)

Chris Zhangd5d38b58684c
diff --git a/apps/desktop/src/main/__tests__/authBoundaryQuarantine.test.ts b/apps/desktop/src/main/__tests__/authBoundaryQuarantine.test.ts
index a9df303d65..53ad85cf0c 100644
--- a/apps/desktop/src/main/__tests__/authBoundaryQuarantine.test.ts
+++ b/apps/desktop/src/main/__tests__/authBoundaryQuarantine.test.ts
@@ -54,6 +54,8 @@ import {
withStableOwnerBoundaryMutation,
} from '../authBoundaryQuarantine.js';
+import { removeGhostSkillLinksForRoots } from '../cindy-brain/skillSlot.js';
+
function readPersistedState(): unknown {
return JSON.parse(
fs.readFileSync(
@@ -173,6 +175,66 @@ describe('Ghost skill projection boundary state', () => {
expect(shared).not.toHaveBeenCalled();
});
+ it('commits the account and revokes private links while unrelated shared roots are busy', async () => {
+ const root = path.join(pathMock.userDataDir, 'owner-a');
+ const privateDir = path.join(root, 'agent-skills', 'skills');
+ const snapshot = path.join(root, 'snapshot');
+ fs.mkdirSync(privateDir, { recursive: true });
+ fs.mkdirSync(snapshot);
+ fs.symlinkSync(snapshot, path.join(privateDir, 'plugin'), 'junction');
+ lockMock.busyPaths.add(__testing.sharedSkillRootsLockPath());
+ const commit = vi.fn(async () => 'signed-in');
+
+ await expect(withGhostSkillProjectionOwnerCommit({
+ previousOwnerId: null,
+ nextOwnerId: 'owner-b',
+ prepareTransition: async () => {
+ const cleanup = await removeGhostSkillLinksForRoots(
+ [root], pathMock.homeDir, [privateDir], withSharedSkillRootsLock,
+ );
+ if (cleanup.blockers.length) throw new Error('cleanup blocked');
+ },
+ commit,
+ })).resolves.toBe('signed-in');
+
+ expect(commit).toHaveBeenCalledOnce();
+ expect(fs.existsSync(path.join(privateDir, 'plugin'))).toBe(false);
+ expect(fs.existsSync(snapshot)).toBe(true);
+ expect(isGhostSkillProjectionBoundaryStableForOwner('owner-b')).toBe(true);
+ expect(lockMock.acquired).toEqual([__testing.lockPath()]);
+ });
+
+ it('keeps the account quarantined until real legacy shared links can be revoked', async () => {
+ const root = path.join(pathMock.userDataDir, 'owner-a');
+ const sharedDir = path.join(pathMock.homeDir, '.agents', 'skills');
+ fs.mkdirSync(root, { recursive: true });
+ fs.mkdirSync(sharedDir, { recursive: true });
+ const link = path.join(sharedDir, 'legacy-plugin');
+ fs.symlinkSync(root, link, 'junction');
+ lockMock.busyPaths.add(__testing.sharedSkillRootsLockPath());
+ const commit = vi.fn(async () => 'signed-in');
+ const transition = () => withGhostSkillProjectionOwnerCommit({
+ previousOwnerId: null,
+ nextOwnerId: 'owner-b',
+ prepareTransition: async () => {
+ const cleanup = await removeGhostSkillLinksForRoots(
+ [root], pathMock.homeDir, [], withSharedSkillRootsLock,
+ );
+ if (cleanup.blockers.length) throw new Error('cleanup blocked');
+ },
+ commit,
+ });
+
+ await expect(transition()).rejects.toThrow('lock is busy or unavailable');
+ expect(commit).not.toHaveBeenCalled();
+ expect(fs.lstatSync(link).isSymbolicLink()).toBe(true);
+ expect(isGhostSkillProjectionBoundaryStableForOwner('owner-b')).toBe(false);
+ lockMock.busyPaths.clear();
+ await expect(transition()).resolves.toBe('signed-in');
+ expect(fs.existsSync(link)).toBe(false);
+ expect(isGhostSkillProjectionBoundaryStableForOwner('owner-b')).toBe(true);
+ });
+
it('accepts only versioned stable and pending records', () => {
expect(
__testing.normalizeRecord({
diff --git a/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts b/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
index 6aa569158f..675ee0f76e 100644
--- a/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
+++ b/apps/desktop/src/main/__tests__/authSessionExpiredDetection.test.ts
@@ -189,7 +189,7 @@ describe('desktop auth session-expiry detection', () => {
const body = ghostSource.slice(start, end);
expect(body).toContain('const roots = listGhostOwnerProjectionRoots();');
- expect(body).toContain('await withSharedSkillRootsLock(() =>');
+ expect(body).toContain('withSharedSkillRootsLock);');
expect(body).toContain('removeGhostSkillLinksForRoots(roots, undefined,');
expect(body).toContain("roots.map((root) => path.join(ghostSkillPluginRoot(root), 'skills'))");
expect(body).toContain('throw new Error(`ghost owner skill cleanup incomplete');
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts
index 7916dc1560..208b8f802e 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/skillSlot.test.ts
@@ -672,6 +672,84 @@ describe('skillSlot · reconcileGhostSkillLinks', () => {
}
});
+ it('revokes private owner links without waiting for an unrelated shared-root writer', async () => {
+ await fs.promises.mkdir(sharedDir(), { recursive: true });
+ const privateLink = path.join(sharedDir(), 'old-owner--skill');
+ await fs.promises.symlink(brainRoot, privateLink, 'junction');
+ await fs.promises.mkdir(legacySharedDir(), { recursive: true });
+ const foreignLink = path.join(legacySharedDir(), 'other-instance');
+ await fs.promises.symlink(homeDir, foreignLink, 'junction');
+ const busyLock = vi.fn(async () => { throw new Error('shared roots busy'); });
+
+ const result = await removeGhostSkillLinksForRoots(
+ [brainRoot, approvalStateRoot], homeDir, [sharedDir()], busyLock,
+ );
+
+ expect(result).toEqual({ changed: true, warnings: [], blockers: [] });
+ expect(busyLock).not.toHaveBeenCalled();
+ expect(fs.existsSync(privateLink)).toBe(false);
+ expect(fs.existsSync(foreignLink)).toBe(true);
+ expect(fs.existsSync(brainRoot)).toBe(true);
+ });
+
+ it('still requires the shared lock for a dangling legacy owner link', async () => {
+ await fs.promises.mkdir(legacySharedDir(), { recursive: true });
+ const legacyLink = path.join(legacySharedDir(), 'old-owner--skill');
+ await fs.promises.symlink(brainRoot, legacyLink, 'junction');
+ await fs.promises.rmdir(brainRoot);
+ const busyLock = vi.fn(async () => { throw new Error('shared roots busy'); });
+
+ await expect(removeGhostSkillLinksForRoots(
+ [brainRoot], homeDir, [], busyLock,
+ )).rejects.toThrow('shared roots busy');
+ expect(busyLock).toHaveBeenCalledOnce();
+ expect((await fs.promises.lstat(legacyLink)).isSymbolicLink()).toBe(true);
+
+ const heldLock = vi.fn();
+ const result = await removeGhostSkillLinksForRoots([brainRoot], homeDir, [], async (task) => {
+ heldLock();
+ return task();
+ });
+ expect(heldLock).toHaveBeenCalledOnce();
+ expect(result).toEqual({ changed: true, warnings: [], blockers: [] });
+ await expect(fs.promises.lstat(legacyLink)).rejects.toMatchObject({ code: 'ENOENT' });
+ });
+
+ it('rechecks legacy link ownership after waiting for the shared lock', async () => {
+ await fs.promises.mkdir(legacySharedDir(), { recursive: true });
+ const legacyLink = path.join(legacySharedDir(), 'old-owner--skill');
+ await fs.promises.symlink(brainRoot, legacyLink, 'junction');
+ const delayedLock = vi.fn();
+ const result = await removeGhostSkillLinksForRoots([brainRoot], homeDir, [], async (task) => {
+ delayedLock();
+ await fs.promises.unlink(legacyLink);
+ await fs.promises.symlink(homeDir, legacyLink, 'junction');
+ return task();
+ });
+
+ expect(delayedLock).toHaveBeenCalledOnce();
+ expect(result).toEqual({ changed: false, warnings: [], blockers: [] });
+ expect(await fs.promises.realpath(legacyLink)).toBe(await fs.promises.realpath(homeDir));
+ });
+
+ it('does not treat an unreadable legacy root as an empty sweep', async () => {
+ const readdir = fs.promises.readdir;
+ vi.spyOn(fs.promises, 'readdir').mockImplementation(async (...args) => {
+ if (String(args[0]) === legacySharedDir()) {
+ throw Object.assign(new Error('denied'), { code: 'EACCES' });
+ }
+ return readdir(...args);
+ });
+ const busyLock = vi.fn(async () => { throw new Error('shared roots busy'); });
+
+ const result = await removeGhostSkillLinksForRoots([brainRoot], homeDir, [], busyLock);
+
+ expect(result.changed).toBe(false);
+ expect(result.blockers).toHaveLength(1);
+ expect(result.blockers[0]).toContain(legacySharedDir());
+ expect(busyLock).not.toHaveBeenCalled();
+ });
+
it('账号边界删除已识别的旧 owner 投影失败:记 blocker,不能静默跨账号保留', async () => {
const ownerRoot = path.join(workDir, 'owners', 'bbb', 'cindy-brain');
const managedTarget = path.join(ownerRoot, 'other-ghost', 'skills', 'foo');
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 75a66859a1..6fcad343db 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -1251,10 +1251,11 @@ export async function suspendAllGhosts(): Promise<void> {
resetNodeRuntimeBrokerForAccountBoundary();
brainRootCache = null;
const roots = listGhostOwnerProjectionRoots();
- // Legacy links live in the shared home roots; serialize with other Cindy processes.
- const skillCleanup = await withSharedSkillRootsLock(() =>
- removeGhostSkillLinksForRoots(roots, undefined,
- roots.map((root) => path.join(ghostSkillPluginRoot(root), 'skills'))));
+ // Private revocation is protected by this instance's owner boundary. Only
+ // an actual legacy shared-link removal needs the machine-wide writer lock.
+ const skillCleanup = await removeGhostSkillLinksForRoots(roots, undefined,
+ roots.map((root) => path.join(ghostSkillPluginRoot(root), 'skills')),
+ withSharedSkillRootsLock);
for (const warning of skillCleanup.warnings) {
log.warn('ghost owner skill cleanup warning', { warning });
}
diff --git a/apps/desktop/src/main/cindy-brain/skillSlot.ts b/apps/desktop/src/main/cindy-brain/skillSlot.ts
index 0ffa6733d9..927ae8e34e 100644
--- a/apps/desktop/src/main/cindy-brain/skillSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/skillSlot.ts
@@ -94,10 +94,44 @@ export async function removeGhostSkillLinksForRoots(
managedRoots: readonly string[],
homeDir?: string,
additionalSkillDirs: readonly string[] = [],
+ withSharedRootsLock?: <T>(task: () => Promise<T>) => Promise<T>,
): Promise<{ changed: boolean; warnings: string[]; blockers: string[] }> {
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ const sharedDirs = [paths.claudeSkillsDir, paths.codexSkillsDir, paths.sharedSkillsDir];
+ // The caller holds this instance's owner boundary. Other instances cannot
+ // publish its private projections, and current builds never create shared
+ // Ghost links. An empty legacy sweep is read-only and must not wait for an
+ // unrelated instance's shared-root writer (which may be unresponsive).
+ // Actual shared mutations still re-scan and revalidate under the same lock
+ // used by older builds; an unreadable directory remains a boundary blocker.
+ const inspected = withSharedRootsLock
+ ? await sweepGhostSkillLinksForRoots(managedRoots, homeDir, sharedDirs, false)
+ : null;
+ const shared = withSharedRootsLock && inspected?.managedLinksPresent
+ ? await withSharedRootsLock(() =>
+ sweepGhostSkillLinksForRoots(managedRoots, homeDir, sharedDirs, true))
+ : inspected ?? await sweepGhostSkillLinksForRoots(managedRoots, homeDir, sharedDirs, true);
+ const privateLinks = await sweepGhostSkillLinksForRoots(
+ managedRoots, homeDir, additionalSkillDirs, true,
+ );
+ return {
+ changed: shared.changed || privateLinks.changed,
+ warnings: [...new Set([...shared.warnings, ...privateLinks.warnings])],
+ blockers: [...new Set([...shared.blockers, ...privateLinks.blockers])],
+ };
+}
+
+/** Use identical ownership checks for the read-only legacy probe and locked removal. */
+async function sweepGhostSkillLinksForRoots(
+ managedRoots: readonly string[],
+ homeDir: string | undefined,
+ skillDirs: readonly string[],
+ removeLinks: boolean,
+): Promise<{ changed: boolean; managedLinksPresent: boolean; warnings: string[]; blockers: string[] }> {
const warnings: string[] = [];
const blockers: string[] = [];
let changed = false;
+ let managedLinksPresent = false;
const paths = sharedGlobalSkillsPaths(homeDir);
const lexicalRoots = [...new Set(managedRoots.map(normalizeForCompare))];
const resolvedRoots: string[] = [];
@@ -119,9 +153,9 @@ export async function removeGhostSkillLinksForRoots(
}
}
}
- if (lexicalRoots.length === 0) return { changed, warnings, blockers };
+ if (lexicalRoots.length === 0) return { changed, managedLinksPresent, warnings, blockers };
- for (const dir of [paths.claudeSkillsDir, paths.codexSkillsDir, paths.sharedSkillsDir, ...additionalSkillDirs]) {
+ for (const dir of skillDirs) {
let entries: Dirent[];
try {
entries = await fsp.readdir(dir, { withFileTypes: true });
@@ -181,6 +215,8 @@ export async function removeGhostSkillLinksForRoots(
} catch { /* An unprovable/user-owned dangling link is left untouched. */ }
}
if (!managed) continue;
+ managedLinksPresent = true;
+ if (!removeLinks) continue;
try {
const stat = await fsp.lstat(linkPath);
if (!stat.isSymbolicLink()) {
@@ -214,7 +250,7 @@ export async function removeGhostSkillLinksForRoots(
}
}
}
- return { changed, warnings, blockers };
+ return { changed, managedLinksPresent, warnings, blockers };
}
interface ReconcileOptions {