@@ -47,6 +47,45 @@ enum SnapshotXCTestChannelPlanState: Equatable {
4747 case boundedXCTestProbe
4848}
4949
50+ /// The XCTest accessibility channel penalty (#1105/#1156). While it is active, regular plans for its
51+ /// bundle defer XCTest tiers. Each recovery interval, one deferred plan runs the bounded tree probe
52+ /// first, inside that capture, so a screen that is light again can clear the penalty before it
53+ /// expires (#3844).
54+ struct SnapshotXCTestChannelPenalty : Equatable {
55+ static let duration : TimeInterval = 120
56+ static let recoveryProbeInterval : TimeInterval = 30
57+
58+ private( set) var bundleId : String ?
59+ private( set) var until = Date . distantPast
60+ private( set) var probeDueAt = Date . distantFuture
61+
62+ /// A penalty armed without a bundle id applies to whatever target is current.
63+ func isActive ( for bundleId : String ? , at now : Date ) -> Bool {
64+ guard now < until else { return false }
65+ guard let penalized = self . bundleId else { return true }
66+ return penalized == bundleId
67+ }
68+
69+ func isProbeDue ( for bundleId : String ? , at now : Date ) -> Bool {
70+ isActive ( for : bundleId , at : now ) && now >= probeDueAt
71+ }
72+
73+ mutating func arm ( bundleId : String ? , at now : Date ) {
74+ self . bundleId = bundleId
75+ until = now . addingTimeInterval ( Self . duration )
76+ probeDueAt = now . addingTimeInterval ( Self . recoveryProbeInterval )
77+ }
78+
79+ /// A started probe moves the next one a full interval out, whatever the probe finds.
80+ mutating func claimProbe ( at now : Date ) {
81+ probeDueAt = now . addingTimeInterval ( Self . recoveryProbeInterval )
82+ }
83+
84+ mutating func clear ( ) {
85+ self = SnapshotXCTestChannelPenalty ( )
86+ }
87+ }
88+
5089/// How one tier's bounded work ended. `deadlineExhausted` is a tier timeout: the tier stopped
5190/// starting work it could not finish inside its own slice, so what it returns is a partial result
5291/// it never completed collecting. The plan keeps that result only as the fallback and lets the next
@@ -64,6 +103,12 @@ struct EffectiveSnapshotCapturePlan {
64103 /// Non-nil only when the plan was narrowed by an explicit internal backend preference. This
65104 /// keeps the quality marker tied to the plan decision rather than to an untrusted request field.
66105 let preferredBackend : SnapshotBackendKind ?
106+
107+ /// A deferred plan that starts with an XCTest tier is running the due recovery probe.
108+ var leadsWithXCTestRecoveryProbe : Bool {
109+ xCTestChannelState == . deferredToIndependentBackend
110+ && plan . first ? . usesXCTestAccessibilityChannel == true
111+ }
67112}
68113
69114/// What the plan runner does when every backend failed or stayed sparse.
@@ -144,8 +189,7 @@ extension RunnerTests {
144189
145190 func penalizeSnapshotXCTestChannel ( bundleId : String ? , reason : String ) {
146191 snapshotXCTestChannelPenaltyLock . lock ( )
147- snapshotXCTestChannelPenaltyBundleId = bundleId
148- snapshotXCTestChannelPenaltyUntil = Date ( ) . addingTimeInterval ( snapshotXCTestChannelPenaltyDuration )
192+ snapshotXCTestChannelPenalty . arm ( bundleId : bundleId , at : Date ( ) )
149193 snapshotXCTestChannelPenaltyLock . unlock ( )
150194 NSLog (
151195 " AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_CHANNEL_PENALIZED bundle=%@ reason=%@ " ,
@@ -156,9 +200,8 @@ extension RunnerTests {
156200
157201 func clearSnapshotXCTestChannelPenalty ( reason : String ) {
158202 snapshotXCTestChannelPenaltyLock . lock ( )
159- let hadActivePenalty = Date ( ) < snapshotXCTestChannelPenaltyUntil
160- snapshotXCTestChannelPenaltyBundleId = nil
161- snapshotXCTestChannelPenaltyUntil = Date . distantPast
203+ let hadActivePenalty = Date ( ) < snapshotXCTestChannelPenalty . until
204+ snapshotXCTestChannelPenalty . clear ( )
162205 snapshotXCTestChannelPenaltyLock . unlock ( )
163206 if hadActivePenalty {
164207 NSLog ( " AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_CHANNEL_PENALTY_CLEARED reason=%@ " , reason )
@@ -168,10 +211,20 @@ extension RunnerTests {
168211 func isSnapshotXCTestChannelPenalized ( bundleId : String ? ) -> Bool {
169212 snapshotXCTestChannelPenaltyLock . lock ( )
170213 defer { snapshotXCTestChannelPenaltyLock . unlock ( ) }
171- guard Date ( ) < snapshotXCTestChannelPenaltyUntil else { return false }
172- // A penalty recorded without a bundle id applies to whatever target is current.
173- guard let penalized = snapshotXCTestChannelPenaltyBundleId else { return true }
174- return penalized == bundleId
214+ return snapshotXCTestChannelPenalty . isActive ( for : bundleId , at : Date ( ) )
215+ }
216+
217+ func isSnapshotXCTestRecoveryProbeDue ( bundleId : String ? ) -> Bool {
218+ snapshotXCTestChannelPenaltyLock . lock ( )
219+ defer { snapshotXCTestChannelPenaltyLock . unlock ( ) }
220+ return snapshotXCTestChannelPenalty . isProbeDue ( for : bundleId , at : Date ( ) )
221+ }
222+
223+ func claimSnapshotXCTestRecoveryProbe ( bundleId : String ? ) {
224+ snapshotXCTestChannelPenaltyLock . lock ( )
225+ snapshotXCTestChannelPenalty . claimProbe ( at : Date ( ) )
226+ snapshotXCTestChannelPenaltyLock . unlock ( )
227+ NSLog ( " AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_RECOVERY_PROBE bundle=%@ " , bundleId ?? " " )
175228 }
176229
177230 /// The pre-seeded first-failure a penalized plan stamps into its verdict. The deferred case
@@ -244,11 +297,13 @@ extension RunnerTests {
244297 /// Pure plan-reorder rule: an internal preferred backend pins the regular plan to that backend;
245298 /// this is the only force seam used by same-backend evidence and conformance captures. A
246299 /// penalized XCTest accessibility channel uses independent backends when the platform has one,
247- /// otherwise it keeps XCTest work on a short probe. The raw diagnostic plan keeps tree-first
248- /// errors, and unknown plans are left untouched.
300+ /// otherwise it keeps XCTest work on a short probe. A due recovery probe puts that short tree
301+ /// ahead of the independent backend. The raw diagnostic plan keeps tree-first errors, and unknown
302+ /// plans are left untouched.
249303 static func effectiveSnapshotCapturePlan (
250304 _ plan : [ SnapshotBackendKind ] ,
251305 xCTestChannelPenalized : Bool ,
306+ xCTestRecoveryProbeDue : Bool = false ,
252307 availableBackends : Set < SnapshotBackendKind > = Set ( SnapshotBackendKind . allCases ) ,
253308 preferredBackend : String ? = nil
254309 ) -> EffectiveSnapshotCapturePlan {
@@ -278,10 +333,12 @@ extension RunnerTests {
278333 if ! recoveryPlan . isEmpty {
279334 // The independent backend reads only the app it can match as the active AX application, so
280335 // an out-of-process surface over the app (the Save Password sheet) leaves it empty for the
281- // whole penalty. The tree, on the bounded probe's short slice, stays behind it; the query
282- // sweep does not, since its grind is what the penalty keeps off the main thread.
336+ // whole penalty. The tree, on the bounded probe's short slice, stays behind it, or leads when
337+ // the recovery probe is due; the query sweep does not, since its grind is what the penalty
338+ // keeps off the main thread.
339+ let boundedTree = availablePlan . filter { $0 == . recursiveTree }
283340 return EffectiveSnapshotCapturePlan (
284- plan : recoveryPlan + availablePlan . filter { $0 == . recursiveTree } ,
341+ plan : xCTestRecoveryProbeDue ? boundedTree + recoveryPlan : recoveryPlan + boundedTree ,
285342 xCTestChannelState : . deferredToIndependentBackend ,
286343 treeCaptureSliceBudgetOverride : Self . penalizedXCTestProbeTreeSliceBudget ,
287344 preferredBackend : nil
@@ -324,8 +381,10 @@ extension RunnerTests {
324381 // deliberately, so no degradation warning should render for it.
325382 var xCTestChannelPenalized = false
326383 var xCTestChannelPenalizedByBreaker = false
384+ var xCTestRecoveryProbeDue = false
327385#if os( iOS )
328386 xCTestChannelPenalizedByBreaker = isSnapshotXCTestChannelPenalized ( bundleId : target . bundleId )
387+ xCTestRecoveryProbeDue = isSnapshotXCTestRecoveryProbeDue ( bundleId : target . bundleId )
329388 xCTestChannelPenalized = Self . snapshotXCTestChannelTreatedAsPenalized (
330389 penalized : xCTestChannelPenalizedByBreaker ,
331390 preferredBackend : options . preferredBackend
@@ -334,6 +393,7 @@ extension RunnerTests {
334393 let effective = Self . effectiveSnapshotCapturePlan (
335394 plan ,
336395 xCTestChannelPenalized : xCTestChannelPenalized ,
396+ xCTestRecoveryProbeDue : xCTestRecoveryProbeDue ,
337397 availableBackends : Set ( SnapshotBackendKind . allCases . filter ( \. isAvailableOnCurrentPlatform ) ) ,
338398 preferredBackend : options . preferredBackend
339399 )
@@ -374,6 +434,9 @@ extension RunnerTests {
374434 }
375435 continue
376436 }
437+ if kind == effectivePlan . first && effective . leadsWithXCTestRecoveryProbe {
438+ claimSnapshotXCTestRecoveryProbe ( bundleId : target . bundleId )
439+ }
377440 if kind . usesXCTestAccessibilityChannel { xCTestTierRan = true }
378441 let attempt = try captureWithBackend (
379442 kind ,
@@ -386,7 +449,8 @@ extension RunnerTests {
386449 kind ,
387450 attempt : attempt ,
388451 bundleId : target . bundleId ,
389- penaltySuppressed : suppressXCTestPenalty
452+ penaltySuppressed : suppressXCTestPenalty ,
453+ channelState : effective . xCTestChannelState
390454 )
391455 if case let . failed ( failure , phase : _) = attempt . outcome {
392456 if Self . isAxSnapshotFailure ( failure ) { axFailure = failure }
0 commit comments