Skip to content

Commit 4bfd23a

Browse files
committed
fix(android): bound the scroll-hint dumpsys probe and skip it in wait polling (#1270)
deriveScrollableContentHintsIfNeeded's dumpsys activity top probe ran with an 8s timeout equal to a whole wait/get budget, and measured latency ranges from 37ms to ~5.9s under contention. Cap it at 1.5s so one pathological call can't starve a caller's timeout, and skip hint derivation entirely during find ... wait polling, since a presence check never consumes scroll hints. The wait polling loop actually lives in commands/interaction/runtime/selector-read.ts's waitForFindMatch (daemon/handlers/find.ts's own handleFindWait is unreachable for wait today — dispatchFindReadOnlyViaRuntime always intercepts read-only find actions first), so the skip-hints option threads through that capture path down to snapshotAndroid via the existing flags -> contextFromFlags -> dispatchCommand -> interactor.snapshot channel.
1 parent 1a1ef7c commit 4bfd23a

12 files changed

Lines changed: 131 additions & 8 deletions

File tree

‎src/backend.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type BackendSnapshotResult = {
6363

6464
export type BackendSnapshotOptions = SnapshotOptions & {
6565
includeRects?: boolean;
66+
includeHiddenContentHints?: boolean;
6667
outPath?: string;
6768
};
6869

‎src/commands/interaction/runtime/selector-read-shared.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function captureSelectorSnapshot(
5151
scope?: string;
5252
includeRects?: boolean;
5353
interactiveOnly?: boolean;
54+
includeHiddenContentHints?: boolean;
5455
} = {
5556
updateSession: true,
5657
},
@@ -67,6 +68,9 @@ export async function captureSelectorSnapshot(
6768
scope: captureOptions.scope ?? options.scope,
6869
raw: options.raw,
6970
includeRects: captureOptions.includeRects,
71+
...(captureOptions.includeHiddenContentHints !== undefined
72+
? { includeHiddenContentHints: captureOptions.includeHiddenContentHints }
73+
: {}),
7074
});
7175
const snapshot =
7276
result.snapshot ??

‎src/commands/interaction/runtime/selector-read.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,50 @@ test('runtime find wait reports sparse snapshot verdicts on the selector-read ro
406406
assert.equal(session.snapshot, initialSnapshot);
407407
});
408408

409+
test('runtime find wait skips hidden-content hint derivation on every poll (#1270)', async () => {
410+
// A `wait`'s presence check never consumes scroll hints, so every poll must ask the capture
411+
// layer to skip deriving them — otherwise a single pathological `dumpsys activity top` call
412+
// can eat the whole wait budget from inside this loop.
413+
const snapshot = selectorReadSnapshot();
414+
const captureOptionsCalls: Array<BackendSnapshotOptions | undefined> = [];
415+
let elapsed = 0;
416+
const device = createAgentDevice({
417+
backend: {
418+
platform: 'android',
419+
captureSnapshot: async (_context, options) => {
420+
captureOptionsCalls.push(options);
421+
return { snapshot };
422+
},
423+
} satisfies AgentDeviceBackend,
424+
artifacts: createLocalArtifactAdapter(),
425+
sessions: createMemorySessionStore([{ name: 'default', snapshot }]),
426+
policy: localCommandPolicy(),
427+
clock: {
428+
now: () => elapsed,
429+
sleep: async () => {
430+
elapsed += 300;
431+
},
432+
},
433+
});
434+
435+
await assert.rejects(
436+
() =>
437+
device.selectors.find({
438+
session: 'default',
439+
locator: 'text',
440+
query: 'Never appears',
441+
action: 'wait',
442+
timeoutMs: 500,
443+
}),
444+
/find wait timed out/,
445+
);
446+
447+
assert.equal(captureOptionsCalls.length >= 2, true);
448+
for (const options of captureOptionsCalls) {
449+
assert.equal(options?.includeHiddenContentHints, false);
450+
}
451+
});
452+
409453
test('runtime wait can use backend text search', async () => {
410454
const device = createSelectorDevice(selectorReadSnapshot(), {
411455
findText: true,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ async function waitForFindMatch(
448448
const timeout = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
449449
const start = now(runtime);
450450
while (now(runtime) - start < timeout) {
451-
const { match } = await findFirstLocatorMatch(runtime, options, locator);
451+
// A presence check never consumes scroll hints, so every poll skips deriving them —
452+
// otherwise a single pathological `dumpsys activity top` call can eat the whole wait
453+
// budget from inside this loop (#1270).
454+
const { match } = await findFirstLocatorMatch(runtime, options, locator, {
455+
includeHiddenContentHints: false,
456+
});
452457
if (match) return { kind: 'found', found: true, waitedMs: now(runtime) - start };
453458
await sleep(runtime, POLL_INTERVAL_MS);
454459
}
@@ -459,11 +464,13 @@ async function findFirstLocatorMatch(
459464
runtime: AgentDeviceRuntime,
460465
options: FindReadCommandOptions,
461466
locator: FindLocator,
467+
captureOverrides?: { includeHiddenContentHints?: boolean },
462468
): Promise<{ capture: CapturedSnapshot; match: SnapshotNode | undefined }> {
463469
const selectorChain = parseFindSelectorExpression(locator, options.query);
464470
const capture = await captureSelectorSnapshot(runtime, options, {
465471
updateSession: true,
466472
scope: findSnapshotScope(runtime, locator, options.query, selectorChain),
473+
includeHiddenContentHints: captureOverrides?.includeHiddenContentHints,
467474
...deriveSelectorCapturePolicy({ selectorChain }),
468475
});
469476
if (isSparseSnapshotQualityVerdict(capture.snapshot.snapshotQuality)) {

‎src/core/dispatch-context.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type CommandFlags = Omit<CliFlags, DaemonExcludedCliFlag> & {
2626
kind?: string;
2727
maestro?: MaestroRuntimeFlags;
2828
postGestureStabilization?: boolean;
29+
snapshotIncludeHiddenContentHints?: boolean;
2930
leaseProvider?: string;
3031
provider?: string;
3132
deviceKey?: string;
@@ -61,6 +62,7 @@ export type DispatchContext = ScreenshotDispatchFlags & {
6162
snapshotScope?: string;
6263
snapshotRaw?: boolean;
6364
snapshotIncludeRects?: boolean;
65+
snapshotIncludeHiddenContentHints?: boolean;
6466
skipIosSimulatorBootCheck?: boolean;
6567
count?: number;
6668
intervalMs?: number;

‎src/core/dispatch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ async function handleSnapshotCommand(
636636
scope: context?.snapshotScope,
637637
raw: context?.snapshotRaw,
638638
includeRects: context?.snapshotIncludeRects,
639+
includeHiddenContentHints: context?.snapshotIncludeHiddenContentHints,
639640
surface: context?.surface,
640641
});
641642
}

‎src/core/interactor-types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const MAESTRO_NON_HITTABLE_FALLBACK_MESSAGE = 'tapped via non-hittable co
7272
export type SnapshotOptions = BaseSnapshotOptions & {
7373
appBundleId?: string;
7474
includeRects?: boolean;
75+
includeHiddenContentHints?: boolean;
7576
surface?: SessionSurface;
7677
};
7778

‎src/core/interactors/android.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function createAndroidInteractor(device: DeviceInfo): Interactor {
6666
depth: options?.depth,
6767
scope: options?.scope,
6868
raw: options?.raw,
69+
includeHiddenContentHints: options?.includeHiddenContentHints,
6970
// appBundleId is present for app-backed daemon sessions; keep the helper warm there,
7071
// but release it after standalone device snapshots so UiAutomation is not squatted.
7172
helperSessionScope: options?.appBundleId ? 'daemon-session' : 'command',

‎src/daemon/context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function contextFromFlags(
4141
snapshotDepth: flags?.snapshotDepth,
4242
snapshotScope: flags?.snapshotScope,
4343
snapshotRaw: flags?.snapshotRaw,
44+
snapshotIncludeHiddenContentHints: flags?.snapshotIncludeHiddenContentHints,
4445
...screenshotFlagsFromOptions(flags),
4546
count: flags?.count,
4647
intervalMs: flags?.intervalMs,

‎src/daemon/selector-runtime-backend.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ type AppleRunnerFindTextTarget = {
4242
};
4343

4444
type SnapshotFlagOverrides = Partial<
45-
Pick<CommandFlags, 'snapshotInteractiveOnly' | 'snapshotScope' | 'snapshotDepth' | 'snapshotRaw'>
45+
Pick<
46+
CommandFlags,
47+
| 'snapshotInteractiveOnly'
48+
| 'snapshotScope'
49+
| 'snapshotDepth'
50+
| 'snapshotRaw'
51+
| 'snapshotIncludeHiddenContentHints'
52+
>
4653
>;
4754

4855
export function createSelectorRuntimeForDevice(params: SelectorRuntimeDeviceParams) {
@@ -145,12 +152,15 @@ function createSelectorBackend(params: SelectorRuntimeDeviceParams): AgentDevice
145152
}
146153

147154
function snapshotFlagOverrides(options: BackendSnapshotOptions | undefined): SnapshotFlagOverrides {
155+
const { interactiveOnly, scope, depth, raw, includeHiddenContentHints } = options ?? {};
148156
const flags: SnapshotFlagOverrides = {};
149-
if (options?.interactiveOnly !== undefined)
150-
flags.snapshotInteractiveOnly = options.interactiveOnly;
151-
if (options?.scope !== undefined) flags.snapshotScope = options.scope;
152-
if (options?.depth !== undefined) flags.snapshotDepth = options.depth;
153-
if (options?.raw !== undefined) flags.snapshotRaw = options.raw;
157+
if (interactiveOnly !== undefined) flags.snapshotInteractiveOnly = interactiveOnly;
158+
if (scope !== undefined) flags.snapshotScope = scope;
159+
if (depth !== undefined) flags.snapshotDepth = depth;
160+
if (raw !== undefined) flags.snapshotRaw = raw;
161+
if (includeHiddenContentHints !== undefined) {
162+
flags.snapshotIncludeHiddenContentHints = includeHiddenContentHints;
163+
}
154164
return flags;
155165
}
156166

0 commit comments

Comments
 (0)