Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(file-browser): watcher 重建前先转发合并窗口内的 pending 事件(PR #4398 轮六)
Zhang Yunjin4c13affaa9fc
diff --git a/packages/remote-file-service/src/__tests__/watchOptions.test.ts b/packages/remote-file-service/src/__tests__/watchOptions.test.tsindex 2b0e4d64a2..d9821d11b8 100644--- a/packages/remote-file-service/src/__tests__/watchOptions.test.ts+++ b/packages/remote-file-service/src/__tests__/watchOptions.test.ts@@ -270,6 +270,34 @@ describe('WorkdirWatchManager 过滤开关', () => {manager.stopAll();});+ /**+ * 评审 P2(PR #4398 轮六):并集变化触发 watcher 重建时,旧 entry 的 `pending`+ * 不能随 entry 一起丢 —— 已入队(尚未过 50ms 合并窗口)的事件仍要转发给还在线的+ * 消费者,否则文件树陈旧到同目录下一次事件或手动刷新。+ */+ it('重建 watcher 前先转发 pending 事件:并集收窄不丢已入队事件', async () => {+ const manager = new WorkdirWatchManager((event) => emitted.push(event));+ await manager.start('/repo', { showIgnoredDirs: true }, 'desktop-tree');+ await manager.start('/repo', { showIgnoredDirs: false }, 'device-link');+ expect(h.created).toHaveLength(1);++ // 可见文件的 change 事件进入 50ms 合并窗口(还没到 flush)。+ h.created[0].cb?.('change', 'src/app.ts');+ expect(emitted).toHaveLength(0);++ // desktop 消费者在窗口内停止 → 并集收窄 → 重建 watcher。+ manager.stop('/repo', 'desktop-tree');++ // pending 必须先转发给仍在线的 device-link 客户端,而不是随旧 entry 丢弃。+ expect(emitted.map((e) => e.relPath)).toEqual(['src/app.ts']);++ await new Promise((resolve) => setTimeout(resolve, 0)); // 等异步 reconcile+ expect(h.created).toHaveLength(2);+ expect(h.created.at(-1)?.closed).toBe(false);+ expect(h.matcherOpts.at(-1)?.showIgnoredDirs).toBe(false);+ manager.stopAll();+ });+it('最后一个消费者 stop 才拆 watcher', async () => {const manager = new WorkdirWatchManager(() => {});await manager.start('/repo', { showIgnoredDirs: true }, 'desktop-tree');diff --git a/packages/remote-file-service/src/watch.ts b/packages/remote-file-service/src/watch.tsindex b9133a09cf..156402623b 100644--- a/packages/remote-file-service/src/watch.ts+++ b/packages/remote-file-service/src/watch.ts@@ -256,7 +256,9 @@ export class WorkdirWatchManager {const existing = this.entries.get(workdir);if (existing) {if (sameWatchOptions(existing, want)) return;- this.closeEntry(workdir); // 选项变了:拆掉重建(不能走 stop(),它会撤销 desired)+ // 选项变了:拆掉重建(不能走 stop(),它会撤销 desired)。重建前先转发+ // 合并窗口里的事件 —— 否则还在线的消费者会漏掉这一批(评审 P2)。+ this.closeEntry(workdir, { flushPending: true });}const run = this.startInner(workdir, want);this.starting.set(workdir, run);@@ -364,7 +366,8 @@ export class WorkdirWatchManager {// entry 选项没变而原地返回,直播就静默冻结到手动改开关或 daemon 重启。log.warn('fs.watch error, dropping watcher', workdir, String(err));if (this.entries.get(workdir) !== entry) return; // 已被替换 / 停止:不碰新 entry- this.closeEntry(workdir);+ // 报错前已入队的事件仍是真实事件:重建前先转发,不随旧 entry 丢弃(评审 P2)。+ this.closeEntry(workdir, { flushPending: true });// 就地收敛;失败(挂载短暂不可用 / 目录正在被替换)则带退避重试,直到成功// 或耗尽 —— 只记日志会让这个入口静默失效到用户改开关(评审 P2)。void this.reconcile(workdir)@@ -418,12 +421,32 @@ export class WorkdirWatchManager {this.closeEntry(workdir);}- /** 拆掉已就位的 watcher。不碰 desired —— 选项变化重建时由调用方决定意图。 */- private closeEntry(workdir: string): void {+ /** 把合并窗口里已经入队的事件立刻发出去(重建前保序转发用)。 */+ private flushPending(entry: WatchEntry): void {+ if (entry.flushTimer) {+ clearTimeout(entry.flushTimer);+ entry.flushTimer = null;+ }+ if (entry.pending.size === 0) return;+ const batch = [...entry.pending.values()];+ entry.pending.clear();+ for (const evt of batch) this.emit(evt);+ }++ /**+ * 拆掉已就位的 watcher。不碰 desired —— 选项变化重建时由调用方决定意图。+ *+ * `flushPending`:**重建**路径(选项变化 / watcher 报错后重建)要先转发合并+ * 窗口里的事件 —— 旧 entry 的 `pending` 随 entry 一起被丢的话,还活着的消费者+ * 会漏掉这次事件,文件树陈旧到同目录下一次事件或手动刷新(评审 P2)。+ * 最后一个消费者停止 / stopAll 不带这个旗标:那时事件已无人消费,直接丢。+ */+ private closeEntry(workdir: string, opts?: { flushPending?: boolean }): void {const entry = this.entries.get(workdir);if (!entry) return;this.entries.delete(workdir);- if (entry.flushTimer) clearTimeout(entry.flushTimer);+ if (opts?.flushPending) this.flushPending(entry);+ else if (entry.flushTimer) clearTimeout(entry.flushTimer);try {entry.watcher.close();} catch {@@ -480,9 +503,7 @@ export class WorkdirWatchManager {if (entry.flushTimer) return;entry.flushTimer = setTimeout(() => {entry.flushTimer = null;- const batch = [...entry.pending.values()];- entry.pending.clear();- for (const evt of batch) this.emit(evt);+ this.flushPending(entry);}, COALESCE_MS);entry.flushTimer.unref?.();}