The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): re-target find scope on a keep-alive surface flip (#81726)
Chen Jin0d1828294ad8
diff --git a/apps/desktop/src/components/find-bar.test.tsx b/apps/desktop/src/components/find-bar.test.tsxindex 1325d51a3e5..e8c06bd56b6 100644--- a/apps/desktop/src/components/find-bar.test.tsx+++ b/apps/desktop/src/components/find-bar.test.tsx@@ -440,6 +440,47 @@ describe('find-in-page store', () => {expect($findInPage.get().matchCount).toBe(1)})+ it('re-targets the scope after a keep-alive tab flip while the bar stays open', () => {+ // Regression (#81726): the bar only closes on a ROUTE change; a keep-+ // alive tab flip hides the captured surface without changing the+ // pathname, so a typed query used to resolve no scope and report 0/0+ // while the viewed surface contains the text. The scope must re-resolve+ // to the now-foreground surface.+ const surfaceA = plantSurface('a')+ const surfaceB = document.createElement('div')++ surfaceB.setAttribute('data-chat-surface', '')+ surfaceB.id = 'b'+ document.body.appendChild(surfaceB)++ surfaceA.textContent = 'needle in A'+ surfaceB.textContent = 'needle in B needle'++ openFindBar()+ setFindQuery('needle')++ // Surface A is foreground; only its match is wrapped.+ expect(surfaceA.querySelectorAll('mark.find-hit').length).toBe(1)+ expect(surfaceB.querySelectorAll('mark.find-hit').length).toBe(0)+ expect($findInPage.get().matchCount).toBe(1)++ // Tab flip while the bar stays open: A's pane layer hides, B is now the+ // foreground surface the user is reading.+ surfaceA.setAttribute('data-pane-hidden', '')++ setFindQuery('needle')++ // The scope re-targeted to B — its matches are wrapped, and A's stale+ // highlights are cleared rather than left to resurface on its next visit.+ expect(surfaceB.querySelectorAll('mark.find-hit').length).toBe(2)+ expect(surfaceA.querySelectorAll('mark.find-hit').length).toBe(0)+ expect($findInPage.get().matchCount).toBe(2)++ // Stepping still works on the re-targeted surface.+ findNext()+ expect($findInPage.get().matchOrdinal).toBe(2)+ })+it('a no-match query leaves the count at zero and the DOM untouched', () => {const surface = plantSurface()surface.textContent = 'hello world'diff --git a/apps/desktop/src/lib/find-in-page-scope.test.ts b/apps/desktop/src/lib/find-in-page-scope.test.tsindex e52788466e9..6254991502e 100644--- a/apps/desktop/src/lib/find-in-page-scope.test.ts+++ b/apps/desktop/src/lib/find-in-page-scope.test.ts@@ -319,6 +319,48 @@ describe('scope lifecycle', () => {const result = performScopedFind(foreground, 'needle', { forward: true, findNext: false })expect(result.count).toBe(1)})++ it('re-targets the scope to the foreground surface after a keep-alive flip hides the captured one', () => {+ // Regression (#81726): the bar stays open across a tab flip (the route+ // doesn't change, so the FindBar's pathname cleanup never runs). The+ // captured surface's pane layer goes `data-pane-hidden` and the flipped-+ // to surface is left unmarked — currentFindScope must re-resolve to the+ // new foreground surface instead of going dead (0/0 forever).+ const a = plantSurface('a', '<p>needle in A</p>')+ const b = plantSurface('b', '<p>needle in B</p>')++ captureFindScope()+ expect(currentFindScope()).toBe(a)+ expect(a.hasAttribute('data-find-root')).toBe(true)++ // Keep-alive tab flip: A's pane layer hides, B becomes the foreground.+ a.setAttribute('data-pane-hidden', '')++ const scope = currentFindScope()++ // The scope re-targeted to B and the marker moved with it.+ expect(scope).toBe(b)+ expect(b.hasAttribute('data-find-root')).toBe(true)+ expect(a.hasAttribute('data-find-root')).toBe(false)++ // The walker searches the new surface — and the old one is left clean.+ const result = performScopedFind(scope!, 'needle', { forward: true, findNext: false })+ expect(result.count).toBe(1)+ expect(b.querySelectorAll('mark.find-hit').length).toBe(1)+ expect(a.querySelectorAll('mark.find-hit').length).toBe(0)+ })++ it('re-targeting to a non-chat pane resolves to no scope (nothing on screen is a view)', () => {+ // The user flipped to a pane that isn't a chat surface — nothing to+ // search, matching the existing "no chat surface" contract. A later flip+ // back to a visible surface re-targets again.+ const a = plantSurface('a', 'needle')+ captureFindScope()++ a.setAttribute('data-pane-hidden', '')++ expect(currentFindScope()).toBeNull()+ })})describe('scoped find survives React re-render', () => {diff --git a/apps/desktop/src/lib/find-in-page-scope.ts b/apps/desktop/src/lib/find-in-page-scope.tsindex 940dcaa06ce..4dff9b53218 100644--- a/apps/desktop/src/lib/find-in-page-scope.ts+++ b/apps/desktop/src/lib/find-in-page-scope.ts@@ -14,7 +14,11 @@* Strategy. At bar-open time, capture the active chat surface element and* remember it for the lifetime of the find bar. Subsequent queries highlight* only text nodes inside that subtree. ⌘G / ⌘⇧G step between highlights- * inside the same subtree. Closing the bar unwraps the highlights.+ * inside the same subtree. Closing the bar unwraps the highlights. If a+ * keep-alive tab flip hides the captured surface while the bar stays open+ * (the route didn't change, so the FindBar's pathname cleanup never ran),+ * the scope re-resolves to the new foreground surface — "find wherever the+ * user is reading" holds across a flip, not just at open time (#81726).** "Current view" is the foreground `[data-chat-surface]` element after* filtering out inactive keep-alive tabs. That is the same policy every@@ -114,7 +118,15 @@ function resetScopeState(): void {scheduled = false}-/** The scope captured by the open bar, or null if none is active. */+/**+ * The scope captured by the open bar, or null if none is active.+ *+ * When the captured surface was hidden by a keep-alive tab flip (the bar only+ * closes on a ROUTE change, so a flip keeps it open over a now-inactive pane),+ * re-resolves to the foreground surface instead of going dead — otherwise+ * every query reports 0/0 and ⌘G becomes a no-op on the surface the user is+ * actually reading (#81726).+ */export function currentFindScope(): HTMLElement | null {const roots = document.querySelectorAll<HTMLElement>(`[${ROOT_ATTR}]`)@@ -124,7 +136,50 @@ export function currentFindScope(): HTMLElement | null {}}- return null+ if (roots.length === 0) {+ // No captured scope — the bar was never opened, or releaseFindScope+ // already tore it down. Nothing to re-target to.+ return null+ }++ // Every marked root is hidden — the captured surface lost a tab flip while+ // the bar stayed open. Move the scope to the now-foreground surface.+ return retargetFindScope(roots)+}++/**+ * Move the find scope to the foreground chat surface after a keep-alive tab+ * flip hid the captured one. Clears the old surface's highlights and marker+ * (they'd otherwise resurface as stray marks when its tab is revisited), then+ * stamps `data-find-root` on the new surface so the walker keeps searching+ * where the user is now reading. Returns the new scope, or null when no chat+ * surface is visible (the user flipped to a non-chat pane — nothing on screen+ * qualifies as a view).+ */+function retargetFindScope(roots: NodeListOf<HTMLElement>): HTMLElement | null {+ const foreground = resolveCurrentFindScope()++ if (!foreground) {+ return null+ }++ for (const root of roots) {+ if (root === foreground) {+ continue+ }++ clearHighlights(root)+ root.removeAttribute(ROOT_ATTR)+ }++ // The re-render watcher is still observing the OLD root; detach it so the+ // next performScopedFind re-attaches to the new scope (ensureObserver only+ // trusts an observer whose root matches scopeRoot).+ stopObserver()+ scopeRoot = foreground+ foreground.setAttribute(ROOT_ATTR, '')++ return foreground}/** Same predicate pane-visibility exposes, kept local so this module is