Skip to content

fix(snapshot): preserve element identity across renderer changes - #2788

Closed
steipete wants to merge 1 commit into
ChromeDevTools:mainfrom
steipete:codex/fix-snapshot-element-identities
Closed

steipete wants to merge 1 commit into
ChromeDevTools:mainfrom
steipete:codex/fix-snapshot-element-identities

Conversation

@steipete

Copy link
Copy Markdown

Snapshot UIDs can collide across renderer documents or be reused after a renderer replacement. A later input, evaluation, or screenshot request can then resolve the wrong element. Extra DOM nodes can also disappear when their backend ID matches an accessibility node in another frame.

This repair keeps identity in the snapshot owner:

  • Reuse an AX-node UID only when its captured CDP session, document loader, and backend node identity are complete and unambiguous. Give every ambiguous occurrence a fresh UID and evict its previous stable mapping.
  • Validate the captured frame session and loader generation before and after lazy element-handle resolution, so an old UID cannot resolve a replacement renderer's node.
  • Scope extra-node deduplication, ancestor lookup, and descendant reparenting to the actual frame. Return a cloned handle for each action so disposing one action's handle does not invalidate the retained snapshot handle.
  • Pass the known frame when converting third-party DOM results back to UIDs. Refuse an ambiguous backend-only lookup instead of choosing a node from another frame.

The repaired consumers are reference-based input, evaluate_script, take_screenshot, and third-party DOM-result conversion. No tool schema, dependency, or package version changes are included.

Validation:

  • Five regression cases cover missing/empty identity, duplicate keys across reordering and disappearance, a replacement session repeating loader/backend metadata, stale UIDs after renderer replacement, and extra iframe nodes with backend-ID collisions. Each demonstrated failure without its corresponding repair.
  • The Chromium cases use synthetic local pages with --site-per-process and assert that backend IDs really collide. They verify stale-reference rejection, fresh-snapshot recovery, correct iframe handle routing and descendants, and successive actions using the retained extra node.
  • Full npm run test and npm run check-format passed on Linux. npm run bundle and npm run test:notices:no-build passed as well. A locally packed tarball was extracted into a clean directory and its CLI startup was verified.
  • No snapshot baselines or assertions were relaxed.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR:

  • Revert the manual ## Unreleased addition in CHANGELOG.md. CHANGELOG.md is managed automatically by Release Please and should not be edited manually in pull requests.
  • Rebase tests/mocks.ts onto main and import CDPSession from ../src/third_party/index.js instead of puppeteer-core. main replaced parser from src/config/mcp-options.ts with ConfigParser from src/config/ConfigParser.js, and runtime Puppeteer classes in tests/mocks.ts are re-exported through src/third_party/index.js.
  • Make nodesByFrame optional in the TextSnapshot constructor in src/TextSnapshot.ts (defaulting to new Map()). This avoids forcing existing new TextSnapshot(...) callers in tests/McpPage.test.ts to pass an empty nodesByFrame map.
  • Convert the two --site-per-process browser tests in tests/TextSnapshot.test.ts (rejects old UIDs when a replacement renderer reuses backend IDs and keeps extra iframe nodes separate from colliding main-frame backend IDs) into mock-based unit tests. Per AGENTS.md, unit tests with mocked frames and handles should be preferred over launching real browsers, especially when the browser tests rely on non-deterministic backendNodeId collisions across 32–64 DOM elements.
@OrKoN

OrKoN commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Closing as stale

@OrKoN OrKoN closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants