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(file-browser): 引用计数改用结构化 key + 偏好缓存窗口期后重读

Zhang Yunjin9b9c3b75127d
diff --git a/apps/desktop/src/main/file-browser/__tests__/remoteWatch.test.ts b/apps/desktop/src/main/file-browser/__tests__/remoteWatch.test.ts
index 312a4c2046..41de80851a 100644
--- a/apps/desktop/src/main/file-browser/__tests__/remoteWatch.test.ts
+++ b/apps/desktop/src/main/file-browser/__tests__/remoteWatch.test.ts
@@ -128,4 +128,31 @@ describe('RemoteWatchRegistry 失败清理', () => {
consumerId: 'desktop-tree',
});
});
+
+ /**
+ * 评审 P1:workdir 是合法 POSIX 路径,可以包含 `::`(如 `/srv/foo::bar`)。旧实现
+ * 把引用计数扫描建立在 `${windowId}::${hostId}::${workdir}` 的字符串拆分上 ——
+ * 拆分会把路径截断,于是认不出另一个窗口在看同一 workdir,停一个窗口会误发
+ * watchStop 把另一个也停掉。现在 hostId / workdir 结构化存在 entry 上。
+ */
+ it('workdir 含 :: 时引用计数仍准确:停一个窗口不误发 watchStop', async () => {
+ const { registry, request } = setup();
+ const w1 = makeWindow(1);
+ const w2 = makeWindow(2);
+ const workdir = '/srv/foo::bar';
+
+ await registry.start(w1, 'host-1', workdir, {}, vi.fn());
+ await registry.start(w2, 'host-1', workdir, {}, vi.fn());
+ request.mockClear();
+
+ await registry.stop(w1.id, 'host-1', workdir);
+ // 另一个窗口还在看 → 不能发 watchStop。
+ expect(request).not.toHaveBeenCalledWith('host-1', 'watchStop', expect.anything());
+
+ await registry.stop(w2.id, 'host-1', workdir);
+ expect(request).toHaveBeenCalledWith('host-1', 'watchStop', {
+ workdir,
+ consumerId: 'desktop-tree',
+ });
+ });
});
diff --git a/apps/desktop/src/main/file-browser/remote-watch.ts b/apps/desktop/src/main/file-browser/remote-watch.ts
index 7221a5fad8..4bda42a8c1 100644
--- a/apps/desktop/src/main/file-browser/remote-watch.ts
+++ b/apps/desktop/src/main/file-browser/remote-watch.ts
@@ -29,6 +29,12 @@ const log = createLogger('file-browser/remote-watch');
const WATCH_CONSUMER_ID = 'desktop-tree';
interface RegistryEntry {
+ /** 结构化保存 host / workdir:引用计数扫描不再拆 `${windowId}::${host}::${workdir}`
+ * —— workdir 里合法出现 `::` 时(如 `/srv/foo::bar`),拆分会截断路径,扫描就
+ * 认不出另一个窗口在看同一 workdir,停一个窗口会误发 watchStop 把另一个也停掉
+ * (评审 P1)。 */
+ hostId: string;
+ workdir: string;
offEvent: () => void;
offReconnect: () => void;
}
@@ -78,7 +84,7 @@ export class RemoteWatchRegistry {
.request(hostId, 'watchStart', watchOpts)
.catch((err) => log.warn('watch replay failed', { hostId, workdir, error: String(err) }));
});
- const entry: RegistryEntry = { offEvent, offReconnect };
+ const entry: RegistryEntry = { hostId, workdir, offEvent, offReconnect };
this.entries.set(k, entry);
window.once('closed', () => {
@@ -107,10 +113,9 @@ export class RemoteWatchRegistry {
// 没有任何客户端再去 stop 它(评审 P1),它还会抬高 device-link 的可见性
// 并集。只在确认本地再无同 (host, workdir) 条目时才发:还有别的窗口在
// watch(或它的 start 正在飞,条目已先入表)时不能撤。
- const stillLocal = [...this.entries.keys()].some((key) => {
- const [, h, w] = key.split('::');
- return h === hostId && w === workdir;
- });
+ const stillLocal = [...this.entries.values()].some(
+ (e) => e.hostId === hostId && e.workdir === workdir,
+ );
if (!stillLocal) {
void this.mgr
.request(hostId, 'watchStop', { workdir, consumerId: WATCH_CONSUMER_ID })
@@ -129,10 +134,9 @@ export class RemoteWatchRegistry {
entry.offReconnect();
// 同 host 其它 window/workdir 还在 watch 时不能全局 watchStop;仅当这是该
// (host, workdir) 的最后一个订阅者才让 daemon 停 watch。
- const stillWatching = [...this.entries.keys()].some((key) => {
- const [, h, w] = key.split('::');
- return h === hostId && w === workdir;
- });
+ const stillWatching = [...this.entries.values()].some(
+ (e) => e.hostId === hostId && e.workdir === workdir,
+ );
if (!stillWatching) {
await this.mgr
.request(hostId, 'watchStop', { workdir, consumerId: WATCH_CONSUMER_ID })
diff --git a/apps/desktop/src/renderer/hooks/__tests__/useFileBrowserPreference.test.ts b/apps/desktop/src/renderer/hooks/__tests__/useFileBrowserPreference.test.ts
index 6ae12b2b0d..a7b6593094 100644
--- a/apps/desktop/src/renderer/hooks/__tests__/useFileBrowserPreference.test.ts
+++ b/apps/desktop/src/renderer/hooks/__tests__/useFileBrowserPreference.test.ts
@@ -32,6 +32,25 @@ describe('useFileBrowserPreference', () => {
expect(result.current.isCustomized).toBe(false);
});
+ /**
+ * 评审 P1:本窗口在没有任何消费者的窗口期里摘掉了 storage listener,期间另一个
+ * 窗口改了偏好就收不到事件;重挂载时 readPreference 又会走模块级缓存早退,会让
+ * 开关与文件树永久陈旧到下一次变更或刷新。所以首个监听者(重)挂载时要让缓存
+ * 失效、回落 localStorage。
+ */
+ it('无消费者窗口期后重挂载会重读 localStorage', () => {
+ const first = renderHook(() => useFileBrowserPreference());
+ expect(first.result.current.showIgnoredDirs).toBe(false);
+ first.unmount(); // → 本窗口没有监听者了
+
+ // 另一个窗口改了偏好:只落 localStorage,本窗口收不到 storage 事件。
+ localStorage.setItem(KEY, 'true');
+
+ const second = renderHook(() => useFileBrowserPreference());
+ expect(second.result.current.showIgnoredDirs).toBe(true);
+ expect(second.result.current.isCustomized).toBe(true);
+ });
+
it('读取已存的开启 override', () => {
localStorage.setItem(KEY, 'true');
expect(getShowIgnoredDirs()).toBe(true);
diff --git a/apps/desktop/src/renderer/hooks/useFileBrowserPreference.ts b/apps/desktop/src/renderer/hooks/useFileBrowserPreference.ts
index 14c390a78a..bb109d3698 100644
--- a/apps/desktop/src/renderer/hooks/useFileBrowserPreference.ts
+++ b/apps/desktop/src/renderer/hooks/useFileBrowserPreference.ts
@@ -86,6 +86,14 @@ export function useFileBrowserPreference(): {
useEffect(() => {
const sync = () => setPreference(readPreference());
+ // 首个监听者(重)挂载时让模块级缓存失效、回落 localStorage:本窗口可能在
+ // 没有任何消费者的窗口期里错过另一个窗口的 storage 事件(那时 listener 不在
+ // 场),不重读就会让开关与文件树永久陈旧到下一次变更或刷新(评审 P1)。
+ if (listeners.size === 0) {
+ memoryValue = null;
+ memoryCustomized = null;
+ sync();
+ }
listeners.add(sync);
const onStorage = (e: StorageEvent) => {
if (e.key !== STORAGE_KEY) return;