Skip to content

Commit 2ffd18a

Browse files
committed
fix(apple-runner): decide the restart resend in the connect-failure classifier
shouldRestartRunnerBeforeCommandSend now takes the command and owns the write-proof rule: a connect-loop failure restarts and resends only when no attempt could have written the command, or the command is read-only. An exit-28 POST that carries dispatched unknown no longer restarts a mutation, whatever call site asks. The call-site guard in executeRunnerCommandAttempt is gone.
1 parent 4c9c1df commit 2ffd18a

4 files changed

Lines changed: 62 additions & 27 deletions

File tree

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
shouldRestartRunnerBeforeCommandSend,
1616
shouldRetryRunnerConnectError,
1717
} from '../runner-error-classification.ts';
18-
import { runnerConnectFailure } from './runner-session-fixtures.ts';
18+
import { runnerConnectFailure, unwrittenConnectRefusal } from './runner-session-fixtures.ts';
19+
20+
const TAP = { command: 'tap' } as const;
21+
const SNAPSHOT = { command: 'snapshot' } as const;
1922

2023
function commandFailed(message: string, details?: Record<string, unknown>): AppError {
2124
return new AppError('COMMAND_FAILED', message, details);
@@ -165,7 +168,7 @@ test('a deadline on its own earns no recovery verdict', () => {
165168
timeoutMs: 45_000,
166169
});
167170
assert.equal(isRetryableRunnerError(deadline), false);
168-
assert.equal(shouldRestartRunnerBeforeCommandSend(deadline), false);
171+
assert.equal(shouldRestartRunnerBeforeCommandSend(deadline, SNAPSHOT), false);
169172
assert.equal(shouldRestartRunnerAfterReadinessPreflight(deadline), false);
170173
assert.equal(shouldRebuildCachedRunnerArtifact(deadline), false);
171174
assert.equal(shouldRetryRunnerConnectError(deadline), true);
@@ -244,21 +247,28 @@ test('ordinary errors are never session-fatal', () => {
244247
// --- restart-before-send axis (shouldRestartRunnerBeforeCommandSend) ---
245248

246249
test('a refused connection before send restarts the session', () => {
247-
assert.equal(
248-
shouldRestartRunnerBeforeCommandSend(
249-
runnerConnectFailure('runner_connect_refused', 'Runner did not accept connection'),
250-
),
251-
true,
252-
);
250+
assert.equal(shouldRestartRunnerBeforeCommandSend(unwrittenConnectRefusal(), TAP), true);
251+
});
252+
253+
test('a connect failure whose POST may have been written restarts only a read', () => {
254+
// simctl curl exit 28: the POST left, then the request timed out.
255+
const writtenThenTimedOut = runnerConnectFailure('runner_connect_refused', undefined, {
256+
dispatched: 'unknown',
257+
});
258+
assert.equal(shouldRestartRunnerBeforeCommandSend(writtenThenTimedOut, TAP), false);
259+
assert.equal(shouldRestartRunnerBeforeCommandSend(writtenThenTimedOut, SNAPSHOT), true);
253260
});
254261

255262
test('an early exit or a foreign transport failure earns no restart before send', () => {
256263
const earlyExit = runnerConnectFailure(
257264
'xcodebuild_exited_early',
258265
'xcodebuild exited early: runner did not accept connection',
259266
);
260-
assert.equal(shouldRestartRunnerBeforeCommandSend(earlyExit), false);
261-
assert.equal(shouldRestartRunnerBeforeCommandSend(commandFailed('socket hang up')), false);
267+
assert.equal(shouldRestartRunnerBeforeCommandSend(earlyExit, SNAPSHOT), false);
268+
assert.equal(
269+
shouldRestartRunnerBeforeCommandSend(commandFailed('socket hang up'), SNAPSHOT),
270+
false,
271+
);
262272
});
263273

264274
// --- typed connect-failure reasons (agent-device's own connect path) ---
@@ -269,7 +279,7 @@ test('xcodebuild_exited_early is decided by the typed reason, not the message',
269279
assert.equal(isRetryableRunnerError(error), false, message);
270280
assert.equal(shouldRetryRunnerConnectError(error), false, message);
271281
assert.equal(shouldRebuildCachedRunnerArtifact(error), false, message);
272-
assert.equal(shouldRestartRunnerBeforeCommandSend(error), false, message);
282+
assert.equal(shouldRestartRunnerBeforeCommandSend(error, SNAPSHOT), false, message);
273283
}
274284
// The same words without the reason earn no terminal verdict.
275285
const untyped = commandFailed('Runner did not accept connection (xcodebuild exited early)');
@@ -282,20 +292,20 @@ test('runner_connect_refused is decided by the typed reason, not the message', (
282292
assert.equal(isRetryableRunnerError(error), true, message);
283293
assert.equal(shouldRetryRunnerConnectError(error), true, message);
284294
assert.equal(shouldRebuildCachedRunnerArtifact(error), true, message);
285-
assert.equal(shouldRestartRunnerBeforeCommandSend(error), true, message);
295+
assert.equal(shouldRestartRunnerBeforeCommandSend(error, SNAPSHOT), true, message);
286296
}
287297
const untyped = commandFailed('Runner did not accept connection');
288298
assert.equal(isRetryableRunnerError(untyped), false);
289299
assert.equal(shouldRebuildCachedRunnerArtifact(untyped), false);
290-
assert.equal(shouldRestartRunnerBeforeCommandSend(untyped), false);
300+
assert.equal(shouldRestartRunnerBeforeCommandSend(untyped, SNAPSHOT), false);
291301
});
292302

293303
test('runner_endpoint_probe_exhausted is decided by the typed reason, not the message', () => {
294304
for (const message of ['Runner endpoint probe failed', 'boom']) {
295305
const error = runnerConnectFailure('runner_endpoint_probe_exhausted', message);
296306
assert.equal(shouldRebuildCachedRunnerArtifact(error), true, message);
297307
assert.equal(isRetryableRunnerError(error), false, message);
298-
assert.equal(shouldRestartRunnerBeforeCommandSend(error), false, message);
308+
assert.equal(shouldRestartRunnerBeforeCommandSend(error, SNAPSHOT), false, message);
299309
assert.equal(shouldRetryRunnerConnectError(error), true, message);
300310
}
301311
assert.equal(

‎packages/platform-apple/src/runner/__tests__/runner-lifecycle-dispatch-disclosure.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ for (const row of ROWS) {
254254
});
255255
}
256256

257+
test('a mutation whose connect-loop POST timed out after writing is not restarted or resent', async () => {
258+
stubConnectLoopFailure(writtenThenLost);
259+
mockEnsureRunnerSession.mockResolvedValueOnce(makeRunnerSession());
260+
mockExecuteRunnerCommandWithSession.mockRejectedValue(
261+
new AppError('COMMAND_FAILED', 'status probe failed'),
262+
);
263+
await assert.rejects(tap(), (error: unknown) => {
264+
assert.ok(error instanceof AppError);
265+
assert.equal(error.details?.runnerRestarted, undefined);
266+
assert.equal(error.details?.dispatched, 'unknown');
267+
return true;
268+
});
269+
assert.equal(mockEnsureRunnerSession.mock.calls.length, 1, 'the runner is not restarted');
270+
const taps = mockExecuteRunnerCommandWithSession.mock.calls.filter(
271+
([, , command]) => command.command === 'tap',
272+
);
273+
assert.equal(taps.length, 1, 'the tap is sent once');
274+
});
275+
257276
test('a plain Error before the exchange is normalized and discloses no', async () => {
258277
mockEnsureRunnerSession.mockRejectedValueOnce(new Error('spawn EACCES'));
259278
await assert.rejects(tap(), (error: unknown) => {

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
MAIN_THREAD_TIMEOUT_RUNNER_CODE,
1414
RUNNER_BUSY_RUNNER_CODE,
1515
RUNNER_WEDGED_RUNNER_CODE,
16+
type RunnerCommand,
1617
} from './runner-contract.ts';
18+
import { isReadOnlyRunnerCommand } from './runner-command-traits.ts';
1719

1820
export const RUNNER_CACHE_RECOVERY_HINT =
1921
'If runner build products look stale or corrupted, run `pnpm clean:xcuitest` in a local checkout, or remove ~/.agent-device/apple-runner/derived, then retry.';
@@ -610,11 +612,21 @@ export function resolveRunnerFatalErrorReason(error: unknown): string | undefine
610612
}
611613

612614
/**
613-
* A connect-shaped failure that surfaced before the command was sent: restart
614-
* the runner session and replay the command, rather than probing a runner
615-
* that never accepted the connection.
615+
* A connect-shaped failure that lets the session restart and resend `command`, rather than probing a
616+
* runner that never accepted the connection. The connect loop posts the command on every attempt, so
617+
* its failure restarts only when no attempt could have written the command, or when the command is
618+
* read-only: a POST that timed out after it was written (simctl curl exit 28) is no proof the
619+
* command did not run, and a mutation is never resent on it.
616620
*/
617-
export function shouldRestartRunnerBeforeCommandSend(error: unknown): boolean {
621+
export function shouldRestartRunnerBeforeCommandSend(
622+
error: unknown,
623+
command: RunnerCommand,
624+
): boolean {
625+
if (!isRunnerConnectRefusal(error)) return false;
626+
return isRunnerCommandProvablyUnwritten(error) || isReadOnlyRunnerCommand(command);
627+
}
628+
629+
function isRunnerConnectRefusal(error: unknown): boolean {
618630
return runnerErrorVerdict(error, 'restartBeforeSend') ?? false;
619631
}
620632

@@ -625,7 +637,7 @@ export function shouldRestartRunnerBeforeCommandSend(error: unknown): boolean {
625637
*/
626638
export function isRunnerPreSendRefusal(error: unknown): boolean {
627639
return (
628-
(shouldRestartRunnerBeforeCommandSend(error) && isRunnerCommandProvablyUnwritten(error)) ||
640+
(isRunnerConnectRefusal(error) && isRunnerCommandProvablyUnwritten(error)) ||
629641
shouldRestartRunnerAfterReadinessPreflight(error) ||
630642
isRunnerBusyError(error)
631643
);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import type {
4343
} from './runner-provider.ts';
4444
import { markRunnerXctestrunArtifactBadForRun } from './runner-xctestrun.ts';
4545
import { handleRunnerTransportErrorAfterCommandSend } from './runner-command-recovery.ts';
46-
import { isReadOnlyRunnerCommand } from './runner-command-traits.ts';
4746
import {
4847
buildRunnerRecycleBudgetExhaustedError,
4948
commitRunnerRecycle,
@@ -352,12 +351,7 @@ async function executeRunnerCommandAttempt(
352351
appErr,
353352
);
354353
}
355-
const firstAttemptUnwritten = isRunnerPreSendRefusal(appErr);
356-
if (
357-
shouldRestartRunnerBeforeCommandSend(appErr) &&
358-
session &&
359-
(firstAttemptUnwritten || isReadOnlyRunnerCommand(command))
360-
) {
354+
if (shouldRestartRunnerBeforeCommandSend(appErr, command) && session) {
361355
assertRunnerRequestActive(options.requestId);
362356
return await restartSessionAndRunCommand({
363357
device,
@@ -366,7 +360,7 @@ async function executeRunnerCommandAttempt(
366360
options,
367361
signal,
368362
restartReason: 'runner_connect_failed_before_command_send',
369-
firstAttemptUnwritten,
363+
firstAttemptUnwritten: isRunnerPreSendRefusal(appErr),
370364
});
371365
}
372366
if (session && shouldRestartRunnerAfterReadinessPreflight(appErr)) {

0 commit comments

Comments
 (0)