Skip to content

The agent that grows with you

fix(desktop): re-wrap on findNext when stale marks no longer cover all matches (#81778)

Chen Jin4e86caa1e2a0
diff --git a/apps/desktop/src/lib/find-in-page-scope.test.ts b/apps/desktop/src/lib/find-in-page-scope.test.ts
index 2f99c66bb14..334d510bc2f 100644
--- a/apps/desktop/src/lib/find-in-page-scope.test.ts
+++ b/apps/desktop/src/lib/find-in-page-scope.test.ts
@@ -167,6 +167,48 @@ describe('performScopedFind', () => {
expect(result.activeOrdinal).toBe(2)
})
+ it('re-wraps when a stale mark from an earlier query survives (fast path, #81778 review)', () => {
+ // Regression: a mark left over from an earlier query can match the
+ // current query case-insensitively even though the mark set no longer
+ // covers every occurrence — the genuine mark was destroyed by a
+ // re-render, the stale one survived, and the current query's live
+ // matches arrived unwrapped. The fast path must not step on the stale
+ // set — it must drop it and re-wrap, or the live match stays dark.
+ const surface = plantSurface('surface', '<p>hermes</p>')
+ performScopedFind(surface, 'hermes', { forward: true, findNext: false })
+ // Re-render replaces the paragraph (destroying the genuine mark)…
+ surface.querySelector('p')!.innerHTML = 'beta'
+ // …but a stale mark survives the edit (external write / raced cleanup),
+ // and the live content that matches the current query arrives unwrapped.
+ surface.insertAdjacentHTML('beforeend', '<mark class="find-hit">Hermes</mark>')
+ surface.insertAdjacentHTML('beforeend', '<p>hermes</p>')
+
+ const result = performScopedFind(surface, 'hermes', { forward: true, findNext: true })
+
+ // Re-wrapped: the stale mark's text is re-wrapped as a live occurrence
+ // and the previously unwrapped match is wrapped too.
+ expect(result.count).toBe(2)
+ const marks = [...surface.querySelectorAll('mark.find-hit')]
+ expect(marks.length).toBe(2)
+ expect(marks.every(mark => mark.textContent.toLowerCase() === 'hermes')).toBe(true)
+ })
+
+ it('re-wraps when new unmarked content contains the query (fast path, #81778 review)', () => {
+ // Regression: content appended after the last wrap (streamed tokens,
+ // external writes) carries no mark, so the existing marks no longer
+ // cover every occurrence. Stepping must re-wrap instead of walking the
+ // stale set, or the new match would never be highlighted.
+ const surface = plantSurface('surface', '<p>needle</p>')
+ performScopedFind(surface, 'needle', { forward: true, findNext: false })
+
+ surface.insertAdjacentHTML('beforeend', '<p>needle</p>')
+
+ const result = performScopedFind(surface, 'needle', { forward: true, findNext: true })
+
+ expect(result.count).toBe(2)
+ expect(surface.querySelectorAll('mark.find-hit').length).toBe(2)
+ })
+
it('re-wraps when the query changes (marks are not reused across queries)', () => {
const surface = plantSurface('surface', '<p>alpha beta alpha</p>')
performScopedFind(surface, 'alpha', { forward: true, findNext: false })
diff --git a/apps/desktop/src/lib/find-in-page-scope.ts b/apps/desktop/src/lib/find-in-page-scope.ts
index 1b364eefad2..8b2578cde21 100644
--- a/apps/desktop/src/lib/find-in-page-scope.ts
+++ b/apps/desktop/src/lib/find-in-page-scope.ts
@@ -246,6 +246,39 @@ function shouldSkipElement(el: Element): boolean {
return false
}
+/**
+ * True when `root` contains an occurrence of `query` that is NOT inside one
+ * of our find-hit marks. Read-only counterpart of `highlightMatches` with
+ * the identical skip rules, used by the findNext fast path to verify that
+ * the existing marks still cover every match — a stale mark from an earlier
+ * query can satisfy the per-mark text comparison yet leave live occurrences
+ * unwrapped, and stepping on that set would never highlight them.
+ */
+function hasUnmarkedMatch(root: Element, query: string): boolean {
+ const lowerQuery = query.toLowerCase()
+
+ if (!lowerQuery) {
+ return false
+ }
+
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
+
+ while (walker.nextNode()) {
+ const node = walker.currentNode as Text
+ const parent = node.parentElement
+
+ if (parent && shouldSkipElement(parent)) {
+ continue
+ }
+
+ if (node.nodeValue?.toLowerCase().includes(lowerQuery)) {
+ return true
+ }
+ }
+
+ return false
+}
+
/** Remove every find-hit mark we previously added, restoring original text. */
function clearHighlights(root: Element): void {
const marks = root.querySelectorAll<HTMLElement>(`mark.${HIGHLIGHT_CLASS}`)
@@ -327,10 +360,17 @@ export function performScopedFind(
// case-insensitive comparison, every Enter/⌘G step re-wraps all
// highlights, `data-find-active` is lost on the fresh DOM, and the
// active ordinal resets to 1 forever (triage finding on #81778).
+ // The per-mark comparison alone is not enough: a stale mark from an
+ // earlier query that survived a raced re-wrap (or external DOM writes)
+ // can match the current query case-insensitively while the mark set no
+ // longer covers every match — stepping would walk old highlights and
+ // never wrap the live ones. Only step when the marks match AND cover
+ // every occurrence (review finding on #81778).
const sameQuery =
options.findNext &&
existingMarks.length > 0 &&
- existingMarks.every(mark => mark.textContent.toLowerCase() === query.toLowerCase())
+ existingMarks.every(mark => mark.textContent.toLowerCase() === query.toLowerCase()) &&
+ !hasUnmarkedMatch(root, query)
let marks = existingMarks