Skip to content

Commit 6f831af

Browse files
committed
fix: address runner recovery ci failures
1 parent 974674f commit 6f831af

4 files changed

Lines changed: 88 additions & 44 deletions

File tree

‎apple-runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandExecution.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ extension RunnerTests {
195195

196196
XCTAssertFalse(response.ok)
197197
XCTAssertEqual(response.error?.code, "RUNNER_BUSY")
198-
XCTAssertTrue(response.error?.message?.contains("previous command") == true)
198+
XCTAssertTrue(response.error?.message.contains("previous command") == true)
199199
}
200200

201201
func testExecuteDispatchedReturnsWedgedBeforeMainThreadFastPath() throws {

‎src/platforms/apple/core/__tests__/runner-command-retry.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ const {
1010
mockEnsureRunnerSession,
1111
mockExecuteRunnerCommandWithSession,
1212
mockEmitDiagnostic,
13+
mockGetRunnerSessionSnapshot,
1314
mockInvalidateRunnerSession,
1415
mockMarkRunnerXctestrunArtifactBadForRun,
1516
} = vi.hoisted(() => ({
1617
mockEnsureRunnerSession: vi.fn(),
1718
mockExecuteRunnerCommandWithSession: vi.fn(),
1819
mockEmitDiagnostic: vi.fn(),
20+
mockGetRunnerSessionSnapshot: vi.fn(),
1921
mockInvalidateRunnerSession: vi.fn(),
2022
mockMarkRunnerXctestrunArtifactBadForRun: vi.fn(),
2123
}));
@@ -38,6 +40,7 @@ vi.mock('../runner/runner-session.ts', async () => {
3840
...actual,
3941
ensureRunnerSession: mockEnsureRunnerSession,
4042
executeRunnerCommandWithSession: mockExecuteRunnerCommandWithSession,
43+
getRunnerSessionSnapshot: mockGetRunnerSessionSnapshot,
4144
invalidateRunnerSession: mockInvalidateRunnerSession,
4245
};
4346
});
@@ -63,6 +66,7 @@ import type { RunnerXctestrunArtifact } from '../runner/runner-xctestrun.ts';
6366
beforeEach(() => {
6467
vi.resetAllMocks();
6568
resetRunnerRecycleLedgerForTests();
69+
mockGetRunnerSessionSnapshot.mockReturnValue(null);
6670
mockMarkRunnerXctestrunArtifactBadForRun.mockResolvedValue(undefined);
6771
});
6872

@@ -1207,6 +1211,37 @@ test('a failed replacement boot does not consume the request recycle budget', as
12071211
assert.equal(mockEnsureRunnerSession.mock.calls.length, 3);
12081212
});
12091213

1214+
test('alive session reuse in the same request does not consume recycle budget', async () => {
1215+
const requestId = 'req-alive-reuse-before-recycle';
1216+
mockGetRunnerSessionSnapshot
1217+
.mockReturnValueOnce(null)
1218+
.mockReturnValueOnce({ alive: true })
1219+
.mockReturnValueOnce(null);
1220+
mockEnsureRunnerSession
1221+
.mockResolvedValueOnce(makeRunnerSession({ port: 8100, ready: true }))
1222+
.mockResolvedValueOnce(makeRunnerSession({ port: 8100, ready: true }))
1223+
.mockResolvedValueOnce(makeRunnerSession({ port: 8101, ready: true }));
1224+
mockExecuteRunnerCommandWithSession
1225+
.mockResolvedValueOnce({ message: 'first tap' })
1226+
.mockResolvedValueOnce({ message: 'second tap' })
1227+
.mockResolvedValueOnce({ message: 'recovered tap' });
1228+
1229+
assert.deepEqual(
1230+
await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }, { requestId }),
1231+
{ message: 'first tap' },
1232+
);
1233+
assert.deepEqual(
1234+
await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }, { requestId }),
1235+
{ message: 'second tap' },
1236+
);
1237+
assert.deepEqual(
1238+
await runAppleRunnerCommand(IOS_SIMULATOR, { command: 'tap', x: 120, y: 240 }, { requestId }),
1239+
{ message: 'recovered tap' },
1240+
);
1241+
1242+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 3);
1243+
});
1244+
12101245
test('a later command in the same request cannot pay for a second recycle boot', async () => {
12111246
const requestId = 'req-restart-cap';
12121247
const staleSession = makeRunnerSession({ port: 8100, ready: true });

‎src/platforms/apple/core/runner/runner-lifecycle.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,19 @@ export async function executeRunnerCommand(
252252
const signal = getRequestSignal(options.requestId);
253253
const recycleKey = runnerRecycleLedgerKey(options, command);
254254
let session: RunnerSession | undefined;
255+
let recycleBootBegun = false;
255256
try {
256257
// A request that already used a runner session and finds none alive is about to pay for
257258
// a recycle boot (~25s): bound that to the per-request recycle budget so a hostile screen
258259
// fails fast with a preserved session instead of stacking runner boots (#1105).
259-
if (
260-
!getRunnerSessionSnapshot(device.id)?.alive &&
261-
hasRunnerRequestTouchedSession(recycleKey) &&
262-
!tryBeginRunnerRecycle(recycleKey)
263-
) {
264-
throw buildRunnerRecycleBudgetExhaustedError(command, options);
260+
if (!getRunnerSessionSnapshot(device.id)?.alive && hasRunnerRequestTouchedSession(recycleKey)) {
261+
if (!tryBeginRunnerRecycle(recycleKey)) {
262+
throw buildRunnerRecycleBudgetExhaustedError(command, options);
263+
}
264+
recycleBootBegun = true;
265265
}
266266
session = await ensureRunnerSession(device, options);
267-
if (hasRunnerRequestTouchedSession(recycleKey)) {
267+
if (recycleBootBegun) {
268268
commitRunnerRecycle(recycleKey);
269269
}
270270
markRunnerRequestTouchedSession(recycleKey);

‎src/platforms/apple/core/runner/runner-session.ts‎

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -843,49 +843,58 @@ export async function parseRunnerResponse(
843843
session: Pick<RunnerSession, 'ready'>,
844844
logPath?: string,
845845
): Promise<Record<string, unknown>> {
846-
const text = await response.text();
847-
let json: RunnerResponsePayload;
848-
try {
849-
const parsed: unknown = JSON.parse(text);
850-
json = parsed && typeof parsed === 'object' ? (parsed as RunnerResponsePayload) : {};
851-
} catch {
852-
throw new AppError('COMMAND_FAILED', 'Invalid runner response', { text });
853-
}
846+
const json = parseRunnerResponsePayload(await response.text());
854847
if (!json.ok) {
855-
const rawCode = json.error?.code;
856-
const runnerErrorCode =
857-
typeof rawCode === 'string' && rawCode.trim().length > 0 ? rawCode.trim() : undefined;
858-
const errorCode =
859-
runnerErrorCode === 'RUNNER_BUSY'
860-
? 'COMMAND_FAILED'
861-
: runnerErrorCode
862-
? toAppErrorCode(runnerErrorCode)
863-
: 'COMMAND_FAILED';
864-
const errorMessage = typeof json.error?.message === 'string' ? json.error.message : undefined;
865-
const hint = typeof json.error?.hint === 'string' ? json.error.hint : undefined;
866848
throw await enrichRunnerFailureFromLog({
867-
error: new AppError(errorCode, errorMessage ?? 'Runner error', {
868-
runner: json,
869-
runnerErrorCode,
870-
retriable: runnerErrorCode === 'RUNNER_BUSY' ? true : undefined,
871-
xcodebuild: {
872-
exitCode: 1,
873-
stdout: '',
874-
stderr: '',
875-
},
876-
hint,
877-
logPath,
878-
}),
849+
error: buildRunnerResponseError(json, logPath),
879850
logPath,
880851
});
881852
}
882853
session.ready = true;
883-
if (json.data && typeof json.data === 'object' && !Array.isArray(json.data)) {
884-
const data = json.data as Record<string, unknown>;
885-
emitRunnerResponseDiagnostics(data);
886-
return data;
854+
return readRunnerResponseData(json);
855+
}
856+
857+
function parseRunnerResponsePayload(text: string): RunnerResponsePayload {
858+
try {
859+
const parsed: unknown = JSON.parse(text);
860+
return parsed && typeof parsed === 'object' ? (parsed as RunnerResponsePayload) : {};
861+
} catch {
862+
throw new AppError('COMMAND_FAILED', 'Invalid runner response', { text });
887863
}
888-
return {};
864+
}
865+
866+
function buildRunnerResponseError(json: RunnerResponsePayload, logPath?: string): AppError {
867+
const runnerErrorCode = readRunnerErrorCode(json.error?.code);
868+
const errorMessage = typeof json.error?.message === 'string' ? json.error.message : undefined;
869+
const hint = typeof json.error?.hint === 'string' ? json.error.hint : undefined;
870+
return new AppError(runnerAppErrorCode(runnerErrorCode), errorMessage ?? 'Runner error', {
871+
runner: json,
872+
runnerErrorCode,
873+
retriable: runnerErrorCode === 'RUNNER_BUSY' ? true : undefined,
874+
xcodebuild: {
875+
exitCode: 1,
876+
stdout: '',
877+
stderr: '',
878+
},
879+
hint,
880+
logPath,
881+
});
882+
}
883+
884+
function readRunnerErrorCode(rawCode: unknown): string | undefined {
885+
return typeof rawCode === 'string' && rawCode.trim().length > 0 ? rawCode.trim() : undefined;
886+
}
887+
888+
function runnerAppErrorCode(runnerErrorCode: string | undefined): AppError['code'] {
889+
if (runnerErrorCode === 'RUNNER_BUSY') return 'COMMAND_FAILED';
890+
return runnerErrorCode ? toAppErrorCode(runnerErrorCode) : 'COMMAND_FAILED';
891+
}
892+
893+
function readRunnerResponseData(json: RunnerResponsePayload): Record<string, unknown> {
894+
if (!json.data || typeof json.data !== 'object' || Array.isArray(json.data)) return {};
895+
const data = json.data as Record<string, unknown>;
896+
emitRunnerResponseDiagnostics(data);
897+
return data;
889898
}
890899

891900
function emitRunnerResponseDiagnostics(data: Record<string, unknown>): void {

0 commit comments

Comments
 (0)