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): verify plugin leafs with fresh handles

Yuewei-LLecf4cafc1ab1
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 fd47f4d27d..b7dda3fff1 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
@@ -53,32 +53,24 @@ async function tryLinkFile(target: string, linkPath: string): Promise<boolean> {
}
}
-async function mockStaleLeafStatUntilHandleClose(file: string): Promise<{
- handleOpened: () => void;
- handleClosed: () => void;
+async function mockStaleLeafPathStatAfterMutation(file: string): Promise<{
markMutated: () => void;
restore: () => void;
}> {
+ // GitHub Windows runner 上,句柄关闭后按路径 lstat 仍可能短暂返回 mutation 前的状态。
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)) {
+ if (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;
},
@@ -324,14 +316,13 @@ describe('hashGhostContentFiles', () => {
});
const splitAt = 'old-prefix|'.length;
- const stalePathStat = await mockStaleLeafStatUntilHandleClose(file);
+ const stalePathStat = await mockStaleLeafPathStatAfterMutation(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) {
@@ -357,15 +348,6 @@ describe('hashGhostContentFiles', () => {
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;
},
@@ -394,14 +376,13 @@ describe('hashGhostContentFiles', () => {
label: 'test',
});
- const stalePathStat = await mockStaleLeafStatUntilHandleClose(file);
+ const stalePathStat = await mockStaleLeafPathStatAfterMutation(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) {
@@ -421,15 +402,6 @@ describe('hashGhostContentFiles', () => {
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;
},
diff --git a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
index bb56aa5704..1e0bcc7142 100644
--- a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
@@ -350,6 +350,7 @@ 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;
+ let initialRealFilePath: string | undefined;
try {
handleStat = await handle.stat({ bigint: true });
if (!handleStat.isFile()) {
@@ -369,6 +370,7 @@ export async function hashGhostContentFiles(
fs.promises.stat(filePath, { bigint: true }),
fs.promises.realpath(filePath),
]);
+ initialRealFilePath = realFilePath;
const relativeRealPath = path.relative(rootIdentity.realPath, realFilePath);
const outsideRoot =
relativeRealPath === '..' ||
@@ -387,21 +389,40 @@ export async function hashGhostContentFiles(
} 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),
+ // Windows 的按路径 stat/lstat 在 rename/replace 后可能继续返回旧目录元数据。
+ // 重新打开同一路径,用新句柄的状态确认它仍是刚才读取的那一份文件。
+ const verificationHandle = await fs.promises.open(
+ filePath,
+ fs.constants.O_RDONLY | (noFollow ?? 0),
);
+ try {
+ const verificationStat = await verificationHandle.stat({ bigint: true });
+ if (!sameStableFileState(handleStat, verificationStat)) {
+ throw new Error(`ghost content entry changed while reading: ${relativePath}`);
+ }
+ const [afterReadPathStat, afterReadRealFilePath] = await Promise.all([
+ fs.promises.lstat(filePath, { bigint: true }),
+ fs.promises.realpath(filePath),
+ ]);
+ if (
+ initialRealFilePath === undefined ||
+ afterReadPathStat.isSymbolicLink() ||
+ !afterReadPathStat.isFile() ||
+ !sameFileIdentity(afterReadPathStat, verificationStat) ||
+ afterReadRealFilePath !== initialRealFilePath
+ ) {
+ throw new Error(`ghost content entry path changed while reading: ${relativePath}`);
+ }
+ if (!sameStableFileState(verificationStat, afterReadPathStat)) {
+ throw new Error(`ghost content entry changed while reading: ${relativePath}`);
+ }
+ assertGhostContentAncestorIdentities(
+ ancestorIdentities,
+ await captureGhostContentAncestorIdentities(rootIdentity.realPath, relativePath),
+ );
+ } finally {
+ await verificationHandle.close();
+ }
hash.update(fileHash.digest());
}
await assertGhostContentRootIdentity(rootDir, rootIdentity);