From ada71da92d78c28494a0421b909e0772f837fa1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 5 Nov 2026 08:19:17 +0000 Subject: [PATCH] fix(cli): capture takes a refused screenshot once more before naming it (#7222) * fix(cli): capture takes a refused screenshot once more before naming it * fix(cli): keep the screenshot retry's doc to one line * test(cli): refuse the same capture shot twice now that one refusal is retried --- .../src/capture/coreExtractionPhase.test.ts | 10 +++++----- .../cli/src/capture/screenshotCapture.test.ts | 18 +++++++++++++++++- packages/cli/src/capture/screenshotCapture.ts | 5 +++-- packages/cli/src/commands/snapshot.ts | 13 +------------ .../screenshotOnceMore.test.ts} | 2 +- packages/cli/src/utils/screenshotOnceMore.ts | 13 +++++++++++++ 6 files changed, 40 insertions(+), 21 deletions(-) rename packages/cli/src/{commands/snapshot.screenshotRetry.test.ts => utils/screenshotOnceMore.test.ts} (95%) create mode 100644 packages/cli/src/utils/screenshotOnceMore.ts diff --git a/packages/cli/src/capture/coreExtractionPhase.test.ts b/packages/cli/src/capture/coreExtractionPhase.test.ts index 1593a3e1e5..944508e6ee 100644 --- a/packages/cli/src/capture/coreExtractionPhase.test.ts +++ b/packages/cli/src/capture/coreExtractionPhase.test.ts @@ -75,15 +75,15 @@ afterEach(() => { const REFUSED = "Protocol error (Page.captureScreenshot): Unable to capture screenshot"; -/** `refuseShot`: the screenshot call (1-based) Chrome refuses. */ -async function extract(expireScreenshot = false, refuseShot?: number) { +/** `refuseShots`: the screenshot calls (1-based) Chrome refuses. */ +async function extract(expireScreenshot = false, refuseShots: number[] = []) { vi.useFakeTimers(); const outputDir = mkdtempSync(join(tmpdir(), "hf-core-budget-")); mkdirSync(join(outputDir, "extracted")); const state = createPartialCaptureState({ url: "https://example.com", outputDir }); let shots = 0; const screenshot = vi.fn(async () => { - if (++shots === refuseShot) throw new Error(REFUSED); + if (refuseShots.includes(++shots)) throw new Error(REFUSED); if (expireScreenshot) control.remaining = 0; return new Uint8Array(24); }); @@ -162,8 +162,8 @@ describe("core extraction screenshot budget", () => { expect(screenshot).not.toHaveBeenCalled(); }, ); - it("names the screenshot Chrome refused and keeps the rest", async () => { - const { result, warnings } = await extract(false, 2); + it("names the screenshot Chrome refused twice and keeps the rest", async () => { + const { result, warnings } = await extract(false, [2, 3]); expect(result.screenshots).toEqual(["screenshots/scroll-000.png", "screenshots/full-page.png"]); expect(warnings).toContain( `2/3 requested screenshot files captured: could not capture screenshots/scroll-100.png (scrolled to 8920px): ${REFUSED}`, diff --git a/packages/cli/src/capture/screenshotCapture.test.ts b/packages/cli/src/capture/screenshotCapture.test.ts index c24f93a6b5..716501132d 100644 --- a/packages/cli/src/capture/screenshotCapture.test.ts +++ b/packages/cli/src/capture/screenshotCapture.test.ts @@ -299,12 +299,28 @@ describe("captureScrollScreenshots — a shot Chrome will not take", () => { } } - it("names the tile it could not take, and still takes the tiles after it and the plate", async () => { + it("takes again, once, a tile Chrome refused, so a passing refusal costs no shot", async () => { let calls = 0; const result = await captureWith(async () => { if (++calls === 2) throw refusal; return pngBuffer(1080); }); + expect(result.failures).toEqual([]); + expect(result.written).toEqual([ + "full-page.png", + "scroll-000.png", + "scroll-055.png", + "scroll-100.png", + ]); + }); + + it("names the tile Chrome refused twice, and still takes the tiles after it and the plate", async () => { + let calls = 0; + const result = await captureWith(async () => { + calls++; + if (calls === 2 || calls === 3) throw refusal; + return pngBuffer(1080); + }); expect(result.interruption).toBeNull(); expect(result.failures).toEqual([ { file: "screenshots/scroll-055.png", scrollY: 3780, message: refusal.message }, diff --git a/packages/cli/src/capture/screenshotCapture.ts b/packages/cli/src/capture/screenshotCapture.ts index 35b53fa1d8..ccdf1d7f26 100644 --- a/packages/cli/src/capture/screenshotCapture.ts +++ b/packages/cli/src/capture/screenshotCapture.ts @@ -8,6 +8,7 @@ import { DEFAULT_MAX_SCREENSHOTS } from "./types.js"; import { CaptureDirRefusedError } from "./captureErrors.js"; import { normalizeErrorMessage } from "../utils/errorMessage.js"; +import { screenshotOnceMore } from "../utils/screenshotOnceMore.js"; import type { Page } from "puppeteer-core"; import { isDegradableEvaluateTimeoutError } from "./captureTimeout.js"; import { waitForPageReady } from "./navigateForCapture.js"; @@ -124,7 +125,7 @@ export async function captureFullPagePlate( if (docHeight > MAX_PLATE_HEIGHT_PX) return { kind: "omitted", reason: "height-limit" }; if ((budget.remainingMs?.() ?? 1) <= 0) return { kind: "omitted", reason: "budget-exhausted" }; - const buffer = await page.screenshot({ type: "png", fullPage: true }); + const buffer = await screenshotOnceMore(() => page.screenshot({ type: "png", fullPage: true })); // Confirm what Chrome produced instead of trusting the measurement: the capture itself can // trigger another round of lazy loading. A clipped plate is undetectable downstream — the // skill only teaches the tile fallback when the file is *absent* — so emit nothing rather @@ -324,7 +325,7 @@ export async function captureScrollScreenshots( if (budgetSpent()) break; let buffer: Uint8Array; try { - buffer = await page.screenshot({ type: "png" }); + buffer = await screenshotOnceMore(() => page.screenshot({ type: "png" })); } catch (err) { result.failures.push(shotFailure(err, `screenshots/${filename}`, finalPositions[i]!)); continue; diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index 63098964bd..84220342c4 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -1,7 +1,6 @@ import { DEFAULT_CONFIG, buildZeroDurationDiagnostic, - isTransientBrowserError, downloadToTemp, evaluateHfDiagnostic, extractMediaMetadata, @@ -10,6 +9,7 @@ import { isHdrColorSpace, sdrCanvasColourFilters, } from "@hyperframes/engine"; +import { screenshotOnceMore } from "../utils/screenshotOnceMore.js"; import { parseTimeoutMs } from "../utils/timeoutArg.js"; import type { Page } from "puppeteer-core"; import { reportArtifacts } from "../utils/toolContext.js"; @@ -355,17 +355,6 @@ interface SnapshotCompositePage { }): Promise; } -export async function screenshotOnceMore(shoot: () => Promise): Promise { - try { - return await shoot(); - } catch (err) { - if (!isTransientBrowserError(err)) throw err; - return shoot().catch(() => { - throw err; - }); - } -} - /** Recapture canvas composites once the decoded video overlays have been injected. */ export async function recaptureSnapshotComposite(page: SnapshotCompositePage): Promise { const hasPageComposite = await page.evaluate(async () => { diff --git a/packages/cli/src/commands/snapshot.screenshotRetry.test.ts b/packages/cli/src/utils/screenshotOnceMore.test.ts similarity index 95% rename from packages/cli/src/commands/snapshot.screenshotRetry.test.ts rename to packages/cli/src/utils/screenshotOnceMore.test.ts index 5b29c6f00f..550c0ff5d9 100644 --- a/packages/cli/src/commands/snapshot.screenshotRetry.test.ts +++ b/packages/cli/src/utils/screenshotOnceMore.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it, vi } from "vitest"; -import { screenshotOnceMore } from "./snapshot.js"; +import { screenshotOnceMore } from "./screenshotOnceMore.js"; const dropped = () => new Error("Protocol error (Page.captureScreenshot): Unable to capture screenshot"); diff --git a/packages/cli/src/utils/screenshotOnceMore.ts b/packages/cli/src/utils/screenshotOnceMore.ts new file mode 100644 index 0000000000..fd14c3d4be --- /dev/null +++ b/packages/cli/src/utils/screenshotOnceMore.ts @@ -0,0 +1,13 @@ +import { isTransientBrowserError } from "@hyperframes/engine"; + +/** Retries once on a passing Chrome refusal; a second failure rethrows the first error. */ +export async function screenshotOnceMore(shoot: () => Promise): Promise { + try { + return await shoot(); + } catch (err) { + if (!isTransientBrowserError(err)) throw err; + return shoot().catch(() => { + throw err; + }); + } +}