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
This commit is contained in:
Miguel Ángel
2026-11-05 08:19:17 +00:00
committed by GitHub
parent 7bebcb9fcb
commit ada71da92d
6 changed files with 40 additions and 21 deletions
@@ -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}`,
@@ -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 },
@@ -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;
+1 -12
View File
@@ -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<Uint8Array>;
}
export async function screenshotOnceMore<T>(shoot: () => Promise<T>): Promise<T> {
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<void> {
const hasPageComposite = await page.evaluate(async () => {
@@ -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");
@@ -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<T>(shoot: () => Promise<T>): Promise<T> {
try {
return await shoot();
} catch (err) {
if (!isTransientBrowserError(err)) throw err;
return shoot().catch(() => {
throw err;
});
}
}