55 discloseDispatch ,
66 discloseUnclassifiedDispatch ,
77 isRequestCanceledError ,
8+ type DispatchDisclosure ,
89} from '@agent-device/kernel/errors' ;
910import type { DeviceInfo } from '@agent-device/kernel/device' ;
1011import 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 */
511513function 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