Skip to content

Write HTML. Render video. Built for agents.

feat(producer): one router predicate for segmented capture, multi-worker allowed

Vance Ingallsf78c3cb8d4f7
diff --git a/packages/producer/src/services/render/capturePlan.test.ts b/packages/producer/src/services/render/capturePlan.test.ts
index 8ec1b0beb3..55aebe02ba 100644
--- a/packages/producer/src/services/render/capturePlan.test.ts
+++ b/packages/producer/src/services/render/capturePlan.test.ts
@@ -389,7 +389,7 @@ describe("sdr_segmented capture plan", () => {
useSegmentedCapture: true,
};
- it("selects segmented capture only for single-worker streaming-eligible renders", () => {
+ it("selects segmented capture for streaming-eligible renders at any worker count", () => {
const plan = createCapturePlan(segmented);
expect(plan).toMatchObject({
kind: "sdr_segmented",
@@ -397,11 +397,22 @@ describe("sdr_segmented capture plan", () => {
forceParallelStream: false,
});
expect(Object.isFrozen(plan)).toBe(true);
- expect(createCapturePlan({ ...segmented, workerCount: 3 }).kind).toBe("sdr_streaming");
+ // Phase 2d: several workers each own a segment and an encoder.
+ expect(createCapturePlan({ ...segmented, workerCount: 3 })).toMatchObject({
+ kind: "sdr_segmented",
+ workerCount: 3,
+ forceParallelStream: false,
+ });
});
- it("does not segment when streaming is off or the route is layered", () => {
- expect(createCapturePlan({ ...segmented, useStreamingEncode: false }).kind).toBe("sdr_disk");
+ it("loses to the layered route but not to the single-encoder streaming flag", () => {
+ // useStreamingEncode answers "one encoder for the whole render", which a
+ // segmented render never wants — it goes false for multi-worker, and
+ // requiring it here would silently drop those renders onto the disk path.
+ // Viability is decided by shouldSegmentCapture before this is set.
+ expect(createCapturePlan({ ...segmented, useStreamingEncode: false }).kind).toBe(
+ "sdr_segmented",
+ );
expect(createCapturePlan({ ...segmented, useLayeredComposite: true }).kind).toBe("hdr_layered");
});
diff --git a/packages/producer/src/services/render/capturePlan.ts b/packages/producer/src/services/render/capturePlan.ts
index f61c4226ca..4fb0abfe47 100644
--- a/packages/producer/src/services/render/capturePlan.ts
+++ b/packages/producer/src/services/render/capturePlan.ts
@@ -196,7 +196,14 @@ export function createCapturePlan(input: CreateCapturePlanInput): CapturePlan {
forceParallelStream: false,
});
}
- if (input.useStreamingEncode && input.useSegmentedCapture === true && input.workerCount === 1) {
+ // Checked before `useStreamingEncode`, which answers a question segmented
+ // capture does not ask: that flag is about ONE encoder for the whole render
+ // and so goes false for multi-worker, while a segmented render gives every
+ // worker its own. Whether streaming is viable at all is settled by the
+ // caller's predicate (`shouldSegmentCapture`) before this is ever true.
+ // forceParallelStream stays false for the same reason: the interleaved
+ // single-encoder writer has nothing to do here.
+ if (input.useSegmentedCapture === true) {
return Object.freeze({ ...base, kind: "sdr_segmented", forceParallelStream: false });
}
if (input.useStreamingEncode) {
diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.ts
index 39c53affa9..f8b3f324c6 100644
--- a/packages/producer/src/services/renderOrchestrator.test.ts
+++ b/packages/producer/src/services/renderOrchestrator.test.ts
@@ -54,7 +54,8 @@ import {
shouldPreferParallelDrawElement,
shouldPreferSingleWorkerDrawElement,
isCaptureParallelStreamRouterEnabled,
- isSegmentedCaptureRequested,
+ shouldSegmentCapture,
+ SEGMENTED_MIN_SECONDS_AFTER_SOAK,
resolveParallelCaptureMode,
shouldStreamParallelCapture,
shouldUseStreamingEncode,
@@ -3349,16 +3350,70 @@ describe("resolveParallelCaptureMode", () => {
});
});
-describe("isSegmentedCaptureRequested", () => {
- it("is opt-in via HF_SEGMENTED_CAPTURE=true", () => {
- expect(isSegmentedCaptureRequested({})).toBe(false);
- expect(isSegmentedCaptureRequested({ HF_SEGMENTED_CAPTURE: "true" })).toBe(true);
- expect(isSegmentedCaptureRequested({ HF_SEGMENTED_CAPTURE: " TRUE " })).toBe(true);
- expect(isSegmentedCaptureRequested({ HF_SEGMENTED_CAPTURE: "false" })).toBe(false);
- // Phase 2a is opt-in only: anything that is not an explicit "true" is off,
- // including the values Phase 2d will later treat as a kill switch.
- expect(isSegmentedCaptureRequested({ HF_SEGMENTED_CAPTURE: "1" })).toBe(false);
- expect(isSegmentedCaptureRequested({ HF_SEGMENTED_CAPTURE: "" })).toBe(false);
+describe("shouldSegmentCapture", () => {
+ const base = {
+ env: {} as Record<string, string | undefined>,
+ workerCount: 1,
+ durationSeconds: 900,
+ outputFormat: "mp4",
+ layeredOrEffectRoute: false,
+ streamingOk: true,
+ };
+
+ it("does not route on duration until the soak flip", () => {
+ // The threshold ships disabled: the release that introduces the route
+ // must not also change which route every long render takes.
+ expect(shouldSegmentCapture(base)).toBe(false);
+ expect(shouldSegmentCapture({ ...base, durationSeconds: 36_000 })).toBe(false);
+ });
+
+ it("routes long mp4/mov renders once a threshold is set", () => {
+ const env = { HF_SEGMENTED_MIN_SECONDS: String(SEGMENTED_MIN_SECONDS_AFTER_SOAK) };
+ expect(shouldSegmentCapture({ ...base, env })).toBe(true);
+ expect(shouldSegmentCapture({ ...base, env, outputFormat: "mov" })).toBe(true);
+ expect(shouldSegmentCapture({ ...base, env, workerCount: 4 })).toBe(true);
+ expect(shouldSegmentCapture({ ...base, env, durationSeconds: 599 })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, durationSeconds: 600 })).toBe(true);
+ });
+
+ it("is forced at any duration by the explicit opt-in", () => {
+ expect(
+ shouldSegmentCapture({
+ ...base,
+ durationSeconds: 5,
+ env: { HF_SEGMENTED_CAPTURE: "true" },
+ }),
+ ).toBe(true);
+ });
+
+ it("honours the kill switch and the exclusions", () => {
+ const env = { HF_SEGMENTED_MIN_SECONDS: "60" };
+ expect(shouldSegmentCapture({ ...base, env: { ...env, HF_SEGMENTED_CAPTURE: "false" } })).toBe(
+ false,
+ );
+ // The kill switch beats the force flag's other spellings too.
+ expect(shouldSegmentCapture({ ...base, env: { HF_SEGMENTED_CAPTURE: "off" } })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, outputFormat: "webm" })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, outputFormat: "png-sequence" })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, outputFormat: "hls" })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, layeredOrEffectRoute: true })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env, streamingOk: false })).toBe(false);
+ // Even the explicit force cannot route an excluded output.
+ expect(
+ shouldSegmentCapture({
+ ...base,
+ env: { HF_SEGMENTED_CAPTURE: "true" },
+ outputFormat: "webm",
+ }),
+ ).toBe(false);
+ });
+
+ it("ignores an unparseable threshold rather than routing on it", () => {
+ expect(shouldSegmentCapture({ ...base, env: { HF_SEGMENTED_MIN_SECONDS: "soon" } })).toBe(
+ false,
+ );
+ expect(shouldSegmentCapture({ ...base, env: { HF_SEGMENTED_MIN_SECONDS: "-5" } })).toBe(false);
+ expect(shouldSegmentCapture({ ...base, env: { HF_SEGMENTED_MIN_SECONDS: "0" } })).toBe(true);
});
});
diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.ts
index e62e6db212..f83c5a3aa8 100644
--- a/packages/producer/src/services/renderOrchestrator.ts
+++ b/packages/producer/src/services/renderOrchestrator.ts
@@ -2501,11 +2501,51 @@ export function isCaptureParallelStreamRouterEnabled(
return true;
}
-/** Segmented capture opt-in (spec §5 Phase 2). Phase 2d makes it the default for long renders. */
-export function isSegmentedCaptureRequested(
- env: Readonly<Record<string, string | undefined>>,
-): boolean {
- return env.HF_SEGMENTED_CAPTURE?.trim().toLowerCase() === "true";
+/**
+ * The duration at which segmented capture is meant to become the default
+ * (spec §5 Phase 2d). Not the shipped default: that flip is its own release
+ * step, gated on the 40-minute soak, and shipping it alongside the rest of
+ * this work would change the route for every long render in the same release
+ * that introduced the route. Set `HF_SEGMENTED_MIN_SECONDS=600` to opt a
+ * fleet in ahead of the flip; flipping the constant below is the whole
+ * change when the soak passes.
+ */
+export const SEGMENTED_MIN_SECONDS_AFTER_SOAK = 600;
+
+/** No duration routes to segmented capture until the soak gate is cleared. */
+const DEFAULT_SEGMENTED_MIN_SECONDS = Number.POSITIVE_INFINITY;
+
+function resolveSegmentedMinSeconds(env: Readonly<Record<string, string | undefined>>): number {
+ const raw = env.HF_SEGMENTED_MIN_SECONDS;
+ if (raw === undefined || raw.trim() === "") return DEFAULT_SEGMENTED_MIN_SECONDS;
+ const parsed = Number(raw);
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : DEFAULT_SEGMENTED_MIN_SECONDS;
+}
+
+/**
+ * Whether this render captures in segments. `HF_SEGMENTED_CAPTURE=true`
+ * forces it at any duration and `=false` is the kill switch; otherwise it is
+ * the duration threshold above. The exclusions are the routes whose
+ * concat-copy or capture loop the segment contract does not cover: webm's VP9
+ * concat is fragile across ffmpeg versions, png-sequence and gif have no
+ * encoded video output, and HDR/shader-transition renders run their own
+ * compositor.
+ */
+export function shouldSegmentCapture(args: {
+ env: Readonly<Record<string, string | undefined>>;
+ workerCount: number;
+ durationSeconds: number;
+ outputFormat: string;
+ layeredOrEffectRoute: boolean;
+ /** shouldUseStreamingEncode(cfg, format, 1, duration) at the call site. */
+ streamingOk: boolean;
+}): boolean {
+ const raw = args.env.HF_SEGMENTED_CAPTURE?.trim().toLowerCase();
+ if (raw === "false" || raw === "0" || raw === "off" || raw === "no") return false;
+ if (!args.streamingOk || args.layeredOrEffectRoute) return false;
+ if (args.outputFormat !== "mp4" && args.outputFormat !== "mov") return false;
+ if (raw === "true") return true;
+ return args.durationSeconds >= resolveSegmentedMinSeconds(args.env);
}
export function shouldStreamParallelCapture(args: {
@@ -4145,12 +4185,14 @@ async function executeRenderPipeline(input: {
// excluded routes are the ones whose concat-copy or capture loop the
// segment contract does not cover (webm VP9 concat is fragile, HDR and
// shader transitions run their own compositor).
- useSegmentedCapture:
- isSegmentedCaptureRequested(process.env) &&
- workerCount === 1 &&
- (outputFormat === "mp4" || outputFormat === "mov") &&
- !hasHdrContent &&
- !compiled.hasShaderTransitions,
+ useSegmentedCapture: shouldSegmentCapture({
+ env: process.env,
+ workerCount,
+ durationSeconds: job.duration,
+ outputFormat,
+ layeredOrEffectRoute: hasHdrContent || compiled.hasShaderTransitions,
+ streamingOk: shouldUseStreamingEncode(cfg, outputFormat, 1, job.duration),
+ }),
useLayeredComposite,
usePageSideCompositing: usePageSideCompositingForTransitions,
hasHdrContent,
@@ -4398,6 +4440,7 @@ async function executeRenderPipeline(input: {
dedupPerfs,
segmentFrames,
segmentDir,
+ workerCount: segmentedPlan.workerCount,
browserRecycleEverySegments: resolveSegmentBrowserRecycle(process.env),
completedSegments,
onSegmentComplete: (entry) => {