Skip to content

fix(browser): click by index reaches the last read's element - #954

Merged
omartelo merged 1 commit into
mainfrom
fix/browser-stale-index
Oct 27, 2026
Merged

omartelo merged 1 commit into
mainfrom
fix/browser-stale-index

Conversation

@omartelo

Copy link
Copy Markdown
Collaborator

Problem

pageInfoScript stamps data-lich-idx on the visible interactive elements on every read and never removed the stamps of the read before. resolveJS finds a target with document.querySelector('[data-lich-idx="N"]'), the first match in document order. Read the page (button A gets 0), scroll, read again (button B gets 0, A still carries 0 off the viewport): browser_click index 0 clicked A, with no error.

Fix

Each read removes every existing data-lich-idx before stamping. One line in the script; click, type and press all resolve through resolveJS, so all three are covered.

Why this over a per-read token that makes a stale index fail: the tool contract already says the index is "from the last browser_info", and after this change the DOM holds exactly the last read's indexes. The edge cases land on the error that already exists ("no element matches that target: the page may have changed since it was read; read it again for fresh indexes"):

  • an element removed from the DOM takes its stamp with it, so its index finds nothing;
  • an index from a read before a navigation finds nothing in the new document, which was never stamped;
  • an index the last read did not hand out (an element out of view or past the 200 cap) finds nothing.

A token would add an argument to every index target and a field to every page read for the agent to echo back, without catching a case the wipe misses. The MCP tool wording is unchanged: the behavior the agent was promised is now the behavior it gets.

Test plan

  • TestChromeClickByIndexHitsTheLastReadNotAnEarlierOne (live Chrome, same skip rule as the other live tests): fails before the fix (index 0 from the last read clicked "top", want bottom), passes after.
  • gofmt -l ., go vet ./..., go test ./... green.

pageInfoScript stamped data-lich-idx on every read and never removed the
stamps of an earlier one. After a scroll and a second read, an element
scrolled out of view still carried an index the new read gave to another
element, and querySelector, which returns the first match in document
order, clicked the stale one with no error.
@omartelo
omartelo merged commit 1d43079 into main Oct 27, 2026
7 checks passed
@omartelo
omartelo deleted the fix/browser-stale-index branch October 27, 2026 23:32
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