@@ -131,35 +131,46 @@ function findCoveringNode(
131131) : RawSnapshotNode | null {
132132 const cached = scan . coverCache . get ( targetPosition ) ;
133133 if ( cached !== undefined ) return cached ;
134- // Reentrancy guard: `visibleCoverRect` recurses into `findCoveringNode` for
135- // the SAME targetPosition only through a cycle in `overlayPositions`
136- // ordering, which cannot happen (positions strictly increase along any
137- // recursive path — see the `position <= targetPosition` filter below) —
138- // but seed `null` before recursing regardless, so a future edit that
139- // breaks that invariant fails closed (no cover) instead of re-entering.
134+ // Reentrancy guard: an additional Android overlay may precede an app target
135+ // in the cross-window traversal, while that target may itself be overlay-like.
136+ // Seed `null` before following either direction so such a cycle fails closed
137+ // instead of re-entering.
140138 scan . coverCache . set ( targetPosition , null ) ;
141139
142140 const targetRect = positiveRect ( target . rect ) ;
143141 if ( ! targetRect ) return finishFindCoveringNode ( scan , targetPosition , null ) ;
144142 const center = centerOfRect ( targetRect ) ;
143+ const targetIsAdditionalOverlay = options . isAdditionalOverlayNode ?.( target ) === true ;
145144
146- // Mutation-lane note: relaxing `<=` to `<` here would only change behavior
147- // if `position === targetPosition` were reachable — a node covering
148- // itself. That case is already excluded one line below regardless: a
149- // self-candidate has `candidateRect === targetRect` (same node, same
150- // object), so `areRectsApproximatelyEqual` in `visibleCoverRect` always
151- // excludes it.
152145 for ( const position of scan . overlayPositions ) {
153- if ( position <= targetPosition ) continue ;
154- const candidate = scan . nodes [ position ] ;
155- if ( candidate && canCoverPoint ( scan , position , target , targetRect , center , options ) ) {
146+ if (
147+ ! canPositionCoverTarget ( scan , position , targetPosition , targetIsAdditionalOverlay , options )
148+ ) {
149+ continue ;
150+ }
151+ const candidate = scan . nodes [ position ] ! ;
152+ if ( canCoverPoint ( scan , position , target , targetRect , center , options ) ) {
156153 return finishFindCoveringNode ( scan , targetPosition , candidate ) ;
157154 }
158155 }
159156
160157 return finishFindCoveringNode ( scan , targetPosition , null ) ;
161158}
162159
160+ function canPositionCoverTarget (
161+ scan : OcclusionScan ,
162+ candidatePosition : number ,
163+ targetPosition : number ,
164+ targetIsAdditionalOverlay : boolean ,
165+ options : SnapshotOcclusionOptions ,
166+ ) : boolean {
167+ const candidate = scan . nodes [ candidatePosition ] ;
168+ if ( ! candidate ) return false ;
169+ if ( candidatePosition > targetPosition ) return true ;
170+ if ( candidatePosition === targetPosition || targetIsAdditionalOverlay ) return false ;
171+ return options . isAdditionalOverlayNode ?.( candidate ) === true ;
172+ }
173+
163174function finishFindCoveringNode (
164175 scan : OcclusionScan ,
165176 targetPosition : number ,
0 commit comments