From ce185a42f447c1cf4b494ba437cb931d1f6318a9 Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 13:11:19 +0530 Subject: [PATCH 1/2] fix(gestures): name the viewport and the point outside it when a gesture leaves the viewport --- packages/gestures/src/gesture-plan.ts | 36 ++++++++++++++++++++------- website/docs/docs/commands.md | 1 + 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/gestures/src/gesture-plan.ts b/packages/gestures/src/gesture-plan.ts index 65a15335b8..60f7d965e5 100644 --- a/packages/gestures/src/gesture-plan.ts +++ b/packages/gestures/src/gesture-plan.ts @@ -358,9 +358,8 @@ function buildTransformPlan( const initialSpan = motion.scale > 1 ? maximumSpan / motion.scale : maximumSpan; const finalSpan = initialSpan * motion.scale; if (Math.min(initialSpan, finalSpan) < GESTURE_MINIMUM_RELIABLE_SPAN_PX) { - throw trajectoryOutOfBounds({ + throw trajectoryOutOfBounds(viewport, { intent: motion.intent, - viewport, initialSpan, finalSpan, minimumReliableSpan: GESTURE_MINIMUM_RELIABLE_SPAN_PX, @@ -586,17 +585,36 @@ function assertSamplesInViewport( y < viewport.y + GESTURE_VIEWPORT_INSET_PX || y > maxY ) { - throw trajectoryOutOfBounds({ ...details, viewport, sample }); + throw trajectoryOutOfBounds(viewport, { ...details, sample }, sample.point); } } } -function trajectoryOutOfBounds(details: Record): AppError { - return new AppError('INVALID_ARGS', 'Gesture trajectory does not fit inside the viewport', { - ...details, - reason: GESTURE_PLAN_REFUSAL_REASONS.trajectoryOutOfBounds.reason, - hint: 'Move the gesture away from the edge or reduce its translation, scale, or rotation.', - }); +/** The message names the viewport and the first point outside it, so a unit mix-up shows at once. */ +function trajectoryOutOfBounds( + viewport: Rect, + details: Record, + point?: Point, +): AppError { + const origin = viewport.x === 0 && viewport.y === 0 ? '' : ` at (${viewport.x}, ${viewport.y})`; + const frame = `the ${viewport.width}x${viewport.height} viewport${origin}`; + return new AppError( + 'INVALID_ARGS', + point + ? `Gesture point (${roundForMessage(point.x)}, ${roundForMessage(point.y)}) is outside the usable area of ${frame}` + : `Gesture trajectory does not fit inside ${frame}`, + { + ...details, + viewport, + reason: GESTURE_PLAN_REFUSAL_REASONS.trajectoryOutOfBounds.reason, + hint: `${point ? 'Use the coordinate units snapshot reports: points on Apple platforms, not screenshot pixels. ' : ''}Move the gesture away from the edge or reduce its translation, scale, or rotation.`, + }, + ); +} + +/** Interpolated samples carry long fractions; `details.sample` keeps the exact point. */ +function roundForMessage(value: number): number { + return Math.round(value * 100) / 100; } function normalizeViewport(viewport: Rect): Rect { diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 18c5f1cc1c..c50d34adc6 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -1225,6 +1225,7 @@ Exact behavior: `agent-device help gesture`. - `gesture pan --hold-ms ` holds one pointer before it moves, for drag handles that expose no element and so no ref, such as reorder grips. - `gesture drag source destination` holds one pointer from a selector or ref to another. It and `--hold-ms` work on Android touch devices and iOS/iPadOS only. - `gesture pinch`, `gesture rotate`, `gesture transform`, and two-finger `gesture pan` work in Android, iOS, and iPadOS app sessions. iOS sessions through a WebDriver provider refuse them. A path that does not fit the viewport fails instead of being clamped. Use `orientation` to rotate the device. +- `swipe` and `gesture` coordinates use the units `snapshot` reports: points on Apple platforms, not screenshot pixels. A gesture that leaves the viewport fails with `GESTURE_TRAJECTORY_OUT_OF_BOUNDS`, and the message names the viewport size and the first point outside it. - `gesture tap` touches 2 to 5 points at once in Android and iOS/iPadOS app sessions. WebDriver providers refuse it unless they declare `tapGesture` support. Three or more fingers can trigger iOS system gestures such as undo, so verify app state afterwards. Use `press` for one finger. - On iOS, a long press, scroll, or swipe that fails with `synthesized_gesture_not_dispatched` sent nothing, so a retry is safe. `synthesized_gesture_outcome_unknown` means it may have run; check the screen first. - On iOS, a one-finger `gesture pan` can cross into the keyboard area. Use it for an intended drag where the app supports interactive keyboard dismissal. From c6f1ba8bc0bfddaed29b00de6c980dcdcb6a918c Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 13:12:34 +0530 Subject: [PATCH 2/2] fix(find): mark a find that matched nothing as not dispatched and say how to look an element up --- .../internal/find-match-resolution.ts | 16 +++++++++++-- .../execution/interaction/internal/find.ts | 1 + .../find-focus-dispatch.contract.test.ts | 23 +++++++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/daemon/src/execution/interaction/internal/find-match-resolution.ts b/packages/daemon/src/execution/interaction/internal/find-match-resolution.ts index e32b20559f..e6bfcc7d2b 100644 --- a/packages/daemon/src/execution/interaction/internal/find-match-resolution.ts +++ b/packages/daemon/src/execution/interaction/internal/find-match-resolution.ts @@ -1,6 +1,7 @@ import type { DaemonResponse } from '@agent-device/kernel/contracts'; import { findBestMatchesByLocator, + type FindAction, type FindLocator, type SelectorResolutionPolicy, } from '@agent-device/selectors'; @@ -14,6 +15,11 @@ import type { SessionState } from '../../../core/session-state.ts'; import { buildAmbiguousMatchError } from '../../selector-match-errors.ts'; import { errorResponse } from '@agent-device/kernel/contracts'; +const FIND_NO_MATCH_HINT = + 'Run snapshot -i to see current elements and refs, or scroll first if the element may be off-screen.'; +// A bare `find ` is this action, so an agent that meant a lookup learns the read form here. +const FIND_CLICK_NO_MATCH_HINT = `${FIND_NO_MATCH_HINT} This find clicks its match; to look an element up without acting on it, use find text "..." exists.`; + export type FindMatchResult = | { ok: true; node: SnapshotState['nodes'][number] } | { ok: false; response: DaemonResponse }; @@ -28,11 +34,12 @@ export function resolveFindMatch(params: { nodes: SnapshotState['nodes']; locator: FindLocator; query: string; + action: FindAction['kind']; selectorExpression: string | null; flags: DaemonRequest['flags']; platform: SessionState['device']['platform']; }): FindMatchResult { - const { nodes, locator, query, selectorExpression, flags, platform } = params; + const { nodes, locator, query, action, selectorExpression, flags, platform } = params; const pipeline = SELECTOR_PIPELINE_POLICIES.findAct; const rooted = nodes.filter((node) => !isRootInteractionContainer(node, nodes[0])); // #1625: selector-shaped and text-shaped queries share ONE ambiguity @@ -75,7 +82,12 @@ export function resolveFindMatch(params: { if (!node) { return { ok: false, - response: errorResponse('COMMAND_FAILED', 'find did not match any element'), + // Nothing was dispatched: a bare `find` defaults to click, which would otherwise read as a + // mutation that may have reached the device. + response: errorResponse('COMMAND_FAILED', 'find did not match any element', { + dispatched: 'no', + hint: action === 'click' ? FIND_CLICK_NO_MATCH_HINT : FIND_NO_MATCH_HINT, + }), }; } return { ok: true, node }; diff --git a/packages/daemon/src/execution/interaction/internal/find.ts b/packages/daemon/src/execution/interaction/internal/find.ts index ed3d89f7c9..a917c561ff 100644 --- a/packages/daemon/src/execution/interaction/internal/find.ts +++ b/packages/daemon/src/execution/interaction/internal/find.ts @@ -213,6 +213,7 @@ export async function handleFindCommands(params: FindRouteInput): Promise { const ambiguous = assertRpcError( await daemon.callCommand('find', ['label', 'Form', 'focus']), @@ -784,12 +788,17 @@ test( '@e3 [button] "Form"', ]); - assertRpcError( - await daemon.callCommand('find', ['label', 'Nowhere', 'focus']), - 500, - 'COMMAND_FAILED', - /find did not match any element/, - ); + // A bare find is a click. Click and focus share resolveFindMatch, so this row also pins the + // click no-match: neither sends anything. + for (const args of [['label', 'Nowhere', 'focus'], ['Nowhere']]) { + const noMatch = assertRpcError( + await daemon.callCommand('find', args), + 500, + 'COMMAND_FAILED', + /find did not match any element/, + ); + assert.equal(readErrorDetails(noMatch).dispatched, 'no'); + } }, ); },