Skip to content

fix(browse): one ambiguous ref no longer kills the whole annotated screenshot - #2601

Closed
namtrok wants to merge 1 commit into
garrytan:mainfrom
namtrok:fix/annotate-strict-mode-multiple-elements
Closed

namtrok wants to merge 1 commit into
garrytan:mainfrom
namtrok:fix/annotate-strict-mode-multiple-elements

Conversation

@namtrok

@namtrok namtrok commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

The symptom

browse snapshot -a exits 1 with Selector matched multiple elements on most real-world pages. Because -a is hardcoded in qa/SKILL.md (3 sites), canary/SKILL.md (3 sites) and land-and-deploy/SKILL.md, those skills silently produce reports whose screenshots do not exist — the command fails, the report still references a path, and a stale file from a previous run makes it look fine.

Plain browse screenshot <path> is unaffected, which is what makes this easy to miss.

Root cause

Refs are built as getByRole(role, { name }) and disambiguated with .nth() when a role+name pair repeats (snapshot.ts:210-229). That works for duplicate named elements.

It cannot fire for a node with no accessible name. The locator degrades to getByRole(role) with no name filter, and the count driving .nth() comes from the filtered aria snapshot while getByRole matches the unfiltered DOM. Measured on a live page: the tree surfaced 2 unnamed paragraph nodes; the DOM had 9.

boundingBox() then trips Playwright strict mode. The catch at snapshot.ts:396-399 allowlisted only Timeout / closed / Target / Execution context, so the strict-mode error was re-thrown and one ambiguous ref aborted every remaining annotation.

Worth stressing: landmarks (banner, main, contentinfo) and paragraph are correctly unnamed per ARIA. On the page I reproduced against, 7 refs had no name and the three <nav> elements were all properly labelled — so this fires on well-built pages, not broken ones. It is the common case.

The fix

  • .first() before boundingBox() — an ambiguous ref draws a box on its first match instead of aborting. The heatmap path at what is now line ~538 has always tolerated this with a bare catch {}; annotate was the only path that could be killed outright.
  • The catch no longer re-throws on unrecognised messages. A box that cannot be measured is a box that is not drawn, never a reason to lose the rest of the page. BROWSE_DEBUG=1 prints what was skipped.
  • -o without -a/-H now warns instead of silently doing nothing (exit 0, no file). That silence reads as "screenshots are broken" rather than "you forgot a flag", and cost a real debugging session.

Verification

Rebuilt both ways and ran the identical command against the identical page, with a positive control that refs actually existed (51 present, so the test could not pass vacuously):

build result
upstream main Selector matched multiple elements — no file written
with this change exit 0 — 229 KB PNG, 1280×1468

The only variable between the two runs is this diff.

Note

No test added. browse/test/ has no fixture that reproduces this — it needs a page with an unnamed landmark whose role also appears more often in the DOM than in the aria snapshot. Happy to add one if you'd like a particular shape.

…reenshot

`snapshot -a` exits 1 with "Selector matched multiple elements" on most real
pages, so /qa, /canary and /land-and-deploy silently produce reports whose
screenshots do not exist. Plain `screenshot <path>` is unaffected.

Refs are built as getByRole(role, {name}) and disambiguated with .nth() when
role+name repeats. That disambiguation cannot fire for a node with NO accessible
name: the locator degrades to getByRole(role) with no name filter, and the count
driving .nth() is taken from the FILTERED aria snapshot while getByRole matches
the unfiltered DOM. Measured on a live page: the tree surfaced 2 unnamed
paragraphs, the DOM had 9. Landmarks (banner/main/contentinfo) and paragraphs are
correctly unnamed per ARIA, so this is the common case rather than an edge case.

boundingBox() then hits Playwright strict mode, and the catch allowlisted only
timeout/closed/Target/Execution-context messages — so the strict-mode error was
re-thrown and aborted every remaining annotation.

Two changes:

- `.first()` before boundingBox(), so an ambiguous ref draws a box on its first
  match instead of aborting. The heatmap path below has always tolerated this via
  a bare `catch {}`; annotate was the only path that could be killed outright.
- the catch no longer re-throws on unrecognised messages. A box we cannot measure
  is a box we do not draw, never a reason to lose the rest of the page. Set
  BROWSE_DEBUG to see what was skipped.

Also: `-o` passed without `-a`/`-H` was silently ignored (exit 0, no file), which
reads as "screenshots are broken" rather than "you forgot a flag". It now warns
and points at `browse screenshot <path>`.

Verified by rebuilding both ways against the same page with 51 refs present:
  before — "Selector matched multiple elements", no file written
  after  — exit 0, 229KB PNG
@trunk-io

trunk-io Bot commented Aug 16, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, @namtrok. The fix is now on main: snapshot handles ambiguous refs by taking .first() and reporting them instead of failing the annotated screenshot (browse/src/snapshot.ts) (fixed in v1.68.0.0, #2632). Closing as superseded; if you still see the problem on the latest gstack, please open an issue with ./setup --status output.

@capy-ai capy-ai Bot closed this Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant