diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandDispatch.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandDispatch.swift index c3edc1ab95..8abef176e3 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandDispatch.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandDispatch.swift @@ -694,11 +694,13 @@ extension RunnerTests { return XCUIApplication(bundleIdentifier: host.bundleId).state } - func observeOnlyUnavailableResponse() -> Response { + func observeOnlyUnavailableResponse( + hint: String = "XCUIApplication must report runningForeground before and after capture. This state report does not prove screen ownership (#2696)." + ) -> Response { Response(ok: false, error: ErrorPayload( code: "OBSERVATION_UNAVAILABLE", message: "The session app cannot be observed without activation.", - hint: "XCUIApplication must report runningForeground before and after capture. This state report does not prove screen ownership (#2696)." + hint: hint )) } diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift index 82f6d386cc..e2d3b80d48 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+Snapshot.swift @@ -168,20 +168,11 @@ extension RunnerTests { // in RunnerTests.swift), so reverting this entry point to bypass the bounded probe fails the // regression test. func snapshotFast(target: SnapshotCaptureTarget, options: PresentationOptions) throws -> DataPayload { - let deadline = Date().addingTimeInterval(Self.snapshotPlanBudget) - if let blocking = boundedBlockingSystemAlertSnapshot( - deadline: deadline, - penaltyTarget: .prepared(bundleId: target.bundleId), - options: options - ) { - return blocking - } - return try runSnapshotCapturePlan( - Self.regularVisiblePlan, - target: target, - options: options, + try snapshotAfterSystemAlertTier( + plan: Self.regularVisiblePlan, terminal: .sparseWithFatalOnAXFailure, - deadline: deadline + target: target, + options: options ) } @@ -336,19 +327,36 @@ extension RunnerTests { // See `snapshotFast` above: the single production entry point, no unit-test overload. func snapshotRaw(target: SnapshotCaptureTarget, options: PresentationOptions) throws -> DataPayload { + try snapshotAfterSystemAlertTier( + plan: Self.rawDiagnosticPlan, + terminal: .throwOnAXFailure, + target: target, + options: options + ) + } + + private func snapshotAfterSystemAlertTier( + plan: [SnapshotBackendKind], + terminal: SnapshotCaptureTerminalPolicy, + target: SnapshotCaptureTarget, + options: PresentationOptions + ) throws -> DataPayload { let deadline = Date().addingTimeInterval(Self.snapshotPlanBudget) if let blocking = boundedBlockingSystemAlertSnapshot( deadline: deadline, penaltyTarget: .prepared(bundleId: target.bundleId), options: options ) { + if target.refusesSystemAlertSubstitution { + throw SystemAlertSubstitutionRefused() + } return blocking } return try runSnapshotCapturePlan( - Self.rawDiagnosticPlan, + plan, target: target, options: options, - terminal: .throwOnAXFailure, + terminal: terminal, deadline: deadline ) } diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCaptureTarget.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCaptureTarget.swift index 7a9b5eaaaf..8eefa99322 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCaptureTarget.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotCaptureTarget.swift @@ -14,8 +14,14 @@ struct SnapshotCaptureTarget { let processIdentifier: Int? /// The lit panel's window the request booked; set only on a multi-panel request. var litPanelSurface: LitPanelSurface? = nil + /// Set on an observe-only capture, whose answer asserts app provenance: the system-alert tier then + /// refuses the capture instead of answering with the SpringBoard tree (#3348). + var refusesSystemAlertSubstitution = false } +/// The system-alert tier would have answered a capture that must come from the app. +struct SystemAlertSubstitutionRefused: Error {} + /// What snapshot command preparation hands the off-main capture. enum SnapshotCommandPreparation { case response(Response) diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotExecution.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotExecution.swift index 471827e1c0..1f2e9866d3 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotExecution.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+SnapshotExecution.swift @@ -32,6 +32,7 @@ extension RunnerTests { processIdentifier: Self.processIdentifier(of: context.app) ) target.litPanelSurface = self.litPanelState.surface + target.refusesSystemAlertSubstitution = context.observation != nil return .capture( target, systemSurface: context.systemSurface, @@ -138,6 +139,10 @@ extension RunnerTests { return Response(ok: true, data: payload) } catch let refusal as RunnerAppScreenCaptureFailure { return refusal.response + } catch is SystemAlertSubstitutionRefused { + return observeOnlyUnavailableResponse( + hint: "A system alert is presented over the session app. Resolve it with alert, or read it with a regular snapshot." + ) } catch let failure as SnapshotCaptureFailure { invalidateCachedTargetAfterSnapshotFailure() return Response( diff --git a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+CommandDispatchTests.swift b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+CommandDispatchTests.swift index bf1a5121eb..ec4be101ea 100644 --- a/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+CommandDispatchTests.swift +++ b/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+CommandDispatchTests.swift @@ -108,6 +108,49 @@ extension RunnerTests { XCTAssertNil(pendingTargetActivation) } + @MainActor + func testObserveOnlySnapshotRefusesOnlyASubstitutedSystemAlertTree() throws { + app.launch() + XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10)) + invalidateCachedTarget(reason: "unit_test_setup") + defer { + systemModalProbeOverrideForTesting = nil + invalidateCachedTarget(reason: "unit_test_cleanup") + app.terminate() + } + let appField = "\"appBundleId\":\"com.callstack.agentdevice.runner\"" + + let alertTreeMessage = "substituted system alert tree" + systemModalProbeOverrideForTesting = { _ in DataPayload(message: alertTreeMessage) } + for rawField in ["", ",\"raw\":true"] { + let observeOnly = try executeSnapshotDispatched(command: runnerCommandFixture( + "{\"command\":\"snapshot\",\(appField),\"observeOnly\":true\(rawField)}" + )) + XCTAssertFalse(observeOnly.ok, "request suffix: \(rawField)") + XCTAssertEqual(observeOnly.error?.code, "OBSERVATION_UNAVAILABLE", "request suffix: \(rawField)") + XCTAssertEqual( + observeOnly.error?.hint, + "A system alert is presented over the session app. Resolve it with alert, or read it with a regular snapshot.", + "request suffix: \(rawField)" + ) + XCTAssertNil(observeOnly.data, "request suffix: \(rawField)") + + let regular = try executeSnapshotDispatched(command: runnerCommandFixture( + "{\"command\":\"snapshot\",\(appField)\(rawField)}" + )) + XCTAssertTrue(regular.ok, "request suffix: \(rawField)") + XCTAssertEqual(regular.data?.message, alertTreeMessage, "request suffix: \(rawField)") + XCTAssertNil(regular.data?.observation, "request suffix: \(rawField)") + } + + systemModalProbeOverrideForTesting = { _ in nil } + let noAlert = try executeSnapshotDispatched(command: runnerCommandFixture( + "{\"command\":\"snapshot\",\(appField),\"observeOnly\":true}" + )) + XCTAssertTrue(noAlert.ok) + XCTAssertEqual(noAlert.data?.observation?.mode, "observe-only") + } + @MainActor func testRegularSnapshotStillRepairsBackgroundAndDisclosesPriorState() throws { app.launch() diff --git a/packages/command-surface/src/capture/snapshot.ts b/packages/command-surface/src/capture/snapshot.ts index cf17b470ef..3f1f409281 100644 --- a/packages/command-surface/src/capture/snapshot.ts +++ b/packages/command-surface/src/capture/snapshot.ts @@ -75,7 +75,7 @@ export const snapshotCommandFacet = defineCommandFacet({ name: SNAPSHOT_COMMAND_NAME, text: { summary: 'Capture or diff the accessibility tree', - cliDetail: `Regular iOS snapshots clip overflowing elements to their visible area and omit fully outside elements. --json reports clipping counts in snapshotQualityNotes with code regular-geometry-clipped; --raw retains reported frames. --observe-only is local iOS snapshot-only: an already-ready compatible runner reads the session app without activation or foreground repair, refuses unavailable observation, and reports XCUIApplication state rather than verified foreground ownership (#2696). Regular targetActivation disclosure stays unchanged (#2682, #2694). Repeated equivalent unfiltered Android snapshots return a compact unchanged acknowledgement. Use --force-full to re-emit the tree; --json and --raw retain full output. Android Appium JSON snapshots publish a display viewport only when measurements before and after the tree agree, independent of tree extent or keyboard content resizing. On Android, JSON value carries field content rather than static labels; saved selectors and replay scripts using value= for static labels must use label= instead; a reported showing hint means value is empty, and text equal to the placeholder is preserved when hint state is unknown. For iOS raw-coordinate fallback after a no-op ref press, inspect rects with snapshot -i --json, press the rect center, then verify with diff snapshot -i or snapshot --diff. iOS backend capability contract: ${snapshotBackendCapabilityHelp}.`, + cliDetail: `Regular iOS snapshots clip overflowing elements to their visible area and omit fully outside elements. --json reports clipping counts in snapshotQualityNotes with code regular-geometry-clipped; --raw retains reported frames. --observe-only is local iOS snapshot-only: an already-ready compatible runner reads the session app without activation or foreground repair, refuses unavailable observation or a readable system alert over the app, and reports XCUIApplication state rather than verified foreground ownership (#2696). Regular targetActivation disclosure stays unchanged (#2682, #2694). Repeated equivalent unfiltered Android snapshots return a compact unchanged acknowledgement. Use --force-full to re-emit the tree; --json and --raw retain full output. Android Appium JSON snapshots publish a display viewport only when measurements before and after the tree agree, independent of tree extent or keyboard content resizing. On Android, JSON value carries field content rather than static labels; saved selectors and replay scripts using value= for static labels must use label= instead; a reported showing hint means value is empty, and text equal to the placeholder is preserved when hint state is unknown. For iOS raw-coordinate fallback after a no-op ref press, inspect rects with snapshot -i --json, press the rect center, then verify with diff snapshot -i or snapshot --diff. iOS backend capability contract: ${snapshotBackendCapabilityHelp}.`, }, metadata: snapshotCommandMetadata, run: (client, input) => client.capture.snapshot(input), diff --git a/website/docs/docs/snapshots.md b/website/docs/docs/snapshots.md index 5d7f4e47d0..3990f79799 100644 --- a/website/docs/docs/snapshots.md +++ b/website/docs/docs/snapshots.md @@ -51,7 +51,9 @@ left exactly as it was. Other platforms, provider-owned sessions, and `--diff` a The runner refuses with `OBSERVATION_UNAVAILABLE` (also named by `details.runnerErrorCode`) unless the app reports `runningForeground` before and after the capture under one process identity. It does -not substitute SpringBoard or a presented system surface. The host refuses with `COMMAND_FAILED` and +not substitute SpringBoard or a presented system surface, and it refuses the same way when it can +read a system alert over the app. A success does not prove that no alert is present: an alert the +runner cannot read leaves the app tree as the answer. The host refuses with `COMMAND_FAILED` and `details.reason: "observation-unavailable"` when a runner answers an observe-only request without `observation` or with a `targetActivation` fact. Successful JSON includes: