11import type { Point } from '@agent-device/kernel/snapshot' ;
2+ import { AppError , createRequestCanceledError } from '@agent-device/kernel/errors' ;
23import { presentIosSnapshotAcquisition } from '@agent-device/capture-kit/ios-snapshot-runtime' ;
34import { presentIosRunnerSnapshot } from '@agent-device/capture-kit/ios-snapshot-engine' ;
45import { buildSnapshotState } from '@agent-device/capture-kit/snapshot-state' ;
@@ -71,6 +72,7 @@ async function exercise(input: {
7172 transportFailure ?: boolean ;
7273 callerAbort ?: boolean ;
7374 nativeBudgetAbort ?: boolean ;
75+ nativeAbortError ?: 'canceled' | 'transport' ;
7476 expireInRead ?: boolean ;
7577 expireInRecheck ?: boolean ;
7678 resultDeadline ?: boolean ;
@@ -123,11 +125,21 @@ async function exercise(input: {
123125 context . signal ! . throwIfAborted ( ) ;
124126 }
125127 if ( input . nativeBudgetAbort && reads > 1 ) {
126- context . signal ! . throwIfAborted ( ) ;
127128 await new Promise < void > ( ( _resolve , reject ) => {
129+ context . signal ! . throwIfAborted ( ) ;
128130 context . signal ! . addEventListener ( 'abort' , ( ) => reject ( context . signal ! . reason ) , {
129131 once : true ,
130132 } ) ;
133+ } ) . catch ( ( error : unknown ) => {
134+ events . push ( 'native-budget-abort-rejected' ) ;
135+ if ( input . nativeAbortError === 'canceled' )
136+ throw createRequestCanceledError ( undefined , error ) ;
137+ if ( input . nativeAbortError === 'transport' )
138+ throw new AppError ( 'COMMAND_FAILED' , 'Native transport failed' , {
139+ reason : 'native-transport-failed' ,
140+ backendCode : 1234 ,
141+ } ) ;
142+ throw error ;
131143 } ) ;
132144 }
133145 const state = input . states [ Math . min ( reads - 1 , input . states . length - 1 ) ] ! ;
@@ -292,11 +304,35 @@ test('a final canonical recheck that crosses the absolute deadline cannot send i
292304 expect ( points ) . toEqual ( [ ] ) ;
293305} ) ;
294306
295- test ( 'a production result deadline preserves the last blocked native state' , async ( ) => {
307+ for ( const nativeAbortError of [ undefined , 'canceled' ] as const ) {
308+ test ( `a production result deadline preserves the last blocked native state after ${ nativeAbortError ?? 'raw' } native abort` , async ( ) => {
309+ const { run, events, points } = await exercise ( {
310+ states : [ 'blocked' ] ,
311+ resultDeadline : true ,
312+ nativeBudgetAbort : true ,
313+ nativeAbortError,
314+ timeoutMs : 150 ,
315+ } ) ;
316+ const failure = await run . then (
317+ ( ) => undefined ,
318+ ( error : unknown ) => error ,
319+ ) ;
320+ expect ( events ) . toContain ( 'blocked' ) ;
321+ expect ( events . indexOf ( 'readiness-aborted' ) ) . toBeGreaterThan ( events . indexOf ( 'blocked' ) ) ;
322+ expect ( events ) . toContain ( 'native-budget-abort-rejected' ) ;
323+ expect ( points ) . toEqual ( [ ] ) ;
324+ expect ( failure ) . toMatchObject ( {
325+ details : { reason : 'target_not_ready' , readiness : 'blocked' , dispatched : 'no' } ,
326+ } ) ;
327+ } ) ;
328+ }
329+
330+ test ( 'an unrelated typed native failure survives an owned budget abort' , async ( ) => {
296331 const { run, events, points } = await exercise ( {
297332 states : [ 'blocked' ] ,
298333 resultDeadline : true ,
299334 nativeBudgetAbort : true ,
335+ nativeAbortError : 'transport' ,
300336 timeoutMs : 150 ,
301337 } ) ;
302338 const failure = await run . then (
@@ -305,9 +341,12 @@ test('a production result deadline preserves the last blocked native state', asy
305341 ) ;
306342 expect ( events ) . toContain ( 'blocked' ) ;
307343 expect ( events . indexOf ( 'readiness-aborted' ) ) . toBeGreaterThan ( events . indexOf ( 'blocked' ) ) ;
344+ expect ( events ) . toContain ( 'native-budget-abort-rejected' ) ;
308345 expect ( points ) . toEqual ( [ ] ) ;
309346 expect ( failure ) . toMatchObject ( {
310- details : { reason : 'target_not_ready' , readiness : 'blocked' , dispatched : 'no' } ,
347+ code : 'COMMAND_FAILED' ,
348+ message : 'Native transport failed' ,
349+ details : { reason : 'native-transport-failed' , backendCode : 1234 , dispatched : 'no' } ,
311350 } ) ;
312351} ) ;
313352
0 commit comments