Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): focusTraversal 隐藏判定缓存过期值回写(Greptile P1)
Zhang Yunjin2e050e65d1a5
diff --git a/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx b/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsxindex e0c144b10c..ca54f72812 100644--- a/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx+++ b/apps/desktop/src/renderer/lib/__tests__/focusTraversal.test.tsx@@ -66,6 +66,33 @@ describe('handOffTabFromCard', () => {expect(document.activeElement).toBe(document.getElementById('card-region'));});+ it('display:none 容器里的按钮不会被当成焦点目标(缓存不吞 Tab)', () => {+ // 回归(Greptile P1):隐藏判定记忆化曾把「尚未发现外层隐藏」的中间包装层缓存为+ // 可见;同一容器里的第二个按钮命中过期缓存后,隐藏按钮被当成出卡目标 → Tab 被吞。+ render(+ <>+ <button type="button" id="usage-chip">用量明细</button>+ <div data-testid="card-host">+ <div id="card-root">+ <button type="button" id="card-last">卡内最后一档</button>+ </div>+ </div>+ <div style={{ display: 'none' }}>+ <div id="hidden-wrap">+ <button type="button" id="hidden-a">隐藏A</button>+ <button type="button" id="hidden-b">隐藏B</button>+ </div>+ </div>+ <button type="button" id="ctx-chip">任务上下文窗口</button>+ </>,+ );+ const cardRoot = document.getElementById('card-root')!;+ document.getElementById('card-last')!.focus();+ expect(handOffTabFromCard(cardRoot, { shiftKey: false })).toBe(true);+ // 焦点必须越过 display:none 的两个按钮,落到可见的下一枚 chip。+ expect(document.activeElement).toBe(document.getElementById('ctx-chip'));+ });+it('卡内中间的 Tab 只扫卡片子树,不扫全文档(长聊天记录下的键盘性能)', () => {render(<CardHarness extraInsideMiddle />);const cardRoot = document.getElementById('card-root')!;diff --git a/apps/desktop/src/renderer/lib/focusTraversal.ts b/apps/desktop/src/renderer/lib/focusTraversal.tsindex d574ab3f5a..ac059ce566 100644--- a/apps/desktop/src/renderer/lib/focusTraversal.ts+++ b/apps/desktop/src/renderer/lib/focusTraversal.ts@@ -35,22 +35,32 @@ function collectTabbables(scope: Document | HTMLElement): HTMLElement[] {return nodes.filter((element) => !isHiddenForFocus(element, hiddenCache));}-/** 隐藏判定带记忆化:一次扫描里共享父节点(卡片根、容器等)只查一次 getComputedStyle。 */+/**+ * 隐藏判定带记忆化:一次扫描里共享父节点(卡片根、容器等)只查一次 getComputedStyle。+ * 缓存语义:`cache.get(el) === true` = el 因自身或祖先隐藏而不可聚焦(其子树同判);+ * `false` = el 到根全可见。路径节点的缓存值在**定论后统一回写**——中途发现外层+ * 隐藏时,先前按可见缓存的中间层必须一起改判,否则同容器的兄弟元素会命中过期的+ * false,把 display:none 里的按钮当成可聚焦目标(Greptile P1)。+ */function isHiddenForFocus(node: HTMLElement, hiddenCache: Map<Element, boolean>): boolean {const cached = hiddenCache.get(node);if (cached !== undefined) return cached;const view = node.ownerDocument.defaultView;- let result = view?.getComputedStyle(node).visibility === 'hidden';+ const path: HTMLElement[] = [];+ let hidden = view?.getComputedStyle(node).visibility === 'hidden';let cursor: HTMLElement | null = node;- while (!result && cursor) {- const cursorCached = hiddenCache.get(cursor);- if (cursorCached !== undefined) { result = cursorCached; break; }- result = view?.getComputedStyle(cursor).display === 'none';- hiddenCache.set(cursor, result);- cursor = cursor.parentElement;+ while (!hidden && cursor) {+ path.push(cursor);+ hidden = view?.getComputedStyle(cursor).display === 'none';+ const parent: HTMLElement | null = cursor.parentElement;+ if (!hidden && parent) {+ const parentCached = hiddenCache.get(parent);+ if (parentCached !== undefined) { hidden = parentCached; break; }+ }+ cursor = parent;}- hiddenCache.set(node, result);- return result;+ for (const element of path) hiddenCache.set(element, hidden);+ return hidden;}/**