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): rehash plugin leafs after reopen

Yuewei-LLf021e74a0d32
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 b7dda3fff1..ece3d79792 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostContentTree.test.ts
@@ -318,21 +318,24 @@ describe('hashGhostContentFiles', () => {
const splitAt = 'old-prefix|'.length;
const stalePathStat = await mockStaleLeafPathStatAfterMutation(file);
const realOpen = fs.promises.open;
+ let targetOpenCount = 0;
+ 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;
- let pinnedHandleStat: fs.BigIntStats | undefined;
+ targetOpenCount += 1;
+ const mutateDuringRead = targetOpenCount === 1;
return new Proxy(handle, {
get(target, key) {
if (key === 'stat') {
return async () => {
- pinnedHandleStat ??= await handle.stat({ bigint: true });
- return pinnedHandleStat;
+ staleHandleStat ??= await handle.stat({ bigint: true });
+ return staleHandleStat;
};
}
- if (key === 'createReadStream') {
+ if (key === 'createReadStream' && mutateDuringRead) {
return () => Readable.from((async function*() {
const first = Buffer.alloc(splitAt);
const firstRead = await handle.read(first, 0, first.byteLength, 0);
@@ -378,21 +381,24 @@ describe('hashGhostContentFiles', () => {
const stalePathStat = await mockStaleLeafPathStatAfterMutation(file);
const realOpen = fs.promises.open;
+ let targetOpenCount = 0;
+ 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;
- let pinnedHandleStat: fs.BigIntStats | undefined;
+ targetOpenCount += 1;
+ const mutateDuringRead = targetOpenCount === 1;
return new Proxy(handle, {
get(target, key) {
if (key === 'stat') {
return async () => {
- pinnedHandleStat ??= await handle.stat({ bigint: true });
- return pinnedHandleStat;
+ staleHandleStat ??= await handle.stat({ bigint: true });
+ return staleHandleStat;
};
}
- if (key === 'createReadStream') {
+ if (key === 'createReadStream' && mutateDuringRead) {
return () => Readable.from((async function*() {
const bytes = Buffer.alloc(originalBytes.byteLength);
const read = await handle.read(bytes, 0, bytes.byteLength, 0);
diff --git a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
index 1e0bcc7142..0aaa87e90a 100644
--- a/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostContentTree.ts
@@ -389,8 +389,9 @@ export async function hashGhostContentFiles(
} finally {
await handle.close();
}
+ const fileDigest = fileHash.digest();
// Windows 的按路径 stat/lstat 在 rename/replace 后可能继续返回旧目录元数据。
- // 重新打开同一路径,用新句柄的状态确认它仍是刚才读取的那一份文件。
+ // 重新打开同一路径,并再次流式摘要;新句柄的 stat 也可能陈旧,字节对账才是最终证据。
const verificationHandle = await fs.promises.open(
filePath,
fs.constants.O_RDONLY | (noFollow ?? 0),
@@ -400,6 +401,16 @@ export async function hashGhostContentFiles(
if (!sameStableFileState(handleStat, verificationStat)) {
throw new Error(`ghost content entry changed while reading: ${relativePath}`);
}
+ const verificationFileHash = crypto.createHash('sha256');
+ const verificationStream = verificationHandle.createReadStream({ autoClose: false });
+ for await (const chunk of verificationStream) verificationFileHash.update(chunk as Buffer);
+ const afterVerificationReadStat = await verificationHandle.stat({ bigint: true });
+ if (!sameStableFileState(verificationStat, afterVerificationReadStat)) {
+ throw new Error(`ghost content entry changed while reading: ${relativePath}`);
+ }
+ if (!crypto.timingSafeEqual(fileDigest, verificationFileHash.digest())) {
+ 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),
@@ -408,12 +419,12 @@ export async function hashGhostContentFiles(
initialRealFilePath === undefined ||
afterReadPathStat.isSymbolicLink() ||
!afterReadPathStat.isFile() ||
- !sameFileIdentity(afterReadPathStat, verificationStat) ||
+ !sameFileIdentity(afterReadPathStat, afterVerificationReadStat) ||
afterReadRealFilePath !== initialRealFilePath
) {
throw new Error(`ghost content entry path changed while reading: ${relativePath}`);
}
- if (!sameStableFileState(verificationStat, afterReadPathStat)) {
+ if (!sameStableFileState(afterVerificationReadStat, afterReadPathStat)) {
throw new Error(`ghost content entry changed while reading: ${relativePath}`);
}
assertGhostContentAncestorIdentities(
@@ -423,7 +434,7 @@ export async function hashGhostContentFiles(
} finally {
await verificationHandle.close();
}
- hash.update(fileHash.digest());
+ hash.update(fileDigest);
}
await assertGhostContentRootIdentity(rootDir, rootIdentity);
return hash.digest('hex');