@@ -20,27 +20,35 @@ extension RunnerTests {
2020 return depths . filter { $0 <= remembered }
2121 }
2222
23- func rememberPrivateAXAcceptedDepth ( bundleId : String ? , depth : Int ) {
23+ func rememberPrivateAXAcceptedDepth ( bundleId : String ? , processIdentifier : Int ? , depth : Int ) {
24+ // No PID means no way to notice a relaunch later; record nothing rather than risk serving
25+ // a stale shallow rung to a fresh process.
26+ guard let processIdentifier else { return }
2427 privateAXAcceptedDepthLock . lock ( )
2528 privateAXAcceptedDepthBundleId = bundleId
29+ privateAXAcceptedDepthProcessIdentifier = processIdentifier
2630 privateAXAcceptedDepth = depth
2731 privateAXAcceptedDepthUntil = Date ( ) . addingTimeInterval ( snapshotXCTestChannelPenaltyDuration )
2832 privateAXAcceptedDepthLock . unlock ( )
2933 NSLog ( " AGENT_DEVICE_RUNNER_PRIVATE_AX_DEPTH_REMEMBERED depth=%ld bundle=%@ " , depth , bundleId ?? " " )
3034 }
3135
32- func rememberedPrivateAXAcceptedDepth ( bundleId : String ? ) -> Int ? {
36+ func rememberedPrivateAXAcceptedDepth ( bundleId : String ? , processIdentifier : Int ? ) -> Int ? {
3337 privateAXAcceptedDepthLock . lock ( )
3438 defer { privateAXAcceptedDepthLock . unlock ( ) }
3539 guard Date ( ) < privateAXAcceptedDepthUntil else { return nil }
3640 guard privateAXAcceptedDepthBundleId == bundleId else { return nil }
41+ guard let processIdentifier , privateAXAcceptedDepthProcessIdentifier == processIdentifier else {
42+ return nil
43+ }
3744 return privateAXAcceptedDepth
3845 }
3946
4047 func clearPrivateAXAcceptedDepth ( reason : String ) {
4148 privateAXAcceptedDepthLock . lock ( )
4249 let hadMemory = privateAXAcceptedDepth != nil && Date ( ) < privateAXAcceptedDepthUntil
4350 privateAXAcceptedDepthBundleId = nil
51+ privateAXAcceptedDepthProcessIdentifier = nil
4452 privateAXAcceptedDepth = nil
4553 privateAXAcceptedDepthUntil = . distantPast
4654 privateAXAcceptedDepthLock . unlock ( )
@@ -59,7 +67,12 @@ extension RunnerTests {
5967 // An explicit --depth request is honored as asked; only default-depth captures consult
6068 // (and feed) the accepted-depth memory.
6169 let rememberedDepth =
62- options . depth == nil ? rememberedPrivateAXAcceptedDepth ( bundleId : currentBundleId ) : nil
70+ options . depth == nil
71+ ? rememberedPrivateAXAcceptedDepth (
72+ bundleId : currentBundleId ,
73+ processIdentifier : currentAppProcessIdentifier
74+ )
75+ : nil
6376 let attemptDepths = Self . privateAXAttemptDepths (
6477 requestedDepth : requestedDepth ,
6578 rememberedDepth : rememberedDepth
@@ -99,7 +112,11 @@ extension RunnerTests {
99112 // remembered depth deliberately does NOT refresh the TTL, so expiry re-probes the full
100113 // requested depth once per window instead of capping this screen class forever.
101114 if options . depth == nil , effectiveDepth != attemptDepths . first {
102- rememberPrivateAXAcceptedDepth ( bundleId : currentBundleId , depth : effectiveDepth )
115+ rememberPrivateAXAcceptedDepth (
116+ bundleId : currentBundleId ,
117+ processIdentifier : currentAppProcessIdentifier ,
118+ depth : effectiveDepth
119+ )
103120 }
104121 guard let root = response [ " root " ] as? [ String : Any ] else {
105122 NSLog ( " AGENT_DEVICE_RUNNER_PRIVATE_AX_SNAPSHOT_FAILED=missing root " )
@@ -138,15 +155,17 @@ extension RunnerTests {
138155 #endif
139156 }
140157
158+ /// The viewport read is XCTest main-thread work — the exact channel the penalty marks as
159+ /// grinding on this screen class. Under penalty it reliably burns its full timeout and
160+ /// falls back anyway (~1s added to every private AX capture on the Bluesky bench feed),
161+ /// so honor the penalty here the same way capture plans do.
162+ func shouldReadPrivateAXViewportViaXCTest ( ) -> Bool {
163+ ! hasAbandonedTreeCapture ( ) && ! isSnapshotXCTestChannelPenalized ( bundleId : currentBundleId )
164+ }
165+
141166 private func privateAXSnapshotViewport ( app : XCUIApplication , rootFrame : CGRect ) -> CGRect {
142167 let fallback = rootFrame . isEmpty ? CGRect . infinite : rootFrame
143- // The viewport read is XCTest main-thread work — the exact channel the penalty marks as
144- // grinding on this screen class. Under penalty it reliably burns its full timeout and
145- // falls back anyway (~1s added to every private AX capture on the Bluesky bench feed),
146- // so honor the penalty here the same way capture plans do.
147- guard ! hasAbandonedTreeCapture ( ) ,
148- ! isSnapshotXCTestChannelPenalized ( bundleId : currentBundleId )
149- else {
168+ guard shouldReadPrivateAXViewportViaXCTest ( ) else {
150169 return fallback
151170 }
152171 do {
@@ -311,16 +330,54 @@ extension RunnerTests {
311330 XCTAssertEqual ( Self . privateAXAttemptDepths ( requestedDepth : 24 , rememberedDepth : 56 ) , [ 24 , 12 ] )
312331 }
313332
314- func testPrivateAXAcceptedDepthMemoryMatchesBundleAndExpires ( ) {
333+ func testPrivateAXAcceptedDepthMemoryMatchesBundleProcessAndExpires ( ) {
315334 defer { clearPrivateAXAcceptedDepth ( reason : " test-cleanup " ) }
316335
317- rememberPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , depth : 56 )
318- XCTAssertEqual ( rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " ) , 56 )
319- XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " com.other.app " ) )
336+ rememberPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : 111 , depth : 56 )
337+ XCTAssertEqual (
338+ rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : 111 ) ,
339+ 56
340+ )
341+ XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " com.other.app " , processIdentifier : 111 ) )
342+ // A relaunch changes the PID; the new process must re-probe the full depth even inside the
343+ // TTL, and an unknown current PID (post-invalidation) must never match.
344+ XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : 222 ) )
345+ XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : nil ) )
320346
321347 // Expired memory stops applying (the expiry re-probes the full requested depth).
322348 privateAXAcceptedDepthUntil = Date ( timeIntervalSinceNow : - 1 )
323- XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " ) )
349+ XCTAssertNil ( rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : 111 ) )
350+ }
351+
352+ func testPrivateAXAcceptedDepthMemoryRequiresProcessIdentifierToRecord ( ) {
353+ defer { clearPrivateAXAcceptedDepth ( reason : " test-cleanup " ) }
354+
355+ rememberPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : nil , depth : 56 )
356+ XCTAssertNil (
357+ rememberedPrivateAXAcceptedDepth ( bundleId : " xyz.blueskyweb.app " , processIdentifier : nil )
358+ )
359+ }
360+
361+ func testViewportReadSkippedWhileXCTestChannelPenalized ( ) {
362+ // Pins the viewport fast path (#1587 review): every penalized private AX capture used to burn
363+ // the full 1s main-thread timeout on a doomed viewport read before falling back.
364+ currentBundleId = " xyz.blueskyweb.app "
365+ defer {
366+ currentBundleId = nil
367+ clearSnapshotXCTestChannelPenalty ( reason : " test-cleanup " )
368+ abandonedTreeCaptureCount = 0
369+ }
370+
371+ XCTAssertTrue ( shouldReadPrivateAXViewportViaXCTest ( ) )
372+
373+ penalizeSnapshotXCTestChannel ( bundleId : " xyz.blueskyweb.app " , reason : " test " )
374+ XCTAssertFalse ( shouldReadPrivateAXViewportViaXCTest ( ) )
375+
376+ clearSnapshotXCTestChannelPenalty ( reason : " test " )
377+ XCTAssertTrue ( shouldReadPrivateAXViewportViaXCTest ( ) )
378+
379+ abandonedTreeCaptureCount = 1
380+ XCTAssertFalse ( shouldReadPrivateAXViewportViaXCTest ( ) )
324381 }
325382
326383 func testPrivateAXScopeSelectsSubtreeNotMatchingLabels ( ) {
0 commit comments