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): guard the local stat cache against stale cancelled writes

MagicLiziec55d2706b5e
diff --git a/apps/desktop/src/renderer/components/chat/GeneratedFilesCard.tsx b/apps/desktop/src/renderer/components/chat/GeneratedFilesCard.tsx
index 213463047a..3348b931ff 100644
--- a/apps/desktop/src/renderer/components/chat/GeneratedFilesCard.tsx
+++ b/apps/desktop/src/renderer/components/chat/GeneratedFilesCard.tsx
@@ -897,21 +897,25 @@ export const GeneratedFilesCard = memo(function GeneratedFilesCard({
if (ok) confirmedPaths.add(toStat[index].path);
});
} else {
- const checks = await Promise.all(
+ // 先收集结果、不写入共享缓存:本次检查在 statPath 返回前被 checkKey 变化取消时
+ // (见本 effect 末尾的 cleanup),若这里仍把过期的 stat 写进模块缓存,它可能比
+ // 同路径更新一轮的结果更晚返回、反覆盖那个新结果;下次重挂就会用这个已失效的
+ // 缓存重新点亮已删文件。只有本次未被取消才许写。
+ const statResults = await Promise.all(
toStat.map(async (file) => {
try {
const stat = await window.electronAPI.fsBrowse.statPath(file.path);
- rememberLocalGeneratedFileStat(file.path, stat);
- return isLocalGeneratedFileInTurn(file, stat, turnStartMs, turnEndMs);
+ return { path: file.path, stat, ok: isLocalGeneratedFileInTurn(file, stat, turnStartMs, turnEndMs) };
} catch {
- rememberLocalGeneratedFileStat(file.path, null);
- return false;
+ return { path: file.path, stat: null, ok: false };
}
}),
);
- checks.forEach((ok, index) => {
- if (ok) confirmedPaths.add(toStat[index].path);
- });
+ if (cancelled) return;
+ for (const result of statResults) {
+ rememberLocalGeneratedFileStat(result.path, result.stat);
+ if (result.ok) confirmedPaths.add(result.path);
+ }
}
if (cancelled) return;
const merged = mergeGeneratedFileStatResults({
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/GeneratedFilesCard.localSeed.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/GeneratedFilesCard.localSeed.test.tsx
index 3fb51e77ce..4ae64f0a41 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/GeneratedFilesCard.localSeed.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/GeneratedFilesCard.localSeed.test.tsx
@@ -182,6 +182,49 @@ describe('local generated files remount', () => {
await waitFor(() => expect(document.querySelector('img')).not.toBeNull());
});
+ it('does not let a stale cancelled stat overwrite a fresher cache verdict', async () => {
+ const pendingStat: Array<(stat: unknown) => void> = [];
+ const statPath = vi.fn(
+ () =>
+ new Promise((resolve) => {
+ pendingStat.push(resolve);
+ }),
+ );
+ stubStat(statPath);
+
+ const { rerender, unmount } = render(
+ cardElement({ renderItemKey: 'genfiles-a', turnStartMs: START, turnEndMs: null }),
+ );
+ await waitFor(() => expect(pendingStat).toHaveLength(1));
+
+ // The turn boundary lands mid-check: the in-flight check is cancelled and
+ // a fresh one starts for the same path.
+ rerender(
+ cardElement({ renderItemKey: 'genfiles-a', turnStartMs: START, turnEndMs: START + 10_000 }),
+ );
+ await waitFor(() => expect(pendingStat).toHaveLength(2));
+
+ // The fresh check lands first and finds the file gone.
+ pendingStat[1]({ kind: 'missing' });
+ await waitFor(() => expect(screen.queryByText('report.md')).toBeNull());
+
+ // The stale, already-cancelled check now resolves with a stat that would
+ // wrongly confirm the file. It must not win the race against the fresher
+ // 'missing' verdict already recorded above.
+ pendingStat[0]({ kind: 'file', birthtimeMs: START + 5_000, mtimeMs: START + 5_000 });
+ await Promise.resolve();
+ expect(screen.queryByText('report.md')).toBeNull();
+ unmount();
+
+ // A fresh remount under the same turn window must not be seeded from the
+ // stale cached stat; the module cache should still hold the fresher
+ // 'missing' verdict, not the cancelled check's 'file' result.
+ render(
+ cardElement({ renderItemKey: 'genfiles-b', turnStartMs: START, turnEndMs: START + 10_000 }),
+ );
+ expect(screen.queryByText('report.md')).toBeNull();
+ });
+
it('re-checks a seeded path when another file finishes during the first check', async () => {
const notes: GeneratedFileRef = {
path: 'C:\\work\\notes.md',