Repository navigation
feat(extension): add role and text matching engine - #3188
supremeboxlogos wants to merge 5 commits into
Conversation
🦋 Changeset detectedLatest commit: 2bf8ee3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 24 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
4 issues found and verified against the latest diff
Confidence score: 3/5
semanticSelectors.tsmisses matches inside closed shadow roots, so semantic role, text, and scoped CSS queries can fail on those elements. UsegetOpenOrClosedShadowRootfor the scope host and descendant hosts.semanticLocator.test.tsreadsmock.instances[0]even though the method spy is called withoutnew, so the assertion fails. Usemock.contexts[0]to inspect the receiver.selectorParser.tstreats>>inside a CSS comment as a frame-hop separator, so valid selectors such asbutton/* >> */are misparsed. Skip CSS comments while scanning for separators.semanticLocator.tslets role/state mismatches through host validation, so invalid combinations fail only afterRuntime.evaluaterunsroleQueryin the page. Add role-aware refinement to the query schema.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/extension/understudy/selectorParser.ts">
<violation number="1" location="packages/extension/understudy/selectorParser.ts:25">
P2: This treats `>>` inside a CSS comment as a frame-hop separator, so `deepLocator('button/* >> */')` tries to resolve `button/*` as a frame selector instead of matching the valid CSS selector. Skip CSS comments while scanning.</violation>
</file>
<file name="packages/extension/types/private/semanticLocator.ts">
<violation number="1" location="packages/extension/types/private/semanticLocator.ts:147">
P2: Role/state mismatches pass host validation and fail only after `Runtime.evaluate` runs `roleQuery` in the page. Add a role-aware refinement to the query schema so invalid combinations are rejected on the host.</violation>
</file>
<file name="packages/extension/understudy/semanticLocator.test.ts">
<violation number="1" location="packages/extension/understudy/semanticLocator.test.ts:166">
P2: This method spy is called without `new`, so `mock.instances[0]` is undefined and this assertion fails; use `mock.contexts[0]` to inspect the receiver.</violation>
</file>
<file name="packages/extension/dom/locatorScripts/semanticSelectors.ts">
<violation number="1" location="packages/extension/dom/locatorScripts/semanticSelectors.ts:196">
P2: Semantic role, text, and scoped CSS queries miss elements inside closed shadow roots because this traversal reads only `.shadowRoot`. Use `getOpenOrClosedShadowRoot` for the scope host and descendant hosts, matching the existing selector resolver.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| .strictObject({ | ||
| kind: z.literal("role"), | ||
| role: AriaRoleSchema, | ||
| options: GetByRoleOptionsSchema.optional(), |
There was a problem hiding this comment.
P2: Role/state mismatches pass host validation and fail only after Runtime.evaluate runs roleQuery in the page. Add a role-aware refinement to the query schema so invalid combinations are rejected on the host.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/extension/types/private/semanticLocator.ts, line 147:
<comment>Role/state mismatches pass host validation and fail only after `Runtime.evaluate` runs `roleQuery` in the page. Add a role-aware refinement to the query schema so invalid combinations are rejected on the host.</comment>
<file context>
@@ -0,0 +1,205 @@
+ .strictObject({
+ kind: z.literal("role"),
+ role: AriaRoleSchema,
+ options: GetByRoleOptionsSchema.optional(),
+ })
+ .meta({ id: "RoleQueryStep" }),
</file context>
| const shadows: ShadowRoot[] = []; | ||
| if (scope instanceof Element && scope.shadowRoot) shadows.push(scope.shadowRoot); | ||
| for (const element of scope.querySelectorAll("*")) { | ||
| if (element.shadowRoot) shadows.push(element.shadowRoot); |
There was a problem hiding this comment.
P2: Semantic role, text, and scoped CSS queries miss elements inside closed shadow roots because this traversal reads only .shadowRoot. Use getOpenOrClosedShadowRoot for the scope host and descendant hosts, matching the existing selector resolver.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/extension/dom/locatorScripts/semanticSelectors.ts, line 196:
<comment>Semantic role, text, and scoped CSS queries miss elements inside closed shadow roots because this traversal reads only `.shadowRoot`. Use `getOpenOrClosedShadowRoot` for the scope host and descendant hosts, matching the existing selector resolver.</comment>
<file context>
@@ -0,0 +1,242 @@
+ const shadows: ShadowRoot[] = [];
+ if (scope instanceof Element && scope.shadowRoot) shadows.push(scope.shadowRoot);
+ for (const element of scope.querySelectorAll("*")) {
+ if (element.shadowRoot) shadows.push(element.shadowRoot);
+ }
+ for (const shadow of shadows) result.push(...queryElements(shadow, selector, true));
</file context>
There was a problem hiding this comment.
1 existing issue remains and 1 new issue found across 18 files
Confidence score: 3/5
- Chained XPath selectors in
semanticSelectors.tscan miss descendants inside shadow roots even whenpierceShadowis enabled. Use a scoped composed XPath resolver for nested steps. QueryStepSchemainsemanticLocator.tsacceptscheckedfor roles thatroleQueryrejects, so invalid plans can pass validation and fail in the browser. Cross-validate role and option pairs here.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/extension/dom/locatorScripts/semanticSelectors.ts">
<violation number="1" location="packages/extension/dom/locatorScripts/semanticSelectors.ts:235">
P2: Nested XPath steps use native `evaluate`, which cannot reach descendants inside shadow roots even when `pierceShadow` is true. Use a scoped composed XPath resolver so chained XPath selectors honor shadow piercing.</violation>
</file>
Architecture diagram
sequenceDiagram
participant Client as SDK Client
participant Protocol as Protocol Layer
participant Extension as Stagehand Extension
participant Resolver as FrameSelectorResolver
participant Parser as SelectorParser
participant Codec as SemanticLocator Codec
participant DeepLocator as DeepLocator
participant CDP as CDP/Browser Context
participant DOM as DOM/ARIA Engine
Note over Client,DOM: Semantic Locator Runtime Flow
Client->>Protocol: Send semantic locator request
Protocol->>Extension: Encode locator query (internal:locator= prefix)
Extension->>Codec: encodeLocatorQuery(steps)
Codec->>Codec: Validate steps (ARIA roles, states, regex)
Codec-->>Extension: Encoded selector string
Extension->>Resolver: parseSelector(encodedSelector)
Resolver->>Codec: decodeLocatorQuery(selector)
alt Valid semantic query
Codec-->>Resolver: QueryStep[] (kind: semantic)
Resolver->>DeepLocator: resolveLocatorTarget()
else Malformed query
Codec-->>Resolver: Throw error (Invalid semantic locator query)
Resolver-->>Extension: Reject request
end
DeepLocator->>Parser: splitSelectorHops(initialSelector)
alt Has iframe hop
Parser-->>DeepLocator: Frame chain segments
DeepLocator->>CDP: Resolve iframe target frame
CDP-->>DeepLocator: Target frame context
DeepLocator->>Codec: Re-encode with resolved frame
Codec-->>DeepLocator: Updated selector with frame context
else No iframe hop
DeepLocator-->>Resolver: Same frame, original selector
end
DeepLocator-->>Resolver: Resolved target (frame + selector)
Resolver->>CDP: Runtime.evaluate (resolveSemanticSelector)
CDP->>DOM: Execute querySemanticSelector(steps)
DOM->>DOM: Process step sequence
loop Each QueryStep
alt Role step
DOM->>DOM: roleQuery() with ARIA state validation
else Text step
DOM->>DOM: textQuery() with regex/literal matching
else Selector step
DOM->>DOM: selectorQuery() (CSS/XPath/text=)
else Nth step
DOM->>DOM: Filter to specific index
end
DOM->>DOM: Apply shadow DOM piercing
end
DOM-->>CDP: Matching Element[] results
CDP-->>Resolver: Remote object references
alt Count operation
Resolver->>CDP: Runtime.evaluate (countSemanticMatches)
CDP->>DOM: Execute count query
DOM-->>CDP: Match count
CDP-->>Resolver: Numeric result
end
Resolver-->>Extension: Resolved nodes or count
Extension-->>Protocol: Response with results
Protocol-->>Client: Final locator result
Note over Resolver,DOM: Error handling
alt Semantic query error (invalid ARIA state, bad regex)
DOM-->>CDP: Throw exception with details
CDP-->>Resolver: Exception details
Resolver-->>Extension: Reject with cause details
end
Requires human review: Auto-approval blocked because this review re-detected 4 unresolved issues already reported by Cubic.
View guided diff | Turn on auto-fix | Re-trigger cubic
| let expression = selector.replace(/^xpath=/i, ""); | ||
| if (scope instanceof Element && expression.startsWith("/")) expression = "." + expression; | ||
| const owner = scope instanceof Document ? scope : scope.ownerDocument; | ||
| const iterator = owner.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE); |
There was a problem hiding this comment.
P2: Nested XPath steps use native evaluate, which cannot reach descendants inside shadow roots even when pierceShadow is true. Use a scoped composed XPath resolver so chained XPath selectors honor shadow piercing.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/extension/dom/locatorScripts/semanticSelectors.ts, line 235:
<comment>Nested XPath steps use native `evaluate`, which cannot reach descendants inside shadow roots even when `pierceShadow` is true. Use a scoped composed XPath resolver so chained XPath selectors honor shadow piercing.</comment>
<file context>
@@ -0,0 +1,242 @@
+ let expression = selector.replace(/^xpath=/i, "");
+ if (scope instanceof Element && expression.startsWith("/")) expression = "." + expression;
+ const owner = scope instanceof Document ? scope : scope.ownerDocument;
+ const iterator = owner.evaluate(expression, scope, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE);
+ const result: Element[] = [];
+ let node: Node | null;
</file context>
why
Add accessible-role and text matching as an isolated extension layer. Stacked on #3187; this PR does not expose SDK builders or add wait/snapshot integration.
what changed
>>inside selector strings intact.test plan