Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

docs: scope the positional-wrapper document-order promise to css/xpath/text, since a wrapper over a semantic form ignores its index (PIN-044)

Luigi Agosti5bd135db1b3e
diff --git a/docs/commands.md b/docs/commands.md
index 2aef13d2..d819a501 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -107,7 +107,7 @@ Most element commands accept a unified selector:
- label, placeholder, alt, title, or test id selectors such as `label:Email`, `placeholder:Search`, `alt:Logo`, `title:Close`, `testid:submit`
- positional wrappers such as `first:button`, `last:role:button`, or `nth:2:button` (`nth` is zero-based)
-Positional wrappers index the candidates in **document order** for every selector kind, so `nth:0:` is the first match in the page and `nth:1:` always comes after it. A bare `text:` selector is the one form that does not index: it picks the most control-like match among the smallest ones, so `text:Save` prefers a `<button>` over a `<div>` carrying the same label — which means `text:X` and `first:text:X` can resolve to different elements. A wrapper only ever chooses among the matches a bare selector would find; it never changes which matches exist.
+Positional wrappers index the candidates in **document order** over `css:`, `xpath:`, and `text:` selectors, so `nth:0:` is the first match in the page and `nth:1:` always comes after it. A bare `text:` selector is the one form of those that does not index: it picks the most control-like match among the smallest ones, so `text:Save` prefers a `<button>` over a `<div>` carrying the same label — which means `text:X` and `first:text:X` can resolve to different elements. A wrapper over a semantic form (`role:`, `label:`, `placeholder:`, `alt:`, `title:`, `testid:`) **does not index at all**: the best-scoring match is returned whatever the index, so `nth:2:role:button` resolves the same element as `role:button`. A wrapper only ever chooses among the matches a bare selector would find; it never changes which matches exist.
Selector prefixes are case-insensitive, so `CSS:#login` and `css:#login` mean the same thing. Only the prefix is case-folded; the value after it is passed through unchanged.
diff --git a/docs/reference/mcp-tools.md b/docs/reference/mcp-tools.md
index 3087747a..d9be07eb 100644
--- a/docs/reference/mcp-tools.md
+++ b/docs/reference/mcp-tools.md
@@ -17,7 +17,7 @@ Selector forms include:
- `label:Email`, `placeholder:Search`, `alt:Logo`, `title:Close`, `testid:submit`
- `first:button`, `last:button`, `nth:2:button`
-Positional wrappers index the candidates in **document order** for every selector kind, so `nth:0:` is the first match in the page and `nth:1:` always comes after it. A bare `text:` selector is the one form that does not index: it picks the most control-like match among the smallest ones, so `text:Save` prefers a `<button>` over a `<div>` carrying the same label — which means `text:X` and `first:text:X` can resolve to different elements. A wrapper only ever chooses among the matches a bare selector would find; it never changes which matches exist.
+Positional wrappers index the candidates in **document order** over `css:`, `xpath:`, and `text:` selectors, so `nth:0:` is the first match in the page and `nth:1:` always comes after it. A bare `text:` selector is the one form of those that does not index: it picks the most control-like match among the smallest ones, so `text:Save` prefers a `<button>` over a `<div>` carrying the same label — which means `text:X` and `first:text:X` can resolve to different elements. A wrapper over a semantic form (`role:`, `label:`, `placeholder:`, `alt:`, `title:`, `testid:`) **does not index at all**: the best-scoring match is returned whatever the index, so `nth:2:role:button` resolves the same element as `role:button`. A wrapper only ever chooses among the matches a bare selector would find; it never changes which matches exist.
Structured semantic locators are matched by the semantic engine; CSS, XPath, refs, the existing `text:` action selector, and bare CSS/text wrappers stay browser-side.
diff --git a/internal/mcp/argcheck_test.go b/internal/mcp/argcheck_test.go
index 2da8ce35..5af4dfae 100644
--- a/internal/mcp/argcheck_test.go
+++ b/internal/mcp/argcheck_test.go
@@ -636,6 +636,9 @@ func TestSelectorSchemasThatOfferWrappersStateWhatAnIndexMeans(t *testing.T) {
if !strings.Contains(property.Description, "text:X and first:text:X can differ") {
t.Errorf("%s.%s does not warn that a bare text: selector ranks rather than indexes: %q", tool.Name, name, property.Description)
}
+ if !strings.Contains(property.Description, "does not index at all") {
+ t.Errorf("%s.%s does not warn that a wrapper over a semantic form ignores its index: %q", tool.Name, name, property.Description)
+ }
}
}
if checked == 0 {
diff --git a/internal/mcp/tools.go b/internal/mcp/tools.go
index 24c3b481..b0528395 100644
--- a/internal/mcp/tools.go
+++ b/internal/mcp/tools.go
@@ -2,12 +2,7 @@ package mcp
import "github.com/mark3labs/mcp-go/mcp"
-// positionalWrapperGrammar states what an index means, for every tool whose
-// selector accepts first/last/nth. It is one constant because the rule is one
-// rule: a client that reads it in a click description must not learn something
-// different from the snapshot description. Indices used to follow an internal
-// ranking for text:, which made nth:1 resolve earlier in the page than nth:0.
-const positionalWrapperGrammar = " first/last/nth index matches in document order for every kind, so nth:0 is the first match in the page. A bare text: selector instead picks the most control-like smallest match, so text:X and first:text:X can differ."
+const positionalWrapperGrammar = " first/last/nth index matches in document order over css:, xpath: and text: selectors, so nth:0 is the first match in the page. A bare text: selector instead picks the most control-like smallest match, so text:X and first:text:X can differ. A wrapper over a semantic form (role:, label:, placeholder:, alt:, title:, testid:) does not index at all: the best-scoring match is returned whatever the index, so nth:2:role:button resolves the same element as role:button."
// allTools returns every MCP tool exposed by the PinchTab MCP server.
func allTools() []mcp.Tool {
diff --git a/internal/selector/selector_test.go b/internal/selector/selector_test.go
index 19d4076e..85ff7005 100644
--- a/internal/selector/selector_test.go
+++ b/internal/selector/selector_test.go
@@ -389,6 +389,41 @@ func TestSelector_SemanticQuery(t *testing.T) {
}
}
+func TestPositionalWrapperOverSemanticFormDoesNotApplyItsIndex(t *testing.T) {
+ semanticPrefixes := []string{}
+ for _, pk := range prefixKinds {
+ if rawSelectorCanUseSemantic(pk.Prefix + "Save") {
+ semanticPrefixes = append(semanticPrefixes, pk.Prefix)
+ }
+ }
+ if len(semanticPrefixes) == 0 {
+ t.Fatal("no prefix routes to the semantic matcher, so this guard checked nothing")
+ }
+
+ for _, prefix := range semanticPrefixes {
+ bare := prefix + "Save"
+ for _, wrapped := range []string{"first:" + bare, "last:" + bare, "nth:0:" + bare, "nth:2:" + bare} {
+ query, ok := Parse(wrapped).SemanticQuery()
+ if !ok {
+ t.Errorf("%s no longer routes to the semantic matcher; docs/commands.md and positionalWrapperGrammar say a wrapper over a semantic form does not index, and that sentence must change with this", wrapped)
+ continue
+ }
+ if query != wrapped {
+ t.Errorf("SemanticQuery(%q) = %q: the index is now stripped from the query, so docs/commands.md and positionalWrapperGrammar must stop saying a wrapper over a semantic form does not index", wrapped, query)
+ }
+ }
+ }
+
+ for _, prefix := range []string{"css:", "xpath:", "text:"} {
+ bare := prefix + "Save"
+ for _, wrapped := range []string{bare, "first:" + bare, "last:" + bare, "nth:2:" + bare} {
+ if _, ok := Parse(wrapped).SemanticQuery(); ok {
+ t.Errorf("%s routes to the semantic matcher, so it cannot index in document order as the docs promise", wrapped)
+ }
+ }
+ }
+}
+
func TestFromConstructors(t *testing.T) {
if s := FromRef("e5"); s.Kind != KindRef || s.Value != "e5" {
t.Errorf("FromRef(\"e5\"): %+v", s)