Skip to content

Commit 8951f93

Browse files
fix(ios-runner): let an early exit carry the disk-image state it read
Co-Authored-By: Apex <[email protected]>
1 parent 22fd7b6 commit 8951f93

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-early-exit-diagnosis.test.ts‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const PROVISIONING_FAILURE_STDERR = [
1616
'** TEST EXECUTE FAILED **',
1717
].join('\n');
1818

19-
function sessionFailingWith(stdout: string, stderr: string): RunnerSession {
19+
function sessionFailingWith(
20+
stdout: string,
21+
stderr: string,
22+
startupDeviceStates?: RunnerSession['startupDeviceStates'],
23+
): RunnerSession {
2024
return {
2125
sessionId: 'early-exit-session',
2226
device: { platform: 'apple', id: 'device-1', name: 'iPhone', kind: 'device', booted: true },
@@ -27,9 +31,16 @@ function sessionFailingWith(stdout: string, stderr: string): RunnerSession {
2731
testPromise: Promise.resolve({ exitCode: 1, stdout, stderr }),
2832
child: { pid: 4242, exitCode: 1 } as ExecBackgroundResult['child'],
2933
state: 'starting',
34+
startupDeviceStates,
3035
};
3136
}
3237

38+
const IMAGE_DOWN_STATES = {
39+
developerMode: 'enabled' as const,
40+
developerDiskImage: 'unavailable' as const,
41+
developerDiskImageHint: 'Unlock the iPhone so it can mount the developer disk image.',
42+
};
43+
3344
test('the early-exit error a user actually receives names the provisioning cause', async () => {
3445
// Regression: the reason was classified correctly while the hint was built
3546
// separately and always returned connect-timeout guidance, so the shipped
@@ -67,3 +78,44 @@ test('a busy connecting device keeps its own targeted hint', async () => {
6778

6879
assert.match(String(error.details?.hint), /still connecting/);
6980
});
81+
82+
test('an early exit carries the disk-image state the device was read in (#2683)', async () => {
83+
// A locked iPhone lets the build finish and kills `xcodebuild test-without-building` instead, so
84+
// the startup build catch never runs and the readiness facts read before the build would be
85+
// dropped. Captured on hardware: this is the failure an image-down locked phone actually produces.
86+
const error = (await buildRunnerEarlyExitError({
87+
session: sessionFailingWith(
88+
'',
89+
'xcodebuild: error: Timed out waiting for the test runner',
90+
IMAGE_DOWN_STATES,
91+
),
92+
port: 8100,
93+
})) as AppError;
94+
95+
assert.equal(error.details?.developerDiskImage, 'unavailable');
96+
});
97+
98+
test('an early exit that already names a cause keeps it, and only gains the fact (#2683)', async () => {
99+
const error = (await buildRunnerEarlyExitError({
100+
session: sessionFailingWith(
101+
'',
102+
'xcodebuild: error: Timed out waiting for the test runner',
103+
IMAGE_DOWN_STATES,
104+
),
105+
port: 8100,
106+
})) as AppError;
107+
108+
// The connect reason was proved by the tool output, so the device's image state is never allowed
109+
// to overwrite a claimed cause or swap the hint beside it.
110+
assert.equal(error.details?.reason, 'IOS_RUNNER_CONNECT_TIMEOUT');
111+
assert.match(String(error.details?.hint), /Retry runner startup/);
112+
});
113+
114+
test('a session that never probed the device publishes no disk-image claim (#2683)', async () => {
115+
const error = (await buildRunnerEarlyExitError({
116+
session: sessionFailingWith('', 'xcodebuild: error: Timed out waiting for the test runner'),
117+
port: 8100,
118+
})) as AppError;
119+
120+
assert.equal('developerDiskImage' in (error.details ?? {}), false);
121+
});

‎packages/platform-apple/src/runner/runner-contract.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ export async function buildRunnerEarlyExitError(params: {
844844
// exec-guard-allow: xcodebuild can exit 0 and still count as an early exit;
845845
// the trio is nested tool context under `xcodebuild`, classified into
846846
// `reason`/`hint` above — not a process-exit wrap.
847-
return new AppError('COMMAND_FAILED', message, {
847+
const error = new AppError('COMMAND_FAILED', message, {
848848
port,
849849
logPath,
850850
xcodebuild: {
@@ -855,6 +855,11 @@ export async function buildRunnerEarlyExitError(params: {
855855
reason,
856856
hint: resolveRunnerEarlyExitHint(message, result.stdout, result.stderr, reason),
857857
});
858+
// The build catch is not the only way a runner stops before serving a command. A locked phone lets
859+
// the build finish and kills `xcodebuild test-without-building` instead, so nothing reaches that
860+
// catch and the disk-image state read before the build would be dropped. Same enrichment, applied
861+
// to the failure this path actually produces (#2683).
862+
return enrichRunnerStartupFailureWithDeviceStates(error, session.startupDeviceStates) as AppError;
858863
}
859864

860865
/**

‎packages/platform-apple/src/runner/runner-session-types.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { DeviceInfo } from '@agent-device/kernel/device';
44
import type { RunnerXctestrunArtifact } from './runner-xctestrun.ts';
55
import type { RunnerLease } from './runner-lease.ts';
66
import type { XcodebuildSimulatorSetRedirectHandle } from './runner-device-set.ts';
7+
import type { IosRunnerDeviceStates } from './runner-contract.ts';
78

89
/**
910
* Where one runner process stands in the lifecycle of the session that owns it (#2662). The state
@@ -86,6 +87,12 @@ export type RunnerSession = {
8687
speculative?: boolean;
8788
startupTimings?: Record<string, number>;
8889
startupTimingsReported?: boolean;
90+
/**
91+
* Device-readiness facts the pre-build probe read for this startup. An adopted session has none:
92+
* it skipped the probe. Carried so a failure raised after the build still reports the disk image
93+
* state the device was in, which is the only way a locked phone's early exit says why (#2683).
94+
*/
95+
startupDeviceStates?: IosRunnerDeviceStates;
8996
logicalLeaseContext?: RunnerLogicalLeaseContext;
9097
simulatorSetRedirect?: XcodebuildSimulatorSetRedirectHandle;
9198
lease?: RunnerLease;

‎packages/platform-apple/src/runner/runner-session.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ async function startRunnerSessionWithLease(
322322
startupRetryWake: runnerProcess.startupRetryWake,
323323
startupTimeoutMs: normalizeRunnerStartupTimeoutMs(startupTimeoutMs),
324324
startupTimings,
325+
startupDeviceStates: deviceStates,
325326
logicalLeaseContext,
326327
simulatorSetRedirect: simulatorSetRedirect ?? undefined,
327328
lease,

0 commit comments

Comments
 (0)