Skip to content

Commit fc1e04c

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 3395242 commit fc1e04c

10 files changed

Lines changed: 297 additions & 83 deletions

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ extension RunnerTests {
114114
/// remembered depth deliberately does NOT refresh the TTL, so expiry re-probes the full
115115
/// requested depth once per window instead of capping this screen class forever.
116116
func recordPrivateAXAcceptedDepth(
117+
bundleId: String?,
118+
processIdentifier: Int?,
117119
exactDepthRequested: Bool,
118120
effectiveDepth: Int,
119121
attemptDepths: [Int]
120122
) {
121123
guard !exactDepthRequested, effectiveDepth != attemptDepths.first else { return }
122124
rememberPrivateAXAcceptedDepth(
123-
bundleId: currentBundleId,
124-
processIdentifier: currentAppProcessIdentifier,
125+
bundleId: bundleId,
126+
processIdentifier: processIdentifier,
125127
depth: effectiveDepth
126128
)
127129
}
@@ -164,11 +166,12 @@ extension RunnerTests {
164166
}
165167

166168
func privateAXSnapshotAcquisition(
167-
app: XCUIApplication,
169+
target: SnapshotCaptureTarget,
168170
hint: CaptureHint,
169171
deadline: Date = .distantFuture
170172
) -> SnapshotAcquisition? {
171173
#if os(iOS) && targetEnvironment(simulator)
174+
let app = target.app
172175
let requestedDepth = hint.rawTraversalDepth ?? 64
173176
// An explicit --depth request is honored as asked: no accepted-depth
174177
// memory, no frontier extension past it.
@@ -177,8 +180,8 @@ extension RunnerTests {
177180
exactDepthRequested
178181
? nil
179182
: rememberedPrivateAXAcceptedDepth(
180-
bundleId: currentBundleId,
181-
processIdentifier: currentAppProcessIdentifier
183+
bundleId: target.bundleId,
184+
processIdentifier: target.processIdentifier
182185
)
183186
let attemptDepths = Self.privateAXAttemptDepths(
184187
requestedDepth: requestedDepth,
@@ -206,6 +209,8 @@ extension RunnerTests {
206209
return nil
207210
}
208211
recordPrivateAXAcceptedDepth(
212+
bundleId: target.bundleId,
213+
processIdentifier: target.processIdentifier,
209214
exactDepthRequested: exactDepthRequested,
210215
effectiveDepth: effectiveDepth,
211216
attemptDepths: attemptDepths
@@ -216,7 +221,11 @@ extension RunnerTests {
216221
}
217222

218223
let rootFrame = privateAXRect(root["frame"])
219-
let geometry = privateAXSnapshotGeometry(app: app, rootFrame: rootFrame)
224+
let geometry = privateAXSnapshotGeometry(
225+
app: app,
226+
bundleId: target.bundleId,
227+
rootFrame: rootFrame
228+
)
220229
let viewport = geometry.viewport
221230
let nodes = privateAXAcquisition(
222231
rawRoot: root,
@@ -266,8 +275,8 @@ extension RunnerTests {
266275
/// grinding on this screen class. Under penalty it reliably burns its full timeout and
267276
/// falls back anyway (~1s added to every private AX capture on the Bluesky bench feed),
268277
/// so honor the penalty here the same way capture plans do.
269-
func shouldReadPrivateAXViewportViaXCTest() -> Bool {
270-
!hasAbandonedMainThreadWork() && !isSnapshotXCTestChannelPenalized(bundleId: currentBundleId)
278+
func shouldReadPrivateAXViewportViaXCTest(bundleId: String?) -> Bool {
279+
!hasAbandonedMainThreadWork() && !isSnapshotXCTestChannelPenalized(bundleId: bundleId)
271280
}
272281

273282
/// The geometry this tier may anchor a rotation on. The bridge's own root frame is one more
@@ -276,10 +285,11 @@ extension RunnerTests {
276285
/// consumers already treat as geometry they cannot measure (#2612).
277286
private func privateAXSnapshotGeometry(
278287
app: XCUIApplication,
288+
bundleId: String?,
279289
rootFrame: CGRect
280290
) -> (viewport: CGRect, interfaceOrientation: Int) {
281291
let fallback = rootFrame.isEmpty ? CGRect.infinite : rootFrame
282-
guard shouldReadPrivateAXViewportViaXCTest() else {
292+
guard shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId) else {
283293
return (fallback, RunnerInterfaceOrientation.unknown)
284294
}
285295
do {
@@ -446,23 +456,22 @@ extension RunnerTests {
446456
func testViewportReadSkippedWhileXCTestChannelPenalized() {
447457
// Pins the viewport fast path (#1587 review): every penalized private AX capture used to burn
448458
// the full 1s main-thread timeout on a doomed viewport read before falling back.
449-
currentBundleId = "xyz.blueskyweb.app"
459+
let bundleId = "xyz.blueskyweb.app"
450460
defer {
451-
currentBundleId = nil
452461
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
453462
abandonedMainThreadWorkCount = 0
454463
}
455464

456-
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest())
465+
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
457466

458-
penalizeSnapshotXCTestChannel(bundleId: "xyz.blueskyweb.app", reason: "test")
459-
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest())
467+
penalizeSnapshotXCTestChannel(bundleId: bundleId, reason: "test")
468+
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
460469

461470
clearSnapshotXCTestChannelPenalty(reason: "test")
462-
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest())
471+
XCTAssertTrue(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
463472

464473
abandonedMainThreadWorkCount = 1
465-
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest())
474+
XCTAssertFalse(shouldReadPrivateAXViewportViaXCTest(bundleId: bundleId))
466475
}
467476

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

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

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,17 +1264,25 @@ extension RunnerTests {
12641264
"command_preparation",
12651265
timeout: mainThreadExecutionTimeout,
12661266
timeoutError: mainThreadExecutionTimeoutError
1267-
) {
1268-
try self.prepareActiveCommandContextSafely(command: command, routeToSpringboard: false)
1267+
) { () -> SnapshotCommandPreparation in
1268+
switch try self.prepareActiveCommandContextSafely(command: command, routeToSpringboard: false) {
1269+
case .response(let response):
1270+
return .response(response)
1271+
case .context(let context):
1272+
return .capture(
1273+
self.takeSnapshotCaptureTarget(app: context.app),
1274+
systemSurface: context.systemSurface
1275+
)
1276+
}
12691277
}
12701278
switch preparation {
12711279
case .response(let response):
12721280
return response
1273-
case .context(let context):
1281+
case .capture(let target, let systemSurface):
12741282
return try executeSnapshotPrepared(
12751283
command: command,
1276-
activeApp: context.app,
1277-
systemSurface: context.systemSurface
1284+
target: target,
1285+
systemSurface: systemSurface
12781286
)
12791287
}
12801288
}
@@ -1299,16 +1307,16 @@ extension RunnerTests {
12991307

13001308
private func executeSnapshotPrepared(
13011309
command: Command,
1302-
activeApp: XCUIApplication,
1310+
target: SnapshotCaptureTarget,
13031311
systemSurface: SystemSurfaceHost? = nil
13041312
) throws -> Response {
13051313
let options = Self.presentationOptions(from: command)
13061314
do {
13071315
var payload: DataPayload
13081316
if options.raw {
1309-
payload = try snapshotRaw(app: activeApp, options: options)
1317+
payload = try snapshotRaw(target: target, options: options)
13101318
} else {
1311-
payload = try snapshotFast(app: activeApp, options: options)
1319+
payload = try snapshotFast(target: target, options: options)
13121320
}
13131321
if let systemSurface {
13141322
payload.systemSurface = SystemSurfaceProvenancePayload(
@@ -1354,29 +1362,8 @@ extension RunnerTests {
13541362
}
13551363

13561364
private func invalidateCachedTargetAfterSnapshotFailure() {
1357-
if Thread.isMainThread {
1358-
invalidateCachedTarget(reason: "ax_snapshot_failure")
1359-
return
1360-
}
1361-
// Abandoned work ahead of this hop cannot be cancelled: queue the drop behind it without
1362-
// waiting, so the failed capture answers now and the next command still finds the target gone.
1363-
guard !hasAbandonedMainThreadWork() else {
1364-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_INVALIDATION_DEFERRED_XCTEST_OCCUPIED")
1365-
DispatchQueue.main.async {
1366-
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
1367-
}
1368-
return
1369-
}
1370-
do {
1371-
try runMainThreadWork(
1372-
"target_invalidation",
1373-
timeout: 1,
1374-
timeoutError: mainThreadExecutionTimeoutError
1375-
) {
1376-
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
1377-
}
1378-
} catch {
1379-
NSLog("AGENT_DEVICE_RUNNER_SNAPSHOT_INVALIDATION_FAILED=%@", String(describing: error))
1365+
applyMainOwnedSnapshotState("target_invalidation") {
1366+
self.invalidateCachedTarget(reason: "ax_snapshot_failure")
13801367
}
13811368
}
13821369

@@ -2052,7 +2039,10 @@ extension RunnerTests {
20522039
}
20532040
return Response(ok: true, data: DataPayload(text: text))
20542041
case .snapshot:
2055-
return try executeSnapshotPrepared(command: command, activeApp: activeApp)
2042+
return try executeSnapshotPrepared(
2043+
command: command,
2044+
target: takeSnapshotCaptureTarget(app: activeApp)
2045+
)
20562046
case .screenshot:
20572047
#if os(macOS)
20582048
// macOS keeps the app-targeted capture behavior for window-level screenshots.
@@ -2661,7 +2651,8 @@ extension RunnerTests {
26612651
) != nil
26622652
}
26632653
return boundedBlockingSystemAlertSnapshot(
2664-
deadline: probeDeadline
2654+
deadline: probeDeadline,
2655+
penaltyBundleId: currentBundleId
26652656
) != nil
26662657
#else
26672658
return false

0 commit comments

Comments
 (0)