From aadd85c7dd52c8b405c38477de40ad0651abaece Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 18:25:33 +0530 Subject: [PATCH 1/4] test(gestures): check the viewport and point an out-of-bounds refusal names --- .../src/gesture-plan-viewport.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/gestures/src/gesture-plan-viewport.test.ts b/packages/gestures/src/gesture-plan-viewport.test.ts index 29d87f91a2..ae1ec083cf 100644 --- a/packages/gestures/src/gesture-plan-viewport.test.ts +++ b/packages/gestures/src/gesture-plan-viewport.test.ts @@ -86,6 +86,47 @@ describe('viewport-aware multi-touch geometry', () => { } }); + test('an out-of-bounds refusal names the viewport and the first point outside it', () => { + const inset = { x: 0, y: 47, width: 390, height: 750 }; + for (const [viewport, point, frame, named] of [ + [PORTRAIT, { x: 0, y: 100 }, /the 390x844 viewport$/, '(0, 100)'], + [inset, { x: 100, y: 10 }, /the 390x750 viewport at \(0, 47\)$/, '(100, 10)'], + [PORTRAIT, { x: 391.23456, y: 100 }, /the 390x844 viewport$/, '(391.23, 100)'], + ] as const) { + const error = captureError(() => + buildGesturePlan( + { + intent: 'tap', + points: [{ x: 100, y: 200 }, point], + }, + viewport, + ), + ); + assert.ok(isErrorWithReason(error, 'INVALID_ARGS', 'GESTURE_TRAJECTORY_OUT_OF_BOUNDS')); + assert.ok(error.message.includes(`point ${named} `), error.message); + assert.match(error.message, frame); + assert.deepEqual(error.details?.viewport, viewport); + // The message rounds; the details keep the exact sample. + assert.deepEqual(error.details?.sample, { offsetMs: 0, point }); + assert.match(String(error.details?.hint), /snapshot/); + } + }); + + test('a pinch too small for the viewport names the viewport but no point', () => { + const error = captureError(() => + buildGesturePlan( + { intent: 'pinch', origin: { x: 20, y: 20 }, scale: 2 }, + { x: 0, y: 0, width: 40, height: 40 }, + ), + ); + assert.ok(isErrorWithReason(error, 'INVALID_ARGS', 'GESTURE_TRAJECTORY_OUT_OF_BOUNDS')); + assert.match(error.message, /the 40x40 viewport$/); + assert.doesNotMatch(error.message, /point/); + assert.equal(error.details?.minimumReliableSpan, 48); + assert.equal(error.details?.sample, undefined); + assert.doesNotMatch(String(error.details?.hint), /snapshot/); + }); + test('positive and negative translation choose geometry that keeps the full path in bounds', () => { for (const delta of [ { x: 80, y: 40 }, @@ -186,6 +227,16 @@ describe('gesture planning never synthesizes an out-of-bounds point', () => { }); }); +function captureError(build: () => unknown): AppError { + try { + build(); + } catch (error) { + assert.ok(error instanceof AppError, `unexpected error type: ${String(error)}`); + return error; + } + assert.fail('expected the gesture plan to be refused'); +} + function buildTransformPlan(durationMs: number) { return requireTwoPointerPlan( buildGesturePlan( From 8d4e7a41944d79961f7c82c3658d93ba4c82dece Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 18:25:40 +0530 Subject: [PATCH 2/4] docs(gestures): say a two-finger gesture too cramped for the viewport names only the viewport --- packages/gestures/src/gesture-plan.ts | 3 ++- website/docs/docs/commands.md | 2 +- website/docs/docs/error-reasons.md | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/gestures/src/gesture-plan.ts b/packages/gestures/src/gesture-plan.ts index 60f7d965e5..772e64c34e 100644 --- a/packages/gestures/src/gesture-plan.ts +++ b/packages/gestures/src/gesture-plan.ts @@ -43,7 +43,8 @@ const GESTURE_PLAN_REFUSAL_REASONS = { pathInvalidTimeline: { reason: 'GESTURE_PATH_INVALID_TIMELINE' }, // A trajectory sample carries a non-finite coordinate. trajectoryNonFinite: { reason: 'GESTURE_TRAJECTORY_NON_FINITE' }, - // A trajectory sample leaves the viewport, so the contact would press outside the screen. + // A trajectory sample leaves the viewport, so the contact would press outside the screen, or a + // two-finger path cannot keep its contacts the minimum span apart inside it. trajectoryOutOfBounds: { reason: 'GESTURE_TRAJECTORY_OUT_OF_BOUNDS' }, } as const satisfies ReasonTable<'gesture'>; const GESTURE_FLING_DEFAULT_DISTANCE = 180; diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 9629884733..7469255c0e 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -1260,7 +1260,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 refuses coordinates; for a coordinate drag, use `gesture pan [ms] --hold-ms `. 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. +- `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. A `gesture pinch`, `rotate`, `transform`, or two-finger `pan` that cannot keep its fingers far enough apart in the viewport fails the same way, and its message names only the viewport. - `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. diff --git a/website/docs/docs/error-reasons.md b/website/docs/docs/error-reasons.md index ff7e8f53ec..bad3856fb1 100644 --- a/website/docs/docs/error-reasons.md +++ b/website/docs/docs/error-reasons.md @@ -145,7 +145,7 @@ Treat a reason that this page does not list as unknown, not as an error in the r | `GESTURE_PATH_TOO_SHORT` | The gesture path has fewer usable samples than any recognizable motion needs. | | `GESTURE_PATH_INVALID_TIMELINE` | The path's sample timeline is not ordered and finite, so no clock can play it back. | | `GESTURE_TRAJECTORY_NON_FINITE` | A trajectory sample carries a non-finite coordinate. | -| `GESTURE_TRAJECTORY_OUT_OF_BOUNDS` | A trajectory sample leaves the viewport, so the contact would press outside the screen. | +| `GESTURE_TRAJECTORY_OUT_OF_BOUNDS` | A trajectory sample leaves the viewport, so the contact would press outside the screen, or a two-finger path cannot keep its contacts the minimum span apart inside it. | ### `install-from-source` From b09017eaaa6fa105e5923b85c1a8dd92bf275ae7 Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 18:25:41 +0530 Subject: [PATCH 3/4] fix(find): give a read-only find that matched nothing the same recovery hint --- .../commands/interaction/runtime/selector-read.ts | 5 ++++- .../interaction/internal/find-match-resolution.ts | 3 +-- packages/selectors/src/index.ts | 2 ++ packages/selectors/src/internal/find.ts | 4 ++++ .../find-focus-dispatch.contract.test.ts | 9 ++++++++- .../provider-scenarios/android-find.test.ts | 14 ++++++++++++++ website/docs/docs/commands.md | 1 + 7 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts b/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts index 052b8e80fd..1af589989f 100644 --- a/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts +++ b/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts @@ -1,4 +1,5 @@ import { + FIND_NO_MATCH_HINT, FIND_VALUE_REQUIRED_MESSAGE, findBestMatchesByLocator, buildSelectorChainForNode, @@ -119,7 +120,9 @@ export const findCommand: RuntimeCommand` 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.`; diff --git a/packages/selectors/src/index.ts b/packages/selectors/src/index.ts index 157c4e3d79..04e40da178 100644 --- a/packages/selectors/src/index.ts +++ b/packages/selectors/src/index.ts @@ -37,6 +37,7 @@ import { parseFindArgs, parseFindSelectorExpression, FIND_LOCATORS, + FIND_NO_MATCH_HINT, FIND_VALUE_REQUIRED_MESSAGE, UNSUPPORTED_FIND_ACTION_HINT, } from './internal/find.ts'; @@ -96,6 +97,7 @@ export { export { FIND_LOCATORS, + FIND_NO_MATCH_HINT, FIND_VALUE_REQUIRED_MESSAGE, IS_PREDICATE_USAGE_HINT, IS_TEXT_VALUE_REQUIRED_MESSAGE, diff --git a/packages/selectors/src/internal/find.ts b/packages/selectors/src/internal/find.ts index c731345e9a..5ff98ed3a6 100644 --- a/packages/selectors/src/internal/find.ts +++ b/packages/selectors/src/internal/find.ts @@ -173,6 +173,10 @@ export type ParsedFindArgs = { /** Shared by `checkFindArgs` and `findCommand`, which validates already-parsed options. */ export const FIND_VALUE_REQUIRED_MESSAGE = 'find requires a value'; +/** Shared by the acting and read-only `find` no-match refusals, so their recovery cannot drift. */ +export const FIND_NO_MATCH_HINT = + 'Run snapshot -i to see current elements and refs, or scroll first if the element may be off-screen.'; + /** * Shared by both `Unsupported find action` throw sites (this file's raw-token * parser and `packages/command-surface/src/interaction/selectors.ts`'s typed CLI reader) so diff --git a/test/integration/interaction-contract/find-focus-dispatch.contract.test.ts b/test/integration/interaction-contract/find-focus-dispatch.contract.test.ts index 13b25c160a..f46862e63a 100644 --- a/test/integration/interaction-contract/find-focus-dispatch.contract.test.ts +++ b/test/integration/interaction-contract/find-focus-dispatch.contract.test.ts @@ -1,6 +1,7 @@ import assert from 'node:assert/strict'; import { test } from 'vitest'; import type { InteractionGuarantee } from '@agent-device/contracts/interaction-guarantees'; +import { FIND_NO_MATCH_HINT } from '@agent-device/selectors'; import { KEYBOARD_COVERED_BAND_RECT, keyboardCoveredTabBarSnapshot } from '@agent-device/test-kit'; import { assertRpcError, assertRpcOk } from '../provider-scenarios/assertions.ts'; import { PARALLEL_PROVIDER_SCENARIO_TIMEOUT_MS } from '../provider-scenarios/test-timeouts.ts'; @@ -789,7 +790,8 @@ test( ]); // A bare find is a click. Click and focus share resolveFindMatch, so this row also pins the - // click no-match: neither sends anything. + // click no-match: neither sends anything. Only the click hint points at the read-only form. + const noMatchHints: unknown[] = []; for (const args of [['label', 'Nowhere', 'focus'], ['Nowhere']]) { const noMatch = assertRpcError( await daemon.callCommand('find', args), @@ -798,7 +800,12 @@ test( /find did not match any element/, ); assert.equal(readErrorDetails(noMatch).dispatched, 'no'); + noMatchHints.push(noMatch.hint); } + const [focusHint, clickHint] = noMatchHints; + assert.equal(focusHint, FIND_NO_MATCH_HINT); + assert.ok(typeof clickHint === 'string' && clickHint.startsWith(FIND_NO_MATCH_HINT)); + assert.match(clickHint, /find text "\.\.\." exists/); }, ); }, diff --git a/test/integration/provider-scenarios/android-find.test.ts b/test/integration/provider-scenarios/android-find.test.ts index d74890588d..47509ec9fb 100644 --- a/test/integration/provider-scenarios/android-find.test.ts +++ b/test/integration/provider-scenarios/android-find.test.ts @@ -4,6 +4,7 @@ import { ANDROID_IME_HELPER_FIXTURE_ARTIFACT } from '../../../src/__tests__/test import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from '../../../src/__tests__/test-utils/android-snapshot-helper.ts'; import type { AndroidAdbProvider } from '@agent-device/platform-android/mechanics'; import { WAIT_REASONS } from '@agent-device/contracts/wait'; +import { FIND_NO_MATCH_HINT } from '@agent-device/selectors'; import { arrayEqual, assertCommandCall, assertRpcOk } from './assertions.ts'; import { androidSettingsXml, androidSnapshotHelperOutput } from './android-world.ts'; import { @@ -74,6 +75,19 @@ test('Provider-backed integration Android find flow covers refs, wait, ambiguity }); assert.equal(exists.found, true); + // The read-only no-match gets the acting route's recovery hint, without the click-only advice + // to switch to a lookup. + for (const args of [ + ['label', 'Nowhere', 'exists'], + ['label', 'Nowhere', 'get', 'text'], + ['label', 'Nowhere', 'get', 'attrs'], + ]) { + const noMatch = (await daemon.callCommand('find', args, selection)).json?.error?.data; + assert.equal(noMatch?.code, 'COMMAND_FAILED', args.join(' ')); + assert.equal(noMatch?.details?.dispatched, 'no', args.join(' ')); + assert.equal(noMatch?.hint, FIND_NO_MATCH_HINT, args.join(' ')); + } + const pressFoundRef = await client.interactions.press({ ref: attrsPinnedRef, ...selection }); assert.equal(pressFoundRef.x, 88); assert.equal(pressFoundRef.y, 151); diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 7469255c0e..fde144bbf5 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -1326,6 +1326,7 @@ An Apple runner that returns `COMMAND_ALREADY_FINISHED` cannot replay that comma - `malformed_request`: fix the client that built the request; resending it unchanged fails the same way. - `remote_daemon_unavailable`: the remote daemon did not answer its health check. Make it reachable, or clear `--daemon-base-url` or `AGENT_DEVICE_DAEMON_BASE_URL`. For a saved `connect` connection, run `disconnect` to return to the local daemon. - `webdriver_route_unsupported`: the device cloud's driver does not implement the route. The limit is the driver, not the device. +- `find` with no match (`COMMAND_FAILED`, no reason): no `find` action ran. Run `snapshot -i` to see the current elements, or scroll first if the element may be off-screen. iOS runner refusals carry a typed `error.details.reason` too: From b6a0e7eb412307ff8db8a0fc51ec5ad06985f8e1 Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 21:42:07 +0530 Subject: [PATCH 4/4] refactor(find): share the no-match message between the acting and read-only routes, and check the read-only hint on the daemon route --- .../commands/interaction/runtime/selector-read.ts | 3 ++- .../interaction/internal/find-match-resolution.ts | 3 ++- packages/selectors/src/index.ts | 2 ++ packages/selectors/src/internal/find.ts | 3 ++- .../find-focus-dispatch.contract.test.ts | 13 ++++++++++--- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts b/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts index 1af589989f..ec5f751ac9 100644 --- a/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts +++ b/packages/command-runtime/src/commands/interaction/runtime/selector-read.ts @@ -1,5 +1,6 @@ import { FIND_NO_MATCH_HINT, + FIND_NO_MATCH_MESSAGE, FIND_VALUE_REQUIRED_MESSAGE, findBestMatchesByLocator, buildSelectorChainForNode, @@ -120,7 +121,7 @@ export const findCommand: RuntimeCommand { const ambiguous = assertRpcError( @@ -790,9 +791,14 @@ test( ]); // A bare find is a click. Click and focus share resolveFindMatch, so this row also pins the - // click no-match: neither sends anything. Only the click hint points at the read-only form. + // click no-match: neither sends anything. The read-only `exists` row takes the selector + // runtime instead and must refuse the same way. Only the click hint points at the read form. const noMatchHints: unknown[] = []; - for (const args of [['label', 'Nowhere', 'focus'], ['Nowhere']]) { + for (const args of [ + ['label', 'Nowhere', 'focus'], + ['Nowhere'], + ['label', 'Nowhere', 'exists'], + ]) { const noMatch = assertRpcError( await daemon.callCommand('find', args), 500, @@ -802,8 +808,9 @@ test( assert.equal(readErrorDetails(noMatch).dispatched, 'no'); noMatchHints.push(noMatch.hint); } - const [focusHint, clickHint] = noMatchHints; + const [focusHint, clickHint, existsHint] = noMatchHints; assert.equal(focusHint, FIND_NO_MATCH_HINT); + assert.equal(existsHint, FIND_NO_MATCH_HINT); assert.ok(typeof clickHint === 'string' && clickHint.startsWith(FIND_NO_MATCH_HINT)); assert.match(clickHint, /find text "\.\.\." exists/); },