Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): text-only browser snapshots skip the PNG encode (66 ms → 20 ms median) #30305

MergedYuruzuu(GitHub profile, opens in a new tab)merged 9 commits intomain fromrefs/pull/30305/headmerged by juliusmarminge View on GitHub

Problem

Every preview_snapshot call encodes a full-resolution PNG, even though most calls only need the page text and refs. By default the tool returns only the screenshot's width and height. To get those numbers, the server browser captures a 2560×1600 PNG, decodes it in the page, shrinks it to 1280×800, re-encodes it, and then drops the pixels. Agents take snapshots after nearly every browser step, so they pay this cost each time.

Change

When the call sets neither includeImage nor save, the MCP handler now sends pixelsRequired: false to the host:

  • ServerBrowserPage.snapshotMetadata takes a quality-10 JPEG of the same viewport and reads the size from its frame header. It reports the size the PNG path would have shrunk to. The rest of the snapshot is unchanged: tree, refs, text, masking, retry and deadline.
  • It still takes a real capture. The audit tried computing the size from page metrics, but that size drifted from the PNG's. Capturing the frame at a smaller scale broke backdrop-filter rendering.
  • includeImage and save still get the same PNG as before. If a host returns no pixels when pixels were requested, the call fails with a clear error. It never saves or returns an empty image.
  • The text the model sees does not change. The structured result still reports mimeType: "image/png" and the same size, and the tool's description and input schema are unchanged.
  • Older hosts ignore the new field and return a PNG, which the handler accepts. That covers mixed versions across linked environments.

Server only. Desktop tabs go through the same ServerBrowser path over CDP, with renderScale: "display". Web and mobile clients don't change.

Scope and approval

This qualifies for the small, focused bug-fix exception in CONTRIBUTING.md. Encoding an image only to throw it away is an obvious performance defect. There is no feature, product default, or model-facing tool text change. The new contract field is optional, so older hosts are unaffected.

Verification

Benchmark: 20 back-to-back snapshot calls on one page in headless Chromium, using the same launch flags and 1280×800 @2× context as ServerBrowserContexts. main and this branch ran 7 times each per page, alternating which went first. Each value is the median per-call time of one run.

Pagemain medianThis branch median
Plain white page, 120 text cards66.4 ms19.9 ms (3.3×)
Gradients, shadows, sticky backdrop-filter header703.1 ms24.9 ms (28×)

Node CPU per call fell from 5.6 to 2.2 ms on the plain page and from 17.9 to 2.4 ms on the gradient page. In all 28 runs, both sides reported the same screenshot size (1280×800), ref count (208) and visible text length.

Tests:

  • ServerBrowserPage.test.ts › captures a plain page at the shrunk size now also checks that snapshotMetadata reports the same 1280×800 as the PNG path, in real Chromium. A new JPEG screenshot dimensions case covers frame-header parsing and rejects truncated or invalid frames.
  • McpHttpServer.test.ts: preserves PNG output for snapshot options covers all four includeImage/save combinations. rejects a metadata-only response when pixels are requested covers a host that returns no pixels. The existing image-option test now asserts which requests send pixelsRequired: false.
  • vp run check:changed passes.

Not checked here: desktop (Electron) tabs at non-default zoom. The audit checked capture, size and ref clicks at 100% and 125% zoom over direct CDP. I didn't repeat that on this branch. I also didn't time whole agent turns.

Made with Claude Opus 5.5 via Claude Code in T3 Code, from an audit by GPT-6.1 Sol via Codex.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. Approvability

      Verdict: Approved at 0bbb158

      Macroscope's review found this PR approvable — Text-only browser snapshots avoid PNG encoding while preserving page metadata; image and save requests retain PNG capture. The change is limited to internal snapshot processing without changing user-facing defaults or expanding data exposure.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      Note

      Reviews paused

      It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

      • Last reviewed commit: 44901729c720636dd74350d2354dc4241106f9b8
      • Latest commit, not reviewed: dcbf79281e81e71dc09e30ee1ca6abababc0eedb

      Use the following commands to manage reviews:

      • @coderabbitai resume to resume automatic reviews.
      • @coderabbitai review to trigger a single review.

      Use the checkboxes below for quick actions:

      • Resume reviews
      • Trigger review

      No actionable comments were generated in the recent review.

      Walkthrough

      Preview snapshots can return screenshot dimensions without PNG pixels when images are not requested. Requests to save or include an image require PNG data. Metadata-only capture uses JPEG dimensions.

      Changes: Metadata-aware preview snapshots

      Layer / File(s)Summary
      Snapshot contract and routing
      packages/contracts/src/previewAutomation.ts, apps/server/src/mcp/toolkits/preview/*, apps/server/src/preview/ServerBrowser.ts
      Snapshot inputs include the optional pixelsRequired flag. The preview operation returns metadata when pixels are not required, and the result contract accepts metadata or a full snapshot.
      Metadata-only capture
      apps/server/src/preview/ServerBrowserPage.ts, apps/server/src/preview/ServerBrowserPage.test.ts
      Metadata-only capture uses a JPEG to determine scaled dimensions without returning image data. Both capture modes use shared snapshot orchestration and retry handling.
      MCP PNG output and validation
      apps/server/src/mcp/McpHttpServer.ts, apps/server/src/mcp/McpHttpServer.test.ts
      The MCP handler decodes and saves PNG pixels only when requested. It returns an invalid_request failure if requested pixels are missing or not PNG. Tests cover image output, saved bytes, and metadata-only responses.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Refactor

      Sequence Diagram(s)

      sequenceDiagram
        participant MCPCaller
        participant PreviewSnapshot
        participant ServerBrowser
        participant ServerBrowserPage
        MCPCaller->>PreviewSnapshot: Request snapshot without save or image inclusion
        PreviewSnapshot->>ServerBrowser: Set pixelsRequired to false
        ServerBrowser->>ServerBrowserPage: Call snapshotMetadata
        ServerBrowserPage-->>ServerBrowser: Return screenshot dimensions
        ServerBrowser-->>PreviewSnapshot: Return snapshot metadata
        PreviewSnapshot-->>MCPCaller: Return snapshot response

      Possibly related PRs

      Suggested reviewers: t3dotgg

      Merge Risk: Minimal · previous review 44901

      Text-only preview snapshots skip the PNG encode and still return screenshot dimensions. Image and save requests keep PNG output. No actionable merge-blocking risk remains. The author did not check Electron zoom or whole-turn performance.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title names the main change: text-only browser snapshots skip the PNG encode when pixels are not requested, which is the core performance fix. It follows the conventional commit format and gives t…
      Description checkPassedThe description follows the repository template. It has Problem, Change, Scope, and Verification sections, and it includes the conventional commit style title context. Scope explains why the change is…
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Title check

      Explanation

      The title names the main change: text-only browser snapshots skip the PNG encode when pixels are not requested, which is the core performance fix. It follows the conventional commit format and gives the benchmark result. It is accurate to the changeset.

      Full details: Description check

      Explanation

      The description follows the repository template. It has Problem, Change, Scope, and Verification sections, and it includes the conventional commit style title context. Scope explains why the change is a small bug fix and links no issue. The Verification section gives benchmark data, test names, and untested areas, and it names the model and harness used. The description is complete enough to pass.

      Finishing Touches

      Generate unit tests (beta)

      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between 076d3fa1283563a8a9844a36dde6e0ae8738c79d and 44901729c720636dd74350d2354dc4241106f9b8.

      Files selected for processing: 2

      • apps/server/src/mcp/McpHttpServer.ts
      • apps/server/src/preview/ServerBrowser.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

    4. maria-rckscommented

      Note

      Written by claude-opus-5-5 on behalf of Maria

      Merged main (b68c956d61)

      main added a full-resolution fullScreenshot for save, which conflicted with this PR's pixelsRequired path. both are kept: save still requests fullScreenshot and writes the full-res PNG, and text-only snapshots still send pixelsRequired: false and skip the PNG encode. the screenshot test now checks the full size and the metadata size together. no other changes. no bot findings were open.

      blacksmith: vp run check:changed  (format, lint, knip, typecheck, 6 test files) pass
      blacksmith: vp test run src/preview/ServerBrowserPage.test.ts src/mcp/McpHttpServer.test.ts  62 passed

      the runtime verification below was done on the previous head 0bbb1586c3 and was not repeated after the merge.

      Verified in a real dev environment

      verified unchanged head 0bbb1586c345c9acf6268d0fa7fe21a4a8f588d6 on linux, after reproducing the wasted png capture on github/main (15707a84b03e). separate checkouts, fresh isolated state, vp i in both, one background dev server at a time. no source changes, mocks, fixtures, or seeded conversations.

      repo playwright with /usr/bin/google-chrome drove pairing, adding a real git project, selecting codex gpt-6.1-sol, and submitting real provider turns. those turns exercised the actual t3-code mcp preview tools.

      • ten sequential default snapshots on the same real https://example.com/ page retained text, refs, and 1280×800 metadata with no image blocks or errors. a non-pausing node inspector logpoint confirmed main captured png for these calls; the pr captured jpeg at quality 10.
      • elapsed time around each awaited mcp call had a median of 121 ms on main and 46 ms on the pr. this measures this vm and page, not whole-turn performance.
      • includeImage, save, and both together retained real png output. matching main/pr captures were byte-identical: 168,230 bytes, valid png signature, sha256 a73e96f9bcad5872bf6a52c4029c237701add5666b0b09cf230f3aff9e07f2e9.
      • fresh snapshot refs navigated to the iana page with http 200; browser back restored the original content and usable refs. text/image dimensions also matched at an 800×600 viewport (both reported 1280×960 under the existing scaling), then returned to 1280×800 after restoring the viewport.
      blacksmith: vp test run apps/server/src/mcp/McpHttpServer.test.ts apps/server/src/preview/ServerBrowserPage.test.ts
      2 files passed; 61 tests passed
      main default mcp ms: 184,125,117,130,110,106,137,129,103,116
      pr default mcp ms:   43,41,49,43,57,45,59,44,63,47

      command transcript

      main: real codex turn reports snapshot timing and png/save checks

      pr: same page through real codex reports faster text snapshots and preserved image/save behavior

      Not verified

      native electron tabs, browser zoom, native ios/android clients, and mixed-version linked hosts were not exercised on this linux web setup. deadline, retry, and masking coverage here comes from the existing focused tests. whole-agent-turn performance was not measured.

    5. juliusmarminge added labelstakeover:in-progress

    6. Evidence

      Measured through the real T3 server on the same Linux machine, Chromium 157.0.8103.78, 1280×800 @2, with fresh state and an active approval replay thread. Served 120 plain cards and the same cards with gradients, shadows and a sticky backdrop-filter header. Alternated page order over seven batches per page; each batch warmed up once, then timed 20 sequential preview_snapshot {} calls.

      BeforeAfter
      Before: measured snapshot timingsAfter: measured snapshot timings
      88c42f471bdcbf79281e
      Plain: 81.7 ms, run range 77.9–100.1 msPlain: 49.0 ms, run range 48.3–51.0 ms
      Styled: 485.9 ms, run range 452.8–505.6 msStyled: 67.5 ms, run range 65.9–71.6 ms
      Live measurement recordingLive measurement recording

      Plain improved 1.67× and styled improved 7.20×. Both reductions exceed the sum of the before/after run spreads. Text, all 125 refs and boxes, and 1280×800 dimensions matched after normalizing generated snapshot scope IDs. Text-only responses contained no image parts. Explicit image and save requests retained PNG output, byte-identical across commits.

      Median timings and full run ranges

      Measurement driver and page seed · Setup script · Capture-generation script · Reproduction instructions

      Base raw timings and output · Head raw timings and output · Complete evidence bundle, including page PNGs and saved PNGs · Exploratory data

      Not shown:

      • The absolute 19.9/24.9 ms claims were not reproduced: this machine measured 49.0/67.5 ms on head, while demonstrating improvements beyond run spread.
      • The original triage HTML was not supplied. This reconstruction uses 120 short-label cards and returns 125 refs rather than 106.
      • The example.com benchmark was not repeated.
    7. juliusmarminge added labelstakeover:ready

    8. juliusmarminge removed labelstakeover:in-progress

    9. juliusmarminge merged this5c85e93

    10. juliusmarminge closed this

    11. juliusmarminge removed labelstakeover:ready

Sign in to join this conversation.