Skip to content

Commit 37ffe7c

Browse files
committed
fix(ios): take snapshot target identity on main and hop plan state writes
The capture plan runs on the command queue but read currentBundleId, currentAppProcessIdentifier and the penalty warm-up exemption, and wrote runnerAccessibilityHealth and invalidated the cached target directly. Snapshot preparation now takes a SnapshotCaptureTarget on main (app, bundle id, pid, consumed warm-up exemption) and the plan, the modal probe penalty, the XCTest penalty recorder and the private AX tier read only that copy. Health writes and the fail-closed invalidation go through applyMainOwnedSnapshotState, the abandoned-work-guarded main hop that invalidateCachedTargetAfterSnapshotFailure now also uses. Fixes #2781
1 parent b3b58ca commit 37ffe7c

10 files changed

Lines changed: 293 additions & 78 deletions

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ extension RunnerTests {
110110
/// remembered depth deliberately does NOT refresh the TTL, so expiry re-probes the full
111111
/// requested depth once per window instead of capping this screen class forever.
112112
func recordPrivateAXAcceptedDepth(
113+
bundleId: String?,
114+
processIdentifier: Int?,
113115
exactDepthRequested: Bool,
114116
effectiveDepth: Int,
115117
attemptDepths: [Int]
116118
) {
117119
guard !exactDepthRequested, effectiveDepth != attemptDepths.first else { return }
118120
rememberPrivateAXAcceptedDepth(
119-
bundleId: currentBundleId,
120-
processIdentifier: currentAppProcessIdentifier,
121+
bundleId: bundleId,
122+
processIdentifier: processIdentifier,
121123
depth: effectiveDepth
122124
)
123125
}
@@ -160,11 +162,12 @@ extension RunnerTests {
160162
}
161163

162164
func privateAXSnapshotAcquisition(
163-
app: XCUIApplication,
165+
target: SnapshotCaptureTarget,
164166
hint: CaptureHint,
165167
deadline: Date = .distantFuture
166168
) -> SnapshotAcquisition? {
167169
#if os(iOS) && targetEnvironment(simulator)
170+
let app = target.app
168171
let requestedDepth = hint.rawTraversalDepth ?? 64
169172
// An explicit --depth request is honored as asked: no accepted-depth
170173
// memory, no frontier extension past it.
@@ -173,8 +176,8 @@ extension RunnerTests {
173176
exactDepthRequested
174177
? nil
175178
: rememberedPrivateAXAcceptedDepth(
176-
bundleId: currentBundleId,
177-
processIdentifier: currentAppProcessIdentifier
179+
bundleId: target.bundleId,
180+
processIdentifier: target.processIdentifier
178181
)
179182
let attemptDepths = Self.privateAXAttemptDepths(
180183
requestedDepth: requestedDepth,
@@ -202,6 +205,8 @@ extension RunnerTests {
202205
return nil
203206
}
204207
recordPrivateAXAcceptedDepth(
208+
bundleId: target.bundleId,
209+
processIdentifier: target.processIdentifier,
205210
exactDepthRequested: exactDepthRequested,
206211
effectiveDepth: effectiveDepth,
207212
attemptDepths: attemptDepths
@@ -212,7 +217,11 @@ extension RunnerTests {
212217
}
213218

214219
let rootFrame = privateAXRect(root["frame"])
215-
let geometry = privateAXSnapshotGeometry(app: app, rootFrame: rootFrame)
220+
let geometry = privateAXSnapshotGeometry(
221+
app: app,
222+
bundleId: target.bundleId,
223+
rootFrame: rootFrame
224+
)
216225
let viewport = geometry.viewport
217226
let nodes = privateAXAcquisition(
218227
rawRoot: root,
@@ -262,8 +271,8 @@ extension RunnerTests {
262271
/// grinding on this screen class. Under penalty it reliably burns its full timeout and
263272
/// falls back anyway (~1s added to every private AX capture on the Bluesky bench feed),
264273
/// so honor the penalty here the same way capture plans do.
265-
func shouldReadPrivateAXViewportViaXCTest() -> Bool {
266-
!hasAbandonedMainThreadWork() && !isSnapshotXCTestChannelPenalized(bundleId: currentBundleId)
274+
func shouldReadPrivateAXViewportViaXCTest(bundleId: String?) -> Bool {
275+
!hasAbandonedMainThreadWork() && !isSnapshotXCTestChannelPenalized(bundleId: bundleId)
267276
}
268277

269278
/// The geometry this tier may anchor a rotation on. The bridge's own root frame is one more
@@ -272,10 +281,11 @@ extension RunnerTests {
272281
/// consumers already treat as geometry they cannot measure (#2612).
273282
private func privateAXSnapshotGeometry(
274283
app: XCUIApplication,
284+
bundleId: String?,
275285
rootFrame: CGRect
276286
) -> (viewport: CGRect, interfaceOrientation: Int) {
277287
let fallback = rootFrame.isEmpty ? CGRect.infinite : rootFrame
278-
guard shouldReadPrivateAXViewportViaXCTest() else {
288+
guard shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId) else {
279289
return (fallback, RunnerInterfaceOrientation.unknown)
280290
}
281291
do {
@@ -442,23 +452,22 @@ extension RunnerTests {
442452
func testViewportReadSkippedWhileXCTestChannelPenalized() {
443453
// Pins the viewport fast path (#1587 review): every penalized private AX capture used to burn
444454
// the full 1s main-thread timeout on a doomed viewport read before falling back.
445-
currentBundleId = "xyz.blueskyweb.app"
455+
let bundleId = "xyz.blueskyweb.app"
446456
defer {
447-
currentBundleId = nil
448457
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
449458
abandonedMainThreadWorkCount = 0
450459
}
451460

452-
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest())
461+
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
453462

454-
penalizeSnapshotXCTestChannel(bundleId: "xyz.blueskyweb.app", reason: "test")
455-
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest())
463+
penalizeSnapshotXCTestChannel(bundleId: bundleId, reason: "test")
464+
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
456465

457466
clearSnapshotXCTestChannelPenalty(reason: "test")
458-
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest())
467+
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
459468

460469
abandonedMainThreadWorkCount = 1
461-
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest())
470+
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
462471
}
463472

464473
/// The wire field must reach both capture options AND the backend pin: custom

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

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,17 +1171,25 @@ extension RunnerTests {
11711171
"command_preparation",
11721172
timeout: mainThreadExecutionTimeout,
11731173
timeoutError: mainThreadExecutionTimeoutError
1174-
) {
1175-
try self.prepareActiveCommandContextSafely(command: command, routeToSpringboard: false)
1174+
) { () -> SnapshotCommandPreparation in
1175+
switch try self.prepareActiveCommandContextSafely(command: command, routeToSpringboard: false) {
1176+
case .response(let response):
1177+
return .response(response)
1178+
case .context(let context):
1179+
return .capture(
1180+
self.takeSnapshotCaptureTarget(app: context.app),
1181+
systemSurface: context.systemSurface
1182+
)
1183+
}
11761184
}
11771185
switch preparation {
11781186
case .response(let response):
11791187
return response
1180-
case .context(let context):
1188+
case .capture(let target, let systemSurface):
11811189
return try executeSnapshotPrepared(
11821190
command: command,
1183-
activeApp: context.app,
1184-
systemSurface: context.systemSurface
1191+
target: target,
1192+
systemSurface: systemSurface
11851193
)
11861194
}
11871195
}
@@ -1206,16 +1214,16 @@ extension RunnerTests {
12061214

12071215
private func executeSnapshotPrepared(
12081216
command: Command,
1209-
activeApp: XCUIApplication,
1217+
target: SnapshotCaptureTarget,
12101218
systemSurface: SystemSurfaceHost?
12111219
) throws -> Response {
12121220
let options = Self.presentationOptions(from: command)
12131221
do {
12141222
var payload: DataPayload
12151223
if options.raw {
1216-
payload = try snapshotRaw(app: activeApp, options: options)
1224+
payload = try snapshotRaw(target: target, options: options)
12171225
} else {
1218-
payload = try snapshotFast(app: activeApp, options: options)
1226+
payload = try snapshotFast(target: target, options: options)
12191227
}
12201228
if let systemSurface {
12211229
payload.systemSurface = SystemSurfaceProvenancePayload(
@@ -1257,25 +1265,8 @@ extension RunnerTests {
12571265
}
12581266

12591267
private func invalidateCachedTargetAfterSnapshotFailure() {
1260-
// Abandoned work ahead of this hop cannot be cancelled: queue the drop behind it without
1261-
// waiting, so the failed capture answers now and the next command still finds the target gone.
1262-
guard !hasAbandonedMainThreadWork() else {
1263-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_INVALIDATION_DEFERRED_XCTEST_OCCUPIED")
1264-
DispatchQueue.main.async {
1265-
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
1266-
}
1267-
return
1268-
}
1269-
do {
1270-
try runMainThreadWork(
1271-
"target_invalidation",
1272-
timeout: 1,
1273-
timeoutError: mainThreadExecutionTimeoutError
1274-
) {
1275-
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
1276-
}
1277-
} catch {
1278-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_INVALIDATION_FAILED=%@", String(describing: error))
1268+
applyMainOwnedSnapshotState("target_invalidation") {
1269+
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
12791270
}
12801271
}
12811272

@@ -2509,7 +2500,8 @@ extension RunnerTests {
25092500
#endif
25102501
let probeDeadline = Date().addingTimeInterval(systemModalProbeBudget)
25112502
return boundedBlockingSystemAlertSnapshot(
2512-
deadline: probeDeadline
2503+
deadline: probeDeadline,
2504+
penaltyBundleId: currentBundleId
25132505
) != nil
25142506
#else
25152507
return false

0 commit comments

Comments
 (0)