Skip to content

Commit 40edc78

Browse files
committed
fix(interaction): keep a cross-surface look from certifying rest (#3471)
The re-observations that certify a resting position agreed on the rect alone, while the screening that decides whether to recheck at all already compares surfaces. The same line has to hold in both: an iOS system sheet presented during the sample gap carries the same control at the same rect, and the gate would have read that as rest and dispatched onto the sheet (#2438's transition, guarding the other half of the same decision). A measured look now carries the surface it was measured on, and two looks agree only on one surface. The unmeasured refusals also pin what they withhold — no `movedToRect` when no look measured a position — and the `@ref` branch of the published recovery is now asserted on a real refusal rather than in the contracts suite the hint left.
1 parent 9299300 commit 40edc78

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

‎src/commands/interaction/runtime/moving-target-gate.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,53 @@ test('a ref the second observation cannot re-measure is refused, never self-agre
271271
const details = (error as { details?: Record<string, unknown> }).details;
272272
assert.equal(details?.reason, INTERACTION_ERROR_REASONS.targetStillMoving);
273273
assert.equal(details?.dispatched, 'no');
274+
// Nothing was measured, so nothing is reported: a caller reading `movedToRect` would take the
275+
// stale frame position for a fresh observation of where the target went.
276+
assert.equal(details?.movedToRect, undefined);
277+
// The `@ref` branch of the recovery, asserted on a real refusal rather than on the string: the
278+
// hint's only test was the contracts unit suite, and the hint now lives beside this refusal.
279+
assert.match(String(details?.hint), /snapshot -i/);
280+
assert.match(String(details?.hint), /ref frame/);
274281
return true;
275282
},
276283
);
277284
assert.deepEqual(device.taps(), [], 'an unmeasured position licenses no tap');
278285
});
279286

287+
test('a surface that arrives between the two looks is not rest', async () => {
288+
// Rest is one position on one surface. The screening already refuses to compare positions across
289+
// surfaces for exactly the #2438 reason — a system sheet can carry the same control at the same
290+
// rect — and the two looks that certify rest are the same comparison. Without the surface check, a
291+
// sheet presented during the sample gap would let a press land on it while reporting agreement.
292+
const sheet = 'com.apple.springboard';
293+
const device = createGuardedDevice({
294+
stored: slideTree(MID_FLIGHT_X),
295+
trees: [
296+
// The resolution's own capture, then the two looks: the first on the app's content at rest,
297+
// the second on a sheet presented during the sample gap — same control, same rect, another
298+
// surface.
299+
slideTree(MID_FLIGHT_X),
300+
slideTree(RESTING_X),
301+
slideTree(RESTING_X, { surfaceBundleId: sheet }),
302+
],
303+
...DISTURBING_HISTORY,
304+
});
305+
306+
await assert.rejects(
307+
() => device.press('selector'),
308+
(error: unknown) => {
309+
const details = (error as { details?: Record<string, unknown> }).details;
310+
assert.equal(details?.reason, INTERACTION_ERROR_REASONS.targetStillMoving);
311+
assert.equal(details?.dispatched, 'no');
312+
// The sheet's rect is not somewhere the app's target moved to, so the refusal reports no
313+
// position rather than one measured on a presentation this press never asked about.
314+
assert.equal(details?.movedToRect, undefined);
315+
return true;
316+
},
317+
);
318+
assert.deepEqual(device.taps(), [], 'the sheet never received a tap');
319+
});
320+
280321
test('a target the wait loses is refused by resolution, not by a motion refusal', async () => {
281322
// The recheck asked the same question again and the answer was "no such element". That is a more
282323
// accurate account of a vanished target than "still moving" would be, and the caller's retry is a
@@ -346,6 +387,9 @@ test('a replacement that took the ref at the frame coordinates is refused, not a
346387
(error: unknown) => {
347388
const details = (error as { details?: Record<string, unknown> }).details;
348389
assert.equal(details?.reason, INTERACTION_ERROR_REASONS.targetStillMoving);
390+
// A look that measured no target reports no position: the frame rect is the value the whole
391+
// window exists to doubt, and publishing it as `movedToRect` would read as a fresh observation.
392+
assert.equal(details?.movedToRect, undefined);
349393
return true;
350394
},
351395
);

‎src/commands/interaction/runtime/moving-target-gate.ts‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ async function readMovingTargetGate(
103103
* `nodeRectByIdentity` then answers fail-closed — absent, ambiguous and unmeasurable all read as
104104
* "not shown at rest" — because a guard whose failure mode is a refused tap must never grant rest
105105
* on a guess.
106+
*
107+
* The claim is an inference from one tree, not stability evidence: an animation that has not moved
108+
* the control yet reads here exactly as a finished transition does, and no observation available at
109+
* this instant separates them. The tier that carries evidence is `resolveAtRestTarget`, which spends
110+
* two fresh observations and requires them to agree on a position and a surface. This one buys the
111+
* case it can decide for free — the previous transition already finished, which is what the large
112+
* majority of presses inside the window are — and sends everything else to be measured.
106113
*/
107114
function baselineShowsTargetAtRest(
108115
baseline: MovingTargetBaseline,
@@ -150,27 +157,53 @@ async function resolveAtRestTarget(
150157
const first = await reObserveTarget(runtime, options, params);
151158
if (!first.measured) throw motionRefusal(resolved, undefined, now(runtime) - waitedFrom);
152159
await sleep(runtime, MOVING_TARGET_GATE_SAMPLE_GAP_MS);
153-
const second = await reObserveTarget(runtime, options, params);
160+
const second = onSameSurface(first, await reObserveTarget(runtime, options, params));
154161
if (second.measured && movingTargetRectsStable(first.rect, second.rect)) return second.resolved;
155162
throw motionRefusal(resolved, second.rect, now(runtime) - waitedFrom);
156163
}
157164

165+
/**
166+
* Rest is one position on one surface, which is why the screening refuses to compare positions
167+
* across surfaces at all (#2438). A look taken on another presentation — a system sheet arriving
168+
* during the sample gap can carry the same control at the same rect — therefore measures nothing
169+
* about where this target came to rest, and is demoted to the shape that already means that: it
170+
* cannot certify agreement, and no position from it is reported as somewhere the target moved to.
171+
*/
172+
function onSameSurface(
173+
first: MovingTargetObservation & { measured: true },
174+
next: MovingTargetObservation,
175+
): MovingTargetObservation {
176+
return next.measured && next.surfaceBundleId === first.surfaceBundleId
177+
? next
178+
: { measured: false, rect: undefined, resolved: next.resolved };
179+
}
180+
158181
/** One look at the target: what it resolved to, and whether that look measured a position. */
159182
type MovingTargetObservation = MovingTargetMeasurement & {
160183
resolved: ResolvedInteractionTarget;
161184
};
162185

163186
/** Whether a look produced a position of its own, or nothing to compare against. */
164187
type MovingTargetMeasurement =
165-
/** The position this observation's own capture reported for the target. */
166-
| { measured: true; rect: Rect }
188+
/** The position this observation's own capture reported for the target, on that capture's surface. */
189+
| MovingTargetMeasured
167190
/**
168191
* No position of its own. Two of these would "agree" trivially — rest certified by a
169192
* non-measurement, the one outcome the gate must never produce — so the first of them ends the
170193
* recheck, and a refusal reports no `movedToRect` rather than a stale one.
171194
*/
172195
| { measured: false; rect: undefined };
173196

197+
/**
198+
* A look that measured the target: where it put it, and on which surface. The surface rides along
199+
* because a rect is only comparable to one measured on the same presentation (#2438).
200+
*/
201+
type MovingTargetMeasured = {
202+
measured: true;
203+
rect: Rect;
204+
surfaceBundleId: string | undefined;
205+
};
206+
174207
/**
175208
* One observation of the same target, asked as the original question rather than a narrower one.
176209
*
@@ -190,8 +223,11 @@ async function reObserveTarget(
190223
if (options.target.kind !== 'ref') {
191224
const resolved = await resolveInteractionTarget(runtime, options, params);
192225
const rect = resolvedRect(resolved);
226+
// A selector resolution measured its target on the surface it resolved against, which it
227+
// publishes as `preAction` along with the tree.
228+
const surfaceBundleId = resolved.preAction?.surfaceBundleId;
193229
return rect
194-
? { measured: true, resolved, rect }
230+
? { measured: true, resolved, rect, surfaceBundleId }
195231
: { measured: false, resolved, rect: undefined };
196232
}
197233
const { snapshot } = await captureInteractionSnapshot(runtime, options, true);
@@ -226,7 +262,9 @@ function measureRefObservation(
226262
captured !== undefined &&
227263
movingTargetRectsStable(captured.rect, rect) &&
228264
localIdentitiesEqual(readNodeLocalIdentity(captured), readNodeLocalIdentity(dispatchedNode));
229-
return measured ? { measured: true, rect } : { measured: false, rect: undefined };
265+
return measured
266+
? { measured: true, rect, surfaceBundleId: snapshot.iosSystemSurfaceBundleId }
267+
: { measured: false, rect: undefined };
230268
}
231269

232270
function resolvedRect(resolved: ResolvedInteractionTarget): Rect | undefined {

0 commit comments

Comments
 (0)