@@ -140,15 +140,22 @@ export function createAppleSnapshotRoute(
140140 [ unknownGenerationResidue ( ) ] ,
141141 ) ;
142142 }
143- if (
144- surfacePresence !== 'absent' &&
145- ! ( await isEverySurfaceHostInBackground ( device , input , signal , systemSurfaceForeground ) )
146- ) {
147- return await runSurfaceFallback ( device , input , fallback , surfacePresence . host . bundleId ) ;
143+ let lingeringHostTarget : SimulatorSnapshotTarget | undefined ;
144+ if ( surfacePresence !== 'absent' ) {
145+ lingeringHostTarget = await bridgeTargetWhileSurfaceHostRuns ( device , input , signal , {
146+ resolveTarget,
147+ isBridgeDisabled,
148+ probe : systemSurfaceForeground ,
149+ } ) ;
150+ if ( ! lingeringHostTarget ) {
151+ return await runSurfaceFallback ( device , input , fallback , surfacePresence . host . bundleId ) ;
152+ }
148153 }
149154 let target : SimulatorSnapshotTarget ;
150155 try {
151- target = await resolveTargetForObservation ( host , resolveTarget , device , input , signal ) ;
156+ target =
157+ lingeringHostTarget ??
158+ ( await resolveTargetForObservation ( host , resolveTarget , device , input , signal ) ) ;
152159 } catch ( error ) {
153160 rethrowIfResolutionCancelled ( signal , error ) ;
154161 emitRouteDiagnostic ( 'target-resolution-failed' , device , undefined , error ) ;
@@ -233,25 +240,40 @@ export function createAppleSnapshotRoute(
233240}
234241
235242/**
236- * Whether the live runner reports every registered host off screen, which is what lets a capture
237- * whose host process is only lingering keep the bridge. Only a positive read decides: anything the
238- * probe could not establish keeps the runner route that serves a presented surface.
243+ * The bridge target for a capture while a registered surface host process runs, or undefined when the
244+ * capture keeps the runner route that serves a presented surface. A host process outlives what it
245+ * presented, so only the live runner reading every registered host off screen admits the bridge, and
246+ * only for a generation the bridge still serves: a disabled one would pay the runner reads for a
247+ * capture that goes to the runner anyway. The target is resolved once, without waiting on a pending
248+ * discovery; any failure keeps the runner route, as before this refinement.
239249 */
240- async function isEverySurfaceHostInBackground (
250+ async function bridgeTargetWhileSurfaceHostRuns (
241251 device : DeviceInfo ,
242252 input : CaptureSnapshotInput ,
243253 signal : AbortSignal ,
244- probe : SystemSurfaceForegroundProbe ,
245- ) : Promise < boolean > {
254+ deps : Readonly < {
255+ resolveTarget : SimulatorSnapshotTargetResolver ;
256+ isBridgeDisabled : ( target : SimulatorSnapshotTarget ) => boolean ;
257+ probe : SystemSurfaceForegroundProbe ;
258+ } > ,
259+ ) : Promise < SimulatorSnapshotTarget | undefined > {
260+ let target : SimulatorSnapshotTarget ;
261+ try {
262+ target = await deps . resolveTarget ( device , input . options ! . appBundleId ! , signal ) ;
263+ } catch ( error ) {
264+ rethrowIfResolutionCancelled ( signal , error ) ;
265+ return undefined ;
266+ }
267+ if ( deps . isBridgeDisabled ( target ) ) return undefined ;
246268 const requestId = input . execution ?. requestId ;
247- const foreground = await probe ( device , { signal, ...( requestId ? { requestId } : { } ) } ) ;
248- if ( foreground !== 'background' ) return false ;
269+ const foreground = await deps . probe ( device , { signal, ...( requestId ? { requestId } : { } ) } ) ;
270+ if ( foreground !== 'background' ) return undefined ;
249271 emitDiagnostic ( {
250272 level : 'debug' ,
251273 phase : 'ios_snapshot_route_surface_host_background' ,
252- data : { deviceId : device . id } ,
274+ data : { deviceId : device . id , generation : target . generation } ,
253275 } ) ;
254- return true ;
276+ return target ;
255277}
256278
257279/**
0 commit comments