Skip to content

Commit 8b250ca

Browse files
committed
fix(apple-runner): recovery decisions read typed rules, and a deadline gets a verdict
Two private message-substring chains decided runner recovery outside RUNNER_ERROR_RULES, and both got the common case wrong. A command that ran out its connection deadline arrives in a shape whose message matches neither 'timeout' nor 'timed out', so executeRunnerCommand rethrew without restarting a session whose runner never received the command. And shouldRetryPrepareRunnerHealthFailure ORed in shouldRetryRunnerConnectError, whose ?? true default let the connect loop retry past a rule that had just denied it. fetchWithTimeout now reports its own expiry as a COMMAND_FAILED carrying timeoutMs, the shape isCommandTimeoutError already understands. Until then the bare AbortSignal.timeout rejection reached asAppError with no details at all, so neither a rule nor a timeout predicate could see it and the message chains were the only thing catching it. Only a rejection carrying that signal's own reason is relabelled: a refused connection or a canceled request keeps the error it actually failed with. RunnerErrorMatch.details is a predicate rather than a closed union, which is what makes a recorded deadline and the readiness-preflight marker expressible as evidence, and the table gains two axes: restartAfterReadinessPreflight, and artifactSuspect for the rules that say the restored xctestrun itself is at fault. The cache wipe reads that axis, so derived data is destroyed only when the runner refused or never answered on every route, and a boot that exited early says false even though its message also reads as a refused connection. Both chains are deleted; what depends on runtime state (a cached artifact being present, the request being canceled) stays in runner-lifecycle.ts. A bare deadline earns no replay verdict on purpose: the same recorded budget covers a wait inside the connect loop, where waiting is right, and a fetch that died after the command was written, where replaying it is not.
1 parent 28accb8 commit 8b250ca

6 files changed

Lines changed: 267 additions & 43 deletions

File tree

‎packages/platform-apple/src/runner/__tests__/runner-command-retry.test.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,16 @@ test('mutating commands restart stale sessions when readiness preflight fails be
367367
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
368368
});
369369

370-
test('mutating commands restart stale sessions when readiness preflight times out before command send', async () => {
370+
test('mutating commands restart stale sessions when readiness preflight outlives its deadline', async () => {
371371
const staleSession = makeRunnerSession({ port: 8100, ready: true });
372372
const freshSession = makeRunnerSession({ port: 8101, ready: false });
373373

374374
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
375375
mockExecuteRunnerCommandWithSession
376376
.mockRejectedValueOnce(
377-
new AppError('COMMAND_FAILED', 'Runner readiness timed out', {
377+
new AppError('COMMAND_FAILED', 'xcrun timed out after 45000ms', {
378+
cmd: 'xcrun',
379+
timeoutMs: 45_000,
378380
runnerReadinessPreflightFailed: true,
379381
}),
380382
)
@@ -392,6 +394,47 @@ test('mutating commands restart stale sessions when readiness preflight times ou
392394
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
393395
});
394396

397+
test('a readiness preflight refusal that is neither transport-shaped nor deadline-shaped surfaces', async () => {
398+
const staleSession = makeRunnerSession({ port: 8100, ready: true });
399+
400+
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession);
401+
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
402+
new AppError('COMMAND_FAILED', 'Runner readiness refused', {
403+
runnerReadinessPreflightFailed: true,
404+
}),
405+
);
406+
407+
await assert.rejects(
408+
() => runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }),
409+
(error: unknown) => {
410+
assert.ok(error instanceof AppError);
411+
assert.equal(error.message, 'Runner readiness refused');
412+
return true;
413+
},
414+
);
415+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
416+
});
417+
418+
test('a boot that exited early does not wipe a restored runner artifact', async () => {
419+
const fixtures = makeBadCacheRecoveryFixtures();
420+
421+
mockEnsureRunnerSession.mockResolvedValueOnce(fixtures.restoredSession);
422+
mockExecuteRunnerCommandWithSession.mockRejectedValueOnce(
423+
new AppError('COMMAND_FAILED', 'Runner did not accept connection (xcodebuild exited early)'),
424+
);
425+
426+
await assert.rejects(
427+
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
428+
(error: unknown) => {
429+
assert.ok(error instanceof AppError);
430+
assert.equal(error.message, 'Runner did not accept connection (xcodebuild exited early)');
431+
return true;
432+
},
433+
);
434+
assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0);
435+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1);
436+
});
437+
395438
test('mutating commands emit readiness recovery diagnostics after failed preflight restart succeeds', async () => {
396439
const staleSession = makeRunnerSession({ port: 8100, ready: true });
397440
const freshSession = makeRunnerSession({ port: 8101, ready: false });

‎packages/platform-apple/src/runner/__tests__/runner-error-classification.test.ts‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
RUNNER_ERROR_RULES,
66
isRetryableRunnerError,
77
resolveRunnerFatalErrorReason,
8+
shouldRebuildCachedRunnerArtifact,
9+
shouldRestartRunnerAfterReadinessPreflight,
810
shouldRestartRunnerBeforeCommandSend,
911
shouldRetryRunnerConnectError,
1012
} from '../runner-contract.ts';
@@ -32,7 +34,12 @@ test('transport-shaped failures are retryable', () => {
3234
});
3335

3436
test('boot-shaped failures are not retryable', () => {
35-
assert.equal(isRetryableRunnerError(commandFailed('xcodebuild exited early (code 65)')), false);
37+
assert.equal(
38+
isRetryableRunnerError(
39+
commandFailed('Runner did not accept connection (xcodebuild exited early)'),
40+
),
41+
false,
42+
);
3643
assert.equal(
3744
isRetryableRunnerError(commandFailed('Device is busy (Connecting to Simulator)')),
3845
false,
@@ -70,6 +77,79 @@ test('connect loop stops for terminal verdicts', () => {
7077
assert.equal(shouldRetryRunnerConnectError(new AppError('DEVICE_NOT_FOUND', 'gone')), true);
7178
});
7279

80+
// --- connect deadline ---
81+
82+
test('a readiness preflight that outlived its deadline restarts the session', () => {
83+
// The marked shape this recovery actually sees: the simctl-spawn fallback killed at
84+
// its budget, then marked by the readiness preflight that was waiting on it.
85+
const killedSpawn = commandFailed('xcrun timed out after 45000ms', {
86+
cmd: 'xcrun',
87+
timeoutMs: 45_000,
88+
runnerReadinessPreflightFailed: true,
89+
});
90+
assert.equal(shouldRestartRunnerAfterReadinessPreflight(killedSpawn), true);
91+
assert.equal(shouldRetryRunnerConnectError(killedSpawn), true);
92+
// A preflight refusal with no deadline restarts nothing: the runner answered and said no.
93+
assert.equal(
94+
shouldRestartRunnerAfterReadinessPreflight(
95+
commandFailed('Runner readiness refused', { runnerReadinessPreflightFailed: true }),
96+
),
97+
false,
98+
);
99+
});
100+
101+
test('a deadline on its own earns no recovery verdict', () => {
102+
// The same recorded budget covers a wait inside the connect loop, where waiting is
103+
// right, and a fetch that died after the command was written, where replaying is not.
104+
const deadline = commandFailed('Runner command deadline exceeded', {
105+
port: 8100,
106+
timeoutMs: 45_000,
107+
});
108+
assert.equal(isRetryableRunnerError(deadline), false);
109+
assert.equal(shouldRestartRunnerBeforeCommandSend(deadline), false);
110+
assert.equal(shouldRestartRunnerAfterReadinessPreflight(deadline), false);
111+
assert.equal(shouldRebuildCachedRunnerArtifact(deadline), false);
112+
assert.equal(shouldRetryRunnerConnectError(deadline), true);
113+
});
114+
115+
// --- restored-artifact axis (shouldRebuildCachedRunnerArtifact) ---
116+
117+
test('only a runner that never accepted a connection indicts the cached artifact', () => {
118+
assert.equal(
119+
shouldRebuildCachedRunnerArtifact(commandFailed('Runner endpoint probe failed')),
120+
true,
121+
);
122+
assert.equal(
123+
shouldRebuildCachedRunnerArtifact(commandFailed('Runner did not accept connection')),
124+
true,
125+
);
126+
assert.equal(
127+
shouldRebuildCachedRunnerArtifact(
128+
commandFailed('Runner did not accept connection (simctl spawn)', { port: 8100 }),
129+
),
130+
true,
131+
);
132+
// Wiping derived data cannot fix a boot that refuses to compile, and its message
133+
// otherwise reads as a refused connection.
134+
assert.equal(
135+
shouldRebuildCachedRunnerArtifact(
136+
commandFailed('Runner did not accept connection (xcodebuild exited early)', {
137+
port: 8100,
138+
logPath: '/tmp/runner.log',
139+
}),
140+
),
141+
false,
142+
);
143+
assert.equal(shouldRebuildCachedRunnerArtifact(commandFailed('fetch failed')), false);
144+
});
145+
146+
test('a device that is busy connecting is a terminal connect verdict', () => {
147+
assert.equal(
148+
shouldRetryRunnerConnectError(commandFailed('Device is busy (Connecting to Simulator)')),
149+
false,
150+
);
151+
});
152+
73153
// --- session-fatal axis (resolveRunnerFatalErrorReason) ---
74154

75155
test('session-fatal codes map to their invalidation reasons', () => {

‎packages/platform-apple/src/runner/__tests__/runner-transport.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
xctestIosDevice,
1414
} from './runner-transport.fixtures.ts';
1515
import { appleRunnerTestHost } from '../test-host.ts';
16+
import { isCommandTimeoutError } from '../host.ts';
1617
import type { IosPhysicalDeviceRunnerControl } from '../host.ts';
1718

1819
const { mockRunCmd, mockUsbmuxPostCommand } = vi.hoisted(() => ({
@@ -31,7 +32,7 @@ vi.mock('../runner-usbmux.ts', async (importOriginal) => {
3132
});
3233

3334
import { clearDeviceTunnelIpCache } from '../runner-command-route.ts';
34-
import { sendRunnerCommandOnce } from '../runner-transport.ts';
35+
import { fetchWithTimeout, sendRunnerCommandOnce } from '../runner-transport.ts';
3536

3637
// The real `resolveIosPhysicalDeviceControl` resolves the CoreDevice tunnel IP
3738
// through root-level tooling this package cannot reach; a fake control backed
@@ -115,6 +116,28 @@ test('sendRunnerCommandOnce does not retry or simulator fallback after request f
115116
assert.equal(mockRunCmd.mock.calls.length, 0);
116117
});
117118

119+
test('fetchWithTimeout reports its own deadline as a typed command timeout', async () => {
120+
vi.stubGlobal(
121+
'fetch',
122+
vi.fn((_url: string, init: RequestInit) => {
123+
return new Promise((_resolve, reject) => {
124+
init.signal?.addEventListener('abort', () => reject(init.signal?.reason));
125+
});
126+
}),
127+
);
128+
129+
await assert.rejects(
130+
() => fetchWithTimeout('http://127.0.0.1:8100/command', { method: 'POST' }, 5),
131+
(error: unknown) => {
132+
assert.ok(error instanceof AppError);
133+
assert.equal(error.code, 'COMMAND_FAILED');
134+
assert.equal(error.details?.timeoutMs, 5);
135+
assert.equal(isCommandTimeoutError(error), true);
136+
return true;
137+
},
138+
);
139+
});
140+
118141
test('sendRunnerCommandOnce routes xctest physical devices through usbmux', async () => {
119142
const fetchMock = vi.fn();
120143
vi.stubGlobal('fetch', fetchMock);

0 commit comments

Comments
 (0)