Skip to content

fix(maestro): bound selector matching in the optional 0.22 plugin - #5340

Merged
thymikee merged 8 commits into
0.22from
t3/simplify-maestro-replacement
Dec 3, 2026
Merged

thymikee merged 8 commits into
0.22from
t3/simplify-maestro-replacement

Conversation

@thymikee

@thymikee thymikee commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

A Maestro id or text selector regex cannot stall the daemon event loop through catastrophic backtracking. The flow author controls the pattern. The app under test controls the text.

Before / after

  • Before: matchesMaestroRegex ran any valid pattern with native RegExp, so (a+)+$ against app text could block the event loop.
  • After: patterns run on re2js (MIT, no dependencies, bundled into the Maestro package), a linear-time RE2 port.
    • Each id and text value compiles once before the snapshot scan. There is no process-wide cache.
    • Flags map ^(?:pattern)$ with ims to RE2 CASE_INSENSITIVE | MULTILINE | DOTALL. A test compares both engines.
    • One deliberate difference: \s and \S match ASCII whitespace only, as in Maestro (Java regex). 0.22 JavaScript also matched Unicode whitespace such as U+00A0 and U+202F. The Boundaries section of run-maestro-flows.md documents it, and a test pins it.
    • Patterns without regex syntax match one whole line, case-insensitively, with no length cap. Invalid JavaScript regexes compare as literals.
    • A pattern over 256 characters, or one using backreferences or lookaround, fails with maestro_selector_regex_unsafe. The error details carry the exceeded limit.
    • A failed step still reports, without suggestions, when its selector is refused.

Size

About 125 added and 27 removed production lines versus 0.22, in the Maestro selector files plus package and bundling config.

Validation

pnpm exec vitest run packages/maestro/src/internal/__tests__/selector-regex.test.ts: 23 passed at 2f27af8. With native RegExp, the backtracking and refusal tests fail. CI is pending.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Compatibility notice: #5339 advanced to 6ebda4e7a56de3504a3ab5fdecdc4bb86d9ce0fd. Only complete-text assertion admission changed in shared selectors: evaluateIsPredicate now refuses Android textClipped evidence for text with existing typed reason android_text_clipped, before extraction/comparison. Selector parsing/matching and absence semantics are unchanged; no optional-plugin/version/protocol changes. Get/fill reuse shared completeness admission. Modified focused files pass 67 tests; no optional Maestro integration or remote transport validation was run in this bounded task. Your branch and body are untouched.

@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 24 files

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

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

Comment thread website/docs/docs/run-maestro-flows.md Resolved
Comment thread website/docs/docs/run-maestro-flows.md Resolved
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 5.29 MB 5.29 MB 0 B
Package (unpacked) 5.28 MB 5.28 MB 0 B
Package (download) 1.62 MB 1.62 MB -13 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 13.2 ms 15.2 ms +2.0 ms
CLI --help 25.3 ms 27.3 ms +2.0 ms

Affected plugins

Plugin Installed (including dependencies) Unpacked Download
@agent-device/maestro 896.5 kB → 1.05 MB (+152.8 kB) 896.0 kB → 1.05 MB (+152.8 kB) 278.2 kB → 325.7 kB (+47.5 kB)

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

The change at 81ea7db has one regression to fix before merge: the new regex gate refuses safe selectors that work on 0.22 today. CI is green, with 24 checks and none failing at this commit, and there are no conflicts.

assertDisjointQuantifiers compares every pair of variable quantifiers on a path and ignores what separates them. If two quantifiers share any ASCII character, the pattern is refused, even when a mandatory element between them cannot be consumed by the left quantifier. In that case the regex is unambiguous and linear. A scratch copy of the head file throws maestro_selector_regex_unsafe for Item \d+ of \d+, Hello \w+ \w+ and \S+ \S+. The native RegExp on 0.22 matches all three. So tapOn: {text: 'Page \d+ of \d+'} or assertVisible: {text: 'Hello \w+ \w+'} now fails, which breaks the "supported selectors keep working" half of the issue for a common, safe pattern class. No test covers two same-class repetitions separated by a literal. The rule to satisfy: two variable repetitions are ambiguous only if the left one can also consume everything between them (each intervening element is nullable, or its atom overlaps the left atom) and the right atom overlaps the left. Apply it in assertDisjointQuantifiers, the single owner for every path, and reset the comparison at a mandatory element that is disjoint from the left quantifier's atom. Keep a*b*a* and a+(?:|a)a+b refused, and add accepting cases for \d+ of \d+ and \w+ \w+.

Not blocking, and these can be taken or left: the pattern is validated lazily on the first value it meets, so a relation that narrows the scope to zero nodes (or nodes with only empty text) lets an unsafe text pattern resolve to no match, and assertNotVisible then passes instead of refusing as the Boundaries doc states (runtime-selector-resolution.ts:310); validating every id/text pattern once when the resolver starts would make refusal independent of the snapshot and remove the separate long-pattern probe; the PR adds RawSnapshotNode.textClipped and a snapshot-unchanged key without a producer (snapshot.ts:325), and the producer lives in #5339, so whichever PR merges second must reconcile the shared kernel field; and patterns such as .*\d+.* and .+@.+\..+ are refused although their worst case is only polynomial, so a later change could admit them under the existing work budget.

Could the PR be smaller? Would it work to keep selector-regex.ts as the single gate (fixed as above), validate every id/text pattern once at resolver entry, and make work-limit overflow throw maestro_selector_regex_indeterminate directly, with no per-node uncertainty state? That would drop most of the uncertainty plumbing in resolution, ranking, targets, position and the daemon port (roughly 350-400 production lines) and the kernel textClipped field. Clipped-text-aware selection would become a follow-up on top of #5339, which owns the producer. For that, #5339 would need to land first and own RawSnapshotNode.textClipped.

One earlier high-priority inline thread still applies: presentation snapshot test. No lower-priority threads still apply. These threads no longer apply at this commit and can be resolved: unindexed existence fallback (geometry now passes confirmedUnindexedExistence=true), ranking uncertainty (rankMaestroCandidates passes uncertainNodes to the resolver), clipped text identity (selectorIdentity returns uncertain and keeps the candidate), unindexed doc wording (the doc now matches the code), literal canonicalization (canonicalizeLiteral handles the 257-char case), indeterminate reason (the observation port uses the shared reason) and invalid-syntax doc (now qualified to 256 characters).

Pattern behavior was checked by running a scratch copy of the head selector-regex.ts with stubbed AppError and normalizeText, not the package's vitest suite. Not verified here: the author's 180/7-failed comparison, which used the previous head 1743d08 as base rather than 0.22, and whether #5339 also adds RawSnapshotNode.textClipped, since its diff was not visible. Live device evidence was not required, because the change is host-side selector logic over snapshots.

Before merge, assertDisjointQuantifiers must accept separated repetitions such as Item \d+ of \d+ and Hello \w+ \w+ that match on 0.22, with tests that show it.

@thymikee
thymikee force-pushed the t3/simplify-maestro-replacement branch from 81ea7db to d01e84d Compare December 3, 2026 07:32
@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on d01e84d. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@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: 1 new finding and 2 moved to a new line on 45fe431. The verdict and the full list are in the summary comment.

Replace the ambiguous-quantifier checker with re2js. Each selector value
compiles once before the snapshot scan; backreferences, lookaround, and
regex patterns over 256 characters fail with maestro_selector_regex_unsafe.
The value length cap is gone, so long labels stay matchable.
@thymikee
thymikee force-pushed the t3/simplify-maestro-replacement branch from 45fe431 to ea18011 Compare December 3, 2026 09:12
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on 5aa5664. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@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: 1 new finding on d05fa1f. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@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: 2 new findings on 35fdcfd. The verdict and the full list are in the summary comment.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

The earlier findings from 81ea7db (#5340 (comment)) are fixed at 35fdcfd, except one case where the new matcher still differs from 0.22.

RE2 \s matches ASCII whitespace only, but the JavaScript \s also matches Unicode whitespace such as U+00A0, U+202F, U+2000-U+200A, U+3000 and U+FEFF. The value keeps these characters, and only line terminators are rewritten. A selector like text: '\d+:\d+\sAM' matches the iOS 17+ label '10:30\u202fAM' with RegExp but not at this head. Hello\sWorld against 'Hello\u00a0World' behaves the same way, and \S goes the other way. A selector that works on 0.22 then misses: tapOn and assertVisible fail, and assertNotVisible wrongly passes. The PR body says the flags reproduce the 0.22 result, and that is false for \s.

The rule is that the head matcher must give the same result as ^(?:p)$ with the ims flags for every pattern it accepts. The smallest fix extends the existing pair, LINE_TERMINATOR_RUN for values and PATTERN_TERMINATOR for patterns, to map the JavaScript-only whitespace (raw or escaped) to a space before RE2 runs. Please add the U+202F, NBSP and \S cases to the engine-equality test.each. Upstream Maestro (Java regex) also treats \s as ASCII only, so the maintainer may prefer to keep the RE2 behavior, note it in the Boundaries section of run-maestro-flows.md, and correct the PR body.

Not blocking, and fine to take or leave: any RE2 compile failure of a pattern that is valid in JavaScript, such as [^], takes the 'syntax' branch at line 54, but the message names only backreferences and lookaround, so wording it as "syntax the linear-time engine does not support (for example backreferences or lookaround)" or translating [^] to [\s\S] would fit.

The open inline thread at #5340 (comment) does not apply any more, because the presentation code and runtime-targets-typed.test.ts are deleted at head. These threads are fixed at head and can be resolved: #5340 (comment) (literal multi-line match), #5340 (comment) (unsafe regex in suggestions), #5340 (comment) (typed limit details), #5340 (comment) (selector compiled once), #5340 (comment) (line terminators in values) and #5340 (comment) (terminators in patterns).

One check is still running, and the other 23 checks are not failing. Before merge, \s and \S must give the 0.22 result on Unicode whitespace, or the maintainer must accept ASCII-only \s and document it. Not verified here: the package test suite, a live device run, and the differential used re2js 2.8.6 rather than the pinned 2.10.0; the iOS time-label example rests on known iOS 17+ formatting, not a captured snapshot.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

This PR is ready at 5dbe1b8, and the earlier findings from 35fdcfd are fixed. Multi-line literal selectors now match a run of whole lines (https://github.com/callstack/agent-device/blob/5dbe1b8/packages/plugin-maestro/src/selector-regex.ts#L32), and the Header/Footer test covers it. Not blocking: the new docs sentence says \s matches ASCII whitespace as in Maestro, but RE2's \s has no vertical tab, so a value with U+000B would match in Maestro and 0.22 but not here. You can take it or leave it. Saying "\s matches space, tab, and line breaks only" would fix it (website/docs/docs/run-maestro-flows.md:49).

Of the open inline threads, the one on multi-line literal selectors is fixed at this head (#5340 (comment)). The other two were withdrawn and can be resolved: the escape-only pattern thread, because such patterns contain a backslash and take the RE2 route with base semantics (#5340 (comment)), and the nit that was recorded in error, which no code depends on (#5340 (comment)).

Not verified here: the package tests and an RE2-vs-RegExp comparison at re2js 2.10.0 were not run. The \s ASCII claim rests on the test at this head and the passing unit jobs. The literal-path match against base was checked by tracing edge cases by hand.

CI is still pending. The only check not passing is one queued review-bot check, which exercises no route this diff touches, and the other 23 checks are not failing. There are no conflicts. Merge now waits only on that queued check.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 3, 2026
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 5dbe1b8

Since the review of 2f27af8: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

🔧 1 finding marked fixed in their threads
Finding Fixed by Where
Multi-line literal selectors now match only when the whole node text equals them thymikee 5dbe1b8 · reply
Not in the reviewers' scope (1 file)
  • pnpm-lock.yaml: generated or a lockfile, so its lines were left out

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (64dd30eae755).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee
thymikee merged commit b52cb8e into 0.22 Dec 3, 2026
24 checks passed
@thymikee
thymikee deleted the t3/simplify-maestro-replacement branch December 3, 2026 12:28
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