Skip to content

Write HTML. Render video. Built for agents.

feat(producer): stream multi-worker BeginFrame renders by default

Vance Ingalls1ae359ca004f
diff --git a/packages/producer/src/services/render/stages/captureStage.test.ts b/packages/producer/src/services/render/stages/captureStage.test.ts
index d044c6d0c2..6572f02e66 100644
--- a/packages/producer/src/services/render/stages/captureStage.test.ts
+++ b/packages/producer/src/services/render/stages/captureStage.test.ts
@@ -51,7 +51,14 @@ describe("disk capture capacity", () => {
expect(message).toContain("Disk capture may need");
expect(message).toContain("--workers 1");
expect(message).toContain("--low-memory-mode");
- expect(message).not.toContain("PRODUCER_STREAMING_ENCODE_MAX_DURATION_SECONDS");
+ // Every override that can land an otherwise-streaming render here, so the
+ // message explains the case the reader is actually in. The duration cap
+ // matters because it sends even a --workers 1 render to disk, which the
+ // pre-Phase-1 wording told them to "fix" with --workers 1.
+ expect(message).toContain("HF_CAPTURE_PARALLEL_STREAM=true");
+ expect(message).toContain("PRODUCER_STREAMING_ENCODE_DURATION_CAP_ENABLED=true");
+ expect(message).toContain("png-sequence");
+ expect(message).not.toContain("PRODUCER_STREAMING_ENCODE_MAX_DURATION_SECONDS if streaming");
});
it("exposes the same 90% headroom decision to fallback planning", () => {
diff --git a/packages/producer/src/services/render/stages/captureStage.ts b/packages/producer/src/services/render/stages/captureStage.ts
index ed58099681..9de570cfc3 100644
--- a/packages/producer/src/services/render/stages/captureStage.ts
+++ b/packages/producer/src/services/render/stages/captureStage.ts
@@ -194,10 +194,14 @@ export function assertDiskCaptureHeadroom(
throw new Error(
`Disk capture may need ~${(headroom.estimatedBytes / 1e6).toFixed(1)} MB of temporary frame storage, ` +
`but only ${(headroom.freeBytes / 1e6).toFixed(1)} MB is free at ${framesDir}. ` +
- "Disk capture stores every frame as raw RGBA; it is used for multi-worker renders " +
- "and for png-sequence, gif, HDR and shader-transition outputs. " +
- "Re-run with --workers 1 to stream frames into the encoder, use --low-memory-mode, " +
- "or free up disk space.",
+ "Disk capture stores every frame as raw RGBA. mp4/mov renders stream instead: " +
+ "always at --workers 1, and multi-worker on Linux BeginFrame capture. " +
+ "This render landed on disk because the output is png-sequence, gif, HDR or " +
+ "shader-transition, because it is multi-worker screenshot capture (opt in with " +
+ "HF_CAPTURE_PARALLEL_STREAM=true), or because streaming was switched off " +
+ "(HF_CAPTURE_PARALLEL_STREAM=false, PRODUCER_ENABLE_STREAMING_ENCODE=false, " +
+ "PRODUCER_STREAMING_ENCODE_DURATION_CAP_ENABLED=true). " +
+ "Re-run with --workers 1, use --low-memory-mode, or free up disk space.",
);
}
diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.ts
index 82ae280ca3..e018e39a49 100644
--- a/packages/producer/src/services/renderOrchestrator.test.ts
+++ b/packages/producer/src/services/renderOrchestrator.test.ts
@@ -53,6 +53,8 @@ import {
shouldClampDefaultDrawElement,
shouldPreferParallelDrawElement,
shouldPreferSingleWorkerDrawElement,
+ isCaptureParallelStreamRouterEnabled,
+ resolveParallelCaptureMode,
shouldStreamParallelCapture,
shouldUseStreamingEncode,
resolveObservedCaptureMode,
@@ -3315,6 +3317,66 @@ describe("sequential capture stall recovery", () => {
});
});
+describe("resolveParallelCaptureMode", () => {
+ const beginframe = {
+ platform: "linux" as NodeJS.Platform,
+ headlessShell: true,
+ forceScreenshot: false,
+ deviceScaleFactor: 1,
+ };
+
+ it("reports beginframe only for linux headless-shell at DPR 1 without forceScreenshot", () => {
+ expect(resolveParallelCaptureMode(beginframe)).toBe("beginframe");
+ });
+
+ it("reports screenshot wherever the engine's preMode would", () => {
+ expect(resolveParallelCaptureMode({ ...beginframe, platform: "darwin" })).toBe("screenshot");
+ expect(resolveParallelCaptureMode({ ...beginframe, platform: "win32" })).toBe("screenshot");
+ // System Chrome on linux: resolveObservedCaptureMode calls this beginframe,
+ // the engine launches screenshot. Getting this wrong would default-enable
+ // the router for a cohort whose real path is the screenshot one.
+ expect(resolveParallelCaptureMode({ ...beginframe, headlessShell: false })).toBe("screenshot");
+ expect(resolveParallelCaptureMode({ ...beginframe, forceScreenshot: true })).toBe("screenshot");
+ // Supersampling: BeginFrame ignores deviceScaleFactor, so preMode falls back.
+ expect(resolveParallelCaptureMode({ ...beginframe, deviceScaleFactor: 2 })).toBe("screenshot");
+ });
+
+ it("treats an unset deviceScaleFactor as 1", () => {
+ expect(resolveParallelCaptureMode({ ...beginframe, deviceScaleFactor: undefined })).toBe(
+ "beginframe",
+ );
+ });
+});
+
+describe("isCaptureParallelStreamRouterEnabled", () => {
+ it("is on by default only where capture will run BeginFrame", () => {
+ expect(isCaptureParallelStreamRouterEnabled({}, "beginframe")).toBe(true);
+ // macOS/Windows screenshot capture stays opt-in: its opt-in cohort runs a
+ // 5.5% error rate against a ~1.1% baseline (stall watchdog / EPIPE class).
+ expect(isCaptureParallelStreamRouterEnabled({}, "screenshot")).toBe(false);
+ expect(
+ isCaptureParallelStreamRouterEnabled({ HF_CAPTURE_PARALLEL_STREAM: "" }, "screenshot"),
+ ).toBe(false);
+ });
+
+ it("honours the explicit opt-in for either mode", () => {
+ expect(
+ isCaptureParallelStreamRouterEnabled({ HF_CAPTURE_PARALLEL_STREAM: "true" }, "screenshot"),
+ ).toBe(true);
+ expect(
+ isCaptureParallelStreamRouterEnabled({ HF_CAPTURE_PARALLEL_STREAM: " TRUE " }, "screenshot"),
+ ).toBe(true);
+ });
+
+ it("honours the kill switch, using the same off-spellings as the DE router", () => {
+ for (const off of ["false", "FALSE", "0", "off", "no"]) {
+ expect(
+ isCaptureParallelStreamRouterEnabled({ HF_CAPTURE_PARALLEL_STREAM: off }, "beginframe"),
+ ).toBe(false);
+ }
+ });
+});
+
describe("shouldStreamParallelCapture (non-DE parallel streaming router)", () => {
const eligible = {
routerEnabled: true,
@@ -3329,7 +3391,7 @@ describe("shouldStreamParallelCapture (non-DE parallel streaming router)", () =>
expect(shouldStreamParallelCapture(eligible)).toBe(true);
});
- it("is disabled by default (kill switch off is the shipped default)", () => {
+ it("honours the kill switch when the caller passes routerEnabled=false", () => {
expect(shouldStreamParallelCapture({ ...eligible, routerEnabled: false })).toBe(false);
});
diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.ts
index 973f6c03db..22cc92d5e0 100644
--- a/packages/producer/src/services/renderOrchestrator.ts
+++ b/packages/producer/src/services/renderOrchestrator.ts
@@ -2432,8 +2432,60 @@ export async function closeOrphanedProbeForRetry(
* machinery; both DE predicates independently require useDrawElement, making
* the two routers mutually exclusive by construction.
*/
+/**
+ * The capture mode each parallel worker will launch with, mirroring the
+ * engine's `preMode` (frameCapture.ts): BeginFrame only on Linux with
+ * chrome-headless-shell, at DPR 1, with no forced screenshot. The engine's
+ * `drawElementTransparent` term is absent because the router requires
+ * `!useDrawElement`.
+ *
+ * Deliberately stricter than {@link resolveObservedCaptureMode}, which knows
+ * only the platform and so calls Linux-with-system-Chrome and Linux-at-DPR>1
+ * "beginframe" when the engine actually launches screenshot. The default-on
+ * decision keys on this, so being stricter only narrows what is enabled by
+ * default. One case remains that this cannot see: the engine's concrete
+ * software-GPU clamp can still move a Linux headless-shell render to
+ * screenshot. Those renders are inside the cohort the default was measured
+ * on, since the telemetry label uses the looser predicate.
+ */
+export function resolveParallelCaptureMode(args: {
+ platform: NodeJS.Platform;
+ /** A chrome-headless-shell binary was resolved for this render. */
+ headlessShell: boolean;
+ forceScreenshot: boolean;
+ deviceScaleFactor?: number;
+}): "screenshot" | "beginframe" {
+ const supersampling = (args.deviceScaleFactor ?? 1) > 1;
+ return args.headlessShell && args.platform === "linux" && !args.forceScreenshot && !supersampling
+ ? "beginframe"
+ : "screenshot";
+}
+
+/**
+ * Non-DE parallel-stream router switch. Default ON for BeginFrame capture
+ * (Phase 1 of the long-form render plan): those multi-worker mp4/mov renders
+ * stream through the interleaved writer instead of writing raw RGBA frames to
+ * disk, and their opt-in cohort fails at 0.05%. Screenshot capture stays
+ * opt-in — its opt-in cohort fails at 5.5% against a ~1.1% baseline, in two
+ * classes the streaming path owns (the parallel stall watchdog hard-fails
+ * with no retry; a dying encoder surfaces as a bare write EPIPE). Flipping it
+ * waits on those being fixed.
+ *
+ * Off-spellings match {@link isDeParallelRouterEnabled} so the two kill
+ * switches cannot disagree; any other explicit value is an opt-in.
+ */
+export function isCaptureParallelStreamRouterEnabled(
+ env: Readonly<Record<string, string | undefined>>,
+ resolvedMode: "screenshot" | "beginframe",
+): boolean {
+ const raw = env.HF_CAPTURE_PARALLEL_STREAM?.trim().toLowerCase();
+ if (raw === undefined || raw === "") return resolvedMode === "beginframe";
+ if (raw === "false" || raw === "0" || raw === "off" || raw === "no") return false;
+ return true;
+}
+
export function shouldStreamParallelCapture(args: {
- /** HF_CAPTURE_PARALLEL_STREAM === "true" — kill switch, default OFF. */
+ /** Router switch for this render — see isCaptureParallelStreamRouterEnabled. */
routerEnabled: boolean;
workerCount: number;
/** cfg.useDrawElement AFTER resolveConfig clamps. */
@@ -3847,7 +3899,16 @@ async function executeRenderPipeline(input: {
// (both DE predicates require useDrawElement; this requires its negation).
// Reads `cfg.useDrawElement` after the clamp above, so it sees the
// capture mode this render will actually use.
- const captureParallelStreamRouterEnabled = process.env.HF_CAPTURE_PARALLEL_STREAM === "true";
+ const parallelCaptureMode = resolveParallelCaptureMode({
+ platform: process.platform,
+ headlessShell: resolveHeadlessShellPath(cfg) !== null,
+ forceScreenshot: captureForceScreenshot,
+ deviceScaleFactor: captureOptions.deviceScaleFactor,
+ });
+ const captureParallelStreamRouterEnabled = isCaptureParallelStreamRouterEnabled(
+ process.env,
+ parallelCaptureMode,
+ );
const captureParallelStreamArgs = {
workerCount,
useDrawElement: cfg.useDrawElement,
@@ -3861,12 +3922,10 @@ async function executeRenderPipeline(input: {
});
if (captureParallelStreamEligible) {
captureParallelStreamForced = true;
- // Which mode will stream: the engine picks beginframe only on Linux with
- // headless-shell and no forced screenshot (frameCapture.ts preMode);
- // everything else is screenshot. Recorded for telemetry cohorting.
- // Same predicate as the observability field — use the one helper so the
- // two cannot drift if the router's modes ever change.
- const captureParallelStream = resolveObservedCaptureMode(captureForceScreenshot);
+ // Which mode will stream — the same value the enablement decision used,
+ // so the routed cohort in telemetry cannot disagree with the predicate
+ // that routed it.
+ const captureParallelStream = parallelCaptureMode;
log.info(
`[Render] Parallel ${captureParallelStream} capture will stream to the encoder ` +
`(interleaved, ${workerCount} workers) instead of the disk path. ` +
@@ -3881,11 +3940,11 @@ async function executeRenderPipeline(input: {
`parallel ${captureParallelStream} capture routed to streaming`,
);
} else if (shouldStreamParallelCapture({ routerEnabled: true, ...captureParallelStreamArgs })) {
- // The kill switch is the ONLY failed gate: emit a passive cohort-sizing
- // signal (capture_parallel_stream = "eligible_off") so the default-off
- // soak can measure how many fleet renders WOULD route before anyone
- // enables the flag. Observability-only — no behavior change, no log
- // noise on the default path.
+ // The router switch is the ONLY failed gate: emit a passive cohort-sizing
+ // signal (capture_parallel_stream = "eligible_off"). Post-split this is
+ // the screenshot-capture cohort held back by the default plus anyone who
+ // set the kill switch — i.e. exactly the population the Phase 1b flip
+ // would move. Observability-only, no log noise on the default path.
updateCaptureObservability({ captureParallelStream: "eligible_off" });
}