Skip to content

feat(ios): read modal surfaces from a modal-scope pair - #4125

Merged
thymikee merged 3 commits into
feat/ios-modal-scope-capturefrom
feat/ios-modal-surface-rule
Nov 1, 2026
Merged

thymikee merged 3 commits into
feat/ios-modal-scope-capturefrom
feat/ios-modal-surface-rule

Conversation

@thymikee

@thymikee thymikee commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

PR 2 of 3 for modal-scoped alert on iOS. Stacks on #4124. Host-only: no runner, alert, or snapshot behavior change.

ModalSurface.read pairs a plain and an honor-modal tree. A surface is the children the honor-modal tree keeps under a node where it removes others. When a kept child holds its own surface, that inner surface replaces the outer one, so an underlying modal's controls stay out. Keyboard branches offer no actions. The outcome is modal(surfaces), noExclusion, or unknown(reason). An identical pair never reads as absent.

SnapshotPresentedSurface.classify takes the reading as presenting evidence. The golden table gains modalScopeExpected on two corpus-derived cases. The TS rule is unchanged.

Recording Surface root Actions offered Excluded
SpringBoard alert, folded and unfolded Alert (innermost of 3 levels) Cancel, Open none
Safari prompt, folded SFDialogView's container Cancel, Open Back, bookmark, add, Tabs, refresh
Safari prompt, unfolded same Cancel, Open 7 toolbar buttons
Settings Clear History sheet sheet container dismiss popup, both close buttons, checkmark, 5 cells, Close All Tabs, Clear History none
SpringBoard home, Settings root noExclusion none —

Remaining gaps (not recorded yet): iPad popovers, React Native Modal, SFSafariViewController, physical devices.

Checks: swift test, TS parity tests, check:affected. Tests catch mutations to nested replacement, keyboard skip, dismiss marker, and modal evidence.

🤖 Generated with Claude Code

ModalSurface reads a plain and an honor-modal tree of one screen and names
the subtrees the honor-modal read keeps where it removes others. A kept
child that holds a surface of its own replaces the outer one, so an
underlying modal's controls stay out. Keyboard branches offer no actions.
The reading is modal(surfaces), noExclusion, or unknown(reason); an
identical pair never reads as absent.

SnapshotPresentedSurface takes the reading as presenting evidence. The
golden table gains a modalScopeExpected column on two cases read from the
corpus. Every corpus recording now states the surfaces and actions the rule
must find. The runner passes no reading yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
agent-device-website Ready Ready Preview Oct 31, 2026 8:31pm UTC

Request Review

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 5.66 MB 5.67 MB +6.0 kB
Package (unpacked) 5.66 MB 5.66 MB +6.0 kB
Package (download) 1.72 MB 1.72 MB +1.9 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 40.4 ms 40.2 ms -0.2 ms
CLI --help 88.7 ms 86.7 ms -1.9 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

The input assistant hosts the app's inputAssistantItem buttons, so it no
longer drops its whole subtree. A keyboard's own controls still offer no
actions, and an assistant inside the keyboard ends that claim. The golden
table's description and the parity test's comment now name the modal-scope
evidence and the three readings each modalScopeExpected case states.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

ModalSurface and SnapshotAcquisition read it from SnapshotKeyboardSurface.
It covers only the keyboard and its keys; each caller decides what the input
assistant means to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel

Verdict: approve

7 finding(s): 2 minor, 5 nit. Verdict: approve. reached max rounds (1).

🟡 Minor (2)

Where Finding
apple/snapshot-presentation/Tests/AgentDeviceSnapshotPresentationTests/AlertSurfaceTests.swift:1 Dropped cross-check that the honor-modal read and the alert expect column agree
apple/snapshot-presentation/Sources/AgentDeviceSnapshotPresentation/ModalSurface.swift:62 An unmatched honor-modal child ends the whole walk and discards surfaces found on sibling branches
🔵 Nit (5)
Where Finding
scripts/capture-alert-surface.ts:137 Capture script claims it kept an expectModalScope it never wrote
apple/snapshot-presentation/Sources/AgentDeviceSnapshotPresentation/ModalSurface.swift:57 Roots that do not match are reported as .unorderedRead, and children(of:) accepts a forest
apple/snapshot-presentation/Sources/AgentDeviceSnapshotPresentation/ModalSurface.swift:61 surfaces pairs kept children by element alone, so a parent that lists the same element twice can read as a spurious modal surface
apple/snapshot-presentation/Sources/AgentDeviceSnapshotPresentation/ModalSurface.swift:100 keyboardOwned is redeclared inside the parameter's own scope
apple/snapshot-presentation/Sources/AgentDeviceSnapshotPresentation/ModalSurface.swift:101 Hard-coded "Keyboard" and "Key" element types

Reply under an inline comment with @apex-sentinel-staging note ... or @apex-sentinel-staging pivot ... to re-check that finding.
Comment @apex-sentinel-staging review on this conversation to run a full review.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 7 new findings on 4eded74. The verdict and the full list are in the summary comment.

@@ -1,88 +1,7 @@
import CoreGraphics

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · Dropped cross-check that the honor-modal read and the alert expect column agree

The removed testEveryRecordedPromptHidesContentFromTheHonorModalTree asserted, for every corpus recording, removedNodeCount > 0 == !recorded.expect.isEmpty: the pairing result had to agree with the independently hand-written alert expectations. Nothing in the new tests restores that link. ModalSurfaceTests.testEveryRecordingReadsItsExpectedSurfacesAndActions compares ModalSurface.read only against the new hand-written expectModalScope column, and AlertSurfaceTests.testEveryRecordedSurfaceIsFoundByItsSourceOnly reads only the trees. A recording whose honorModal tree removes nothing while its expect names a prompt (a capture taken after the prompt was dismissed, or a mislabelled noExclusion per README step 4) now passes every host test. The positive control between the two expectation columns is lost.

Suggested change

Restore the positive control, e.g. re-add to ModalSurfaceTests: for each recording, assert ModalScopePair.pair(...) == .paired(removedNodeCount: >0) iff recorded.expect is non-empty, so the two hand-written columns cannot drift from each other.

Details

Also raised here

Also raised at this location by javascript: Deleting the honor-modal positive control drops the check that expect and the pair agree — A recording whose expect names a prompt while its pair removes nothing (or the reverse, as a capture drifts or a new device is recorded) now passes. Keep the control, e.g. assert recorded.expect.isEmpty == (reading is .noExclusion) inside the new corpus test.

persona: swift-ios, javascript

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

// Pairing matched elements under parent elements. This walk matches them under the parent node,
// which also tells apart the occurrences of an element that one read lists under two parents.
func surfaces(honorModalNode: Int, plainNode: Int) -> [ModalSurface]? {
var removed = plainChildren[plainNode]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · An unmatched honor-modal child ends the whole walk and discards surfaces found on sibling branches

surfaces(honorModalNode:plainNode:) returns nil as soon as one honor-modal child has no counterpart among its plain parent's children (line 63), and every ancestor propagates that nil (line 70), so read answers .unknown(.unstable(.reparentedElement)) for the entire screen. Because the walk is top-down, a surface already collected on a sibling branch is thrown away too: on a window whose panel A holds a clean alert and whose panel B had one toolbar item move into another container between the two reads, the reading is unknown and SnapshotPresentedSurface.classify answers unknown even though panel A is unambiguous evidence. Making the failure local to the subtree that reparented, instead of aborting, keeps the surviving surface — and the code already distinguishes "no surface on this branch" ([]) from "this branch cannot be read" (nil), so the two are separable.

Suggested change

Collect a flag for "some branch could not be matched" instead of returning nil from the whole walk, and return the surfaces found on the branches that did match (or state why discarding them is intended).

Details

persona: maintainability

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

@@ -131,6 +137,6 @@ console.log(
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Nit · Capture script claims it kept an expectModalScope it never wrote

scripts/capture-alert-surface.ts copies expectModalScope from the previous file only when that key already exists (lines 123-125), but the completion message branches only on previous.expect === undefined (line 139-141). Recapturing a fixture that has expect but no expectModalScope (every fixture captured before this PR) therefore prints Wrote ... Kept its "expect" and "expectModalScope"; check that the node positions still hold. while nothing was kept and the new honorModal/plain trees are written with no modal-scope expectation at all. RecordedAlertSurface decodes expectModalScope as a non-optional field, so the recaptured file then fails to decode in ModalSurfaceTests/AlertSurfaceTests, and the operator is told the file is fine. The message should key the second half on previous.expectModalScope.

Suggested change
);
console.log(
previous.expect === undefined
? `Wrote ${path.relative(root, fixture)}. Add "expect" and "expectModalScope" from what the screen shows.`
: previous.expectModalScope === undefined
? `Wrote ${path.relative(root, fixture)}. Kept its "expect". Add "expectModalScope" for the new trees.`
: `Wrote ${path.relative(root, fixture)}. Kept its "expect" and "expectModalScope"; check that the node positions still hold.`,
);
Details

Also raised here

Also raised at this location by javascript: capture script's "Kept its expectModalScope" message keys on expect, so a fixture that has only expect silently ships without the new field — The recorded object spreads expectModalScope only when previous.expectModalScope !== undefined, but the console message and the pairing of the two fields are decided by previous.expect === undefined alone. A fixture recorded between #​4115 and this PR has expect but no expectModalScope; recapturing it with this script prints Kept its "expect" and "expectModalScope"; check that the node positions still hold. while no expectModalScope is written, and RecordedAlertSurface.load then throws a decode error for the missing required key in both AlertSurfaceTests and ModalSurfaceTests. Each field's presence should be reported for itself (or the missing one flagged), since the two are written by independent conditions.

Also raised at this location by maintainability: The recapture message claims expectModalScope was kept based only on expect — The message still branches on previous.expect === undefined alone (line 138) while it now claims both fields were kept. A fixture recaptured for the first time after expectModalScope was introduced — expect present, expectModalScope absent — prints Kept its "expect" and "expectModalScope"; check that the node positions still hold. even though nothing wrote expectModalScope (line 123 skipped it), so the author who trusts the line skips the step the README's new item 4 requires. Pick the message from the field the sentence is about.

persona: swift-ios, javascript, maintainability

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

}
guard let plainChildren = children(of: plain), let honorModalChildren = children(of: honorModal),
plain.first?.element == honorModal.first?.element
else { return .unknown(.unorderedRead) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Nit · Roots that do not match are reported as .unorderedRead, and children(of:) accepts a forest

Two mismatches share the wrong label. (1) guard let plainChildren = …, plain.first?.element == honorModal.first?.element else { return .unknown(.unorderedRead) } (lines 57-59) calls a differing root element an unordered read, while ModalSurfaceUnknownReason.unorderedRead is documented as "A read lists a node before the parent it names" — and children(of:) (line 108) already rejects that, so the root-element comparison is the only thing that can reach this case today. A future producer that legitimately reorders nodes would make a reordered pair read as a root mismatch and be misdiagnosed. (2) children(of:) only requires parentElement == nil at position 0 (line 112); a node later in the list with no parentElement is appended to ancestors but never attached to a parent, so it keeps its own children while being unreachable from the root — its subtree is silently excluded from the walk rather than reported.

Suggested change
else { return .unknown(.unorderedRead) }
guard let plainChildren = children(of: plain), let honorModalChildren = children(of: honorModal),
plain.first?.element == honorModal.first?.element
else { return .unknown(.unorderedRead) }

Suggested change

Give the differing-root case its own ModalSurfaceUnknownReason case instead of reusing .unorderedRead, and reject (or attach) a non-first node whose parentElement is nil in children(of:) so its subtree cannot be dropped silently.

Details

Also raised here

Also raised at this location by senior-developer: children(of:) rejects any multi-root read and mislabels it as an unordered read — children(of:) returns nil for any read that lists a node with parentElement == nil at a position other than 0 (ModalSurface.swift:114-116), and read maps every nil to .unknown(.unorderedRead) (line 55-57). The remaining gaps the PR lists (iPad popovers, React Native Modal, SFSafariViewController) are exactly the shapes where an AX read can emit more than one top-level node — several windows, or a host application beside the app — so the pair cannot be read at all there, and the silent assumption is that a read has exactly one root. Two consequences. (1) The failure is a silent false negative: a screen that did remove content and kept a modal surface answers unknown, so SnapshotPresentedSurface.classify falls back to element-type evidence and answers unknown instead of presented — with no hint that the reason is read shape rather than a changed screen. …

persona: maintainability, senior-developer

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.


// Pairing matched elements under parent elements. This walk matches them under the parent node,
// which also tells apart the occurrences of an element that one read lists under two parents.
func surfaces(honorModalNode: Int, plainNode: Int) -> [ModalSurface]? {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Nit · surfaces pairs kept children by element alone, so a parent that lists the same element twice can read as a spurious modal surface

ModalScopePair.pair matches nodes on the pair (element, parentElement) and tolerates a plain read listing the same pair twice (unmatched[key].count up to 2), so a parent with two children of the same element is a paired result. The walk then matches only by element: removed.firstIndex(where: { plain[$0].element == honorModal[child].element }) takes the FIRST plain child with that element, which need not be the occurrence the honor-modal read kept. If the earlier occurrence has children and the kept one does not (or their subtrees differ), the recursion descends into the wrong subtree, the earlier occurrence's children stay in removed, and the guard !removed.isEmpty, !kept.isEmpty reports that node as a modal surface. ModalSurfaceReading.modal is exactly the evidence SnapshotPresentedSurface.classify turns into .presented, so a tree with no modal answers presented. The corpus records the same element under two parents (README: "The same element can appear under two parents in one tree"; recorded files carry duplicate element keys), and testARemovedOccurrenceOfASharedElementIsNotAnExclusion only covers the two-parent case, where children(of:) already separates the occurrences; same-parent duplicates are untested. Pair the occurrence by (element, parentElement) as the pairing does, or by position among same-element siblings, so the kept occurrence cannot be swapped for a removed one.

Suggested change
func surfaces(honorModalNode: Int, plainNode: Int) -> [ModalSurface]? {
func surfaces(honorModalNode: Int, plainNode: Int) -> [ModalSurface]? {
var removed = plainChildren[plainNode]
var kept: [(honorModal: Int, plain: Int)] = []
var taken: [String: Int] = [:]
for child in honorModalChildren[honorModalNode] {
let element = honorModal[child].element
let nth = taken[element, default: 0]
taken[element] = nth + 1
var occurrence = -1
for candidate in removed where honorModal[child].element == plain[candidate].element {
occurrence += 1
if occurrence == nth {
kept.append((child, removed.remove(at: removed.firstIndex(of: candidate)!))
break
}
}
guard occurrence == nth else { return nil }
}

Suggested change

Match a kept honor-modal child to the plain sibling with the same element AND the same occurrence index among that element's siblings, so a plain tree that lists one element twice under one parent cannot send the walk into the removed occurrence's subtree. Add a unit test for two same-parent children of one element where the removed occurrence has children.

Details

Also raised here

Also raised at this location by senior-developer: surfaces() pairs duplicate child occurrences by first match, so a kept child can be compared against the wrong plain subtree — ModalScopePair.pair deliberately tolerates one element listed under one parent more than once — it counts occurrences by (element, parentElement) and only fails when the honor-modal read has more of a key than the plain read (ModalScopePair.swift:55-70). The walk in surfaces does not carry that count semantics: it picks the first plain child whose element matches (removed.firstIndex(where:), line 65) and pairs that occurrence positionally, and it recurses into that occurrence only (line 71-74). Two failure modes when one parent lists the same element twice in the plain read: Wrong subtree: if the host element itself appears twice under one node in the plain read (positions p1 and p2) and the honor-modal read keeps one occurrence, the walk maps the kept node to p1 arbitrarily. …

persona: javascript, senior-developer

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

&& (AlertSurface.actionableTypes.contains(node.type)
|| AlertSurface.isDismissPopupMarker(node.label) || AlertSurface.isDismissPopupMarker(node.identifier))
return (isAction ? [node] : []) + node.children.flatMap { actions(in: $0, keyboardOwned: keyboardOwned) }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Nit · keyboardOwned is redeclared inside the parameter's own scope

actions(in:keyboardOwned:) declares let keyboardOwned = … ? false : keyboardOwned || … (lines 101-103), which shadows the parameter while still reading it on the right-hand side, and hides a separate fact: the same value is also used under the name isAction (line 105) with the meaning inverted. Name the incoming and outgoing values differently (e.g. keyboardOwned: in, ownsSubtree out) so the keyboard claim and the action test can be read without tracking which keyboardOwned each line sees.

Suggested change
}
private static func actions(in node: AlertSurfaceNode, keyboardOwned: Bool) -> [AlertSurfaceNode] {
let ownsSubtree = SnapshotKeyboardSurface.isInputAssistant(type: node.type, identifier: node.identifier)
? false
: keyboardOwned || node.type == "Keyboard" || node.type == "Key"
let isAction = !ownsSubtree
Details

persona: maintainability

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

|| AlertSurface.isDismissPopupMarker(node.label) || AlertSurface.isDismissPopupMarker(node.identifier))
return (isAction ? [node] : []) + node.children.flatMap { actions(in: $0, keyboardOwned: keyboardOwned) }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Nit · Hard-coded "Keyboard" and "Key" element types

actions(in:keyboardOwned:) compares node.type against the literals "Keyboard" and "Key" (line 103). The neighbouring rules use shared vocabularies instead — AlertSurface.actionableTypes, AlertSurface.isDismissPopupMarker, SnapshotKeyboardSurface.isInputAssistant — and the element-type vocabulary is already hand-mirrored elsewhere (SnapshotPresentedSurface.presentingElementTypes). A maintainer would want these two spellings in the same owning vocabulary as the keyboard rule they sit beside, so a producer that spells a key differently is changed in one place.

Suggested change

Move the keyboard/key element types next to SnapshotKeyboardSurface.isInputAssistant (or into the same vocabulary as AlertSurface.actionableTypes) and match against that instead of the inline literals.

Details

persona: maintainability

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if this is wrong — I will re-check the cited code.

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

@thymikee

Copy link
Copy Markdown
Member Author

The code looks good at 538a295, and I found nothing that needs to change.

All 13 checks pass. The Swift corpus and parity tests run in the ios-snapshot-differential gate (smoke-macos), which covers the changed files. I did not run swift test locally and relied on that CI run. There are no conflicts. This PR is stacked on #4124, so merge order follows the base PR.

Not blocking, and you can take or leave these: (1) add XCTAssertEqual(expected.reading == "modal", !recorded.expect.isEmpty, name) in testEveryRecordingReadsItsExpectedSurfacesAndActions, so the two hand-written columns cannot disagree; (2) the two new golden cases in ios-presented-surface.json share one modalScopeExpected map and the parity test uses a synthetic ModalSurface, so state the rule once or derive the reading from the recorded pair; (3) the closing message in capture-alert-surface.ts should branch on expect and expectModalScope separately; (4) an empty honor-modal read over a non-empty plain read reports .unorderedRead, so return .unknown(.noSurvivingSurface) before the ordering guard.

Of the open inline threads, #4125 (comment) (modal expectation test), #discussion_r4433176617 (capture message) and #discussion_r4433176623 (empty honor-modal read) still apply, plus two lower-priority threads. The thread at #4125 (comment) does not apply: an unmatched kept child means the pair is inconsistent, so failing closed to unknown is correct. The thread at #4125 (comment) does not apply: 538a295 moved both sites to SnapshotKeyboardSurface.isKeyboardOrKey. Please resolve those two.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Oct 31, 2026
@thymikee
thymikee added this pull request to stack #4126 November 1, 2026 06:12
@thymikee
thymikee merged commit f7879cf into main Nov 1, 2026
18 checks passed
@thymikee
thymikee deleted the feat/ios-modal-surface-rule branch November 1, 2026 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant