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): 评审处置——watcher 退避不再永久停摆 + 滚动锚点两处(PR #4398 轮四)

Zhang Yunjin259dd125e17e
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useTreeScrollRestore.test.tsx b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useTreeScrollRestore.test.tsx
index ad49332e18..891ba499a3 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useTreeScrollRestore.test.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/__tests__/useTreeScrollRestore.test.tsx
@@ -182,6 +182,56 @@ describe('useTreeScrollRestore', () => {
expect(viewportOf(container).scrollTop).toBe(8 + 3 * 29 + 1);
});
+ it('首次切到没有历史锚点的 scope 时继承切换前的顶部行(评审 P2)', () => {
+ const scopeA = makeTreeScrollScope('tab-1', '/repo');
+ const scopeB = makeTreeScrollScope('tab-1', '/repo::reveal');
+ const { container, rerender } = render(
+ <Harness scope={scopeA} rows={makeRows(['a.ts', 'b.ts', 'c.ts', 'd.ts'])} />,
+ );
+ const el = viewportOf(container);
+
+ // 用户滚动到 c.ts 在顶部:这次滚动同时被记成「最近位置」。
+ el.scrollTop = 8 + 2 * 29;
+ fireEvent.scroll(el);
+
+ // 切到还没有锚点的放行态 store,且新树在 c.ts 上方插入两行(被忽略的目录)。
+ rerender(
+ <Harness scope={scopeB} rows={makeRows(['x.ts', 'y.ts', 'a.ts', 'b.ts', 'c.ts', 'd.ts'])} />,
+ );
+
+ // 继承锚点:c.ts 仍对齐视口顶部,而不是保留旧像素落到 x.ts / y.ts 那一批。
+ expect(loadTreeScrollAnchor(scopeB)).toEqual({ rowKey: 'c.ts', offset: 0 });
+ expect(el.scrollTop).toBe(8 + 4 * 29);
+ });
+
+ it('用户接管后的普通 resize 不回拉过期锚点;重新可见时仍恢复(评审 P2)', () => {
+ const scope = makeTreeScrollScope('tab-1', '/repo');
+ saveTreeScrollAnchor(scope, { rowKey: 'c.ts', offset: 0 });
+ const { container, rerender } = render(
+ <Harness scope={scope} rows={makeRows(['a.ts', 'b.ts', 'c.ts', 'd.ts'])} />,
+ );
+ const el = viewportOf(container);
+ expect(el.scrollTop).toBe(8 + 2 * 29);
+
+ // 用户接管(点击行),随后上方行增删但滚动位置没变、锚点没更新。
+ fireEvent.pointerDown(el);
+ rerender(
+ <Harness scope={scope} rows={makeRows(['x.ts', 'y.ts', 'a.ts', 'b.ts', 'c.ts', 'd.ts'])} />,
+ );
+ expect(el.scrollTop).toBe(8 + 2 * 29);
+
+ // 普通 resize(尺寸未变,例如拖动侧栏):不能按过期锚点回拉。
+ FakeResizeObserver.instances[0].trigger();
+ expect(el.scrollTop).toBe(8 + 2 * 29);
+
+ // 真正重新可见(0 → 非 0):display:none 期间 scrollTop 可能被复位,要恢复。
+ setTestViewportSize(0);
+ FakeResizeObserver.instances[0].trigger();
+ setTestViewportSize(600);
+ FakeResizeObserver.instances[0].trigger();
+ expect(el.scrollTop).toBe(8 + 4 * 29);
+ });
+
it('锚点行因上方插入而位移时,视图继续跟随锚点行(直到用户滚动)', () => {
const scope = makeTreeScrollScope('tab-1', '/repo');
saveTreeScrollAnchor(scope, { rowKey: 'c.ts', offset: 0 });
diff --git a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useTreeScrollRestore.ts b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useTreeScrollRestore.ts
index 4a293f4773..2ed5515fd2 100644
--- a/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useTreeScrollRestore.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/workdir-browse/hooks/useTreeScrollRestore.ts
@@ -11,9 +11,10 @@
* 绘制前完成,不闪。
* - 锚点行还不在树里(新 store 数据未到 / 父目录还没展开):保持 pending,
* 每次 rows 变化再试,行一出现就恢复。用户只要一滚动就放弃(用户接管)。
- * - 容器尺寸变化(RSB 隐藏 tab 切回、doc 模式搜索态切回):每次 ResizeObserver
- * 回调(可见时)都重新对齐 —— 浏览器对 display:none 期间 scrollTop 的处理
- * 并不一致,不能赌它自己保留。用户没移动时这一步就是 no-op。
+ * - 容器尺寸变化(RSB 隐藏 tab 切回、doc 模式搜索态切回):只在容器「从隐藏变
+ * 可见」(0 → 非 0)或仍有待完成的恢复时对齐 —— 浏览器对 display:none 期间
+ * scrollTop 的处理并不一致,不能赌它自己保留;但用户接管后的普通 resize
+ * (拖动侧栏 / 缩放窗口)不能无条件回拉可能已过期的锚点(评审 P2)。
*
* 不依赖虚拟器:行高固定,目标 scrollTop 由「行索引 × pitch」直接算出,
* 设置后浏览器会派发 scroll 事件让虚拟器自己跟上。
@@ -26,6 +27,7 @@ import {
computeTreeScrollAnchor,
loadTreeScrollAnchor,
saveTreeScrollAnchor,
+ type TreeScrollAnchor,
} from '../lib/treeScrollStore';
import type { TreeRow } from '../lib/treeRows';
@@ -41,6 +43,10 @@ export function useTreeScrollRestore(
const rowsRef = useRef(rows);
rowsRef.current = rows;
const scopeRef = useRef(scope);
+ /** 最近一次观察到的视口顶部行(onScroll 更新)。换到没有历史锚点的 scope 时
+ * 继承它 —— 否则新树的 DOM 保留旧像素 scrollTop,而被忽略目录 / 分批数据在
+ * 上方插入行后,视口会落到另一批条目上(评审 P2)。 */
+ const lastAnchorRef = useRef<TreeScrollAnchor | null>(null);
/** 还有一次「待完成」的恢复:scope 变化 / 重新激活时置位。
* 注意:找到锚点行后**不**立即清除 —— 换 store 时新树是分批长出来的,锚点行
* 的 index 会随后续数据到达而位移,要跟到树稳定为止。清除只发生在用户自己
@@ -50,7 +56,14 @@ export function useTreeScrollRestore(
* 否则会把程序性滚动误判成用户滚动、提前交还控制权。 */
const programmaticScrollRef = useRef<number | null>(null);
if (scopeRef.current !== scope) {
+ const previousScope = scopeRef.current;
scopeRef.current = scope;
+ // 首次进入的 scope(例如第一次打开「显示被忽略的目录」)没有历史锚点:继承
+ // 切换前视口顶部的行,保持「看同一批条目」。只继承一次,之后由滚动 / 接管更新。
+ if (!loadTreeScrollAnchor(scope)) {
+ const inherited = lastAnchorRef.current ?? loadTreeScrollAnchor(previousScope);
+ if (inherited) saveTreeScrollAnchor(scope, inherited);
+ }
pendingRestoreRef.current = true;
}
// 宿主 tab 从非激活变激活(RSB 多标签):重新尝试恢复。宿主直接告知比赌
@@ -95,20 +108,22 @@ export function useTreeScrollRestore(
// 容器尺寸恢复(补充路径):隐藏 tab / 隐藏视图切回时恢复视口位置。
//
- // 不跟踪 0 → 非 0 的跳变:浏览器对 display:none 是否派发 RO 回调并不一致,
- // 漏一次 0 就会丢掉后续的恢复机会。每次回调(可见时)都调 tryRestore —— 用户
- // 没有移动时锚点就是当前顶部行,目标是当前 scrollTop,写入被 eps 守回;只有
- // 浏览器把位置复位(或锚点行被折叠后复活)时才会真的动。
- //
- // 这里**不受 pending 门控**:doc 模式隐藏→回显没有 active 翻转,RO 是唯一
- // 恢复路径。取舍:用户接管后若锚点行 index 在无 scroll 事件的情况下位移(上方
- // 行增删但 scrollTop 未变),随后一次无关 resize 可能把视图对齐回锚点行 ——
- // 已知代价,影响是行内/一行级,不因此把 doc 模式的恢复路径废掉。
+ // 只在两种情况下恢复:
+ // - 容器刚从隐藏变可见(0 → 非 0):display:none 期间 scrollTop 可能被浏览器
+ // 复位,这里是唯一可靠的恢复时机;
+ // - 仍有一次待完成的恢复(pending):对齐锚点行还在进行中。
+ // 其余 resize(拖动侧栏 / 缩放窗口)不动视口 —— 用户接管后锚点可能已经落后于
+ // 当前行(上方行在无 scroll 事件的情况下增删),无条件恢复会把刚展开的内容
+ // 推出视口(评审 P2:过期锚点)。
useEffect(() => {
const el = containerRef.current;
if (!el || typeof ResizeObserver === 'undefined') return;
+ let lastHeight = el.clientHeight;
const observer = new ResizeObserver(() => {
- if (el.clientHeight > 0) tryRestore();
+ const height = el.clientHeight;
+ const wasHidden = lastHeight === 0;
+ lastHeight = height;
+ if (height > 0 && (pendingRestoreRef.current || wasHidden)) tryRestore();
});
observer.observe(el);
return () => observer.disconnect();
@@ -134,6 +149,9 @@ export function useTreeScrollRestore(
const el = containerRef.current;
// 隐藏态 scrollTop 会复位成 0,那不是用户位置,不能覆盖已有锚点。
if (!el || el.clientHeight === 0) return;
+ const current = computeTreeScrollAnchor(rowsRef.current, el.scrollTop);
+ // 记录最近一次真实视口位置:换到没有历史锚点的 scope 时用它继承(评审 P2)。
+ if (current) lastAnchorRef.current = current;
if (pendingRestoreRef.current) {
const anchor = loadTreeScrollAnchor(scopeRef.current);
const expected = programmaticScrollRef.current;
@@ -142,13 +160,11 @@ export function useTreeScrollRestore(
if (expected !== null && Math.abs(el.scrollTop - expected) < RESTORE_EPSILON_PX) return;
// 浏览器 scroll anchoring:行插入/删除时 Chrome 会自行调 scrollTop,但顶部仍是
// 锚点行 —— 这不代表用户接管,保持 pending,继续跟随树的变化。
- const currentTop = computeTreeScrollAnchor(rowsRef.current, el.scrollTop);
- if (anchor && currentTop && currentTop.rowKey === anchor.rowKey) return;
+ if (anchor && current && current.rowKey === anchor.rowKey) return;
}
- const anchor = computeTreeScrollAnchor(rowsRef.current, el.scrollTop);
- if (!anchor) return;
+ if (!current) return;
pendingRestoreRef.current = false; // 用户自己滚了 = 放弃本次恢复
- saveTreeScrollAnchor(scopeRef.current, anchor);
+ saveTreeScrollAnchor(scopeRef.current, current);
}, [containerRef]);
return onScroll;
diff --git a/docs/design-evidence/2026-09-11/file-tree-header-icon-buttons.md b/docs/design-evidence/2026-09-11/file-tree-header-icon-buttons.md
index 7ae046e3ff..afe826ca28 100644
--- a/docs/design-evidence/2026-09-11/file-tree-header-icon-buttons.md
+++ b/docs/design-evidence/2026-09-11/file-tree-header-icon-buttons.md
@@ -79,12 +79,13 @@ RSB 文件浏览器(会话视图右栏)与 doc 模式侧栏(`#/cc-agent/fi
## 采集版本源码 SHA-256
哈希按文件内容的 LF 规范化形态计算(`sed 's/\r$//' <file> | sha256sum`,与 Git blob 一致)。
-下表已更新到**评审修复轮之后**的源码:默认态 / 按下态的实测值仍然适用(本轮只加 aria-label、
-unsupported 禁用态与展开态分片),unsupported 禁用态本身未实机目检(见「缺口登记」)。
+下表已更新到**评审修复轮 + 虚拟滚动轮之后**的源码:默认态 / 按下态的实测值仍然适用
+(此后只加过 aria-label、unsupported 禁用态、展开态分片与虚拟滚动接线;标题行控件本身的
+配色 / 圆角 / 尺寸未变),unsupported 禁用态本身未实机目检(见「缺口登记」)。
| 文件 | SHA-256 |
| --- | --- |
-| `features/cc-agent/workdir-browse/fileTreeHeaderButtonClass.ts` | `9fedf81c4ce1120f241934ddcc72ae8f2373032cc467938ce5196ff85b1b93b8` |
+| `features/cc-agent/workdir-browse/fileTreeHeaderButtonClass.ts` | `571c2021d9589b99141e8ca2c07331f7bab7994384f0a860d3a55534a2232840` |
| `features/cc-agent/workdir-browse/FileTreeIgnoredDirsToggle.tsx` | `de2b2070c559c3f82410761c34e35f84c6fb0cf62fcc78b2eff49b5991f69726` |
-| `features/right-sidebar/plugins/file-browser/FileBrowserBody.tsx` | `31dd114e80825405e1ef53a7af4fadb4ba716312a02d27f5af5b2ae6f8361562` |
-| `features/cc-agent/workdir-browse/WorkdirBrowseSidebar.tsx` | `096c4eeeb7bc754850b163bb03b4542b21f5bf7f32922a84335da2fffdc29b2b` |
+| `features/right-sidebar/plugins/file-browser/FileBrowserBody.tsx` | `9733c40b3d319a37c1ab79fe060c4216c835f3967a55472a6562b157a73eb1ea` |
+| `features/cc-agent/workdir-browse/WorkdirBrowseSidebar.tsx` | `67a095a2d548546bf7cc39dd1e1a9c2083d9079bbba1db7a939d0defb8cbd0e3` |
diff --git a/packages/remote-file-service/src/__tests__/watchOptions.test.ts b/packages/remote-file-service/src/__tests__/watchOptions.test.ts
index 4d0de9d678..f0de86c164 100644
--- a/packages/remote-file-service/src/__tests__/watchOptions.test.ts
+++ b/packages/remote-file-service/src/__tests__/watchOptions.test.ts
@@ -339,6 +339,38 @@ describe('WorkdirWatchManager 过滤开关', () => {
}
});
+ /**
+ * 评审 P1(PR #4398):退避到顶(约 15.5s)后不能永久放弃 —— 挂载 / 权限故障
+ * 持续超过退避窗口时旧实现删掉重试状态,故障恢复后再没有定时器收敛;SSH 连接
+ * 没断的情况下文件事件永久静默,只有重挂面板 / 改开关才能救。
+ */
+ it('持续失败超过退避窗口后仍会继续重建(不永久停摆)', async () => {
+ vi.useFakeTimers();
+ try {
+ const manager = new WorkdirWatchManager(() => {});
+ await manager.start('/repo', { showIgnoredDirs: true }, 'desktop-tree');
+ expect(h.created).toHaveLength(1);
+
+ // watcher 报错触发重建,并让之后所有 reconcile 都失败(挂载持续不可用)。
+ h.failNext = Number.MAX_SAFE_INTEGER;
+ const onSpy = h.created[0].watcher.on as unknown as ReturnType<typeof vi.fn>;
+ const handler = onSpy.mock.calls.find(([evt]) => evt === 'error')?.[1] as (err: Error) => void;
+ handler(new Error('boom'));
+
+ // 跑过退避窗口(500+1000+2000+4000+8000 ≈ 15.5s)并再等几轮。
+ await vi.advanceTimersByTimeAsync(60_000);
+ expect(h.created.at(-1)?.closed).toBe(true); // 一直在失败,尚未建回
+
+ // 故障恢复:下一次封顶退避(8s)内的重试必须把 watcher 建回来。
+ h.failNext = 0;
+ await vi.advanceTimersByTimeAsync(9_000);
+ expect(h.created.at(-1)?.closed).toBe(false);
+ manager.stopAll();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
/**
* 评审 P1:回滚后的恢复也失败时同样要退避重试 —— 只记日志的话,原有消费者会在
* 「选项变化 → 拆旧 watcher → 重建失败 → 回滚恢复意图」之后既没有 watcher、也没
diff --git a/packages/remote-file-service/src/watch.ts b/packages/remote-file-service/src/watch.ts
index 7e8418d882..6a8a9bdc6a 100644
--- a/packages/remote-file-service/src/watch.ts
+++ b/packages/remote-file-service/src/watch.ts
@@ -129,7 +129,8 @@ const COALESCE_MS = 50;
/** watcher 报错后重建的退避参数:起点 / 上限 / 次数上限。 */
const RECONCILE_RETRY_BASE_MS = 500;
const RECONCILE_RETRY_MAX_MS = 8_000;
-const RECONCILE_RETRY_LIMIT = 5;
+/** 退避指数的封顶档位:计数到这里后不再变大,转为固定 RECONCILE_RETRY_MAX_MS 重试。 */
+const RECONCILE_RETRY_BACKOFF_CAP = 5;
export class WorkdirWatchManager {
private readonly entries = new Map<string, WatchEntry>();
@@ -273,26 +274,32 @@ export class WorkdirWatchManager {
}
/**
- * watcher 报错后的收敛重试(退避 + 上限)。
+ * watcher 报错后的收敛重试(退避封顶,不设总上限)。
*
* 为什么不能只 catch 一下:错误把 entry 拆掉后,如果紧接着的 reconcile 又失败
* (远程挂载短暂不可用 / 目录正在被替换),消费者意图仍留在 desired、但已经
* 没有 watcher,也没有任何定时器会再来收敛 —— SSH 连接没断的情况下,后续文件
* 事件会永久停止,直到用户改开关或重挂载面板(评审 P2)。退避避免持续失败时
* 形成紧密重建循环。
+ *
+ * 为什么退避到顶还要继续:挂载 / 权限故障持续超过退避窗口(约 15.5s)在真实
+ * 环境里很常见(扩容挂载、目录正在被替换),此时不能永久放弃 —— 意图还在,
+ * watcher 就必须最终建回来,否则故障恢复后事件永久静默、只能靠重挂面板救
+ * (评审 P1)。封顶后按固定频率重试,单次只做一次 matcher 加载 + fs.watch,
+ * 代价可忽略;消费者撤销由 clearReconcileRetry / stopAll 结束这个循环。
*/
private scheduleReconcileRetry(workdir: string): void {
if (this.retryTimers.has(workdir)) return;
if (!this.desired.has(workdir)) return; // 期间消费者全撤了:不再重试
const attempt = (this.retryAttempts.get(workdir) ?? 0) + 1;
- if (attempt > RECONCILE_RETRY_LIMIT) {
- this.retryAttempts.delete(workdir);
- log.warn('watch reconcile retries exhausted', workdir);
- return;
+ // 计数封顶:延迟计算不会越界,超限后转为固定频率重试。
+ const capped = Math.min(attempt, RECONCILE_RETRY_BACKOFF_CAP);
+ this.retryAttempts.set(workdir, capped);
+ if (attempt === RECONCILE_RETRY_BACKOFF_CAP + 1) {
+ log.warn('watch reconcile still failing, retrying at capped backoff', workdir);
}
- this.retryAttempts.set(workdir, attempt);
const delay = Math.min(
- RECONCILE_RETRY_BASE_MS * 2 ** (attempt - 1),
+ RECONCILE_RETRY_BASE_MS * 2 ** (capped - 1),
RECONCILE_RETRY_MAX_MS,
);
const timer = setTimeout(() => {