Skip to content

Commit 66ef675

Browse files
committed
refactor(apple-runner): carry the first attempt's dispatch disclosure through the restart
restartSessionAndRunCommand takes firstAttemptDispatched: DispatchDisclosure instead of a boolean that three ternaries decoded back into no or unknown. resolveFirstAttemptDispatch computes it once beside isRunnerCommandProvablyUnwritten, and the restart rule reads the same answer. The readiness-preflight call site passes no.
1 parent 2ffd18a commit 66ef675

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,22 @@ test('a mutation whose connect-loop POST timed out after writing is not restarte
273273
assert.equal(taps.length, 1, 'the tap is sent once');
274274
});
275275

276+
test('a read whose first POST may have been written and whose restart fails discloses unknown', async () => {
277+
stubConnectLoopFailure(writtenThenLost);
278+
mockEnsureRunnerSession
279+
.mockResolvedValueOnce(makeRunnerSession())
280+
.mockRejectedValueOnce(new AppError('COMMAND_FAILED', 'runner restart failed'));
281+
await assert.rejects(
282+
runAppleRunnerCommand(IOS_SIMULATOR, { command: 'snapshot' }),
283+
(error: unknown) => {
284+
assert.ok(error instanceof AppError);
285+
assert.equal(error.details?.runnerRestartReason, 'runner_connect_failed_before_command_send');
286+
assert.equal(error.details?.dispatched, 'unknown');
287+
return true;
288+
},
289+
);
290+
});
291+
276292
test('a plain Error before the exchange is normalized and discloses no', async () => {
277293
mockEnsureRunnerSession.mockRejectedValueOnce(new Error('spawn EACCES'));
278294
await assert.rejects(tap(), (error: unknown) => {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
isRequestCanceledDetails,
44
type AppErrorCode,
55
type AppErrorDetails,
6+
type DispatchDisclosure,
67
} from '@agent-device/kernel/errors';
78
import {
89
isCommandTimeoutError,
@@ -623,7 +624,7 @@ export function shouldRestartRunnerBeforeCommandSend(
623624
command: RunnerCommand,
624625
): boolean {
625626
if (!isRunnerConnectRefusal(error)) return false;
626-
return isRunnerCommandProvablyUnwritten(error) || isReadOnlyRunnerCommand(command);
627+
return resolveFirstAttemptDispatch(error) === 'no' || isReadOnlyRunnerCommand(command);
627628
}
628629

629630
function isRunnerConnectRefusal(error: unknown): boolean {
@@ -653,6 +654,11 @@ export function isRunnerCommandProvablyUnwritten(error: unknown): boolean {
653654
return isConnectionRefused(error, 0);
654655
}
655656

657+
/** What a failed connect attempt proves about writing the command: `no` only with that proof. */
658+
export function resolveFirstAttemptDispatch(error: unknown): DispatchDisclosure {
659+
return isRunnerCommandProvablyUnwritten(error) ? 'no' : 'unknown';
660+
}
661+
656662
function isConnectionRefused(error: unknown, depth: number): boolean {
657663
if (depth > 4 || typeof error !== 'object' || error === null) return false;
658664
if ((error as { code?: unknown }).code === 'ECONNREFUSED') return true;

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
discloseDispatch,
66
discloseUnclassifiedDispatch,
77
isRequestCanceledError,
8+
type DispatchDisclosure,
89
} from '@agent-device/kernel/errors';
910
import type { DeviceInfo } from '@agent-device/kernel/device';
1011
import type { ReadinessPhase } from '@agent-device/contracts/wait';
@@ -31,6 +32,7 @@ import {
3132
isRetryableRunnerError,
3233
isRunnerPreSendRefusal,
3334
isStructuredRunnerFailure,
35+
resolveFirstAttemptDispatch,
3436
shouldRebuildCachedRunnerArtifact,
3537
shouldRestartRunnerAfterReadinessPreflight,
3638
shouldRestartRunnerBeforeCommandSend,
@@ -360,7 +362,7 @@ async function executeRunnerCommandAttempt(
360362
options,
361363
signal,
362364
restartReason: 'runner_connect_failed_before_command_send',
363-
firstAttemptUnwritten: isRunnerPreSendRefusal(appErr),
365+
firstAttemptDispatched: resolveFirstAttemptDispatch(appErr),
364366
});
365367
}
366368
if (session && shouldRestartRunnerAfterReadinessPreflight(appErr)) {
@@ -373,7 +375,7 @@ async function executeRunnerCommandAttempt(
373375
signal,
374376
restartReason: 'runner_readiness_preflight_failed_before_command_send',
375377
recoveredDiagnosticPhase: 'ios_runner_readiness_preflight_recovered',
376-
firstAttemptUnwritten: true,
378+
firstAttemptDispatched: 'no',
377379
});
378380
}
379381
// Status recovery answers "did the command I lost the response to run?". A structured reply
@@ -406,10 +408,10 @@ async function restartSessionAndRunCommand(params: {
406408
| 'runner_readiness_preflight_failed_before_command_send';
407409
recoveredDiagnosticPhase?: string;
408410
/**
409-
* The failed first attempt provably never wrote the command. When it may have, the replay can
410-
* double-send, and no failure of this restart may claim `no`.
411+
* What the failed first attempt disclosed about writing the command. After `unknown`, the replay
412+
* can double-send, and no failure of this restart may claim `no`.
411413
*/
412-
firstAttemptUnwritten: boolean;
414+
firstAttemptDispatched: DispatchDisclosure;
413415
}): Promise<Record<string, unknown>> {
414416
const { device, command, options, signal, restartReason } = params;
415417
// At most one recycle per request: when the budget is spent, fail fast and KEEP the current
@@ -419,7 +421,7 @@ async function restartSessionAndRunCommand(params: {
419421
if (!tryBeginRunnerRecycle(recycleKey)) {
420422
throw discloseDispatch(
421423
buildRunnerRecycleBudgetExhaustedError(command, options),
422-
params.firstAttemptUnwritten ? 'no' : 'unknown',
424+
params.firstAttemptDispatched,
423425
);
424426
}
425427
await invalidateRunnerSession(params.session, restartReason);
@@ -478,7 +480,7 @@ function markRunnerRestartError(
478480
error: unknown,
479481
params: Pick<
480482
Parameters<typeof restartSessionAndRunCommand>[0],
481-
'session' | 'command' | 'options' | 'restartReason' | 'firstAttemptUnwritten'
483+
'session' | 'command' | 'options' | 'restartReason' | 'firstAttemptDispatched'
482484
>,
483485
restartedSession?: RunnerSession,
484486
): unknown {
@@ -500,7 +502,7 @@ function markRunnerRestartError(
500502
},
501503
error.cause ?? error,
502504
);
503-
return discloseRestartDispatch(marked, params.firstAttemptUnwritten, restartedSession);
505+
return discloseRestartDispatch(marked, params.firstAttemptDispatched, restartedSession);
504506
}
505507

506508
/**
@@ -510,11 +512,12 @@ function markRunnerRestartError(
510512
*/
511513
function discloseRestartDispatch(
512514
error: AppError,
513-
firstAttemptUnwritten: boolean,
515+
firstAttemptDispatched: DispatchDisclosure,
514516
restartedSession: RunnerSession | undefined,
515517
): AppError {
516-
if (!restartedSession) return discloseDispatch(error, firstAttemptUnwritten ? 'no' : 'unknown');
517-
if (!firstAttemptUnwritten) return discloseDispatch(error, 'unknown');
518+
if (!restartedSession || firstAttemptDispatched === 'unknown') {
519+
return discloseDispatch(error, firstAttemptDispatched);
520+
}
518521
return discloseUnclassifiedDispatch(error, isRunnerPreSendRefusal(error) ? 'no' : 'unknown');
519522
}
520523

0 commit comments

Comments
 (0)