Skip to content

Commit 092d21a

Browse files
thymikeeclaude
andcommitted
fix(ios): run the due XCTest recovery probe inside the deferred capture
Drop the post-response recovery probe: it started XCTest main-thread work after the snapshot had answered, so its abandoned tree work could refuse the next tap with RUNNER_BUSY while the snapshot's occupancy stamp said idle. The penalty is now one SnapshotXCTestChannelPenalty value (arm, clear, isProbeDue, claimProbe). When a probe is due, effectiveSnapshotCapturePlan puts the bounded tree ahead of private AX in the deferred plan, so any abandoned probe work is charged to that capture and its busy stamp. Restore the occupancy test's second-plan assertions that an armed penalty defers the next plan's XCTest tiers and starts no sweep query. Refs #3844 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0d6362d commit 092d21a

7 files changed

Lines changed: 224 additions & 215 deletions

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+AXSnapshotFallback.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extension RunnerTests {
132132
privateAXAcceptedDepthBundleId = bundleId
133133
privateAXAcceptedDepthProcessIdentifier = processIdentifier
134134
privateAXAcceptedDepth = depth
135-
privateAXAcceptedDepthUntil = Date().addingTimeInterval(snapshotXCTestChannelPenaltyDuration)
135+
privateAXAcceptedDepthUntil = Date().addingTimeInterval(SnapshotXCTestChannelPenalty.duration)
136136
privateAXAcceptedDepthLock.unlock()
137137
NSLog("AGENT_DEVICE_RUNNER_PRIVATE_AX_DEPTH_REMEMBERED depth=%ld bundle=%@", depth, bundleId ?? "")
138138
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCapturePlan.swift‎

Lines changed: 77 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -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,10 +189,7 @@ extension RunnerTests {
144189

145190
func penalizeSnapshotXCTestChannel(bundleId: String?, reason: String) {
146191
snapshotXCTestChannelPenaltyLock.lock()
147-
snapshotXCTestChannelPenaltyBundleId = bundleId
148-
let now = Date()
149-
snapshotXCTestChannelPenaltyUntil = now.addingTimeInterval(snapshotXCTestChannelPenaltyDuration)
150-
snapshotXCTestChannelNextRecoveryProbeAt = now.addingTimeInterval(Self.snapshotXCTestRecoveryProbeInterval)
192+
snapshotXCTestChannelPenalty.arm(bundleId: bundleId, at: Date())
151193
snapshotXCTestChannelPenaltyLock.unlock()
152194
NSLog(
153195
"AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_CHANNEL_PENALIZED bundle=%@ reason=%@",
@@ -158,10 +200,8 @@ extension RunnerTests {
158200

159201
func clearSnapshotXCTestChannelPenalty(reason: String) {
160202
snapshotXCTestChannelPenaltyLock.lock()
161-
let hadActivePenalty = Date() < snapshotXCTestChannelPenaltyUntil
162-
snapshotXCTestChannelPenaltyBundleId = nil
163-
snapshotXCTestChannelPenaltyUntil = Date.distantPast
164-
snapshotXCTestChannelNextRecoveryProbeAt = Date.distantFuture
203+
let hadActivePenalty = Date() < snapshotXCTestChannelPenalty.until
204+
snapshotXCTestChannelPenalty.clear()
165205
snapshotXCTestChannelPenaltyLock.unlock()
166206
if hadActivePenalty {
167207
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_CHANNEL_PENALTY_CLEARED reason=%@", reason)
@@ -171,10 +211,20 @@ extension RunnerTests {
171211
func isSnapshotXCTestChannelPenalized(bundleId: String?) -> Bool {
172212
snapshotXCTestChannelPenaltyLock.lock()
173213
defer { snapshotXCTestChannelPenaltyLock.unlock() }
174-
guard Date() < snapshotXCTestChannelPenaltyUntil else { return false }
175-
// A penalty recorded without a bundle id applies to whatever target is current.
176-
guard let penalized = snapshotXCTestChannelPenaltyBundleId else { return true }
177-
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 ?? "")
178228
}
179229

180230
/// The pre-seeded first-failure a penalized plan stamps into its verdict. The deferred case
@@ -247,11 +297,13 @@ extension RunnerTests {
247297
/// Pure plan-reorder rule: an internal preferred backend pins the regular plan to that backend;
248298
/// this is the only force seam used by same-backend evidence and conformance captures. A
249299
/// penalized XCTest accessibility channel uses independent backends when the platform has one,
250-
/// otherwise it keeps XCTest work on a short probe. The raw diagnostic plan keeps tree-first
251-
/// 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.
252303
static func effectiveSnapshotCapturePlan(
253304
_ plan: [SnapshotBackendKind],
254305
xCTestChannelPenalized: Bool,
306+
xCTestRecoveryProbeDue: Bool = false,
255307
availableBackends: Set<SnapshotBackendKind> = Set(SnapshotBackendKind.allCases),
256308
preferredBackend: String? = nil
257309
) -> EffectiveSnapshotCapturePlan {
@@ -281,10 +333,12 @@ extension RunnerTests {
281333
if !recoveryPlan.isEmpty {
282334
// The independent backend reads only the app it can match as the active AX application, so
283335
// an out-of-process surface over the app (the Save Password sheet) leaves it empty for the
284-
// whole penalty. The tree, on the bounded probe's short slice, stays behind it; the query
285-
// 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 }
286340
return EffectiveSnapshotCapturePlan(
287-
plan: recoveryPlan + availablePlan.filter { $0 == .recursiveTree },
341+
plan: xCTestRecoveryProbeDue ? boundedTree + recoveryPlan : recoveryPlan + boundedTree,
288342
xCTestChannelState: .deferredToIndependentBackend,
289343
treeCaptureSliceBudgetOverride: Self.penalizedXCTestProbeTreeSliceBudget,
290344
preferredBackend: nil
@@ -298,104 +352,6 @@ extension RunnerTests {
298352
)
299353
}
300354

301-
static let snapshotXCTestRecoveryProbeInterval: TimeInterval = 30
302-
303-
static func shouldScheduleSnapshotXCTestRecoveryProbe(
304-
effective: EffectiveSnapshotCapturePlan,
305-
acceptedBackend: SnapshotBackendKind,
306-
now: Date,
307-
nextProbeAt: Date,
308-
deadline: Date,
309-
hasAbandonedWork: Bool
310-
) -> Bool {
311-
effective.xCTestChannelState == .deferredToIndependentBackend
312-
&& effective.preferredBackend == nil
313-
&& effective.plan.contains(.recursiveTree)
314-
&& !acceptedBackend.usesXCTestAccessibilityChannel
315-
&& !hasAbandonedWork
316-
&& now >= nextProbeAt
317-
&& deadline.timeIntervalSince(now) >= Self.penalizedXCTestProbeTreeSliceBudget
318-
}
319-
320-
private func scheduleSnapshotXCTestRecoveryProbeIfNeeded(
321-
effective: EffectiveSnapshotCapturePlan,
322-
acceptedBackend: SnapshotBackendKind,
323-
target: SnapshotCaptureTarget,
324-
options: PresentationOptions,
325-
deadline: Date
326-
) {
327-
let abandoned = hasAbandonedMainThreadWork()
328-
let now = Date()
329-
snapshotXCTestChannelPenaltyLock.lock()
330-
let penaltyMatches = now < snapshotXCTestChannelPenaltyUntil
331-
&& (snapshotXCTestChannelPenaltyBundleId == nil || snapshotXCTestChannelPenaltyBundleId == target.bundleId)
332-
let scheduled = penaltyMatches && Self.shouldScheduleSnapshotXCTestRecoveryProbe(
333-
effective: effective,
334-
acceptedBackend: acceptedBackend,
335-
now: now,
336-
nextProbeAt: snapshotXCTestChannelNextRecoveryProbeAt,
337-
deadline: deadline,
338-
hasAbandonedWork: abandoned
339-
)
340-
if scheduled {
341-
snapshotXCTestChannelNextRecoveryProbeAt = .distantFuture
342-
}
343-
let penaltyUntil = snapshotXCTestChannelPenaltyUntil
344-
snapshotXCTestChannelPenaltyLock.unlock()
345-
guard scheduled else { return }
346-
commandExecutionQueue.async {
347-
self.snapshotXCTestChannelPenaltyLock.lock()
348-
let stillCurrent = self.snapshotXCTestChannelPenaltyUntil == penaltyUntil
349-
&& Date() < penaltyUntil
350-
if stillCurrent {
351-
self.snapshotXCTestChannelNextRecoveryProbeAt = Date().addingTimeInterval(Self.snapshotXCTestRecoveryProbeInterval)
352-
}
353-
self.snapshotXCTestChannelPenaltyLock.unlock()
354-
guard stillCurrent, !self.hasAbandonedMainThreadWork(),
355-
deadline.timeIntervalSinceNow >= Self.penalizedXCTestProbeTreeSliceBudget
356-
else { return }
357-
self.runScheduledSnapshotXCTestRecoveryProbe(
358-
target: target, options: options, channelState: effective.xCTestChannelState
359-
)
360-
}
361-
}
362-
363-
private func runScheduledSnapshotXCTestRecoveryProbe(
364-
target: SnapshotCaptureTarget,
365-
options: PresentationOptions,
366-
channelState: SnapshotXCTestChannelPlanState
367-
) {
368-
let now = Date()
369-
let slice = Self.penalizedXCTestProbeTreeSliceBudget
370-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_RECOVERY_PROBE_SCHEDULED bundle=%@", target.bundleId ?? "")
371-
defer {
372-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_XCTEST_RECOVERY_PROBE_FINISHED elapsedMs=%d", Int(Date().timeIntervalSince(now) * 1_000))
373-
}
374-
// One idle-only hop bounds the entire optional probe, including viewport reads and processing.
375-
// Its abandoned work uses the ordinary occupancy gate, but never arms a new penalty.
376-
let attempt = try? runMainThreadWorkIfIdle(
377-
"snapshot_recovery_probe",
378-
timeout: slice,
379-
timeoutError: snapshotMainThreadTimeoutError("probing XCTest recovery")
380-
) {
381-
try self.captureWithBackend(
382-
.recursiveTree,
383-
target: target,
384-
options: options,
385-
deadline: now.addingTimeInterval(slice),
386-
treeCaptureSliceBudgetOverride: slice
387-
)
388-
}
389-
guard let attempt else { return }
390-
recordXCTestSnapshotBackendAttemptIfNeeded(
391-
.recursiveTree,
392-
attempt: attempt,
393-
bundleId: target.bundleId,
394-
penaltySuppressed: false,
395-
channelState: channelState
396-
)
397-
}
398-
399355
func shouldSkipSnapshotBackendForAbandonedMainThreadWork(_ kind: SnapshotBackendKind) -> Bool {
400356
kind.usesXCTestAccessibilityChannel && hasAbandonedMainThreadWork()
401357
}
@@ -425,8 +381,10 @@ extension RunnerTests {
425381
// deliberately, so no degradation warning should render for it.
426382
var xCTestChannelPenalized = false
427383
var xCTestChannelPenalizedByBreaker = false
384+
var xCTestRecoveryProbeDue = false
428385
#if os(iOS)
429386
xCTestChannelPenalizedByBreaker = isSnapshotXCTestChannelPenalized(bundleId: target.bundleId)
387+
xCTestRecoveryProbeDue = isSnapshotXCTestRecoveryProbeDue(bundleId: target.bundleId)
430388
xCTestChannelPenalized = Self.snapshotXCTestChannelTreatedAsPenalized(
431389
penalized: xCTestChannelPenalizedByBreaker,
432390
preferredBackend: options.preferredBackend
@@ -435,6 +393,7 @@ extension RunnerTests {
435393
let effective = Self.effectiveSnapshotCapturePlan(
436394
plan,
437395
xCTestChannelPenalized: xCTestChannelPenalized,
396+
xCTestRecoveryProbeDue: xCTestRecoveryProbeDue,
438397
availableBackends: Set(SnapshotBackendKind.allCases.filter(\.isAvailableOnCurrentPlatform)),
439398
preferredBackend: options.preferredBackend
440399
)
@@ -475,6 +434,9 @@ extension RunnerTests {
475434
}
476435
continue
477436
}
437+
if kind == effectivePlan.first && effective.leadsWithXCTestRecoveryProbe {
438+
claimSnapshotXCTestRecoveryProbe(bundleId: target.bundleId)
439+
}
478440
if kind.usesXCTestAccessibilityChannel { xCTestTierRan = true }
479441
let attempt = try captureWithBackend(
480442
kind,
@@ -536,13 +498,6 @@ extension RunnerTests {
536498
verdictReason?.reason ?? "sparse tree"
537499
)
538500
}
539-
scheduleSnapshotXCTestRecoveryProbeIfNeeded(
540-
effective: effective,
541-
acceptedBackend: kind,
542-
target: target,
543-
options: options,
544-
deadline: deadline
545-
)
546501
return stampedSnapshotPayload(
547502
capture,
548503
backend: kind,

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotTiming.swift‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ extension RunnerTests {
123123
channelState: SnapshotXCTestChannelPlanState = .normal
124124
) -> String? {
125125
guard channelState == .normal, kind.usesXCTestAccessibilityChannel else { return nil }
126-
let isSlow = attempt.timing.acquisitionMs > slowThresholdMs
127126
if case let .failed(failure, phase: .acquisition) = attempt.outcome,
128127
failure.code == Self.xCTestSnapshotTimeoutCode
129128
{
@@ -132,7 +131,7 @@ extension RunnerTests {
132131
if attempt.tierOutcome == .deadlineExhausted {
133132
return "\(kind.rawValue)_backend_timeout"
134133
}
135-
guard isSlow else { return nil }
134+
guard attempt.timing.acquisitionMs > slowThresholdMs else { return nil }
136135
return "slow_\(kind.rawValue)_capture_\(Int(attempt.timing.acquisitionMs))ms"
137136
}
138137

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ final class RunnerTests: XCTestCase {
125125
// channel when an independent recovery backend exists, or use a bounded XCTest probe when it
126126
// does not, for the same screen class (#1105/#1156).
127127
let snapshotXCTestChannelPenaltyLock = NSLock()
128-
var snapshotXCTestChannelPenaltyBundleId: String?
129-
var snapshotXCTestChannelPenaltyUntil = Date.distantPast
130-
var snapshotXCTestChannelNextRecoveryProbeAt = Date.distantFuture
131-
let snapshotXCTestChannelPenaltyDuration: TimeInterval = 120
128+
var snapshotXCTestChannelPenalty = SnapshotXCTestChannelPenalty()
132129
let snapshotXCTestPenaltyWarmupExemption = SnapshotXCTestPenaltyWarmupExemption()
133130
// Sticky per-bundle hint for the private AX depth ladder: deep RN screens reject the default
134131
// depth with kAXErrorIllegalArgument on EVERY capture, so once a shallower rung is accepted

0 commit comments

Comments
 (0)