Skip to content

Commit d6eb2a9

Browse files
committed
refactor(scroll): keep the backend's sparse reason and one scope owner
Check sparse quality before the empty-tree refusal, so a backend that ships no nodes with a sparse verdict (the Android helper's presentation failure) reports its own reason rather than a generic no-capture. Read refused observations through one `isScrollEdgeRefused` guard, and hand the edge loop's scope to `settleAfterPass` so the daemon no longer tracks a second copy.
1 parent 62eb122 commit d6eb2a9

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

‎packages/capture-kit/src/snapshot/scroll-edge-state.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export type ScrollCaptureRefusal = { reason: 'no-capture' | 'sparse-tree'; detai
4545
/** What one edge capture yields: an analyzed state, or the refusal that kept it from being read. */
4646
export type ScrollEdgeObservation = ScrollEdgeState | { refused: ScrollCaptureRefusal };
4747

48+
function isScrollEdgeRefused(
49+
observation: ScrollEdgeObservation,
50+
): observation is { refused: ScrollCaptureRefusal } {
51+
return 'refused' in observation;
52+
}
53+
4854
export type ScrollEdgeTarget = {
4955
point?: Point;
5056
nodeIndex?: number;
@@ -140,10 +146,10 @@ export function scrollSurfaceIsStuck(recentSignatures: readonly string[]): boole
140146
/**
141147
* Records one pass's surface signature, capped to the trailing window the stuck detector reads. An
142148
* empty signature is skipped so a container with no positioned descendants stays on its edge/pass
143-
* signals instead of stacking blanks. Both scroll loops push through here so "one window" has one
144-
* definition rather than a copy that can drift.
149+
* signals instead of stacking blanks. `runScrollPasses` is its one caller, so every scroll loop
150+
* reads the same window.
145151
*/
146-
export function pushScrollSurfaceSignature(
152+
function pushScrollSurfaceSignature(
147153
recentSignatures: string[],
148154
fingerprint: string | undefined,
149155
window: number,
@@ -206,7 +212,8 @@ export async function runScrollEdgePasses<TResult>(params: {
206212
edge: ScrollEdge;
207213
captureState: (scope?: string) => Promise<ScrollEdgeObservation>;
208214
scroll: () => Promise<TResult>;
209-
settleAfterPass?: () => Promise<void>;
215+
/** The rest-wait, handed the scope the loop is deciding on so it watches the same container. */
216+
settleAfterPass?: (scope: string | undefined) => Promise<void>;
210217
}): Promise<{ passes: number; result?: TResult }> {
211218
const { edge, captureState, scroll, settleAfterPass } = params;
212219
// The first capture discovers the container; every later one reads the scope the previous state
@@ -217,18 +224,20 @@ export async function runScrollEdgePasses<TResult>(params: {
217224
let observation = await captureState(scope);
218225
if (!discovered) {
219226
discovered = true;
220-
if (!('refused' in observation) && observation.scope) {
227+
if (!isScrollEdgeRefused(observation) && observation.scope) {
221228
observation = await captureState(observation.scope);
222229
}
223230
}
224-
if (!('refused' in observation)) scope = observation.scope;
231+
if (!isScrollEdgeRefused(observation)) scope = observation.scope;
225232
return observation;
226233
};
227234
return await runScrollPasses({
228235
observe,
229236
refuse: (observation) =>
230-
'refused' in observation ? buildScrollEdgeCaptureError(edge, observation.refused) : undefined,
231-
shouldStop: async (observation) => !('refused' in observation) && !observation.canScroll,
237+
isScrollEdgeRefused(observation)
238+
? buildScrollEdgeCaptureError(edge, observation.refused)
239+
: undefined,
240+
shouldStop: async (observation) => !isScrollEdgeRefused(observation) && !observation.canScroll,
232241
passLimit: SCROLL_EDGE_PASS_LIMIT,
233242
passLimitError: (passes) =>
234243
new AppError(
@@ -242,10 +251,10 @@ export async function runScrollEdgePasses<TResult>(params: {
242251
},
243252
),
244253
fingerprint: async (observation) =>
245-
'refused' in observation ? undefined : observation.fingerprint,
254+
isScrollEdgeRefused(observation) ? undefined : observation.fingerprint,
246255
noProgressError: (passes) => buildScrollEdgeNoProgressError(edge, passes),
247256
scroll,
248-
...(settleAfterPass ? { settleAfterPass } : {}),
257+
...(settleAfterPass ? { settleAfterPass: async () => await settleAfterPass(scope) } : {}),
249258
});
250259
}
251260

‎packages/daemon/src/execution/__tests__/scroll-runtime.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ test.each([
360360
},
361361
refusal: 'sparse-tree',
362362
},
363+
{
364+
// The Android helper's presentation failure: no nodes, and a sparse verdict naming why. The
365+
// backend's reason is the more useful answer, so it outranks the generic empty-tree refusal.
366+
name: 'an empty tree the backend calls sparse',
367+
frame: {
368+
backend: 'xctest',
369+
nodes: [],
370+
quality: { state: 'sparse', backend: 'tree', reason: 'AX bridge unavailable' },
371+
},
372+
refusal: 'sparse-tree',
373+
},
363374
])(
364375
'bound scroll bottom refuses $name instead of reporting the edge',
365376
async ({ frame, refusal }) => {

‎packages/daemon/src/execution/scroll-capture-refusal.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ export function scrollCaptureRefusal(result: SnapshotResult): ScrollCaptureRefus
1414
if (nodes === undefined) {
1515
return { reason: 'no-capture', detail: 'the capture returned no accessibility tree' };
1616
}
17-
if (nodes.length === 0) {
18-
return { reason: 'no-capture', detail: 'the capture returned an empty accessibility tree' };
19-
}
17+
// Sparseness first: a backend that names why its tree is unusable (the Android helper's
18+
// presentation failure ships no nodes and a sparse verdict) gives the better answer.
2019
const sparse = sparseCaptureQuality({
2120
backend: result.backend as SnapshotState['backend'],
2221
nodes: nodes as SnapshotNode[],
@@ -28,5 +27,8 @@ export function scrollCaptureRefusal(result: SnapshotResult): ScrollCaptureRefus
2827
detail: sparse.reason ?? 'the capture backend reported a sparse tree',
2928
};
3029
}
30+
if (nodes.length === 0) {
31+
return { reason: 'no-capture', detail: 'the capture returned an empty accessibility tree' };
32+
}
3133
return undefined;
3234
}

‎packages/daemon/src/execution/scroll-runtime.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,11 @@ async function executeEdgeScroll(
305305
options: ResolvedScrollExecutionOptions,
306306
context: DaemonCommandContext,
307307
): Promise<Record<string, unknown>> {
308-
// The loop discovers its scope from the first capture; the rest-wait has to watch the same
309-
// scoped container the loop decides on, so it reads the scope this closure records.
310-
let scope: string | undefined;
311308
const edgeResult = await runScrollEdgePasses({
312309
edge,
313-
captureState: async (stateScope) => {
314-
const observation = await captureEdgeState(runtime, edge, stateScope, context);
315-
if (!('refused' in observation)) scope = observation.scope ?? scope;
316-
return observation;
317-
},
310+
captureState: async (scope) => await captureEdgeState(runtime, edge, scope, context),
318311
scroll: async () => await scrollOnce(runtime, target, options, context),
319-
settleAfterPass: async () => {
312+
settleAfterPass: async (scope) => {
320313
await pollForScrollRest(
321314
async () =>
322315
(await runtime.operations.captureSnapshot(scrollCaptureInput(context, { scope })))

0 commit comments

Comments
 (0)