Skip to content

Commit ad9b906

Browse files
authored
perf(scroll-until): answer every candidate from one visibility index (#2596)
* perf(scroll-until): answer every candidate from one visibility index `evaluateIsPredicate` derived its visibility index from the `nodes` it was given, so `scroll --until` rebuilt the node map and viewport-rect scan once per candidate a selector matched. It now takes the capture's `SnapshotVisibility` and has no tree of its own, which is the #1970 rule that N candidates share one whole-tree pass: the caller that owns the capture owns the index. The `--until` pass builds that index only once a candidate exists, so a pass whose selector matches nothing indexes nothing. The pipeline still holds its own index while ranking ambiguous matches; sharing that one means widening `SelectorPipelineOutcome`, which is the capture-kit move and not this. * perf(scroll-until): name the index rule the predicate actually depends on `visibility` is not "the array `node` came from": `findSnapshotAncestor` resolves `parentIndex` positionally, so what the predicate needs is the index of the tree `node` belongs to, in the same `index`/`parentIndex` space. Its doc says that now. `createSnapshotVisibility` materializes nothing until consulted, so the no-match early return guarded a cost that does not exist; it is gone. The probe test claims only what a probe can see — the caller's index is the one consulted — keeps `snapshot-visibility.test.ts`'s counter names instead of a parallel vocabulary, and drops the fallback counter this fixture cannot move. The twin test's docstring says what its counts prove. * test(scroll-until): count the visibility indexes one pass materializes Verdicts cannot tell one index from one per candidate, so the shape #2528 changed was unproven: moving `createSnapshotVisibility` back inside the candidate loop leaves every earlier test green. A pass now counts the indexes built through the contracts facade and expects one for a selector that matched two rows — two when the build moves back inside the loop.
1 parent 0326ad6 commit ad9b906

6 files changed

Lines changed: 195 additions & 36 deletions

File tree

‎packages/selectors/src/internal/predicates.test.ts‎

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'node:assert/strict';
22
import { test } from 'vitest';
3+
import { createSnapshotVisibility } from '@agent-device/contracts/snapshot';
34
import type { SnapshotNode } from '@agent-device/kernel/snapshot';
45
import { evaluateIsPredicate, normalizeIsPositionals } from './predicates.ts';
56

@@ -57,7 +58,7 @@ test('focused predicate reads snapshot focus state', () => {
5758
const result = evaluateIsPredicate({
5859
predicate: 'focused',
5960
node,
60-
nodes: [node],
61+
visibility: createSnapshotVisibility([node]),
6162
platform: 'android',
6263
});
6364

@@ -88,7 +89,7 @@ test('visible predicate treats zero-height hittable Android nodes as hidden', ()
8889
const result = evaluateIsPredicate({
8990
predicate: 'visible',
9091
node: nodes[1]!,
91-
nodes,
92+
visibility: createSnapshotVisibility(nodes),
9293
platform: 'android',
9394
});
9495

@@ -115,7 +116,7 @@ test('visible predicate treats rectless hittable Android nodes as hidden', () =>
115116
const result = evaluateIsPredicate({
116117
predicate: 'visible',
117118
node: nodes[1]!,
118-
nodes,
119+
visibility: createSnapshotVisibility(nodes),
119120
platform: 'android',
120121
});
121122

@@ -152,7 +153,7 @@ test('visible predicate uses visible Android ancestor geometry for rectless text
152153
const result = evaluateIsPredicate({
153154
predicate: 'visible',
154155
node: nodes[2]!,
155-
nodes,
156+
visibility: createSnapshotVisibility(nodes),
156157
platform: 'android',
157158
});
158159

@@ -175,7 +176,7 @@ test('visible predicate treats Android nodes hidden from users as hidden', () =>
175176
const result = evaluateIsPredicate({
176177
predicate: 'visible',
177178
node: nodes[0]!,
178-
nodes,
179+
visibility: createSnapshotVisibility(nodes),
179180
platform: 'android',
180181
});
181182

@@ -219,9 +220,80 @@ test('visible predicate does not use non-hittable Android layout ancestors for r
219220
const result = evaluateIsPredicate({
220221
predicate: 'visible',
221222
node: nodes[3]!,
222-
nodes,
223+
visibility: createSnapshotVisibility(nodes),
223224
platform: 'android',
224225
});
225226

226227
assert.equal(result.pass, false);
227228
});
229+
230+
/** One capture whose two rows share a label: one on screen, one below the fold. */
231+
const SHARED_CAPTURE: SnapshotNode[] = [
232+
{ index: 0, ref: 'e0', type: 'Application', rect: { x: 0, y: 0, width: 400, height: 800 } },
233+
{
234+
index: 1,
235+
parentIndex: 0,
236+
ref: 'e1',
237+
type: 'TextField',
238+
label: 'Email',
239+
rect: { x: 0, y: 200, width: 400, height: 40 },
240+
},
241+
{
242+
index: 2,
243+
parentIndex: 0,
244+
ref: 'e2',
245+
type: 'TextField',
246+
label: 'Email',
247+
rect: { x: 0, y: 2400, width: 400, height: 40 },
248+
},
249+
];
250+
251+
/**
252+
* `visible` answers from the index it is handed: two candidates of one capture read the one index the
253+
* caller built, and a predicate that built its own would leave these counters at zero. That an index
254+
* serves many nodes is `snapshot-visibility.test.ts`'s claim; this is the predicate's half of #1970.
255+
*/
256+
test('the visible predicate answers from the visibility index its caller built', () => {
257+
const materialized = { nodeMap: 0, viewportRects: 0 };
258+
const visibility = createSnapshotVisibility(SHARED_CAPTURE, {
259+
onNodeMapBuilt: () => (materialized.nodeMap += 1),
260+
onViewportRectsCollected: () => (materialized.viewportRects += 1),
261+
});
262+
263+
const onScreen = evaluateIsPredicate({
264+
predicate: 'visible',
265+
node: SHARED_CAPTURE[1]!,
266+
visibility,
267+
platform: 'ios',
268+
});
269+
const scrolledOut = evaluateIsPredicate({
270+
predicate: 'visible',
271+
node: SHARED_CAPTURE[2]!,
272+
visibility,
273+
platform: 'ios',
274+
});
275+
276+
assert.equal(onScreen.pass, true);
277+
assert.equal(scrolledOut.pass, false);
278+
assert.deepEqual(materialized, { nodeMap: 1, viewportRects: 1 });
279+
});
280+
281+
/** The closest negative: `text` answers from the node alone and never consults the index. */
282+
test('the text predicate never consults the visibility index', () => {
283+
const materialized = { nodeMap: 0, viewportRects: 0 };
284+
const visibility = createSnapshotVisibility(SHARED_CAPTURE, {
285+
onNodeMapBuilt: () => (materialized.nodeMap += 1),
286+
onViewportRectsCollected: () => (materialized.viewportRects += 1),
287+
});
288+
289+
const match = evaluateIsPredicate({
290+
predicate: 'text',
291+
node: SHARED_CAPTURE[1]!,
292+
visibility,
293+
expectedText: 'Email',
294+
platform: 'ios',
295+
});
296+
297+
assert.equal(match.pass, true);
298+
assert.deepEqual(materialized, { nodeMap: 0, viewportRects: 0 });
299+
});

‎packages/selectors/src/internal/predicates.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { Platform, PublicPlatform } from '@agent-device/kernel/device';
55
import type { SnapshotState } from '@agent-device/kernel/snapshot';
66
import { isPositiveFiniteRect } from '@agent-device/kernel/rect';
77
import {
8-
createSnapshotVisibility,
98
extractNodeText,
109
isUsefulVisibilityAnchor,
1110
type SnapshotVisibility,
@@ -62,19 +61,23 @@ export function normalizeIsPositionals(positionals: string[]): string[] {
6261
export function evaluateIsPredicate(params: {
6362
predicate: Exclude<IsPredicate, 'exists' | 'absent'>;
6463
node: SnapshotState['nodes'][number];
65-
nodes: SnapshotState['nodes'];
64+
/**
65+
* The visibility index of the tree `node` belongs to — same `index`/`parentIndex` space, so the
66+
* ancestor walk and viewport roots it resolves against are that tree's. A caller that asks about
67+
* several nodes of one capture passes the SAME index to every call, so the tree is indexed once
68+
* instead of once per candidate (#1970).
69+
*/
70+
visibility: SnapshotVisibility;
6671
expectedText?: string;
6772
platform: Platform | PublicPlatform;
6873
}): { pass: boolean; actualText: string; details: string } {
69-
const { predicate, node, nodes, expectedText, platform } = params;
74+
const { predicate, node, visibility, expectedText, platform } = params;
7075
const actualText = extractNodeText(node);
7176
const editable = isNodeEditable(node, platform);
7277
const selected = node.selected === true;
7378
const focused = node.focused === true;
7479
const visible =
75-
predicate === 'text'
76-
? isNodeVisible(node)
77-
: isAssertionVisible(node, createSnapshotVisibility(nodes), platform);
80+
predicate === 'text' ? isNodeVisible(node) : isAssertionVisible(node, visibility, platform);
7881
let pass = false;
7982
switch (predicate) {
8083
case 'visible':

‎src/commands/interaction/runtime/selector-is.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { resolveSelectorPipeline } from '@agent-device/selectors/selector-pipeline';
1111
import { SELECTOR_PIPELINE_POLICIES } from '@agent-device/selectors/selector-pipeline-policy';
1212
import type { SnapshotNode } from '@agent-device/kernel/snapshot';
13+
import { createSnapshotVisibility } from '@agent-device/contracts/snapshot';
1314
import type { AgentDeviceRuntime, CommandContext } from '../../../runtime-contract.ts';
1415
import { AppError, isRequestCanceledError } from '@agent-device/kernel/errors';
1516
import type { SelectorTarget } from '@agent-device/contracts/interaction';
@@ -164,7 +165,7 @@ async function resolveAssertedPredicate(
164165
const result = evaluateIsPredicate({
165166
predicate,
166167
node: outcome.node,
167-
nodes: capture.snapshot.nodes,
168+
visibility: createSnapshotVisibility(capture.snapshot.nodes),
168169
expectedText: options.expectedText,
169170
platform: runtime.backend.platform,
170171
});

‎src/daemon/__tests__/is-predicates.test.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
33
import { evaluateIsPredicate } from '@agent-device/selectors';
4+
import { createSnapshotVisibility } from '@agent-device/contracts/snapshot';
45

56
const viewportNode = {
67
ref: 'e1',
@@ -28,13 +29,16 @@ test('evaluateIsPredicate visible and hidden', () => {
2829
const visible = evaluateIsPredicate({
2930
predicate: 'visible',
3031
node: baseNode,
31-
nodes,
32+
visibility: createSnapshotVisibility(nodes),
3233
platform: 'ios',
3334
});
3435
const hidden = evaluateIsPredicate({
3536
predicate: 'hidden',
3637
node: { ...baseNode, rect: { ...baseNode.rect, width: 0 }, hittable: false },
37-
nodes: [viewportNode, { ...baseNode, rect: { ...baseNode.rect, width: 0 }, hittable: false }],
38+
visibility: createSnapshotVisibility([
39+
viewportNode,
40+
{ ...baseNode, rect: { ...baseNode.rect, width: 0 }, hittable: false },
41+
]),
3842
platform: 'ios',
3943
});
4044
assert.equal(visible.pass, true);
@@ -61,7 +65,7 @@ test('evaluateIsPredicate visible uses ancestor rect for visible list text', ()
6165
const visible = evaluateIsPredicate({
6266
predicate: 'visible',
6367
node: listText,
64-
nodes: [viewportNode, listItem, listText],
68+
visibility: createSnapshotVisibility([viewportNode, listItem, listText]),
6569
platform: 'ios',
6670
});
6771
assert.equal(visible.pass, true);
@@ -76,7 +80,7 @@ test('evaluateIsPredicate visible fails for off-screen nodes', () => {
7680
const visible = evaluateIsPredicate({
7781
predicate: 'visible',
7882
node: offscreenNode,
79-
nodes: [viewportNode, offscreenNode],
83+
visibility: createSnapshotVisibility([viewportNode, offscreenNode]),
8084
platform: 'ios',
8185
});
8286
assert.equal(visible.pass, false);
@@ -91,7 +95,7 @@ test('evaluateIsPredicate visible fails for zero-size nodes', () => {
9195
const visible = evaluateIsPredicate({
9296
predicate: 'visible',
9397
node: zeroRectNode,
94-
nodes: [viewportNode, zeroRectNode],
98+
visibility: createSnapshotVisibility([viewportNode, zeroRectNode]),
9599
platform: 'ios',
96100
});
97101
assert.equal(visible.pass, false);
@@ -124,7 +128,7 @@ test('evaluateIsPredicate visible does not inherit viewport visibility from gene
124128
const visible = evaluateIsPredicate({
125129
predicate: 'visible',
126130
node: listText,
127-
nodes: [viewportNode, scrollView, wrapper, listText],
131+
visibility: createSnapshotVisibility([viewportNode, scrollView, wrapper, listText]),
128132
platform: 'ios',
129133
});
130134
assert.equal(visible.pass, false);
@@ -150,7 +154,7 @@ test('evaluateIsPredicate visible resolves parent links by node index instead of
150154
const visible = evaluateIsPredicate({
151155
predicate: 'visible',
152156
node: listText,
153-
nodes: [viewportNode, listText, listItem],
157+
visibility: createSnapshotVisibility([viewportNode, listText, listItem]),
154158
platform: 'ios',
155159
});
156160
assert.equal(visible.pass, true);
@@ -160,13 +164,13 @@ test('evaluateIsPredicate editable and selected', () => {
160164
const editable = evaluateIsPredicate({
161165
predicate: 'editable',
162166
node: baseNode,
163-
nodes: [viewportNode, baseNode],
167+
visibility: createSnapshotVisibility([viewportNode, baseNode]),
164168
platform: 'ios',
165169
});
166170
const selected = evaluateIsPredicate({
167171
predicate: 'selected',
168172
node: { ...baseNode, selected: true },
169-
nodes: [viewportNode, { ...baseNode, selected: true }],
173+
visibility: createSnapshotVisibility([viewportNode, { ...baseNode, selected: true }]),
170174
platform: 'ios',
171175
});
172176
assert.equal(editable.pass, true);
@@ -177,14 +181,14 @@ test('evaluateIsPredicate text uses equality', () => {
177181
const match = evaluateIsPredicate({
178182
predicate: 'text',
179183
node: baseNode,
180-
nodes: [viewportNode, baseNode],
184+
visibility: createSnapshotVisibility([viewportNode, baseNode]),
181185
expectedText: 'Email',
182186
platform: 'ios',
183187
});
184188
const mismatch = evaluateIsPredicate({
185189
predicate: 'text',
186190
node: baseNode,
187-
nodes: [viewportNode, baseNode],
191+
visibility: createSnapshotVisibility([viewportNode, baseNode]),
188192
expectedText: 'email',
189193
platform: 'ios',
190194
});

0 commit comments

Comments
 (0)