Skip to content

Commit 3696f33

Browse files
committed
fix(ios): bind accepted-depth memory to the process, pin deferred through the wire parser
Review follow-up (#1587): - The depth memory was cleared only in refreshCachedTargetIfProcessChanged; resetTargetAfterExternalRelaunch -> invalidateCachedTarget drops the cached PID without clearing it, so the next activation had no old PID to compare and could reuse a stale shallow rung for up to 120s (also A->B->A when A restarted while inactive). The memory now stores the PID it was learned under and only matches the same live process; recording without a PID is refused. Every invalidation path is covered automatically because they all drop currentAppProcessIdentifier. - The deferred settle/warning tests constructed typed verdicts directly, so removing 'deferred' from the accepted reason-code set would silently restore the repeated warning and budget reset while tests stayed green. They now parse a raw runner-wire object through readSnapshotQualityVerdict (red on base: parser strips the code -> warning re-appears, reset fires). - Extracted shouldReadPrivateAXViewportViaXCTest() and pinned the penalized viewport skip with an in-bundle regression test.
1 parent 4c0a5cf commit 3696f33

4 files changed

Lines changed: 97 additions & 18 deletions

File tree

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

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ final class RunnerTests: XCTestCase {
9494
// on target process change, so screens that regain deep-capture ability are re-probed.
9595
let privateAXAcceptedDepthLock = NSLock()
9696
var privateAXAcceptedDepthBundleId: String?
97+
// PID-bound: a relaunch (external or A->B->A while inactive) changes the process, and the new
98+
// tree may accept the full depth again. Any invalidation path that drops the cached PID makes
99+
// the memory unmatchable, so every fresh activation re-probes the full requested depth.
100+
var privateAXAcceptedDepthProcessIdentifier: Int?
97101
var privateAXAcceptedDepth: Int?
98102
var privateAXAcceptedDepthUntil = Date.distantPast
99103
// Bluesky-class screens can grind ~4-8s before an XCTest-backed snapshot tier fails; anything

‎src/commands/interaction/runtime/settle.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { makeSnapshotState } from '../../../__tests__/test-utils/index.ts';
1212
import { ref, selector } from './selector-read-utils.ts';
1313
import { buildSettleTailEntries, NEVER_SETTLED_HINT } from './settle.ts';
14+
import { readSnapshotQualityVerdict } from '../../../snapshot/snapshot-quality.ts';
1415

1516
// #1101 --settle: quiet-window settle loop composition on the interaction
1617
// commands. Budgets are injected (fake clock) — no real waiting.
@@ -213,12 +214,15 @@ test('penalty-deferred private-ax captures do not reset the settle budget', asyn
213214
// and times out instead of being extended.
214215
const before = buttonSnapshot();
215216
const deferredAfter = welcomeSnapshot();
216-
deferredAfter.snapshotQuality = {
217+
// Parsed from a raw runner-wire object on purpose: if 'deferred' is ever dropped from the
218+
// accepted reason-code set, the parser strips it, the reset fires again, and this test fails.
219+
deferredAfter.snapshotQuality = readSnapshotQualityVerdict({
217220
state: 'recovered',
218221
backend: 'private-ax',
219222
reasonCode: 'deferred',
220223
reason: 'XCTest-backed snapshot tiers were deferred after recent slow accessibility work',
221-
};
224+
});
225+
assert.equal(deferredAfter.snapshotQuality?.reasonCode, 'deferred');
222226
let captures = 0;
223227
const clock = createFakeClock();
224228
const device = createSettleDevice({

‎src/utils/__tests__/snapshot-quality.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ test('readSnapshotQualityVerdict keeps the verdict but drops an unknown reasonCo
4545
assert.equal(verdict?.reasonCode, undefined);
4646
});
4747

48+
test("the runner-wire 'deferred' reasonCode survives parsing into warning suppression", () => {
49+
// End-to-end through the raw wire parser: 'deferred' must be a member of the accepted
50+
// reason-code set, or it is silently stripped and every downstream deferred behavior
51+
// (warning suppression, settle budget-reset skip) quietly reverts.
52+
const verdict = readSnapshotQualityVerdict({
53+
state: 'recovered',
54+
backend: 'private-ax',
55+
reason: 'XCTest-backed snapshot tiers were deferred after recent slow accessibility work',
56+
reasonCode: 'deferred',
57+
});
58+
assert.equal(verdict?.reasonCode, 'deferred');
59+
assert.deepEqual(renderSnapshotQualityWarnings(verdict!, []), []);
60+
});
61+
4862
test('penalty-deferred recovered captures suppress the fallback warning but keep depth copy', () => {
4963
// Every capture of a hostile screen re-stamps recovered/deferred; the capture that armed the
5064
// penalty already carried the full warning, so repeating it each capture is context noise.

0 commit comments

Comments
 (0)