Skip to content

Add the snapshot and screenshot queries - #624

Open
vishnuv688 wants to merge 3 commits into
feat/605-trace-query-console-networkfrom
feat/606-trace-query-page-state
Open

vishnuv688 wants to merge 3 commits into
feat/605-trace-query-console-networkfrom
feat/606-trace-query-page-state

Conversation

@vishnuv688

Copy link
Copy Markdown
Member

What & why

The page at an action, for an agent debugging a failed run from its trace (#493), named after @wdio/mcp's trace tools:

Query Returns
traceSnapshot(handle, callId, { locator, text, maxChars, cursor }) The accessibility tree with locators at the action, the nodes and subtrees matching locator or text when scoped, paged within the budget
traceScreenshot(handle, callId, { maxEdge }) The screenshot's bytes and MIME type, its pixel size, the size it fits within maxEdge (1024 by default) and the token cost at that size, or why none came back

Both say which capture they show (capture: its time, the action that took it, and whether that is the action asked about), since an action that took no capture of its own, such as a read, shows the latest earlier one, as the player does. The library returns the image and leaves downscaling to @wdio/mcp, which already depends on sharp; token cost depends only on the pixels the model receives, so it is the same whichever side resizes, and resizing here would add a native or very large dependency to a package whose only dependency is fflate.

Closes #606

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Polish (an improvement to an existing feature)
  • Breaking change (existing behavior changes for users)
  • Documentation
  • Internal (build, CI, dependencies, tooling)

Packages touched

  • shared (types and contracts)
  • core (framework-agnostic capture/reporting)
  • elements (published element/snapshot API: @wdio/elements)
  • service (WebdriverIO adapter)
  • nightwatch-devtools (Nightwatch adapter)
  • selenium-devtools (Selenium adapter)
  • selenium-devtools-py (Selenium Python adapter)
  • backend (server)
  • app (UI)
  • script (page-injected runtime)
  • trace (Trace mode)

Also trace-query, which the list does not name.

Capability matrix

Not applicable yet: no adapter behaviour and no user-facing tool. Whether the trace tools get a row is decided with the docs in #608.

Notes for reviewers

  • Stacked on Add the console and network queries #623 (Add the summary, errors, actions, detail and transcript queries #622, Open a trace from a zip, an output folder or an https URL #620, Add the trace query library package #619 and Share manifest-to-zip resolution and the action key through trace #618 below it). Review this PR's own commit.
  • Which capture an action shows is recorded by the reader, not inferred: TracePlayerData.snapshotSources maps each action's callId to the capture it shows, the action whose frame-snapshot claimed it first, and the image entry it stored (this writer's screenshot override, or the copy an older writer stored under the capture's stamp). It lives on TracePlayerData, which is not in the wire schema, so CommandLog, wire-schema.json and the Python adapter's wire types are untouched. An earlier version put the stamp on CommandLog, which would have forced a PyPI release for a field Python never sends.
  • Snapshot and screenshot always agree. The screenshot reads exactly the claimed capture's image whatever the filmstrip holds (a dense filmstrip names its frames by content hash), and a test checks every action of every committed fixture. A trace from another tool, whose frame snapshots claim no image of ours, falls back to the latest screencast frame at or before the action, with no owner, and never a later frame; tested on a zip shaped like one and checked by hand on a real one.
  • Budget: every snapshot page, header and capture included, holds within maxChars from the 1000-character floor up, header and title fitted to the serialized budget so escape-heavy text cannot overrun it; the every-budget test pages scoped and unscoped snapshots on every fixture and a hostile trace. A screenshot over 16 MB declared size is not read (unavailable says why).
  • Redaction hook: the snapshot's header and every line, and the screenshot's unavailable reason, go through returnedText with their own kinds.
  • App: the A11y tab's line parser moves to shared as parseSnapshotLine, a linear scan (a regular expression would be quadratic on a hostile line), and the serializers and parser share one locator separator. The tab reads names and locators containing → or " whole, no longer takes a quoted string in a locator for a missing name, and still shows and reveals a nameless node by its purpose. @wdio/devtools-app gets a patch changeset.
  • Residual: the first action to claim a capture owns it, which matches the action that took it while claim order matches completion order; a batched assert ending in the same millisecond as a click's capture, or overlapping un-awaited calls, can credit the wrong one. Fixing that needs the writer to record the owner, a format change.
  • Token cost of an image (Anthropic's API, about width x height / 750 at the size the model receives, after scaling a long edge over 1568 down): a 1600x757 frame bills about 1550 tokens unfitted and 661 fitted to 1024; a 1080x2400 phone screen about 1480 and 630. A snapshot of the fixtures' pages is 536 to 718 characters, so the tool descriptions should point agents at a scoped snapshot before an image.
  • Checks run. pnpm build, pnpm test (374 files, 5222 tests), pnpm lint (0 errors) and pnpm typecheck pass; leak grep clean on the trace-query dist. The new A11y-tab component test runs in CI's component-tests.

Screenshots / recordings

Not applicable: the A11y tab looks as before, apart from names and locators it used to misread.

🤖 Generated with Claude Code

traceSnapshot and traceScreenshot give an agent the page at an action,
named after @wdio/mcp's trace tools. The snapshot is the accessibility
tree with locators, scoped to a subtree by locator or text and paged
within its budget; the screenshot is the image with the size it fits
at and what it costs in tokens, left to the caller to downscale.

Both say whose capture they show. The reader now records, outside the
wire contract, which capture each action shows, which action took it
and the image it stored, from the zip's own frame-snapshot claims, so
an action that took none shows the latest earlier one and the two
queries always agree. A trace from another tool, whose frames claim
no action, falls back to the latest screencast frame at or before the
action, never a later one.

The A11y tab's line parser moves into shared, reading a name or
locator that contains an arrow or a quote whole.

Closes #606
@vishnuv688 vishnuv688 self-assigned this Nov 27, 2026
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
devtools Ready Ready Preview Nov 27, 2026 9:44am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; all previous findings are addressed and no new actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; all previous findings are addressed and no new actionable issues were found.

Summary

Adds snapshot and screenshot queries with capture details, scoped snapshot pages, image sizes, and token estimates.

  • Trace queries show the accessibility tree at an action.
  • Trace queries return the screenshot an action shows.
  • The A11y tree keeps quoted names and locators intact.

Reviews (3) · Last reviewed commit: "Read pre-escaping snapshot names literal..." · Reviewed by Greptile

Comment on lines +134 to +140
for (let at = text.indexOf('"', start); at !== -1;) {
const after = at + 1
if (
after === text.length ||
closers.some((closer) => text.startsWith(closer, after))
) {
return { value: text.slice(start, at), end: after }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Quoted labels break locators

A name containing a quote followed by the locator separator is split too early. The writer can produce button "Press " → next" → #go, but this parser returns next" → #go as the locator instead of #go. The A11y tab then copies and highlights the wrong locator, and traceSnapshot finds no match for { locator: "#go" }.

Make names and purposes unambiguous when written and read, and add a round-trip test for this case.

Knowledge Base Used:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in f5eb5a0: both serializers now write names and purposes through the shared snapshotNodeLabel, which escapes quotes and backslashes, and parseSnapshotLine skips escaped quotes while still reading older unescaped traces. Tests in packages/shared and packages/trace round-trip Press " → next to the locator #go.


Generated by Claude Code

bytes,
size,
fit,
tokens: Math.ceil((fit.width * fit.height) / IMAGE_PIXELS_PER_TOKEN)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Large images appear too expensive

tokens overstates the documented image cost when maxEdge exceeds 1568. The supported { maxEdge: 4000 } case keeps a 1080×2400 image and returns 3456 tokens, while the README says that image bills about 1480 after the API scales it down. Callers may reject an image that would fit their token budget.

Keep fit at the requested size, but apply the documented API scaling when calculating tokens, and test a large maxEdge.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in f5eb5a0: fit stays at the requested size, but tokens is now computed after scaling it to the API's 1568-pixel long edge, so { maxEdge: 4000 } on a 1080x2400 frame returns 1477. A test covers that case.


Generated by Claude Code

A name holding a quote followed by the locator separator was read back
with part of the name as its locator, so the A11y tab and traceSnapshot
used the wrong one; serializers now escape quotes and backslashes, and
the reader still accepts traces written before. traceScreenshot counted
tokens at the fitted size even above 1568 pixels, overstating what the
API bills after it scales the image down.

Addresses review comments on #624
Comment thread packages/shared/src/snapshot-format.ts Outdated
Comment on lines +164 to +169
!escapedAt(text, at, start) &&
(after === text.length ||
closers.some((closer) => text.startsWith(closer, after)))
) {
return {
value: text.slice(start, at).replace(/\\([\\"])/g, '$1'),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Older snapshot names change

Older traces stored backslashes literally, but quotedAt now always treats them as escapes. A saved name ending in a backslash loses its name because the closing quote is skipped. Two literal backslashes also become one. This changes names shown in the A11y tab and breaks reveal-by-name matching.

Distinguish the new encoding from older snapshot text before applying escape rules.

Knowledge Base Used: Shared contracts and transport

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in adbe162: a name or purpose that needs escaping now opens with a backslash (\"...), which no older trace writes there, so older lines are read literally (a trailing backslash and \\ keep their text) and plain names are written exactly as before. Tests in packages/shared/tests/snapshot-format.test.ts cover both older-trace cases and the new round trip.


Generated by Claude Code

Comment thread packages/shared/src/snapshot-format.ts Outdated
Comment on lines +124 to +125
function quoteSnapshotText(text: string): string {
return `"${text.replace(/[\\"]/g, '\\$&')}"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Searches miss quoted names

Escaping names and purposes changes what traceSnapshot finds by text. For example, { text: 'Say "hi"' } no longer finds a node named Say "hi" because matcherOf searches the escaped node.label, not the decoded name. Callers can get an empty snapshot even though the element was captured.

Match against the decoded role, name and purpose, and add a scoped-query test containing quotes and backslashes.

Knowledge Base Used: Shared contracts and transport

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in adbe162: matcherOf in packages/trace-query/src/snapshot-scope.ts now matches the decoded role, name and purpose instead of the escaped label, as SnapshotScope.text documents. A scoped-query test finds Say "hi" and C:\dir by text.


Generated by Claude Code

Escaping every quoted name changed how older traces read: a stored name
ending in a backslash lost its name and a double backslash collapsed. A
value that needs escaping now opens with a backslash, which no older
trace writes there, so older lines keep their literal reading and plain
names are written exactly as before. A snapshot text search matched the
escaped label, so a name holding a quote was missed; it now matches the
decoded role, name and purpose, as its documentation says.

Addresses review comments on #624

This branch has not been deployed

No deployments
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