Skip to content

Commit 318d510

Browse files
committed
Merge branch 'codex/2198-runner-demand' into codex/2198-proxy-parity
* codex/2198-runner-demand: fix(ios): keep observation on the bridge while app discovery is pending and no runner is live bench(ios): press an unambiguous target on the catalog and Settings screens
2 parents ddfbd9d + e729321 commit 318d510

5 files changed

Lines changed: 109 additions & 5 deletions

File tree

‎packages/platform-apple/src/snapshot-route.test.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ test('cancelled acquisition does not start a fallback after the request aborts',
204204
expect(fallback).not.toHaveBeenCalled();
205205
});
206206

207-
test('a slow app discovery yields to the XCTest fallback within its wait slice, then serves the bridge', async () => {
207+
test('a slow app discovery yields to a live runner within its wait slice, then serves the bridge', async () => {
208208
// The production resolver over a simctl whose `launchctl list` answers only when released,
209-
// the shape of a loaded CI host: the first capture must not sit on that probe.
209+
// the shape of a loaded CI host: with a runner that can answer at once, the first capture
210+
// must not sit on that probe.
210211
let release!: () => void;
211212
const released = new Promise<void>((resolve) => {
212213
release = resolve;
@@ -232,9 +233,11 @@ test('a slow app discovery yields to the XCTest fallback within its wait slice,
232233
producer: 'simulator-ax-bridge' as const,
233234
nodes: [{ index: 0, type: 'Application' }],
234235
}));
236+
const baseHost = platformRuntimeHostFixture();
235237
const route = createAppleSnapshotRoute(
236238
{
237-
...platformRuntimeHostFixture(),
239+
...baseHost,
240+
appleApplications: { ...baseHost.appleApplications, hasLiveRunnerSession: async () => true },
238241
snapshot: { captureSurface: vi.fn(), presentIosAcquisition },
239242
},
240243
{ source, resolveTarget: createSimulatorSnapshotTargetResolver() },
@@ -302,3 +305,65 @@ function runnerResult() {
302305
function signal(): AbortSignal {
303306
return new AbortController().signal;
304307
}
308+
309+
test('a slow app discovery keeps observation on the bridge while no runner can answer', async () => {
310+
// #2198: the open no longer awaits the runner, so right after a relaunch the fallback would
311+
// wait for a cold runner start. A capture with no live runner rides the single-flight
312+
// discovery instead, however many wait slices that takes.
313+
let release!: () => void;
314+
const released = new Promise<void>((resolve) => {
315+
release = resolve;
316+
});
317+
const run = vi.fn(async (args: string[]) => {
318+
if (args[0] === 'spawn') await released;
319+
return {
320+
stdout:
321+
args[0] === 'spawn'
322+
? `42\t0\tUIKitApplication:${input.options.appBundleId}[launch-a][rb-legacy]`
323+
: JSON.stringify({
324+
devices: { 'com.apple.CoreSimulator.SimRuntime.iOS-26-0': [{ udid: ios.id }] },
325+
}),
326+
stderr: '',
327+
exitCode: 0,
328+
};
329+
});
330+
const runCommand = vi.fn(async () => ({ stdout: 'start-a', stderr: '', exitCode: 0 }));
331+
const fallback = vi.fn(async () => runnerResult());
332+
const source = sourceReturning(bridgeAcquisition());
333+
const presentIosAcquisition = vi.fn(async () => ({
334+
backend: 'xctest' as const,
335+
producer: 'simulator-ax-bridge' as const,
336+
nodes: [{ index: 0, type: 'Application' }],
337+
}));
338+
const hasLiveRunnerSession = vi.fn(async () => false);
339+
const baseHost = platformRuntimeHostFixture();
340+
const route = createAppleSnapshotRoute(
341+
{
342+
...baseHost,
343+
appleApplications: { ...baseHost.appleApplications, hasLiveRunnerSession },
344+
snapshot: { captureSurface: vi.fn(), presentIosAcquisition },
345+
},
346+
{ source, resolveTarget: createSimulatorSnapshotTargetResolver() },
347+
);
348+
vi.useFakeTimers();
349+
try {
350+
await withAppleToolProvider(
351+
createLocalAppleToolProvider({ simctl: { run }, runCommand }),
352+
async () => {
353+
const capture = route.capture(ios, input, signal(), fallback);
354+
await vi.advanceTimersByTimeAsync(4_500);
355+
expect(fallback).not.toHaveBeenCalled();
356+
expect(hasLiveRunnerSession).toHaveBeenCalled();
357+
358+
release();
359+
await vi.advanceTimersByTimeAsync(0);
360+
const result = await capture;
361+
expect(result.producer).toBe('simulator-ax-bridge');
362+
expect(fallback).not.toHaveBeenCalled();
363+
expect(run.mock.calls.filter(([args]) => args[0] === 'spawn')).toHaveLength(1);
364+
},
365+
);
366+
} finally {
367+
vi.useRealTimers();
368+
}
369+
});

‎packages/platform-apple/src/snapshot-route.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function createAppleSnapshotRoute(
6666
if (!isEligible(device, input)) return await fallback(input);
6767
let target: SimulatorSnapshotTarget;
6868
try {
69-
target = await resolveTarget(device, input.options!.appBundleId!, signal);
69+
target = await resolveTargetForObservation(host, resolveTarget, device, input, signal);
7070
} catch (error) {
7171
signal.throwIfAborted();
7272
emitRouteDiagnostic('target-resolution-failed', device, undefined, error);
@@ -143,6 +143,38 @@ export function createAppleSnapshotRoute(
143143
});
144144
}
145145

146+
/**
147+
* A discovery still in flight is not a failure while no runner can answer instead. The XCTest
148+
* fallback would first wait for a runner start, and #2198 keeps observation off that wait, so the
149+
* capture stays on the single-flight discovery: each turn waits one discovery slice, and the
150+
* discovery's own deadline or the request signal ends the loop. A runner that is already live
151+
* answers at once, so there the fallback remains the cheaper route (#2331).
152+
*/
153+
async function resolveTargetForObservation(
154+
host: PlatformRuntimeHost,
155+
resolveTarget: SimulatorSnapshotTargetResolver,
156+
device: DeviceInfo,
157+
input: CaptureSnapshotInput,
158+
signal: AbortSignal,
159+
): Promise<SimulatorSnapshotTarget> {
160+
const appBundleId = input.options!.appBundleId!;
161+
for (;;) {
162+
try {
163+
return await resolveTarget(device, appBundleId, signal);
164+
} catch (error) {
165+
if (!isDiscoveryPending(error)) throw error;
166+
const execution = { requestId: input.execution?.requestId };
167+
if (await host.appleApplications.hasLiveRunnerSession(device, execution)) throw error;
168+
}
169+
}
170+
}
171+
172+
function isDiscoveryPending(error: unknown): boolean {
173+
return (
174+
error instanceof AppError && error.details?.reason === 'simulator-target-discovery-pending'
175+
);
176+
}
177+
146178
function isEligible(device: DeviceInfo, input: CaptureSnapshotInput): boolean {
147179
return (
148180
device.platform === 'apple' &&

‎scripts/ios-snapshot-benchmark/command.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function firstInteractionAfterOpen(context: CliContext, fixture: ScreenFi
8080
const selector =
8181
fixture.launchUrl && deepLinkConfirmationShown(context, opened)
8282
? 'label="Open"'
83-
: `text=${JSON.stringify(fixture.anchorText)}`;
83+
: (fixture.interactionTarget ?? `text=${JSON.stringify(fixture.anchorText)}`);
8484
return pressFixtureTarget(context, selector);
8585
}
8686

‎scripts/ios-snapshot-benchmark/definitions.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const SCREEN_FIXTURES: readonly ScreenFixture[] = [
2929
app: FIXTURE_APP_ID,
3030
launchUrl: `${FIXTURE_SCHEME}/catalog`,
3131
anchorText: 'Catalog',
32+
interactionTarget: 'id="catalog-search"',
3233
},
3334
{
3435
id: 'nested-scroll',
@@ -51,6 +52,7 @@ const SCREEN_FIXTURES: readonly ScreenFixture[] = [
5152
label: 'iOS Settings system surface',
5253
app: IOS_SETTINGS_APP_ID,
5354
anchorText: 'Settings',
55+
interactionTarget: 'text="General"',
5456
},
5557
{
5658
id: 'xctest-stress',

‎scripts/ios-snapshot-benchmark/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export type ScreenFixture = {
3939
anchorText: string;
4040
postSetupAnchorText?: string;
4141
setupAction?: 'open-alert';
42+
/**
43+
* What the first-interaction cell presses when the anchor text names more than one
44+
* actionable element (a native tab and the screen title share it); the anchor otherwise.
45+
*/
46+
interactionTarget?: string;
4247
};
4348

4449
export type Failure = {

0 commit comments

Comments
 (0)