Skip to content

Commit 1b0a1a6

Browse files
test(apple-runner): a deadline route that the old wording check could not see
The route-level case the review asked for: a readiness preflight failure that records the budget it ran out, carries the preflight marker, and says nothing about time in its message. The table restarts the session and replays; the deleted substring chain rethrew. Restoring that message check turns this one test red and leaves the rest of the file green, which is the difference the earlier cases could not show. The other half is the question the narrower wipe raises: a restored artifact whose runner never answers past its deadline on every attempt. It is not rebuilt. `runner-lifecycle-prepare-artifact.test.ts` pins what does happen — the artifact survives, the session is invalidated, the deadline is reported — and it goes red if the wipe goes back to firing on any prepare timeout. The same answer is now in the changelog, because "nothing rebuilds it" is a behavior someone will hit. `isRunnerReadinessPreflightError` moves into the contract module it was duplicating. Co-authored-by: Apex by Callstack <[email protected]>
1 parent ea981ab commit 1b0a1a6

5 files changed

Lines changed: 165 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
## Unreleased
44

5+
- Changed (iOS runner): what recovers a stuck runner is decided by the recorded error, not by its
6+
wording. A command that ran out its connection deadline used to arrive as a bare
7+
`AbortSignal.timeout` rejection whose message matched neither `timeout` nor `timed out`, so the
8+
session was never restarted and the command failed for good. It now carries the budget it ran
9+
out, and a readiness preflight that gave up on that budget restarts the runner and replays the
10+
command whether or not the message mentions time. The other half of this is what no longer
11+
happens: a prepare deadline, a slow boot, or a busy device no longer wipes a restored
12+
`xcodebuild` artifact on the way to a rebuild, because only a runner that refused a connection or
13+
never answered on any route says the artifact itself is at fault. A restored artifact whose runner
14+
hangs past its deadline on every attempt does not rebuild itself: the runner session is
15+
invalidated and the deadline is reported, and the rebuild needs either a failure that indicts the
16+
artifact or the runner cache cleared by hand.
17+
518
- Changed (sessions): the implicit session is now keyed by workspace **and platform**, so one checkout
619
can drive iOS and Android without inventing a `--session` name for every command (#2580). An
720
implicit session was addressed by `cwd:<workspace>:default`, one slot per checkout, and it stayed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import assert from 'node:assert/strict';
2+
import { beforeEach, test, vi } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import { appleRunnerTestHost } from '../test-host.ts';
5+
import type { RunnerXctestrunArtifact } from '../runner-xctestrun.ts';
6+
import { IOS_SIMULATOR } from './device-fixtures.ts';
7+
import { createTestRequestCancellation, makeRunnerSession } from './runner-session-fixtures.ts';
8+
9+
const {
10+
mockEnsureRunnerSession,
11+
mockExecuteRunnerCommandWithSession,
12+
mockEmitDiagnostic,
13+
mockGetRunnerSessionSnapshot,
14+
mockInvalidateRunnerSession,
15+
mockMarkRunnerXctestrunArtifactBadForRun,
16+
} = vi.hoisted(() => ({
17+
mockEnsureRunnerSession: vi.fn(),
18+
mockExecuteRunnerCommandWithSession: vi.fn(),
19+
mockEmitDiagnostic: vi.fn(),
20+
mockGetRunnerSessionSnapshot: vi.fn(),
21+
mockInvalidateRunnerSession: vi.fn(),
22+
mockMarkRunnerXctestrunArtifactBadForRun: vi.fn(),
23+
}));
24+
25+
vi.mock('../runner-session.ts', async () => {
26+
const actual =
27+
await vi.importActual<typeof import('../runner-session.ts')>('../runner-session.ts');
28+
return {
29+
...actual,
30+
ensureRunnerSession: mockEnsureRunnerSession,
31+
executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession,
32+
getRunnerSessionSnapshot: mockGetRunnerSessionSnapshot,
33+
invalidateRunnerSession: mockInvalidateRunnerSession,
34+
};
35+
});
36+
37+
vi.mock('../runner-xctestrun.ts', async () => {
38+
const actual =
39+
await vi.importActual<typeof import('../runner-xctestrun.ts')>('../runner-xctestrun.ts');
40+
return {
41+
...actual,
42+
markRunnerXctestrunArtifactBadForRun: mockMarkRunnerXctestrunArtifactBadForRun,
43+
};
44+
});
45+
46+
import { prepareIosRunner } from '../runner-client.ts';
47+
import { resetRunnerRecycleLedgerForTests } from '../runner-recycle-ledger.ts';
48+
49+
function makeRunnerArtifact(
50+
overrides: Partial<RunnerXctestrunArtifact> = {},
51+
): RunnerXctestrunArtifact {
52+
return {
53+
xctestrunPath: '/tmp/runner.xctestrun',
54+
derived: '/tmp/derived',
55+
cache: 'exact',
56+
artifact: 'valid',
57+
buildMs: 0,
58+
xctestrunPathSource: 'manifest',
59+
...overrides,
60+
};
61+
}
62+
63+
const requestCancellation = createTestRequestCancellation();
64+
const { isRequestCanceled } = requestCancellation;
65+
66+
beforeEach(() => {
67+
vi.resetAllMocks();
68+
resetRunnerRecycleLedgerForTests();
69+
mockGetRunnerSessionSnapshot.mockReturnValue(null);
70+
mockMarkRunnerXctestrunArtifactBadForRun.mockResolvedValue(undefined);
71+
requestCancellation.reset();
72+
appleRunnerTestHost.update({
73+
emitDiagnostic: mockEmitDiagnostic,
74+
isRequestCanceled,
75+
getRequestSignal: () => undefined,
76+
});
77+
});
78+
79+
// What a prepare deadline does to a restored artifact. The wipe that rebuilds a suspect
80+
// artifact comes from the rules that indict the artifact itself; a runner that never
81+
// answers inside its budget indicts the boot, not the derived data it was launched from,
82+
// so the artifact stays and the session goes.
83+
84+
test('a restored artifact whose runner never answers past its deadline is kept while the session is dropped', async () => {
85+
const restoredSession = makeRunnerSession({
86+
port: 8100,
87+
xctestrunPath: '/tmp/restored.xctestrun',
88+
xctestrunArtifact: makeRunnerArtifact({ xctestrunPath: '/tmp/restored.xctestrun' }),
89+
});
90+
91+
mockEnsureRunnerSession.mockResolvedValue(restoredSession);
92+
mockExecuteRunnerCommandWithSession.mockRejectedValue(
93+
new AppError('COMMAND_FAILED', 'xcrun simctl spawn did not answer', {
94+
cmd: 'xcrun',
95+
timeoutMs: 45_000,
96+
}),
97+
);
98+
99+
await assert.rejects(
100+
() => prepareIosRunner(IOS_SIMULATOR, { healthTimeoutMs: 90_000 }),
101+
(error: unknown) => {
102+
assert.ok(error instanceof AppError);
103+
assert.equal(error.message, 'xcrun simctl spawn did not answer');
104+
return true;
105+
},
106+
);
107+
108+
assert.equal(mockMarkRunnerXctestrunArtifactBadForRun.mock.calls.length, 0);
109+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
110+
assert.deepEqual(mockInvalidateRunnerSession.mock.calls.at(-1), [
111+
restoredSession,
112+
'prepare_runner_health_failed',
113+
]);
114+
});

‎packages/platform-apple/src/runner/__tests__/runner-lifecycle-readiness-preflight.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,32 @@ test('mutating commands restart stale sessions when readiness preflight outlives
133133
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
134134
});
135135

136+
test('a readiness preflight deadline with no timeout wording still restarts the session', async () => {
137+
const staleSession = makeRunnerSession({ port: 8100, ready: true });
138+
const freshSession = makeRunnerSession({ port: 8101, ready: false });
139+
140+
// The shape `fetchWithTimeout` reports its own expiry in: a recorded budget and the
141+
// preflight marker, and a message that says nothing about time. A message check had
142+
// been the only way to notice, and it would rethrow here without restarting a session
143+
// whose runner never received the command.
144+
mockEnsureRunnerSession.mockResolvedValueOnce(staleSession).mockResolvedValueOnce(freshSession);
145+
mockExecuteRunnerCommandWithSession
146+
.mockRejectedValueOnce(
147+
new AppError('COMMAND_FAILED', 'xcrun simctl spawn did not answer', {
148+
cmd: 'xcrun',
149+
timeoutMs: 45_000,
150+
runnerReadinessPreflightFailed: true,
151+
}),
152+
)
153+
.mockResolvedValueOnce({ message: 'tapped' });
154+
155+
const result = await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 });
156+
157+
assert.deepEqual(result, { message: 'tapped' });
158+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 2);
159+
assert.equal(mockExecuteRunnerCommandWithSession.mock.calls[1]?.[1], freshSession);
160+
});
161+
136162
test('a readiness preflight refusal that is neither transport-shaped nor deadline-shaped surfaces', async () => {
137163
const staleSession = makeRunnerSession({ port: 8100, ready: true });
138164

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ export function shouldRetryRunnerConnectError(error: unknown): boolean {
351351
return runnerErrorVerdict(error, 'connectRetry') ?? true;
352352
}
353353

354+
/**
355+
* The readiness preflight gave up before the command was written. The marker is the only
356+
* evidence for that: an error carrying it arrives in whatever shape the preflight failed
357+
* in, so no message check can be the test.
358+
*/
359+
export function isRunnerReadinessPreflightFailure(error: unknown): boolean {
360+
if (!(error instanceof AppError)) return false;
361+
return hasReadinessPreflightFailure((error.details ?? {}) as AppErrorDetails);
362+
}
363+
354364
/**
355365
* The readiness preflight ran out of its own deadline, so the runner never saw the
356366
* command: restarting the session and replaying is both safe and the only way out.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import {
1717
assertRunnerRequestActive,
1818
isRetryableRunnerError,
19+
isRunnerReadinessPreflightFailure,
1920
resolveRunnerRequestSignal,
2021
shouldRebuildCachedRunnerArtifact,
2122
shouldRestartRunnerAfterReadinessPreflight,
@@ -600,13 +601,9 @@ function emitPrepareDiagnostic(
600601
});
601602
}
602603

603-
function isRunnerReadinessPreflightError(error: AppError): boolean {
604-
return error.details?.runnerReadinessPreflightFailed === true;
605-
}
606-
607604
function shouldRestartAfterReadinessPreflightError(error: AppError): boolean {
608605
return (
609-
isRunnerReadinessPreflightError(error) &&
606+
isRunnerReadinessPreflightFailure(error) &&
610607
(isRetryableRunnerError(error) || shouldRestartRunnerAfterReadinessPreflight(error))
611608
);
612609
}

0 commit comments

Comments
 (0)