Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DaemonResponse } from '@agent-device/kernel/contracts';
import {
findBestMatchesByLocator,
type FindAction,
type FindLocator,
type SelectorResolutionPolicy,
} from '@agent-device/selectors';
Expand All @@ -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 <text>` 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 };
Expand All @@ -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
Expand Down Expand Up @@ -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 };
Expand Down
1 change: 1 addition & 0 deletions packages/daemon/src/execution/interaction/internal/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export async function handleFindCommands(params: FindRouteInput): Promise<Daemon
nodes,
locator,
query,
action,
selectorExpression,
flags: req.flags,
platform: device.platform,
Expand Down
36 changes: 27 additions & 9 deletions packages/gestures/src/gesture-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, unknown>): 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<string, unknown>,
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,11 @@ test(
// no-match refusal. A candidate-disclosure or message change here fails this scenario, which
// is the cell's own evidence rather than the `disambiguation` scenario's.
await withIosContractDaemon(
[runnerSnapshotEntry(twinFormTree()), runnerSnapshotEntry(KEYBOARD_TREE)],
[
runnerSnapshotEntry(twinFormTree()),
runnerSnapshotEntry(KEYBOARD_TREE),
runnerSnapshotEntry(KEYBOARD_TREE),
],
async (daemon) => {
const ambiguous = assertRpcError(
await daemon.callCommand('find', ['label', 'Form', 'focus']),
Expand All @@ -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');
}
},
);
},
Expand Down
1 change: 1 addition & 0 deletions website/docs/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ Exact behavior: `agent-device help gesture`.
- `gesture pan --hold-ms <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.
Expand Down
Loading