Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
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()
Expand Down
2 changes: 1 addition & 1 deletion packages/command-surface/src/capture/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion website/docs/docs/snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Loading