Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): stabilize plugin content race checks
Yuewei-LLfe60bab45916
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.tsindex ece3d79792..053741a3d4 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts@@ -78,6 +78,27 @@ async function mockStaleLeafPathStatAfterMutation(file: string): Promise<{};}+async function writeDurably(file: string, bytes: Buffer | string, flags: string): Promise<void> {+ const handle = await fs.promises.open(file, flags);+ try {+ if (flags === 'r+') await handle.truncate(0);+ await handle.writeFile(bytes);+ await handle.sync();+ } finally {+ await handle.close();+ }+}++function shiftDirectoryTimes(stat: fs.BigIntStats): fs.BigIntStats {+ return new Proxy(stat, {+ get(target, key) {+ if (key === 'mtimeNs' || key === 'ctimeNs') return target[key] + 1n;+ const value = Reflect.get(target, key);+ return typeof value === 'function' ? value.bind(target) : value;+ },+ });+}+describe('classifyGhostDirEntry', () => {it('separates regular files, real directories and links', async () => {const file = path.join(workDir, 'a.txt');@@ -258,7 +279,7 @@ describe('hashGhostContentFiles', () => {if (!(await tryLinkDir(outside, path.join(root, 'nested')))) return;await expect(hashGhostContentFiles(root, tree.files, tree.rootIdentity)).rejects.toThrow(- /escaped its root|changed into a link/,+ /root changed|escaped its root|changed into a link/,);});@@ -279,7 +300,7 @@ describe('hashGhostContentFiles', () => {if (!(await tryLinkDir(alternate, path.join(root, 'nested')))) return;await expect(hashGhostContentFiles(root, tree.files, tree.rootIdentity)).rejects.toThrow(- /ancestor changed into a link/,+ /root changed|ancestor changed into a link/,);});@@ -314,17 +335,20 @@ describe('hashGhostContentFiles', () => {nonRegular: 'throw',label: 'test',});+ const canonicalFile = path.join(tree.rootIdentity.realPath, 'main.js');const splitAt = 'old-prefix|'.length;- const stalePathStat = await mockStaleLeafPathStatAfterMutation(file);+ const stalePathStat = await mockStaleLeafPathStatAfterMutation(canonicalFile);const realOpen = fs.promises.open;let targetOpenCount = 0;+ let mutationRan = false;let staleHandleStat: fs.BigIntStats | undefined;const openSpy = vi.spyOn(fs.promises, 'open').mockImplementation((async (...args: Parameters<typeof fs.promises.open>) => {const handle = await realOpen(...args);- if (path.resolve(String(args[0])) !== path.resolve(file)) return handle;+ if (path.resolve(String(args[0])) !== canonicalFile) return handle;+ if (typeof args[1] !== 'number') return handle;targetOpenCount += 1;const mutateDuringRead = targetOpenCount === 1;return new Proxy(handle, {@@ -341,10 +365,12 @@ describe('hashGhostContentFiles', () => {const firstRead = await handle.read(first, 0, first.byteLength, 0);yield first.subarray(0, firstRead.bytesRead);- await fs.promises.writeFile(file, replacementBytes);+ await writeDurably(canonicalFile, replacementBytes, 'r+');const changedAt = new Date(Date.now() + 5_000);- await fs.promises.utimes(file, changedAt, changedAt);+ await fs.promises.utimes(canonicalFile, changedAt, changedAt);+ await expect(fs.promises.readFile(canonicalFile)).resolves.toEqual(replacementBytes);stalePathStat.markMutated();+ mutationRan = true;const rest = Buffer.alloc(replacementBytes.byteLength - splitAt);const restRead = await handle.read(rest, 0, rest.byteLength, splitAt);@@ -364,30 +390,37 @@ describe('hashGhostContentFiles', () => {openSpy.mockRestore();stalePathStat.restore();}+ expect(targetOpenCount).toBe(2);+ expect(mutationRan).toBe(true);});it('rejects a leaf file renamed away and replaced while its opened bytes are being hashed', async () => {const root = path.join(workDir, 'plugin');const file = path.join(root, 'main.js');- const detachedFile = path.join(root, 'detached.js');+ const stagedFile = path.join(workDir, 'replacement.js');const originalBytes = Buffer.from('approved bytes');+ const replacementBytes = Buffer.from('replacement bytes');await fs.promises.mkdir(root, { recursive: true });await fs.promises.writeFile(file, originalBytes);+ await writeDurably(stagedFile, replacementBytes, 'wx');const tree = await collectGhostContentFiles(root, {dotEntries: 'include',nonRegular: 'throw',label: 'test',});+ const canonicalFile = path.join(tree.rootIdentity.realPath, 'main.js');+ const canonicalDetachedFile = path.join(tree.rootIdentity.realPath, 'detached.js');- const stalePathStat = await mockStaleLeafPathStatAfterMutation(file);+ const stalePathStat = await mockStaleLeafPathStatAfterMutation(canonicalFile);const realOpen = fs.promises.open;let targetOpenCount = 0;+ let mutationRan = false;let staleHandleStat: fs.BigIntStats | undefined;const openSpy = vi.spyOn(fs.promises, 'open').mockImplementation((async (...args: Parameters<typeof fs.promises.open>) => {const handle = await realOpen(...args);- if (path.resolve(String(args[0])) !== path.resolve(file)) return handle;+ if (path.resolve(String(args[0])) !== canonicalFile) return handle;targetOpenCount += 1;const mutateDuringRead = targetOpenCount === 1;return new Proxy(handle, {@@ -402,9 +435,11 @@ describe('hashGhostContentFiles', () => {return () => Readable.from((async function*() {const bytes = Buffer.alloc(originalBytes.byteLength);const read = await handle.read(bytes, 0, bytes.byteLength, 0);- await fs.promises.rename(file, detachedFile);- await fs.promises.writeFile(file, 'replacement bytes');+ await fs.promises.rename(canonicalFile, canonicalDetachedFile);+ await fs.promises.rename(stagedFile, canonicalFile);+ await expect(fs.promises.readFile(canonicalFile)).resolves.toEqual(replacementBytes);stalePathStat.markMutated();+ mutationRan = true;yield bytes.subarray(0, read.bytesRead);})());}@@ -421,6 +456,72 @@ describe('hashGhostContentFiles', () => {openSpy.mockRestore();stalePathStat.restore();}+ expect(targetOpenCount).toBe(2);+ expect(mutationRan).toBe(true);+ });++ it('rejects root generation drift even when dev and ino are reused', async () => {+ const root = path.join(workDir, 'plugin');+ await fs.promises.mkdir(root, { recursive: true });+ await fs.promises.writeFile(path.join(root, 'main.js'), 'stable bytes');+ const tree = await collectGhostContentFiles(root, {+ dotEntries: 'include',+ nonRegular: 'throw',+ label: 'test',+ });++ const realStat = fs.promises.stat;+ const statSpy = vi.spyOn(fs.promises, 'stat').mockImplementation((async (+ candidate: fs.PathLike,+ ) => {+ const stat = await realStat(candidate, { bigint: true });+ if (stat.dev === tree.rootIdentity.dev && stat.ino === tree.rootIdentity.ino) {+ return shiftDirectoryTimes(stat);+ }+ return stat;+ }) as typeof fs.promises.stat);+ try {+ await expect(+ hashGhostContentFiles(root, tree.files, tree.rootIdentity),+ ).rejects.toThrow(/root changed/);+ } finally {+ statSpy.mockRestore();+ }+ });++ it('rejects ancestor generation drift even when dev and ino are unchanged', async () => {+ const root = path.join(workDir, 'plugin');+ const nested = path.join(root, 'nested');+ await fs.promises.mkdir(nested, { recursive: true });+ await fs.promises.writeFile(path.join(nested, 'main.js'), 'stable bytes');+ const tree = await collectGhostContentFiles(root, {+ dotEntries: 'include',+ nonRegular: 'throw',+ label: 'test',+ });+ const canonicalNested = path.join(tree.rootIdentity.realPath, 'nested');+ const nestedIdentity = await fs.promises.lstat(canonicalNested, { bigint: true });++ const realLstat = fs.promises.lstat;+ let nestedLstatCount = 0;+ const lstatSpy = vi.spyOn(fs.promises, 'lstat').mockImplementation((async (+ candidate: fs.PathLike,+ ) => {+ const stat = await realLstat(candidate, { bigint: true });+ if (stat.dev === nestedIdentity.dev && stat.ino === nestedIdentity.ino) {+ nestedLstatCount += 1;+ return nestedLstatCount === 1 ? stat : shiftDirectoryTimes(stat);+ }+ return stat;+ }) as typeof fs.promises.lstat);+ try {+ await expect(+ hashGhostContentFiles(root, tree.files, tree.rootIdentity),+ ).rejects.toThrow(/ancestor changed/);+ } finally {+ lstatSpy.mockRestore();+ }+ expect(nestedLstatCount).toBe(2);});it('rejects the content root being replaced between collection and hashing', async () => {diff --git a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts b/apps/desktop/src/main/cindy-brain/ghostContentTree.tsindex 0aaa87e90a..cdb33f1c57 100644--- a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts+++ b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts@@ -164,12 +164,16 @@ export interface GhostContentRootIdentity {realPath: string;dev: bigint;ino: bigint;+ mtimeNs: bigint;+ ctimeNs: bigint;}interface GhostContentAncestorIdentity {relativePath: string;dev: bigint;ino: bigint;+ mtimeNs: bigint;+ ctimeNs: bigint;}function sameFileIdentity(@@ -188,16 +192,31 @@ function sameStableFileState(before: fs.BigIntStats, after: fs.BigIntStats): boobefore.ctimeNs === after.ctimeNs;}+function sameStableDirectoryState(before: fs.BigIntStats, after: fs.BigIntStats): boolean {+ // Filesystems may immediately reuse a deleted directory's inode. Timestamps+ // keep that replacement distinct and also expose namespace changes below it.+ return after.isDirectory() &&+ sameFileIdentity(before, after) &&+ before.mtimeNs === after.mtimeNs &&+ before.ctimeNs === after.ctimeNs;+}+async function captureGhostContentRootIdentity(rootDir: string): Promise<GhostContentRootIdentity> {const realPath = await fs.promises.realpath(rootDir);const [pathStat, realStat] = await Promise.all([fs.promises.stat(rootDir, { bigint: true }),fs.promises.stat(realPath, { bigint: true }),]);- if (!pathStat.isDirectory() || !realStat.isDirectory() || !sameFileIdentity(pathStat, realStat)) {+ if (!pathStat.isDirectory() || !sameStableDirectoryState(pathStat, realStat)) {throw new Error(`ghost content root is not a stable directory: ${rootDir}`);}- return { realPath, dev: realStat.dev, ino: realStat.ino };+ return {+ realPath,+ dev: realStat.dev,+ ino: realStat.ino,+ mtimeNs: realStat.mtimeNs,+ ctimeNs: realStat.ctimeNs,+ };}async function assertGhostContentRootIdentity(@@ -208,7 +227,9 @@ async function assertGhostContentRootIdentity(if (current.realPath !== expected.realPath ||current.dev !== expected.dev ||- current.ino !== expected.ino+ current.ino !== expected.ino ||+ current.mtimeNs !== expected.mtimeNs ||+ current.ctimeNs !== expected.ctimeNs) {throw new Error(`ghost content root changed while reading: ${rootDir}`);}@@ -229,7 +250,13 @@ async function captureGhostContentAncestorIdentities(if (stat.isSymbolicLink() || !stat.isDirectory()) {throw new Error(`ghost content ancestor changed into a link: ${currentRelativePath}`);}- identities.push({ relativePath: currentRelativePath, dev: stat.dev, ino: stat.ino });+ identities.push({+ relativePath: currentRelativePath,+ dev: stat.dev,+ ino: stat.ino,+ mtimeNs: stat.mtimeNs,+ ctimeNs: stat.ctimeNs,+ });}return identities;}@@ -244,7 +271,9 @@ function assertGhostContentAncestorIdentities((identity, index) =>identity.relativePath !== expected[index]?.relativePath ||identity.dev !== expected[index]?.dev ||- identity.ino !== expected[index]?.ino,+ identity.ino !== expected[index]?.ino ||+ identity.mtimeNs !== expected[index]?.mtimeNs ||+ identity.ctimeNs !== expected[index]?.ctimeNs,)) {throw new Error('ghost content ancestor changed while reading');