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): recheck plugin leafs after close

Yuewei-LL920c0b4d7f64
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
index d67a00be78..fd47f4d27d 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
@@ -53,6 +53,39 @@ async function tryLinkFile(target: string, linkPath: string): Promise<boolean> {
}
}
+async function mockStaleLeafStatUntilHandleClose(file: string): Promise<{
+ handleOpened: () => void;
+ handleClosed: () => void;
+ markMutated: () => void;
+ restore: () => void;
+}> {
+ const initialStat = await fs.promises.lstat(file, { bigint: true });
+ const realLstat = fs.promises.lstat;
+ let handleOpen = false;
+ let mutated = false;
+ const lstatSpy = vi.spyOn(fs.promises, 'lstat').mockImplementation((async (
+ candidate: fs.PathLike,
+ options?: fs.StatOptions,
+ ) => {
+ if (handleOpen && mutated && path.resolve(String(candidate)) === path.resolve(file)) {
+ return initialStat;
+ }
+ return (realLstat as (...args: unknown[]) => Promise<fs.BigIntStats>)(candidate, options);
+ }) as typeof fs.promises.lstat);
+ return {
+ handleOpened: () => {
+ handleOpen = true;
+ },
+ handleClosed: () => {
+ handleOpen = false;
+ },
+ markMutated: () => {
+ mutated = true;
+ },
+ restore: () => lstatSpy.mockRestore(),
+ };
+}
+
describe('classifyGhostDirEntry', () => {
it('separates regular files, real directories and links', async () => {
const file = path.join(workDir, 'a.txt');
@@ -291,12 +324,14 @@ describe('hashGhostContentFiles', () => {
});
const splitAt = 'old-prefix|'.length;
+ const stalePathStat = await mockStaleLeafStatUntilHandleClose(file);
const realOpen = fs.promises.open;
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;
+ stalePathStat.handleOpened();
let pinnedHandleStat: fs.BigIntStats | undefined;
return new Proxy(handle, {
get(target, key) {
@@ -315,12 +350,22 @@ describe('hashGhostContentFiles', () => {
await fs.promises.writeFile(file, replacementBytes);
const changedAt = new Date(Date.now() + 5_000);
await fs.promises.utimes(file, changedAt, changedAt);
+ stalePathStat.markMutated();
const rest = Buffer.alloc(replacementBytes.byteLength - splitAt);
const restRead = await handle.read(rest, 0, rest.byteLength, splitAt);
yield rest.subarray(0, restRead.bytesRead);
})());
}
+ if (key === 'close') {
+ return async () => {
+ try {
+ await handle.close();
+ } finally {
+ stalePathStat.handleClosed();
+ }
+ };
+ }
const value = Reflect.get(target, key);
return typeof value === 'function' ? value.bind(target) : value;
},
@@ -332,6 +377,7 @@ describe('hashGhostContentFiles', () => {
).rejects.toThrow(/entry changed while reading/);
} finally {
openSpy.mockRestore();
+ stalePathStat.restore();
}
});
@@ -348,12 +394,14 @@ describe('hashGhostContentFiles', () => {
label: 'test',
});
+ const stalePathStat = await mockStaleLeafStatUntilHandleClose(file);
const realOpen = fs.promises.open;
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;
+ stalePathStat.handleOpened();
let pinnedHandleStat: fs.BigIntStats | undefined;
return new Proxy(handle, {
get(target, key) {
@@ -369,9 +417,19 @@ describe('hashGhostContentFiles', () => {
const read = await handle.read(bytes, 0, bytes.byteLength, 0);
await fs.promises.rename(file, detachedFile);
await fs.promises.writeFile(file, 'replacement bytes');
+ stalePathStat.markMutated();
yield bytes.subarray(0, read.bytesRead);
})());
}
+ if (key === 'close') {
+ return async () => {
+ try {
+ await handle.close();
+ } finally {
+ stalePathStat.handleClosed();
+ }
+ };
+ }
const value = Reflect.get(target, key);
return typeof value === 'function' ? value.bind(target) : value;
},
@@ -383,6 +441,7 @@ describe('hashGhostContentFiles', () => {
).rejects.toThrow(/entry (?:changed|path changed) while reading/);
} finally {
openSpy.mockRestore();
+ stalePathStat.restore();
}
});
diff --git a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
index 64206f96d4..bb56aa5704 100644
--- a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
@@ -349,8 +349,9 @@ export async function hashGhostContentFiles(
);
const noFollow = fs.constants.O_NOFOLLOW ?? null;
const handle = await fs.promises.open(filePath, fs.constants.O_RDONLY | (noFollow ?? 0));
+ let handleStat: fs.BigIntStats;
try {
- const handleStat = await handle.stat({ bigint: true });
+ handleStat = await handle.stat({ bigint: true });
if (!handleStat.isFile()) {
throw new Error(`ghost content entry is not a regular file: ${relativePath}`);
}
@@ -383,24 +384,24 @@ export async function hashGhostContentFiles(
if (!sameStableFileState(handleStat, afterReadStat)) {
throw new Error(`ghost content entry changed while reading: ${relativePath}`);
}
- const afterReadPathStat = await fs.promises.lstat(filePath, { bigint: true });
- if (
- afterReadPathStat.isSymbolicLink() ||
- !afterReadPathStat.isFile() ||
- !sameFileIdentity(afterReadPathStat, afterReadStat)
- ) {
- throw new Error(`ghost content entry path changed while reading: ${relativePath}`);
- }
- if (!sameStableFileState(handleStat, afterReadPathStat)) {
- throw new Error(`ghost content entry changed while reading: ${relativePath}`);
- }
- assertGhostContentAncestorIdentities(
- ancestorIdentities,
- await captureGhostContentAncestorIdentities(rootIdentity.realPath, relativePath),
- );
} finally {
await handle.close();
}
+ const afterReadPathStat = await fs.promises.lstat(filePath, { bigint: true });
+ if (
+ afterReadPathStat.isSymbolicLink() ||
+ !afterReadPathStat.isFile() ||
+ !sameFileIdentity(afterReadPathStat, handleStat)
+ ) {
+ throw new Error(`ghost content entry path changed while reading: ${relativePath}`);
+ }
+ if (!sameStableFileState(handleStat, afterReadPathStat)) {
+ throw new Error(`ghost content entry changed while reading: ${relativePath}`);
+ }
+ assertGhostContentAncestorIdentities(
+ ancestorIdentities,
+ await captureGhostContentAncestorIdentities(rootIdentity.realPath, relativePath),
+ );
hash.update(fileHash.digest());
}
await assertGhostContentRootIdentity(rootDir, rootIdentity);