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): 关开关的消失动作改为整体一次性 + 补 loading 态
Zhang Yunjin51200d7cfd73
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useFileTree.test.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useFileTree.test.tsxindex 856fb9ac7c..713f7d976e 100644--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useFileTree.test.tsx+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useFileTree.test.tsx@@ -195,10 +195,10 @@ describe('useFileTree showIgnoredDirs option', () => {/*** 切开关会换一份 store。新 store 若从空快照 + initialLoading 起步,FileTreeView* 会把整树替换成空白占位(本地 <300ms 连 spinner 都没有),视觉上闪一下;- * 「刷新」按钮原地 refetch 所以不闪。新 store 必须继承兄弟 store 的根列表,- * 等新 matcher 的数据回来再校正(子树缓存与展开态不跨 scope 继承,见下一条)。+ * 「刷新」按钮原地 refetch 所以不闪。新 store 必须继承兄弟 store 的整棵可见树,+ * 等新 matcher 的数据回来再整体替换(不是只借根列表,见下一条)。*/- it('切开关继承根列表,不回到 initialLoading 空白', async () => {+ it('切开关继承整棵可见树,不回到 initialLoading 空白', async () => {const hiddenEntries: readonly DirEntry[] = [{ name: 'src', relPath: 'src', type: 'directory', size: 0, mtimeMs: 1 },];@@ -237,22 +237,32 @@ describe('useFileTree showIgnoredDirs option', () => {});/**- * 关开关方向不继承旧 scope 的展开集合与子树缓存:reveal 态展开过的- * node_modules 是 reveal-only 路径,带进 hidden store 会让它进入展开集合与- * warm 列表(hidden 侧要为它发 listDir,SSH 上可达数百个 RPC),并在根列表- * 回来前把被忽略的行显示出来。展开态按各自 scope 从 localStorage 恢复。+ * 关开关的消失动作必须是一次性的:过渡期(hidden 数据还没回来)整棵树保持不动+ * —— 包括展开态 —— 而不是先丢子行再丢父行(那是「从最子级逐级折叠」)。+ * root 数据回来后才整体替换,并把借来的 reveal-only 展开位剪掉:hidden 树下+ * node_modules 已不在 root 列表里,留着会随下次操作写进 hidden scope 的+ * localStorage,下次启动白发一批 listDir。*/- it('关开关不把 reveal-only 路径带进 hidden store', async () => {+ it('关开关整树保持到数据回来,再一次性替换并剪掉 reveal-only 展开位', async () => {const revealedRoot: readonly DirEntry[] = [{ name: 'node_modules', relPath: 'node_modules', type: 'directory', size: 0, mtimeMs: 1 },];- mocks.listDir.mockImplementation((args: { relPath?: string; showIgnoredDirs?: boolean }) =>- Promise.resolve(args.showIgnoredDirs ? revealedRoot : ([] as readonly DirEntry[])),- );+ const revealedChild: readonly DirEntry[] = [+ { name: 'pkg', relPath: 'node_modules/pkg', type: 'directory', size: 0, mtimeMs: 1 },+ ];+ const hiddenRoot: readonly DirEntry[] = [+ { name: 'src', relPath: 'src', type: 'directory', size: 0, mtimeMs: 1 },+ ];+ mocks.listDir.mockImplementation((args: { relPath?: string; showIgnoredDirs?: boolean }) => {+ if (args.showIgnoredDirs) {+ return Promise.resolve(args.relPath === 'node_modules' ? revealedChild : revealedRoot);+ }+ return Promise.resolve(hiddenRoot);+ });const view = renderHook(({ reveal }: { reveal: boolean }) =>- useFileTree({ workdir: '/workdir-seed-filter', showIgnoredDirs: reveal }),+ useFileTree({ workdir: '/workdir-seed-swap', showIgnoredDirs: reveal }),{ initialProps: { reveal: true } },);await waitFor(() => expect(view.result.current.initialLoading).toBe(false));@@ -261,17 +271,70 @@ describe('useFileTree showIgnoredDirs option', () => {});expect(view.result.current.expanded.has('node_modules')).toBe(true);+ // 过渡期:挂住 hidden 侧的 root listDir,模拟慢通道。+ const pendingRoot = deferred<readonly DirEntry[]>();+ mocks.listDir.mockImplementation((args: { showIgnoredDirs?: boolean }) => {+ if (args.showIgnoredDirs) return Promise.resolve(revealedRoot);+ return pendingRoot.promise;+ });mocks.listDir.mockClear();+await act(async () => {view.rerender({ reveal: false });});- await waitFor(() => expect(view.result.current.initialLoading).toBe(false));- // hidden store 不该继承 reveal-only 的展开态,也不该为它发 listDir。+ // 数据还没回来:整棵树保持不动(含展开的子级),不是逐级折叠。+ expect(view.result.current.initialLoading).toBe(false);+ expect(view.result.current.entries.get('')).toEqual(revealedRoot);+ expect(view.result.current.entries.get('node_modules')).toEqual(revealedChild);+ expect(view.result.current.expanded.has('node_modules')).toBe(true);+ // 过渡期不 warm 借来的子树 —— 不给被忽略路径白发 listDir。+ const hiddenCalls = mocks.listDir.mock.calls.filter((c) => c[0].showIgnoredDirs === false);+ expect(hiddenCalls.map((c) => c[0].relPath)).toEqual(['']);++ await act(async () => {+ pendingRoot.resolve(hiddenRoot);+ await Promise.resolve();+ });+ await waitFor(() => expect(view.result.current.entries.get('')).toEqual(hiddenRoot));+ // 整体替换完成:借来的 reveal-only 展开位被剪掉。expect(view.result.current.expanded.has('node_modules')).toBe(false);- expect(mocks.listDir).not.toHaveBeenCalledWith(- expect.objectContaining({ relPath: 'node_modules' }),++ view.unmount();+ });++ /**+ * 兄弟 store 还在首次 listDir 上(慢通道)时不能冒充「已加载」:没有可显示内容+ * 就保持 initialLoading,否则 FileTreeView 会把空 rows 渲染成「此文件夹为空」+ * 而不是延迟 loading 态。+ */+ it('兄弟 store 还没有首帧数据时保持 initialLoading', async () => {+ const pendingReveal = deferred<readonly DirEntry[]>();+ const pendingHidden = deferred<readonly DirEntry[]>();+ mocks.listDir.mockImplementation((args: { showIgnoredDirs?: boolean }) =>+ args.showIgnoredDirs ? pendingReveal.promise : pendingHidden.promise,+ );++ const view = renderHook(+ ({ reveal }: { reveal: boolean }) =>+ useFileTree({ workdir: '/workdir-seed-pending', showIgnoredDirs: reveal }),+ { initialProps: { reveal: true } },);+ expect(view.result.current.initialLoading).toBe(true);++ await act(async () => {+ view.rerender({ reveal: false });+ });+ // 兄弟 store 存在但没有可显示内容 → hidden 侧必须继续 loading。+ expect(view.result.current.initialLoading).toBe(true);++ await act(async () => {+ pendingHidden.resolve([+ { name: 'src', relPath: 'src', type: 'directory', size: 0, mtimeMs: 1 },+ ]);+ await Promise.resolve();+ });+ await waitFor(() => expect(view.result.current.initialLoading).toBe(false));view.unmount();});diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useFileTree.ts b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useFileTree.tsindex 5a1802cab0..45c90423bc 100644--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useFileTree.ts+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useFileTree.ts@@ -237,37 +237,80 @@ function emit(store: FileTreeStore): void {* 空快照 + initialLoading 起步,FileTreeView 会把整棵树替换成占位(本地* <300ms 连 spinner 都没有,就是空白),视觉上闪一下;同一 workdir 的另一半* reveal scope 的 store 此刻通常还在表里 —— 旧 store 的 refCount 归零发生在- * 本次 commit 的 effect cleanup,晚于 render 期间的 useMemo —— 借它的- * **根目录列表**当首帧内容,新 matcher 的 listDir 回来后再原地校正。+ * 本次 commit 的 effect cleanup,晚于 render 期间的 useMemo —— 借它+ * **整棵可见的树**(entries + expanded)当首帧内容,新 matcher 的 listDir 回来+ * 后再整体替换。*- * 只借根列表,不借子树缓存与 expanded:那两者都带旧 matcher 的语义。reveal 态- * 展开过 node_modules 时,把它们带进 hidden store 会让被忽略路径进入展开集合与- * warm 列表(hidden 侧会给它们发 listDir,SSH 上可达数百个 RPC),并在根列表- * 回来前把被忽略的行显示出来。展开态仍按各自 scope 从 localStorage 恢复- * (expandedStore 的分片本意),不跨 scope 合并。+ * 为什么整棵借、而不是只借根列表:只借根列表会把消失动作拆成两段 —— 子行先因+ * expanded 掉落而消失(父行还在),root 数据回来再拿掉父行 —— 多级展开时看起来+ * 就是「从最子级逐级折叠回去」。整棵借过来,关闭开关的动作是一次性的:树先保持+ * 不动,数据回来整体切换。+ *+ * 代价是过渡窗口(reveal → hidden)里旧 matcher 的行还会短暂可见 —— 窗口就是+ * 一次 listDir(本地 <12ms;慢通道到数据回来为止)。这是有意的取舍:全量、一次性+ * 地消失,比先闪空再重建、或逐级折叠都更接近直觉。+ *+ * 借来的 expanded 是**过渡态**:initStore 不据此 warm(免得给被忽略路径白发+ * listDir),root 数据回来时由 pruneExpandedForCurrentTree 按新树剪掉不可达路径+ * —— 否则它们会落进 hidden scope 的 localStorage,下次启动白拉一批。** 「刷新」按钮没有这个问题:它在同一份 store 上原地 refetch,从不经过空态。*/-function findRevealSiblingSeed(opts: Required<UseFileTreeOptions>): {- rootEntries: readonly DirEntry[] | null;- loadError: FileTreeStore['snapshot']['loadError'];-} | null {+function findRevealSiblingSnapshot(+ opts: Required<UseFileTreeOptions>,+): FileTreeStore['snapshot'] | null {const siblingKey = storeKey({ ...opts, showIgnoredDirs: !opts.showIgnoredDirs });- const sibling = stores.get(siblingKey);- if (!sibling) return null;- return {- rootEntries: sibling.snapshot.entries.get(ROOT_KEY) ?? null,- loadError: sibling.snapshot.loadError,+ return stores.get(siblingKey)?.snapshot ?? null;+}++/**+ * root 数据回来后,把「过渡期借来的、在当前树上不可达的展开路径」剪掉。+ *+ * 切开关时 seed 会把另一半 reveal scope 的整棵可见树带过来(见+ * findRevealSiblingSnapshot)。hidden 树下 node_modules / Library 这类路径已经+ * 不在 root 列表里,但它们的展开态还挂在 expanded 上 —— 留着会让用户在 hidden+ * 态做下一次操作时把它们写进 hidden scope 的 localStorage,下次启动就白发一批+ * listDir。逐级查父链:任一级不在对应父列表里即不可达。+ *+ * 查不到祖先列表(缓存被清 / 还没拉过)时保守保留 —— 宁可留一个 stale 展开位,+ * 也不能把用户真实的展开态删掉。+ */+function pruneExpandedForCurrentTree(store: FileTreeStore): void {+ if (!store.snapshot.entries.has(ROOT_KEY)) return;+ const reachable = (relPath: string): boolean => {+ const parts = relPath.split('/');+ let parent = ROOT_KEY;+ for (const part of parts) {+ const list = store.snapshot.entries.get(parent);+ if (!list) return true;+ const child = parent === ROOT_KEY ? part : `${parent}/${part}`;+ if (!list.some((entry) => entry.relPath === child)) return false;+ parent = child;+ }+ return true;};+ const next = new Set<string>();+ let changed = false;+ for (const relPath of store.snapshot.expanded) {+ if (relPath === ROOT_KEY || reachable(relPath)) next.add(relPath);+ else changed = true;+ }+ if (!changed) return;+ store.snapshot = { ...store.snapshot, expanded: next };+ emit(store);}function getOrCreateStore(opts: Required<UseFileTreeOptions>): FileTreeStore {const key = storeKey(opts);const existing = stores.get(key);if (existing) return existing;- // 切开关路径:借另一半 reveal scope 的根列表,第一帧就有内容可渲染,不经过- // initialLoading 空白;首次挂载(无兄弟 store)仍走原来的 loading 路径。- const seed = findRevealSiblingSeed(opts);+ // 切开关路径:借另一半 reveal scope 的整棵树当首帧内容,不经过 initialLoading+ // 空白;首次挂载(无兄弟 store)仍走原来的 loading 路径。+ const seed = findRevealSiblingSnapshot(opts);+ // 兄弟 store 可能还卡在首次 listDir 上(慢通道):没有可显示内容时不得冒充+ // 「已加载」—— 否则 FileTreeView 会把 initialLoading:false + 空 rows 渲染成+ // 「此文件夹为空」,而不是延迟 loading 态。终态错误可显示(渲染错误占位)。+ const seedDisplayable = !!seed && (seed.entries.size > 0 || seed.loadError !== null);const store: FileTreeStore = {key,workdir: opts.workdir,@@ -276,23 +319,24 @@ function getOrCreateStore(opts: Required<UseFileTreeOptions>): FileTreeStore {hideMetaFiles: opts.hideMetaFiles,docMode: opts.docMode,showIgnoredDirs: opts.showIgnoredDirs,- snapshot: seed- ? {- entries: seed.rootEntries ? new Map([[ROOT_KEY, seed.rootEntries]]) : new Map(),- expanded: new Set([ROOT_KEY]),- // 新 store 自己还没有 in-flight 请求,seed 的 loadingPaths 不继承。- loadingPaths: new Set(),- initialLoading: false,- // 错误态一并继承:首帧直接是错误占位,而不是先闪一帧空树再变错误。- loadError: seed.loadError,- }- : {- entries: new Map(),- expanded: new Set([ROOT_KEY]),- loadingPaths: new Set(),- initialLoading: true,- loadError: null,- },+ snapshot:+ seedDisplayable && seed+ ? {+ entries: seed.entries,+ expanded: seed.expanded,+ // 新 store 自己还没有 in-flight 请求,seed 的 loadingPaths 不继承。+ loadingPaths: new Set(),+ initialLoading: false,+ // 错误态一并继承:首帧直接是错误占位,而不是先闪一帧空树再变错误。+ loadError: seed.loadError,+ }+ : {+ entries: new Map(),+ expanded: new Set([ROOT_KEY]),+ loadingPaths: new Set(),+ initialLoading: true,+ loadError: null,+ },tokens: new Map(),inFlight: new Map(),pendingEventParents: new Set(),@@ -326,12 +370,18 @@ async function fetchDirOnce(store: FileTreeStore, relPath: string): Promise<voidstore.snapshot = { ...store.snapshot, loadError: null };}// 结构等价 → 跳过 setEntries,避免子组件无意义重渲(参见函数顶部注释)。+ // root 仍要 prune 一次:seed 的 root 可能与新 root 结构一致而展开态还是借的。const prevList = store.snapshot.entries.get(relPath);- if (prevList && entriesStructurallyEqual(prevList, list)) return;+ if (prevList && entriesStructurallyEqual(prevList, list)) {+ if (relPath === ROOT_KEY) pruneExpandedForCurrentTree(store);+ return;+ }const nextEntries = new Map(store.snapshot.entries);nextEntries.set(relPath, list);store.snapshot = { ...store.snapshot, entries: nextEntries };emit(store);+ // 过渡期(seed)借来的展开态在新树里可能已不可达,root 数据落地即剪。+ if (relPath === ROOT_KEY) pruneExpandedForCurrentTree(store);} catch (err) {log.warn(`listDir failed for ${relPath}`, err);// root 失败要可见:空树 + 无提示会被读成"项目是空的"。device-link 的@@ -449,10 +499,11 @@ function queueEventRefresh(store: FileTreeStore, eventRelPath: string): void {* 幂等:重复调用直接 no-op(refCount 已 >0)。 */async function initStore(store: FileTreeStore): Promise<void> {// 恢复 localStorage 持久化的 expanded 集合(workdir × 视图模式共享,见- // expandedStore 的 scope 说明)。分片是刻意的:两个 scope 各记自己的展开态,- // 不跨 scope 合并 —— 合并会把 reveal 态展开过的 node_modules 带进 hidden 态。+ // expandedStore 的 scope 说明)。seed 带来的展开集合是过渡态(见+ // findRevealSiblingSnapshot):先一并留着让整树平滑替换,root 数据回来时+ // pruneExpandedForCurrentTree 按新树剪掉不可达路径。const restored = loadExpandedSet(store.workdir, { showIgnoredDirs: store.showIgnoredDirs });- const nextExpanded = new Set<string>([ROOT_KEY, ...restored]);+ const nextExpanded = new Set<string>([ROOT_KEY, ...restored, ...store.snapshot.expanded]);store.snapshot = { ...store.snapshot, expanded: nextExpanded };emit(store);@@ -476,6 +527,8 @@ async function initStore(store: FileTreeStore): Promise<void> {// Initial root fetch + 已 restore expanded 目录的并行 lazy fetch。每个 listDir// <12ms,即使 50 个 restored 也能在 <1s 内 warm 完。+ // 只 warm restored:seed 带来的子树缓存是过渡显示用的旧数据,不去重拉 ——+ // 重拉等于给被忽略路径白发 listDir,而它们本来就只活到 root 数据回来。await Promise.all([fetchDir(store, ROOT_KEY),...[...restored].map((p) => fetchDir(store, p)),