Skip to content

Commit 2200150

Browse files
committed
fix(ios): keep surface identity through comparison, find, and probe scope
A ps read that carries no SIMULATOR_UDID at all was reported as absence, so an unreadable or truncated environment could route a live sheet to the occluded app tree. Only a scope naming a different device is a real negative now; a missing one stays unknown. The shared post-gesture comparison token used comparisonKey or the backend alone, so an app capture and a sheet capture — both XCTest — compared equal and a sheet appearing or dismissing read as a stable surface. The token now carries the surface, which covers stabilization, verify and settle through the one path they share. Mutating find rebuilt its capture without iosSystemSurfaceBundleId, so the shared disclosure helper could not report the sheet on either outcome. It is preserved now. Each fix has a regression that fails without it.
1 parent 00e4185 commit 2200150

6 files changed

Lines changed: 218 additions & 3 deletions

File tree

‎packages/platform-apple/src/system-surface-presence.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ test('an unreadable process environment is unknown, never absent', async () => {
7878
await expect(createSystemSurfacePresenceProbe()(sim)).resolves.toBe('unknown');
7979
});
8080

81+
// A successful read that carries no device scope at all proves nothing: reporting it as absence
82+
// would route a live sheet to the occluded app tree.
83+
test('a process environment with no device scope at all is unknown, never absent', async () => {
84+
stubProbes({
85+
pgrep: RUNNING,
86+
ps: { exitCode: 0, stdout: '/…/SafariViewService.app/SafariViewService' },
87+
});
88+
await expect(createSystemSurfacePresenceProbe()(sim)).resolves.toBe('unknown');
89+
});
90+
91+
test('an empty process environment read is unknown, never absent', async () => {
92+
stubProbes({ pgrep: RUNNING, ps: { exitCode: 0, stdout: '' } });
93+
await expect(createSystemSurfacePresenceProbe()(sim)).resolves.toBe('unknown');
94+
});
95+
8196
test('a non-zero process scan exit that is not "no match" is unknown', async () => {
8297
stubProbes({ pgrep: { exitCode: 2, stdout: '' } });
8398
await expect(createSystemSurfacePresenceProbe()(sim)).resolves.toBe('unknown');

‎packages/platform-apple/src/system-surface-presence.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ async function isProcessScopedToDevice(
9595
): Promise<boolean | 'unknown'> {
9696
const result = await runProbe('ps', ['eww', '-p', String(pid), '-o', 'command='], signal);
9797
if (result === 'unknown' || result.exitCode !== 0) return 'unknown';
98-
return result.stdout.includes(`SIMULATOR_UDID=${deviceId}`);
98+
if (result.stdout.includes(`SIMULATOR_UDID=${deviceId}`)) return true;
99+
// Only a scope naming a DIFFERENT device is a real negative. A read that carries no device scope
100+
// at all proves nothing — the environment may have been truncated or withheld — and reporting it
101+
// as absence would route a live sheet to the occluded app tree.
102+
return result.stdout.includes('SIMULATOR_UDID=') ? false : 'unknown';
99103
}
100104

101105
async function runProbe(

‎src/daemon/__tests__/post-gesture-stabilization.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,83 @@ test('capturePostGestureStabilizedResult re-baselines instead of concluding when
492492
assert.equal(staleAccepts + settled, 1);
493493
});
494494

495+
// --- #2438: a system surface appearing or dismissing mid-poll is not comparable evidence ---
496+
497+
test('capturePostGestureStabilizedResult re-baselines when an in-place system surface appears (iOS)', async () => {
498+
// A web sign-in sheet is hosted out of the app's process. Its tree and the app's tree describe
499+
// different surfaces, so a quiet capture of the sheet says nothing about a gesture taken against
500+
// the app. Both captures are XCTest here: without the surface in the comparison token they would
501+
// agree on backend alone and produce a verdict from incomparable node sets.
502+
vi.useFakeTimers();
503+
const session = makeSession('ios');
504+
session.snapshot = makeSnapshotState(pickupSnapshot(500).nodes, {
505+
snapshotQuality: { state: 'healthy', backend: 'tree' },
506+
});
507+
markPostGestureStabilization(session, 'scroll');
508+
509+
const capture = vi.fn(async () =>
510+
makeSnapshotState(pickupSnapshot(500).nodes, {
511+
snapshotQuality: { state: 'healthy', backend: 'tree' },
512+
iosSystemSurfaceBundleId: 'com.apple.SafariViewService',
513+
}),
514+
);
515+
516+
const resultPromise = withDiagnosticsScope({}, async () => {
517+
await capturePostGestureStabilizedResult({
518+
session,
519+
capture,
520+
readSnapshot: (snapshot) => snapshot,
521+
});
522+
return {
523+
rebased: countDiagnosticEventsByPhase(['post_gesture_snapshot_baseline_rebased']),
524+
staleAccepts: countDiagnosticEventsByPhase(['post_gesture_snapshot_stale_accept']),
525+
settled: countDiagnosticEventsByPhase(['post_gesture_snapshot_stabilized']),
526+
};
527+
});
528+
529+
await vi.advanceTimersByTimeAsync(6_000);
530+
const { rebased, staleAccepts, settled } = await resultPromise;
531+
532+
assert.equal(rebased, 1);
533+
assert.equal(staleAccepts + settled, 1);
534+
});
535+
536+
test('capturePostGestureStabilizedResult re-baselines when an in-place system surface dismisses (iOS)', async () => {
537+
// The mirror case: the baseline was the sheet and the app returns underneath it.
538+
vi.useFakeTimers();
539+
const session = makeSession('ios');
540+
session.snapshot = makeSnapshotState(pickupSnapshot(500).nodes, {
541+
snapshotQuality: { state: 'healthy', backend: 'tree' },
542+
iosSystemSurfaceBundleId: 'com.apple.SafariViewService',
543+
});
544+
markPostGestureStabilization(session, 'scroll');
545+
546+
const capture = vi.fn(async () =>
547+
makeSnapshotState(pickupSnapshot(500).nodes, {
548+
snapshotQuality: { state: 'healthy', backend: 'tree' },
549+
}),
550+
);
551+
552+
const resultPromise = withDiagnosticsScope({}, async () => {
553+
await capturePostGestureStabilizedResult({
554+
session,
555+
capture,
556+
readSnapshot: (snapshot) => snapshot,
557+
});
558+
return {
559+
rebased: countDiagnosticEventsByPhase(['post_gesture_snapshot_baseline_rebased']),
560+
staleAccepts: countDiagnosticEventsByPhase(['post_gesture_snapshot_stale_accept']),
561+
settled: countDiagnosticEventsByPhase(['post_gesture_snapshot_stabilized']),
562+
};
563+
});
564+
565+
await vi.advanceTimersByTimeAsync(6_000);
566+
const { rebased, staleAccepts, settled } = await resultPromise;
567+
568+
assert.equal(rebased, 1);
569+
assert.equal(staleAccepts + settled, 1);
570+
});
571+
495572
test('capturePostGestureStabilizedResult still distrusts a same-backend baseline match (iOS)', async () => {
496573
// The guard above must not become a blanket escape hatch: when the backend is
497574
// stable, an unchanged surface is still the stale-read signal #1542 added.

‎src/daemon/__tests__/system-surface-disclosure.test.ts‎

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import { dispatchFindReadOnlyViaRuntime } from '../selector-runtime.ts';
66
import { dispatchWaitViaRuntime } from '../wait-runtime.ts';
77
import type { DaemonRequest, DaemonResponse } from '../daemon-request.ts';
88
import { ANDROID_SYSTEM_SURFACE_DISCLOSURE } from '@agent-device/contracts/android-system-surface-disclosure';
9+
import { IOS_SYSTEM_SURFACE_DISCLOSURE } from '@agent-device/contracts/ios-system-surface';
910
import { snapshotRuntimeFixture } from './snapshot-runtime-fixture.ts';
1011
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
11-
import { makeAndroidSession } from '../../__tests__/test-utils/session-factories.ts';
12+
import {
13+
makeAndroidSession,
14+
makeIosSession,
15+
} from '../../__tests__/test-utils/session-factories.ts';
1216
import { platformResourceCleanup } from '../../platform-runtime-resource-cleanup.ts';
1317

1418
vi.mock('@agent-device/device-selection/dispatch-resolve', async (importOriginal) => {
@@ -257,3 +261,110 @@ test('sessionless wait timeout still discloses the occluding system surface', as
257261
expect(sessionStore.get('default')).toBeUndefined();
258262
expect(String(response.error.details?.hint)).toContain(ANDROID_SYSTEM_SURFACE_DISCLOSURE);
259263
});
264+
265+
// --- #2438: an in-place iOS system surface (web sign-in sheet) discloses on the same shared seam ---
266+
267+
// The runner stamps `systemSurface` on a capture it served from the sheet; buildSnapshotState turns
268+
// that into `iosSystemSurfaceBundleId`, which selector routes must carry and disclose.
269+
const SHEET_SNAPSHOT_DATA = {
270+
backend: 'xctest',
271+
producer: 'apple-runner',
272+
systemSurface: { bundleId: 'com.apple.SafariViewService', kind: 'web-auth' },
273+
nodes: [
274+
{
275+
index: 0,
276+
depth: 0,
277+
type: 'Application',
278+
label: 'Safari',
279+
rect: { x: 0, y: 0, width: 402, height: 874 },
280+
},
281+
{
282+
index: 1,
283+
depth: 1,
284+
parentIndex: 0,
285+
type: 'Button',
286+
label: 'LOGIN',
287+
hittable: true,
288+
rect: { x: 32, y: 366, width: 338, height: 50 },
289+
},
290+
],
291+
};
292+
293+
function serveSheetCapture(): void {
294+
legacyDispatchCapture.mockReset();
295+
legacyDispatchCapture.mockImplementation(async (_device: unknown, command: string) =>
296+
command === 'snapshot' ? SHEET_SNAPSHOT_DATA : {},
297+
);
298+
}
299+
300+
test('mutating find on an in-place system surface discloses it on the found outcome', async () => {
301+
serveSheetCapture();
302+
const sessionStore = makeSessionStore();
303+
sessionStore.set('default', makeIosSession('default', { appBundleId: 'com.example.app' }));
304+
305+
const response = await handleFindCommands({
306+
req: {
307+
token: 't',
308+
session: 'default',
309+
command: 'find',
310+
positionals: ['LOGIN', 'click'],
311+
flags: {},
312+
},
313+
sessionName: 'default',
314+
logPath: '/tmp/test.log',
315+
sessionStore,
316+
invoke: async () => ({ ok: true, data: {} }) as DaemonResponse,
317+
...getRuntimeBindings(),
318+
});
319+
320+
expect(response?.ok).toBe(true);
321+
if (!response?.ok) return;
322+
expect(String((response.data as Record<string, unknown>).warning)).toContain(
323+
IOS_SYSTEM_SURFACE_DISCLOSURE,
324+
);
325+
});
326+
327+
test('mutating find that misses on an in-place system surface still discloses it', async () => {
328+
serveSheetCapture();
329+
const sessionStore = makeSessionStore();
330+
sessionStore.set('default', makeIosSession('default', { appBundleId: 'com.example.app' }));
331+
332+
const response = await handleFindCommands({
333+
req: {
334+
token: 't',
335+
session: 'default',
336+
command: 'find',
337+
positionals: ['Bakery list', 'click'],
338+
flags: {},
339+
},
340+
sessionName: 'default',
341+
logPath: '/tmp/test.log',
342+
sessionStore,
343+
invoke: async () => ({ ok: true, data: {} }) as DaemonResponse,
344+
...getRuntimeBindings(),
345+
});
346+
347+
expect(response?.ok).toBe(false);
348+
if (response?.ok) return;
349+
expect(String(response?.error.details?.hint)).toContain(IOS_SYSTEM_SURFACE_DISCLOSURE);
350+
});
351+
352+
test('the shared disclosure helper reports an iOS system surface on both outcomes', () => {
353+
const ok = withSystemSurfaceDisclosure(
354+
{ ok: true, data: { found: true } },
355+
{ iosSystemSurfaceBundleId: 'com.apple.SafariViewService' },
356+
);
357+
expect(ok.ok).toBe(true);
358+
if (!ok.ok) return;
359+
expect(String((ok.data as Record<string, unknown>).warning)).toContain(
360+
IOS_SYSTEM_SURFACE_DISCLOSURE,
361+
);
362+
363+
const failed = withSystemSurfaceDisclosure(
364+
{ ok: false, error: { code: 'NOT_FOUND', message: 'no match' } },
365+
{ iosSystemSurfaceBundleId: 'com.apple.SafariViewService' },
366+
);
367+
expect(failed.ok).toBe(false);
368+
if (failed.ok) return;
369+
expect(String(failed.error.details?.hint)).toContain(IOS_SYSTEM_SURFACE_DISCLOSURE);
370+
});

‎src/daemon/deferred-interaction-outcome.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,13 @@ export async function capturePostGestureStabilizedResult<T>(params: {
364364
}
365365

366366
function snapshotComparisonKey(snapshot: SnapshotState | undefined): string | undefined {
367-
return snapshot?.comparisonKey ?? snapshot?.snapshotQuality?.backend;
367+
const base = snapshot?.comparisonKey ?? snapshot?.snapshotQuality?.backend;
368+
const surface = snapshot?.iosSystemSurfaceBundleId;
369+
// An app capture and an in-place system-surface capture (a web sign-in sheet) describe different
370+
// surfaces. Without the surface in this token two XCTest captures compare equal on backend alone,
371+
// so a sheet appearing or dismissing would read as a stable surface (#2438).
372+
if (surface === undefined) return base;
373+
return `${base ?? 'unknown-backend'}@${surface}`;
368374
}
369375

370376
function isPostGestureStabilizingAction(

‎src/daemon/interaction/internal/find-target-capture.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type FindTargetTree = {
1212
nodes: SnapshotState['nodes'];
1313
snapshotQuality?: SnapshotQualityVerdict;
1414
systemSurfaceOnly?: boolean;
15+
iosSystemSurfaceBundleId?: string;
1516
};
1617

1718
/**
@@ -65,6 +66,7 @@ export function createFindTargetCapture(
6566
nodes: snapshot.nodes,
6667
snapshotQuality: snapshot.snapshotQuality,
6768
systemSurfaceOnly: snapshot.systemSurfaceOnly,
69+
iosSystemSurfaceBundleId: snapshot.iosSystemSurfaceBundleId,
6870
};
6971
};
7072
}

0 commit comments

Comments
 (0)