Skip to content

Commit f18238c

Browse files
thymikeeclaude
andcommitted
fix(ios-runner): reset log markers on a process rebind and count what they write
refreshCachedTargetIfProcessChanged rebinds the target but only cleared the text-entry tap, so the new process could lose its guard or policy line. It now runs resetTargetBoundState. Markers write through runnerMarkerWriter so tests count emitted lines, and the fast app guard dedup is testable on its own. Drops the unused command parameter of canUseFastForegroundAppGuard. Co-Authored-By: Claude <[email protected]>
1 parent a2f0a5c commit f18238c

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,7 @@ extension RunnerTests {
15681568

15691569
let skipExistenceWait = canUseFastForegroundAppGuard(
15701570
activeApp: activeApp,
1571-
requestedBundleId: requestedBundleId,
1572-
command: command.command
1571+
requestedBundleId: requestedBundleId
15731572
)
15741573
if !skipExistenceWait && !activeApp.waitForExistence(timeout: appExistenceTimeout) {
15751574
if let bundleId = requestedBundleId {
@@ -1591,8 +1590,7 @@ extension RunnerTests {
15911590
}
15921591
let skipInteractionExistenceWait = canUseFastForegroundAppGuard(
15931592
activeApp: activeApp,
1594-
requestedBundleId: requestedBundleId,
1595-
command: command.command
1593+
requestedBundleId: requestedBundleId
15961594
)
15971595
if !skipInteractionExistenceWait && !activeApp.waitForExistence(timeout: 2) {
15981596
if let bundleId = requestedBundleId {

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ extension RunnerTests {
216216
)
217217
currentApp = candidate
218218
currentAppProcessIdentifier = observedProcessIdentifier
219-
clearRememberedTextEntryTap()
219+
resetTargetBoundState()
220220
clearSnapshotXCTestChannelPenalty(reason: "target_process_changed")
221221
clearPrivateAXAcceptedDepth(reason: "target_process_changed")
222222
snapshotXCTestPenaltyWarmupExemptionPending = true
@@ -254,21 +254,24 @@ extension RunnerTests {
254254

255255
func canUseFastForegroundAppGuard(
256256
activeApp: XCUIApplication,
257-
requestedBundleId: String?,
258-
command: CommandType
257+
requestedBundleId: String?
259258
) -> Bool {
260259
guard let requestedBundleId, currentBundleId == requestedBundleId, currentApp != nil else {
261260
return false
262261
}
263262
guard activeApp.state == .runningForeground else { return false }
263+
writeFastAppGuardMarker(bundleId: requestedBundleId, state: activeApp.state)
264+
return true
265+
}
266+
267+
func writeFastAppGuardMarker(bundleId: String, state: XCUIApplication.State) {
264268
// The command is on the adjacent COMMAND_ACCEPTED line; repeating it here would make a deduped
265269
// marker read as if only that command ever passed the guard.
266-
let line = "AGENT_DEVICE_RUNNER_FAST_APP_GUARD bundle=\(requestedBundleId) state=\(activeApp.state.rawValue)"
270+
let line = "AGENT_DEVICE_RUNNER_FAST_APP_GUARD bundle=\(bundleId) state=\(state.rawValue)"
267271
if lastLoggedFastAppGuardLine != line {
268272
lastLoggedFastAppGuardLine = line
269-
NSLog("%@", line)
273+
runnerMarkerWriter(line)
270274
}
271-
return true
272275
}
273276

274277
/// The pid of the one other application holding an active accessibility session, or nil unless
@@ -498,5 +501,22 @@ extension RunnerTests {
498501
XCTAssertNil(lastLoggedFastAppGuardLine, "a rebind must state the guard once more")
499502
XCTAssertTrue(lastLoggedGesturePolicyLines.isEmpty, "a rebind must state the policy once more")
500503
}
504+
505+
func testFastAppGuardMarkerWritesOnceUntilTheFactChanges() {
506+
var written: [String] = []
507+
runnerMarkerWriter = { written.append($0) }
508+
defer {
509+
runnerMarkerWriter = { NSLog("%@", $0) }
510+
invalidateCachedTarget(reason: "unit_test_cleanup")
511+
}
512+
writeFastAppGuardMarker(bundleId: "com.example.app", state: .runningForeground)
513+
writeFastAppGuardMarker(bundleId: "com.example.app", state: .runningForeground)
514+
XCTAssertEqual(written.count, 1, "a repeated fact writes no second line")
515+
writeFastAppGuardMarker(bundleId: "com.example.other", state: .runningForeground)
516+
XCTAssertEqual(written.count, 2, "a changed fact writes a new line")
517+
resetTargetBoundState()
518+
writeFastAppGuardMarker(bundleId: "com.example.other", state: .runningForeground)
519+
XCTAssertEqual(written.count, 3, "a rebind states the same fact once more")
520+
}
501521
}
502522
#endif

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ extension RunnerTests {
148148
// (AX health, keyboard, fallback) is a new one.
149149
if lastLoggedGesturePolicyLines[kind] != line {
150150
lastLoggedGesturePolicyLines[kind] = line
151-
NSLog("%@", line)
151+
runnerMarkerWriter(line)
152152
}
153153
#endif
154154
}
@@ -174,14 +174,22 @@ extension RunnerTests {
174174
#if AGENT_DEVICE_RUNNER_UNIT_TESTS && os(iOS)
175175
extension RunnerTests {
176176
func testSynthesizedGesturePolicyMarkerWritesOncePerKindUntilTheDecisionChanges() {
177-
defer { invalidateCachedTarget(reason: "unit_test_cleanup") }
177+
var written: [String] = []
178+
runnerMarkerWriter = { written.append($0) }
179+
defer {
180+
runnerMarkerWriter = { NSLog("%@", $0) }
181+
invalidateCachedTarget(reason: "unit_test_cleanup")
182+
}
178183
logSynthesizedGesturePolicyDecision(kind: .coordinateTap, context: nil, fallbackAttempted: false)
179184
logSynthesizedGesturePolicyDecision(kind: .coordinateTap, context: nil, fallbackAttempted: false)
185+
XCTAssertEqual(written.count, 1, "a repeated decision writes no second line")
180186
logSynthesizedGesturePolicyDecision(kind: .scroll, context: nil, fallbackAttempted: false)
181-
XCTAssertEqual(lastLoggedGesturePolicyLines.count, 2, "one remembered line per gesture kind")
182-
let before = lastLoggedGesturePolicyLines[.coordinateTap]
187+
XCTAssertEqual(written.count, 2, "each gesture kind states its own decision")
188+
logSynthesizedGesturePolicyDecision(kind: .coordinateTap, context: nil, fallbackAttempted: true)
189+
XCTAssertEqual(written.count, 3, "a changed decision writes a new line")
190+
resetTargetBoundState()
183191
logSynthesizedGesturePolicyDecision(kind: .coordinateTap, context: nil, fallbackAttempted: true)
184-
XCTAssertNotEqual(lastLoggedGesturePolicyLines[.coordinateTap], before, "a changed decision is a new line")
192+
XCTAssertEqual(written.count, 4, "a rebind states the same decision once more")
185193
}
186194
}
187195
#endif

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ final class RunnerTests: XCTestCase {
8282
// target-bound state so a rebind states the fact once more.
8383
var lastLoggedFastAppGuardLine: String?
8484
var lastLoggedGesturePolicyLines: [SynthesizedGesturePolicyKind: String] = [:]
85+
var runnerMarkerWriter: (String) -> Void = { NSLog("%@", $0) }
8586
/// When the first interaction after an activation may run, on the monotonic uptime clock.
8687
/// The guarantee is a minimum gap *since the activation*, not a pause at the interaction:
8788
/// a caller that already spent that gap elsewhere (an agent's round trip is 190-260 ms)

0 commit comments

Comments
 (0)