feat(mcp): add compress option to browser_snapshot to collapse repeated ARIA nodes#41507
Closed
Josef-Le wants to merge 1 commit into
Closed
feat(mcp): add compress option to browser_snapshot to collapse repeated ARIA nodes#41507Josef-Le wants to merge 1 commit into
Josef-Le wants to merge 1 commit into
Conversation
…nodes Adds a `compress?: boolean` parameter to the `browser_snapshot` MCP tool. When set, a two-pass algorithm collapses repeated structural patterns in the ARIA snapshot YAML — keeping the first 10 occurrences of any pattern that appears more than 100 times — and emits a trailing note explaining how to enumerate the full list via browser_evaluate(). This targets pages with large lists, data grids, or navigation menus where the snapshot can grow to thousands of lines. On a 150-item GitHub issues page the output shrinks from ~1 800 lines to ~80 lines while preserving all interactive elements (buttons, inputs, links) unconditionally. Adds ariaCompression.ts with the pure compression function, and four tests covering the happy path, passthrough on small lists, interactive-element preservation, and the compress: false escape hatch.
Member
|
Let's come to a conclusion in the issue first. @pavelfeldman could you take another look there? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
compress?: booleanparameter to thebrowser_snapshotMCP tool.When enabled, a two-pass O(n) algorithm collapses repeated structural patterns in the ARIA snapshot YAML — keeping the first 10 occurrences of any pattern that appears more than 100 times — and emits a trailing note explaining how to enumerate the full list via
browser_evaluate().Motivation (concrete evidence from real pages)
Closes #41395. Evidence requested by @pavelfeldman posted in the issue thread.
Three real-world examples measured with Python Playwright 1.60.0 + headless Chromium:
The agent receives a compressed snapshot that preserves the first 10 items (enough to learn the structure) and a summary note. It can then use
browser_evaluate()for targeted queries on the remaining items.Algorithm
Two-pass, O(n) time and space:
(indent, signature)counts. Signature normalises refs, accessible names, and numbers so structurally identical siblings share the same key.Files changed
packages/playwright-core/src/tools/backend/ariaCompression.ts— new 168-line module (pure function, no I/O, zero dependencies)packages/playwright-core/src/tools/backend/snapshot.ts— +2/-1 (addscompressparam to schema)packages/playwright-core/src/tools/backend/response.ts— +8/-2 (wires compress into snapshot path)tests/mcp/snapshot-compression.spec.ts— new 127-line integration test: fires on 150-item list, no-op on small lists, no-op withcompress: false, preserves interactive elements