Skip to content

Commit c227e6b

Browse files
committed
fix(browser): share snapshot text budget with inline fragments
1 parent 03c971a commit c227e6b

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎apps/desktop/src/main/browser-agent/page-functions.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,23 @@ describe('collectSnapshot', () => {
759759
expect(lines[0]).not.toContain('[ref=999]')
760760
})
761761

762+
it('shares the text budget across inline fragments and leaves room for later controls', () => {
763+
document.body.innerHTML = `${Array.from(
764+
{ length: 650 },
765+
(_, index) => `<p>Before ${index} <span>inline ${index}</span> after ${index}</p>`
766+
).join(
767+
''
768+
)}${Array.from({ length: 100 }, (_, index) => `<button>Action ${index}</button>`).join('')}<input aria-label="Final field">`
769+
for (const element of document.querySelectorAll('*')) visible(element)
770+
771+
const snapshot = collectSnapshot() as { outline: string; truncated: boolean }
772+
expect(snapshot.truncated).toBe(true)
773+
expect(snapshot.outline.match(/^- text /gm)).toHaveLength(120)
774+
expect(snapshot.outline.match(/^- button /gm)).toHaveLength(100)
775+
expect(snapshot.outline).toMatch(/button "Action 99" \[ref=\d+\]/)
776+
expect(snapshot.outline).toMatch(/textbox "Final field" \[ref=\d+\]/)
777+
})
778+
762779
it('indexes only refs that were emitted before snapshot line truncation', () => {
763780
document.body.innerHTML = `${Array.from(
764781
{ length: 599 },

‎apps/desktop/src/main/browser-agent/page-functions.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
163163
const lines: string[] = []
164164
let truncated = false
165165
let refCount = 0
166-
let textRefCount = 0
167-
const textRefCap = 120
166+
let textLineCount = 0
167+
const textLineCap = 120
168168
let visitedNodes = 0
169169
const previousElementId = window.__simAgentNextElementId
170170
const safePreviousElementId =
@@ -510,7 +510,7 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
510510
}
511511

512512
const emitTextLeaf = (el: Element, indent: string, renderedLabel?: string): void => {
513-
if (refCount >= refCap || textRefCount >= textRefCap || lines.length >= lineCap) {
513+
if (refCount >= refCap || textLineCount >= textLineCap || lines.length >= lineCap) {
514514
truncated = true
515515
return
516516
}
@@ -522,7 +522,7 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
522522
)
523523
if (!text) return
524524
const id = registerElement(el, roleFor(el), text)
525-
textRefCount++
525+
textLineCount++
526526
const lineIndex = lines.length
527527
if (push(`${indent}- text ${quote(text)} [ref=${id}]`)) refLineIndexes[id] = lineIndex
528528
}
@@ -589,7 +589,12 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
589589
isVisible(parent) &&
590590
(!suppressTextCoveredBy || !suppressTextCoveredBy.includes(text))
591591
) {
592+
if (textLineCount >= textLineCap) {
593+
truncated = true
594+
continue
595+
}
592596
if (!push(`${indent}- text ${quote(text)}`)) return
597+
textLineCount++
593598
}
594599
continue
595600
}

0 commit comments

Comments
 (0)