@@ -4,9 +4,11 @@ import path from 'node:path';
44import { test , vi } from 'vitest' ;
55import { AppError , isRequestCanceledError } from '@agent-device/kernel/errors' ;
66import type { DeviceInfo } from '@agent-device/kernel/device' ;
7+ import type { DeadlineClock } from '@agent-device/host-kit/retry' ;
78import {
89 XCRUN_SHIM_TOOL_NAMES ,
910 type ArmedXcrunShimFirstLaunchHook ,
11+ type XcrunShimProbeOptions ,
1012 type XcrunShimToolName ,
1113} from '../../core/xcrun-shim-first-launch.ts' ;
1214import { appleRunnerTestHost } from '../test-host.ts' ;
@@ -65,32 +67,36 @@ function scopedSimulator(setPath: string): DeviceInfo {
6567 } ;
6668}
6769
68- async function acquire ( layout : Layout , host : FakeXcrunHost , signal ?: AbortSignal ) {
70+ async function acquire ( layout : Layout , host : FakeXcrunHost , budget : XcrunShimProbeOptions = { } ) {
6971 return await withFakeXcrunHost ( host , ( ) =>
7072 acquireXcodebuildSimulatorSetRedirect ( scopedSimulator ( layout . requestedSetPath ) , {
7173 xctestDeviceSetPath : layout . xctestDeviceSetPath ,
7274 backupPath : layout . backupPath ,
7375 lockDirPath : layout . lockDirPath ,
74- signal ,
76+ ... budget ,
7577 } ) ,
7678 ) ;
7779}
7880
7981async function assertRefused (
8082 layout : Layout ,
8183 host : FakeXcrunHost ,
82- signal ?: AbortSignal ,
84+ budget : XcrunShimProbeOptions = { } ,
85+ expected : 'armed' | 'request_canceled' | 'phase_budget_exhausted' = 'armed' ,
8386) : Promise < AppError > {
8487 let refusal : AppError | undefined ;
85- await assert . rejects ( acquire ( layout , host , signal ) , ( error : unknown ) => {
88+ await assert . rejects ( acquire ( layout , host , budget ) , ( error : unknown ) => {
8689 assert . ok ( error instanceof AppError ) ;
8790 refusal = error ;
8891 return true ;
8992 } ) ;
9093 assert . ok ( refusal ) ;
9194 assert . equal ( refusal . code , 'COMMAND_FAILED' ) ;
92- if ( signal ?. aborted ) {
95+ if ( expected === 'request_canceled' ) {
9396 assert . equal ( isRequestCanceledError ( refusal ) , true ) ;
97+ } else if ( expected === 'phase_budget_exhausted' ) {
98+ assert . equal ( refusal . details ?. reason , 'runner_phase_budget_exhausted' ) ;
99+ assert . equal ( refusal . details ?. xcrunShims , undefined ) ;
94100 } else {
95101 assert . equal ( refusal . details ?. reason , REASON ) ;
96102 assert . equal ( refusal . details ?. hint , ROW_HINT ) ;
@@ -248,7 +254,7 @@ test('an already-canceled request gives the lock back as a cancellation without
248254 devicectl : DEVICECTL_EQUAL ,
249255 } ) ;
250256
251- await assertRefused ( layout , host , AbortSignal . abort ( ) ) ;
257+ await assertRefused ( layout , host , { signal : AbortSignal . abort ( ) } , 'request_canceled' ) ;
252258
253259 assert . deepEqual ( host . finds , [ ] ) ;
254260 assert . deepEqual ( host . plistReads , [ ] ) ;
@@ -263,11 +269,29 @@ test('a request canceled while a shim is read gives the lock back as a cancellat
263269 const request = new AbortController ( ) ;
264270 host . onPlistRead = ( ) => request . abort ( ) ;
265271
266- await assertRefused ( layout , host , request . signal ) ;
272+ await assertRefused ( layout , host , { signal : request . signal } , 'request_canceled' ) ;
267273
268274 assert . notDeepEqual ( host . plistReads , [ ] , 'the probe was reading a shim when the request ended' ) ;
269275} ) ;
270276
277+ test ( 'a phase that runs out while a stalled xcrun is probed is the phase budget, not an armed shim' , async ( ) => {
278+ const layout = makeLayout ( ) ;
279+ const host = writeFakeXcrunShims ( layout . root , {
280+ simctl : SIMCTL_EQUAL ,
281+ devicectl : DEVICECTL_EQUAL ,
282+ } ) ;
283+ host . findStalls = true ;
284+ const deadline : DeadlineClock = {
285+ remainingMs : ( ) => 3 ,
286+ elapsedMs : ( ) => 0 ,
287+ isExpired : ( ) => false ,
288+ } ;
289+
290+ await assertRefused ( layout , host , { deadline } , 'phase_budget_exhausted' ) ;
291+
292+ assert . deepEqual ( host . finds , XCRUN_SHIM_TOOL_NAMES , 'the probe was waiting on xcrun' ) ;
293+ } ) ;
294+
271295test ( 'the message tells a shim xcrun could not locate from one the probe ran out of budget on' , async ( ) => {
272296 const messages : string [ ] = [ ] ;
273297 for ( const armedBy of [ 'shim_not_located' , 'probe_out_of_budget' ] as const ) {
@@ -276,7 +300,7 @@ test('the message tells a shim xcrun could not locate from one the probe ran out
276300 if ( armedBy === 'probe_out_of_budget' ) {
277301 appleRunnerTestHost . update ( {
278302 probeXcrunShimFirstLaunchHooks : async ( ) => ( {
279- canceled : false ,
303+ outcome : 'read' ,
280304 xcrunShims : XCRUN_SHIM_TOOL_NAMES . map ( ( tool ) : ArmedXcrunShimFirstLaunchHook => ( {
281305 tool,
282306 shimPath : null ,
0 commit comments