Skip to content

Commit adca744

Browse files
committed
chore(desktop-browser): compare issued-ref frames by identity instead of a string key
1 parent 91aa3d5 commit adca744

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

‎apps/desktop/src/main/browser-agent/driver.ts‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ function isBrowserWaitElementState(value: string): value is BrowserWaitElementSt
145145

146146
type PageExecutionTarget = WebContents | WebFrameMain
147147

148-
/** Identifies a frame across the separate WebFrameMain wrappers Electron may hand out for it. */
149-
function pageTargetKey(target: PageExecutionTarget): string {
150-
return 'routingId' in target
151-
? `frame:${target.processId}:${target.routingId}`
152-
: `contents:${target.id}`
148+
/** Whether two targets are the same page or frame; Electron may hand out separate wrappers for one frame. */
149+
function samePageTarget(left: PageExecutionTarget | undefined, right: PageExecutionTarget): boolean {
150+
if (left === right) return true
151+
return left !== undefined && 'routingId' in left && 'routingId' in right && sameWebFrame(left, right)
153152
}
154153

155154
type BrowserActionOutcome = { status: 'pending' } | { status: 'acknowledged'; result: unknown }
@@ -335,7 +334,7 @@ interface DriverScopeState {
335334
/** Monotonic ref floor shared by every frame in this browser scope. */
336335
nextElementRefId: number
337336
/** Every ref this scope issued and its frame: a later snapshot may reuse it there, and an action may still name it. */
338-
issuedRefTargets: Map<number, { key: string; target: PageExecutionTarget }>
337+
issuedRefTargets: Map<number, PageExecutionTarget>
339338
/** Invalidates captures that finish after a tab/navigation/timeout race. */
340339
snapshotCaptureEpoch: number
341340
/** Cancels a timed-out multi-step action before any later native dispatch. */
@@ -1789,10 +1788,9 @@ function elementTarget(elementId: number): PageExecutionTarget | undefined {
17891788
const issued = state.issuedRefTargets.get(elementId)
17901789
const contents = session.automationTab()?.view.webContents
17911790
if (!issued || !contents || contents.isDestroyed()) return undefined
1792-
if (!('routingId' in issued.target)) return issued.target === contents ? contents : undefined
1793-
const frame = issued.target
1791+
if (!('routingId' in issued)) return issued === contents ? contents : undefined
17941792
return contents.mainFrame.framesInSubtree.find(
1795-
(candidate) => !candidate.isDestroyed() && sameWebFrame(candidate, frame)
1793+
(candidate) => !candidate.isDestroyed() && sameWebFrame(candidate, issued)
17961794
)
17971795
}
17981796

@@ -1895,13 +1893,7 @@ function assertElementActionCurrent(
18951893
assertActiveContents(contents)
18961894
const state = driverScopeState()
18971895
const active = session.automationTab()
1898-
const owner = elementTarget(elementId)
1899-
if (
1900-
state.snapshotTabId !== active?.id ||
1901-
!owner ||
1902-
(owner !== target &&
1903-
!('routingId' in owner && 'routingId' in target && sameWebFrame(owner, target)))
1904-
) {
1896+
if (state.snapshotTabId !== active?.id || !samePageTarget(elementTarget(elementId), target)) {
19051897
throw new ToolError(
19061898
'The page changed before input could be dispatched. Take a fresh browser_snapshot and try again.'
19071899
)
@@ -2491,7 +2483,7 @@ async function captureSnapshot(
24912483
const mainRefs = validateSnapshotRefs(
24922484
mainSnapshot,
24932485
mainStartingElementId,
2494-
(ref) => state.issuedRefTargets.get(ref)?.key === pageTargetKey(contents)
2486+
(ref) => samePageTarget(state.issuedRefTargets.get(ref), contents)
24952487
)
24962488
if (!mainRefs) {
24972489
throw new ToolError('The page returned invalid element ids. Try browser_snapshot again.')
@@ -2556,7 +2548,7 @@ async function captureSnapshot(
25562548
const frameRefs = validateSnapshotRefs(
25572549
frameSnapshot,
25582550
frameStartingElementId,
2559-
(ref) => state.issuedRefTargets.get(ref)?.key === pageTargetKey(frame)
2551+
(ref) => samePageTarget(state.issuedRefTargets.get(ref), frame)
25602552
)
25612553
if (!frameRefs) {
25622554
unreadableCrossOriginFrames++
@@ -2610,9 +2602,7 @@ async function captureSnapshot(
26102602
(targetLineIndexes.get(elementId) ?? Number.POSITIVE_INFINITY) < visibleOutlineLines.length
26112603
)
26122604
)
2613-
for (const [ref, target] of targets) {
2614-
state.issuedRefTargets.set(ref, { key: pageTargetKey(target), target })
2615-
}
2605+
for (const [ref, target] of targets) state.issuedRefTargets.set(ref, target)
26162606
state.snapshotTabId = capturedTabId
26172607
return {
26182608
...omit(mainSnapshot, ['refIds', 'refLineIndexes', 'nextElementId']),

0 commit comments

Comments
 (0)