Skip to content

Browser backend: an element's number points at the element it was read from - #67

Open
haydenunstacked wants to merge 1 commit into
awlevin:mainfrom
haydenunstacked:fix/handles-stamped-by-element
Open

haydenunstacked wants to merge 1 commit into
awlevin:mainfrom
haydenunstacked:fix/handles-stamped-by-element

Conversation

@haydenunstacked

Copy link
Copy Markdown

On pages where reading order and document order differ in a particular way, data-tscu handles end up on the wrong elements, so a click or typed text goes to a different element from the one the classifier chose. Nothing fails, and the step log shows the right name.

Cause. The page script in browser/perceive.py stamped each element in document order, sorted into reading order, then renumbered by finding each element by its old number:

out.forEach((o, i) => {
  const el = document.querySelector('[data-tscu="' + o.sid + '"]');
  if (el) el.setAttribute("data-tscu", String(i));

Old and new numbers share the attribute while that loop runs. When an element already renumbered holds the number being looked up, and sits earlier in the document, it is found instead and renumbered a second time.

When. Something late in the document sorts ahead of two or more earlier elements: a sidebar beside the main column, a fixed header or a dialog at the end of the document. Swapping two neighbours or reversing a whole list is harmless. Exactly: with s[i] the document rank of the element at sorted position i, some i has s[s[i]] < s[i] < i.

Smallest page. Three links in document order Alpha, Bravo, Charlie, drawn Charlie, Alpha, Bravo:

<nav style="display:flex;flex-direction:column">
  <a style="order:2" href="#a">Alpha</a>
  <a style="order:3" href="#b">Bravo</a>
  <a style="order:1" href="#c">Charlie</a>
</nav>

perceive() reports index 1 as Alpha and index 2 as Bravo. [data-tscu="1"] is Bravo and [data-tscu="2"] is Alpha.

Typing. type_text, field_value and clear_field use the same handle. With three inputs arranged the same way (Search, Password, Email in the document, drawn Email, Search, Password), typing_target(page, 1) returns Search with no refusal, and act.type_text puts the text in the password input.

Measured on main at 44ca11f with the repo's headless Chrome, top of page, handles that sit on a different element from the one reported. Counts move with what the page serves on a given load.

page before after
en.wikipedia.org/wiki/Whataburger 18 of 56 to 20 of 60 0
en.wikipedia.org/wiki/Texas 17 of 80 to 18 of 84 0
github.com/awlevin/typesafe-computer-use 17 of 72 0
news.ycombinator.com, bench/fixture.html 0 0

The last row is why the fixture loop never showed it: the sort moves nothing there.

Change. The script keeps each element and stamps it once, after the sort. Nothing is found by its number any more.

This does not change what the model sees. The old and new scripts return identical output on the fixture, Hacker News, the Whataburger article and this repo's page.

The suite runs no browser, so the new test reads the script text, as its neighbours do. ruff check, ruff format --check and pytest pass (730 passed, 3 skipped, 1 xfailed).

Generated with Claude Code

… the element it was read from

The page script numbered elements in document order, sorted them into reading order, then
renumbered by finding each element by its old number. Old and new numbers shared the
attribute while that loop ran, so an element already renumbered, and earlier in the
document, could be found instead and renumbered a second time. The handles stayed unique,
so nothing failed: a click or typed text went to a live element other than the one reported.

On en.wikipedia.org/wiki/Whataburger 18 to 20 of about 60 handles sat on a different
element. With three inputs drawn out of document order, text chosen for a plain field went
into the password input.

The script now keeps each element and stamps it once, after the sort. What it returns is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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