Skip to content

Commit ffc453a

Browse files
jrusso1020claude
andcommitted
feat(producer): add an auto value for PRODUCER_FORCE_SCREENSHOT
auto captures as false does, except that a page holding a WebGL or WebGPU canvas is captured by screenshot when GL is software. The choice is made once, after the page opens, and a distributed plan records it for every chunk. Unset, true and false behave as before. Signed: Rames Jusso Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 8d220e7 commit ffc453a

7 files changed

Lines changed: 268 additions & 16 deletions

File tree

‎packages/engine/src/config.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,34 @@ describe("resolveConfig", () => {
489489
expect(config.forceScreenshot).toBe(false);
490490
});
491491

492+
it("PRODUCER_FORCE_SCREENSHOT=auto leaves BeginFrame on and marks the per-page choice", () => {
493+
setEnv("PRODUCER_BROWSER_GPU_MODE", "software");
494+
setEnv("PRODUCER_FORCE_SCREENSHOT", "auto");
495+
const config = resolveConfig();
496+
expect(config.forceScreenshot).toBe(false);
497+
expect(config.forceScreenshotExplicitlyOptedOut).toBe(true);
498+
expect(config.screenshotForAcceleratedCanvas).toBe(true);
499+
});
500+
501+
it("unset, true and false never mark the per-page choice", () => {
502+
setEnv("PRODUCER_BROWSER_GPU_MODE", "software");
503+
const resolved = [undefined, "true", "false"].map((value) => {
504+
if (value === undefined) unsetEnv("PRODUCER_FORCE_SCREENSHOT");
505+
else setEnv("PRODUCER_FORCE_SCREENSHOT", value);
506+
const config = resolveConfig();
507+
return [
508+
config.forceScreenshot,
509+
config.forceScreenshotExplicitlyOptedOut,
510+
config.screenshotForAcceleratedCanvas,
511+
];
512+
});
513+
expect(resolved).toEqual([
514+
[true, undefined, undefined],
515+
[true, undefined, undefined],
516+
[false, true, undefined],
517+
]);
518+
});
519+
492520
it("explicit programmatic opt-out is honored on software", () => {
493521
setEnv("PRODUCER_BROWSER_GPU_MODE", "software");
494522
unsetEnv("PRODUCER_FORCE_SCREENSHOT");
@@ -555,6 +583,11 @@ describe("resolveConfig", () => {
555583
}
556584
});
557585

586+
it("treats PRODUCER_FORCE_SCREENSHOT=auto as an opt-out on software", () => {
587+
const env = { PRODUCER_FORCE_SCREENSHOT: "auto" } as NodeJS.ProcessEnv;
588+
expect(shouldClampToScreenshotForConcreteGpu("software", false, env)).toBe(false);
589+
});
590+
558591
it("honors the programmatic opt-out via opts.programmaticOptOut on software", () => {
559592
// The auto→software probe path is what this really guards: `resolveConfig`
560593
// sets `forceScreenshotExplicitlyOptedOut = true` when the caller passed

‎packages/engine/src/config.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ export interface EngineConfig {
115115
* the runtime probe fires). Not intended to be set by callers.
116116
*/
117117
forceScreenshotExplicitlyOptedOut?: boolean;
118+
/**
119+
* INTERNAL. Set to `true` by `resolveConfig` for `PRODUCER_FORCE_SCREENSHOT=auto`:
120+
* capture as with `false`, except that a page holding a WebGL or WebGPU canvas is
121+
* captured by screenshot when the browser's GL is software. Not intended to be set by callers.
122+
*/
123+
screenshotForAcceleratedCanvas?: boolean;
118124
/** How long a BeginFrame capture may take to draw its first frame before the render retries by screenshot. */
119125
beginFrameFirstFrameDeadlineMs?: number;
120126
/**
@@ -351,6 +357,7 @@ const OPTIONAL_ENGINE_CONFIG_FIELDS = [
351357
"expectedChromiumMajor",
352358
"pageSideCompositingAutoDisabled",
353359
"forceScreenshotExplicitlyOptedOut",
360+
"screenshotForAcceleratedCanvas",
354361
"beginFrameFirstFrameDeadlineMs",
355362
"streamingEncodeAutoDisabledOnWin32Compound",
356363
"runtimeManifestPath",
@@ -542,6 +549,7 @@ function validateOptionalEngineConfigFields(config: Record<string, unknown>): vo
542549
for (const field of [
543550
"pageSideCompositingAutoDisabled",
544551
"forceScreenshotExplicitlyOptedOut",
552+
"screenshotForAcceleratedCanvas",
545553
"streamingEncodeAutoDisabledOnWin32Compound",
546554
] as const) {
547555
if (config[field] !== undefined && typeof config[field] !== "boolean") {
@@ -1073,8 +1081,11 @@ export function resolveConfig(overrides?: Partial<EngineConfig>): EngineConfig {
10731081
//
10741082
// Explicit opt-out (env or programmatic override) is honored so BeginFrame-
10751083
// on-software debugging remains possible.
1084+
// `auto` opts out the same way as `false`; the per-page choice is made once the page is open.
1085+
Object.assign(merged, autoCaptureFields(env("PRODUCER_FORCE_SCREENSHOT")));
10761086
const explicitForceScreenshotOptOut =
1077-
env("PRODUCER_FORCE_SCREENSHOT") === "false" || overrides?.forceScreenshot === false;
1087+
optsOutOfSoftwareScreenshot(env("PRODUCER_FORCE_SCREENSHOT")) ||
1088+
overrides?.forceScreenshot === false;
10781089
// Persist provenance so the concrete-resolved-GPU helper can honor the
10791090
// programmatic opt-out too — at that point `forceScreenshot === false` is
10801091
// otherwise ambiguous between default and explicit opt-out.
@@ -1157,6 +1168,17 @@ export function resolveConfig(overrides?: Partial<EngineConfig>): EngineConfig {
11571168
};
11581169
}
11591170

1171+
/** The `PRODUCER_FORCE_SCREENSHOT` values that keep BeginFrame capture on software GL. */
1172+
function optsOutOfSoftwareScreenshot(requested: string | undefined): boolean {
1173+
return requested === "false" || requested === "auto";
1174+
}
1175+
1176+
function autoCaptureFields(
1177+
requested: string | undefined,
1178+
): Pick<EngineConfig, "screenshotForAcceleratedCanvas"> {
1179+
return requested === "auto" ? { screenshotForAcceleratedCanvas: true } : {};
1180+
}
1181+
11601182
/**
11611183
* Runtime-resolved companion to the software-GPU screenshot clamp in
11621184
* `resolveConfig`. Returns `true` iff callers should treat this render as
@@ -1186,7 +1208,7 @@ export function shouldClampToScreenshotForConcreteGpu(
11861208
if (currentForceScreenshot) return false;
11871209
if (resolvedGpuMode !== "software") return false;
11881210
if (opts.programmaticOptOut) return false;
1189-
return env["PRODUCER_FORCE_SCREENSHOT"] !== "false";
1211+
return !optsOutOfSoftwareScreenshot(env["PRODUCER_FORCE_SCREENSHOT"]);
11901212
}
11911213

11921214
/**

‎packages/producer/src/services/distributed/plan.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,52 @@ describe("plan() and page motion blur (R19)", () => {
608608
}, 60_000);
609609
});
610610

611+
describe("plan() and PRODUCER_FORCE_SCREENSHOT=auto", () => {
612+
const WEBGL_CANVAS =
613+
'<canvas id="gl" width="32" height="32"></canvas>' +
614+
'<script>document.getElementById("gl").getContext("webgl");</script>';
615+
616+
const planLockedCapture = async (html: string, name: string, requested: string | undefined) => {
617+
const dir = join(runRoot, name);
618+
mkdirSync(dir, { recursive: true });
619+
writeFileSync(join(dir, "index.html"), html, "utf-8");
620+
const planDir = join(runRoot, `${name}-plan`);
621+
const previous = process.env.PRODUCER_FORCE_SCREENSHOT;
622+
if (requested === undefined) delete process.env.PRODUCER_FORCE_SCREENSHOT;
623+
else process.env.PRODUCER_FORCE_SCREENSHOT = requested;
624+
try {
625+
await plan(dir, { fps: 30, width: 320, height: 240, format: "mp4" }, planDir);
626+
} finally {
627+
if (previous === undefined) delete process.env.PRODUCER_FORCE_SCREENSHOT;
628+
else process.env.PRODUCER_FORCE_SCREENSHOT = previous;
629+
}
630+
const meta = JSON.parse(readFileSync(join(planDir, "meta", "encoder.json"), "utf-8"));
631+
return { captureMode: meta.captureMode ?? null, forceScreenshot: meta.forceScreenshot ?? null };
632+
};
633+
634+
// BeginFrame capture exists only on Linux, so only there is there a choice to lock.
635+
it.skipIf(process.platform !== "linux")(
636+
"locks screenshot capture for every chunk when the page has a WebGL canvas",
637+
async () => {
638+
const webgl = FIXTURE_HTML.replace("</body>", `${WEBGL_CANVAS}</body>`);
639+
expect(webgl).not.toBe(FIXTURE_HTML);
640+
expect(await planLockedCapture(webgl, "auto-webgl", "auto")).toEqual({
641+
captureMode: "screenshot",
642+
forceScreenshot: true,
643+
});
644+
expect(await planLockedCapture(FIXTURE_HTML, "auto-plain", "auto")).toEqual({
645+
captureMode: "beginframe",
646+
forceScreenshot: false,
647+
});
648+
expect(await planLockedCapture(webgl, "false-webgl", "false")).toEqual({
649+
captureMode: "beginframe",
650+
forceScreenshot: false,
651+
});
652+
},
653+
120_000,
654+
);
655+
});
656+
611657
describe("buildChunkSlices", () => {
612658
it("produces consecutive non-overlapping ranges covering all frames", () => {
613659
const slices = buildChunkSlices(700, 3, 240);

‎packages/producer/src/services/distributed/plan.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,8 @@ async function compilePlanComposition(
14281428
assertNotAborted();
14291429
logPlanStage(log, "reference_background", stageStarted);
14301430
}
1431+
// Every chunk must capture the way the plan chose, so the choice is locked here.
1432+
if (probeResult.acceleratedCanvasScreenshot) forceScreenshot = true;
14311433
compiled = probeResult.compiled;
14321434
job.strictVariablesChecked = probeResult.strictVariablesChecked;
14331435
job.duration = probeResult.duration;

‎packages/producer/src/services/render/stages/probeStage.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,95 @@ describe("runProbeStage — transient browser error retry (#1687)", () => {
18771877
expect(result.beginFrameStalled).toBe(true);
18781878
});
18791879

1880+
async function runAutoCaptureMode(input: {
1881+
pageHasAcceleratedCanvas: boolean;
1882+
cfg?: Record<string, unknown>;
1883+
stageForceScreenshot?: boolean;
1884+
}) {
1885+
resetRetryMocks();
1886+
capturedCfgs.length = 0;
1887+
let canvasChecks = 0;
1888+
pageEvaluateHandler = async (fn) => {
1889+
if (String(fn).includes("__hfAcceleratedCanvases")) {
1890+
canvasChecks += 1;
1891+
return input.pageHasAcceleratedCanvas;
1892+
}
1893+
return {
1894+
timelineKeys: [],
1895+
hfDuration: 5,
1896+
gsapLoaded: false,
1897+
totalDurationMs: 5000,
1898+
__hf: {},
1899+
};
1900+
};
1901+
const { runProbeStage } = await import("./probeStage.js");
1902+
const probeInput = makeProbeInput({
1903+
cfgForceScreenshot: input.stageForceScreenshot ?? false,
1904+
stageForceScreenshot: input.stageForceScreenshot ?? false,
1905+
});
1906+
Object.assign(probeInput.cfg, { screenshotForAcceleratedCanvas: true }, input.cfg);
1907+
const result = await runProbeStage(probeInput);
1908+
return { result, canvasChecks };
1909+
}
1910+
1911+
it("auto capture mode switches to screenshot for a WebGL or WebGPU canvas on software GL", async () => {
1912+
const { result } = await runAutoCaptureMode({ pageHasAcceleratedCanvas: true });
1913+
1914+
expect(result.acceleratedCanvasScreenshot).toBe(true);
1915+
expect(result.beginFrameStalled).toBe(false);
1916+
expect(createSessionCallCount).toBe(2);
1917+
expect(closedSessions).toEqual([createdSessions[0]]);
1918+
expect(capturedCfgs[0]).toMatchObject({ forceScreenshot: false });
1919+
expect(capturedCfgs[1]).toMatchObject({ forceScreenshot: true });
1920+
expect(result.probeSession).toBe(createdSessions[1]);
1921+
expect(durationProbeSessions).toEqual([createdSessions[1]]);
1922+
});
1923+
1924+
it("auto capture mode keeps BeginFrame when the page has no WebGL or WebGPU canvas", async () => {
1925+
const { result, canvasChecks } = await runAutoCaptureMode({ pageHasAcceleratedCanvas: false });
1926+
1927+
expect(canvasChecks).toBe(1);
1928+
expect(result.acceleratedCanvasScreenshot).toBe(false);
1929+
expect(createSessionCallCount).toBe(1);
1930+
expect(capturedCfgs[0]).toMatchObject({ forceScreenshot: false });
1931+
expect(result.probeSession?.launchCaptureMode).toBe("beginframe");
1932+
});
1933+
1934+
it("auto capture mode keeps BeginFrame on hardware GL even with a WebGL canvas", async () => {
1935+
const { result, canvasChecks } = await runAutoCaptureMode({
1936+
pageHasAcceleratedCanvas: true,
1937+
cfg: { browserGpuMode: "hardware" },
1938+
});
1939+
1940+
expect(canvasChecks).toBe(0);
1941+
expect(result.acceleratedCanvasScreenshot).toBe(false);
1942+
expect(createSessionCallCount).toBe(1);
1943+
expect(result.probeSession?.launchCaptureMode).toBe("beginframe");
1944+
});
1945+
1946+
it("without the auto value a WebGL canvas on software GL is never looked for", async () => {
1947+
const { result, canvasChecks } = await runAutoCaptureMode({
1948+
pageHasAcceleratedCanvas: true,
1949+
cfg: { screenshotForAcceleratedCanvas: undefined },
1950+
});
1951+
1952+
expect(canvasChecks).toBe(0);
1953+
expect(result.acceleratedCanvasScreenshot).toBe(false);
1954+
expect(createSessionCallCount).toBe(1);
1955+
expect(result.probeSession?.launchCaptureMode).toBe("beginframe");
1956+
});
1957+
1958+
it("auto capture mode does not look at the page when screenshot is already chosen", async () => {
1959+
const { result, canvasChecks } = await runAutoCaptureMode({
1960+
pageHasAcceleratedCanvas: true,
1961+
stageForceScreenshot: true,
1962+
});
1963+
1964+
expect(canvasChecks).toBe(0);
1965+
expect(result.acceleratedCanvasScreenshot).toBe(false);
1966+
expect(createSessionCallCount).toBe(1);
1967+
});
1968+
18801969
it("retries once on a transient 'Navigating frame was detached' error and succeeds", async () => {
18811970
await runWithTransientInitializeError("Navigating frame was detached");
18821971
});

0 commit comments

Comments
 (0)