Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
feat(producer): stream long single-worker renders; name the streaming gate reason
Vance Ingallsc8b21c965bd0
diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.tsindex 1a50fa2f18..82ae280ca3 100644--- a/packages/producer/src/services/renderOrchestrator.test.ts+++ b/packages/producer/src/services/renderOrchestrator.test.ts@@ -25,6 +25,7 @@ import {closeOrphanedProbeForRetry,describeMemoryExhaustion,executeDiskCaptureWithAdaptiveRetry,+ explainStreamingEncodeGate,collectVideoMetadataHints,collectVideoReadinessSkipIds,extractStandaloneEntryFromIndex,@@ -586,17 +587,37 @@ describe("shouldUseStreamingEncode", () => {);});- it("keeps renders over the configured max duration on normal encoding", () => {- expect(shouldUseStreamingEncode(streamingEnabledConfig, "mp4", 1, 240)).toBe(true);- expect(shouldUseStreamingEncode(streamingEnabledConfig, "mp4", 1, 240.001)).toBe(false);+ it("ignores the duration cap unless streamingEncodeDurationCapEnabled is true", () => {+ expect(shouldUseStreamingEncode(streamingEnabledConfig, "mp4", 1, 240.001)).toBe(true);+ expect(shouldUseStreamingEncode(streamingEnabledConfig, "mp4", 1, 3600)).toBe(true);+ expect(+ shouldUseStreamingEncode(+ { ...streamingEnabledConfig, streamingEncodeDurationCapEnabled: true },+ "mp4",+ 1,+ 240.001,+ ),+ ).toBe(false);expect(shouldUseStreamingEncode(- { enableStreamingEncode: true, streamingEncodeMaxDurationSeconds: 120 },+ {+ enableStreamingEncode: true,+ streamingEncodeMaxDurationSeconds: 120,+ streamingEncodeDurationCapEnabled: true,+ },"mp4",1,120.001,),).toBe(false);+ expect(+ shouldUseStreamingEncode(+ { ...streamingEnabledConfig, streamingEncodeDurationCapEnabled: true },+ "mp4",+ 1,+ 240,+ ),+ ).toBe(true);});it("keeps long single-worker renders streaming in low-memory mode", () => {@@ -606,6 +627,93 @@ describe("shouldUseStreamingEncode", () => {});});+describe("explainStreamingEncodeGate", () => {+ const cfg = {+ enableStreamingEncode: true,+ streamingEncodeMaxDurationSeconds: 240,+ lowMemoryMode: false,+ };++ it("names the reason for every decision", () => {+ expect(+ explainStreamingEncodeGate({ ...cfg, enableStreamingEncode: false }, "mp4", 1, 10),+ ).toEqual({ enabled: false, reason: "disabled_by_config" });+ expect(explainStreamingEncodeGate(cfg, "png-sequence", 1, 10)).toEqual({+ enabled: false,+ reason: "format_excluded",+ });+ expect(explainStreamingEncodeGate(cfg, "gif", 1, 10)).toEqual({+ enabled: false,+ reason: "format_excluded",+ });+ expect(explainStreamingEncodeGate(cfg, "mp4", 1, 0)).toEqual({+ enabled: false,+ reason: "invalid_duration",+ });+ expect(explainStreamingEncodeGate(cfg, "mp4", 1, Number.NaN)).toEqual({+ enabled: false,+ reason: "invalid_duration",+ });+ expect(+ explainStreamingEncodeGate(+ { ...cfg, streamingEncodeDurationCapEnabled: true },+ "mp4",+ 1,+ 300,+ ),+ ).toEqual({ enabled: false, reason: "duration_cap" });+ expect(+ explainStreamingEncodeGate(+ { ...cfg, streamingEncodeDurationCapEnabled: true, lowMemoryMode: true },+ "mp4",+ 1,+ 300,+ ),+ ).toEqual({ enabled: true, reason: "low_memory_mode" });+ expect(explainStreamingEncodeGate(cfg, "mp4", 3, 300, true)).toEqual({+ enabled: true,+ reason: "parallel_forced",+ });+ expect(explainStreamingEncodeGate(cfg, "mp4", 1, 300)).toEqual({+ enabled: true,+ reason: "single_worker",+ });+ expect(explainStreamingEncodeGate(cfg, "mp4", 2, 300)).toEqual({+ enabled: false,+ reason: "multi_worker",+ });+ });++ it("low-memory mode only bypasses the cap; it never streams an unforced multi-worker render", () => {+ // `--low-memory-mode --workers 4` is a real combination (only the+ // single-worker pin is bypassed by an explicit worker count). The+ // contiguous-chunk parallel writer stalls, so the gate must still say no.+ expect(+ explainStreamingEncodeGate(+ { ...cfg, streamingEncodeDurationCapEnabled: true, lowMemoryMode: true },+ "mp4",+ 4,+ 300,+ ),+ ).toEqual({ enabled: false, reason: "multi_worker" });+ });++ it("agrees with shouldUseStreamingEncode on every input", () => {+ const cases: Array<[typeof cfg, "mp4" | "webm" | "gif", number, number, boolean]> = [+ [cfg, "mp4", 1, 10, false],+ [cfg, "mp4", 2, 10, false],+ [cfg, "mp4", 2, 10, true],+ [cfg, "gif", 1, 10, false],+ [{ ...cfg, enableStreamingEncode: false }, "mp4", 1, 10, false],+ ];+ for (const [c, format, workers, duration, force] of cases) {+ expect(shouldUseStreamingEncode(c, format, workers, duration, force)).toBe(+ explainStreamingEncodeGate(c, format, workers, duration, force).enabled,+ );+ }+ });+});+describe("createCompiledFrameSrcResolver", () => {it("maps extracted frame paths under compiledDir to encoded server URLs", () => {const resolver = createCompiledFrameSrcResolver("/tmp/hf job/compiled");diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.tsindex 6e9cba3c30..05994331d8 100644--- a/packages/producer/src/services/renderOrchestrator.ts+++ b/packages/producer/src/services/renderOrchestrator.ts@@ -1403,35 +1403,96 @@ function replaceBodyWithRenderClone(body: HTMLElement, renderClone: Element): vobody.appendChild(renderClone);}-export function shouldUseStreamingEncode(- cfg: Pick<EngineConfig, "enableStreamingEncode" | "streamingEncodeMaxDurationSeconds"> &- Partial<Pick<EngineConfig, "lowMemoryMode">>,+export type StreamingEncodeGateReason =+ | "disabled_by_config"+ | "format_excluded"+ | "invalid_duration"+ | "duration_cap"+ | "low_memory_mode"+ | "parallel_forced"+ | "single_worker"+ | "multi_worker";++export interface StreamingEncodeGateDecision {+ enabled: boolean;+ /** Why `enabled` is what it is. Logged as `reason` on the streaming-encode gate line. */+ reason: StreamingEncodeGateReason;+}++type StreamingGateConfig = Pick<+ EngineConfig,+ "enableStreamingEncode" | "streamingEncodeMaxDurationSeconds"+> &+ Partial<Pick<EngineConfig, "lowMemoryMode" | "streamingEncodeDurationCapEnabled">>;++/**+ * Decide whether captured frames stream into ffmpeg (bounded scratch) or land+ * on disk as raw RGBA (`frames × w × h × 4` bytes). Every `false` names the+ * gate that fired so the log line and telemetry can attribute disk-path+ * renders. Order matters and mirrors the historical predicate:+ * config → format → duration validity → duration cap → parallel override →+ * worker count.+ */+export function explainStreamingEncodeGate(+ cfg: StreamingGateConfig,outputFormat: NonNullable<RenderConfig["format"]>,workerCount: number,// Composition timeline duration in seconds.durationSeconds: number,- // Per-render override (set by the DE parallel router) — see- // deParallelStreamForced's declaration in executeRenderJob for why this is- // a parameter instead of an env-var read.+ // Per-render override (set by the DE parallel router or the non-DE+ // parallel-stream router) — see deParallelStreamForced's declaration in+ // executeRenderJob for why this is a parameter instead of an env-var read.forceParallelStream = false,-): boolean {- if (!cfg.enableStreamingEncode) return false;- if (outputFormat === "png-sequence") return false;- if (outputFormat === "gif") return false;- if (!Number.isFinite(durationSeconds) || durationSeconds <= 0) return false;+): StreamingEncodeGateDecision {+ if (!cfg.enableStreamingEncode) return { enabled: false, reason: "disabled_by_config" };+ if (outputFormat === "png-sequence" || outputFormat === "gif") {+ return { enabled: false, reason: "format_excluded" };+ }+ if (!Number.isFinite(durationSeconds) || durationSeconds <= 0) {+ return { enabled: false, reason: "invalid_duration" };+ }+ // The duration cap is an operator opt-in (streamingEncodeDurationCapEnabled);+ // its original reason — a total-render ffmpeg timeout — became an inactivity+ // timeout in efc16a945, so by default long renders keep streaming.+ const overCap =+ cfg.streamingEncodeDurationCapEnabled === true &&+ durationSeconds > cfg.streamingEncodeMaxDurationSeconds;// Low-memory mode already pins capture to one worker. Keep those renders on// the streaming path regardless of duration so captured frames are drained// directly into FFmpeg instead of accumulating hundreds of gigabytes of- // data URIs / disk frames until Chrome OOMs.- if (!cfg.lowMemoryMode && durationSeconds > cfg.streamingEncodeMaxDurationSeconds) return false;+ // data URIs / disk frames until Chrome OOMs. It only bypasses the cap: an+ // explicit `--workers N` under low-memory mode still falls to the+ // worker-count gate below.+ if (overCap && !cfg.lowMemoryMode) return { enabled: false, reason: "duration_cap" };// HF_DE_PARALLEL_STREAM (manual opt-in) / forceParallelStream (router):- // allow multi-worker streaming for the interleaved drawElement produce- // path. Contiguous-chunk parallel streaming stalls (worker k+1's first- // frame waits for ALL of worker k's), so this only makes sense with the+ // allow multi-worker streaming for the interleaved produce paths.+ // Contiguous-chunk parallel streaming stalls (worker k+1's first frame+ // waits for ALL of worker k's), so this only makes sense with the// interleaved distribution the capture stage selects under the same// condition.- if (forceParallelStream || process.env.HF_DE_PARALLEL_STREAM === "true") return true;- return workerCount === 1;+ if (forceParallelStream || process.env.HF_DE_PARALLEL_STREAM === "true") {+ return { enabled: true, reason: "parallel_forced" };+ }+ if (workerCount === 1) {+ return { enabled: true, reason: overCap ? "low_memory_mode" : "single_worker" };+ }+ return { enabled: false, reason: "multi_worker" };+}++export function shouldUseStreamingEncode(+ cfg: StreamingGateConfig,+ outputFormat: NonNullable<RenderConfig["format"]>,+ workerCount: number,+ durationSeconds: number,+ forceParallelStream = false,+): boolean {+ return explainStreamingEncodeGate(+ cfg,+ outputFormat,+ workerCount,+ durationSeconds,+ forceParallelStream,+ ).enabled;}/**@@ -3809,16 +3870,19 @@ async function executeRenderPipeline(input: {// on for this multi-worker render (same formula as the early value above,// now including `captureParallelStreamForced`). This is the value the// rest of the pipeline (encode/writer selection, logging) uses.- useStreamingEncode = shouldUseStreamingEncode(+ const streamingGate = explainStreamingEncodeGate(cfg,outputFormat,workerCount,job.duration,deParallelStreamForced || captureParallelStreamForced,);+ useStreamingEncode = streamingGate.enabled;log.info("streaming-encode gate", {enabled: useStreamingEncode,+ reason: streamingGate.reason,configFlag: cfg.enableStreamingEncode,+ durationCapEnabled: cfg.streamingEncodeDurationCapEnabled,outputFormat,workerCount,durationSeconds: job.duration,