Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(producer): retry parallel stalls and mid-stream encoder death on default routing
Vance Ingallsb05072342a27
diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.tsindex a63befae34..02a3a202c3 100644--- a/packages/producer/src/services/renderOrchestrator.test.ts+++ b/packages/producer/src/services/renderOrchestrator.test.ts@@ -42,6 +42,8 @@ import {shouldRetryViaPinnedFallback,isDeRendererStallError,isSequentialCaptureStallError,+ isParallelCaptureStallError,+ isRetryableEncoderDeath,scanElementTags,envInt,isDeParallelRouterEnabled,@@ -63,6 +65,7 @@ import {createCaptureObservabilityUpdater,} from "./renderOrchestrator.js";import { probeRequiresBrowser } from "./render/stages/probeStage.js";+import { EncoderInterruptedError } from "./render/encoderInterruption.js";import { ensureFrameWritten } from "./render/stages/captureHdrFrameShared.js";import { resolveCompositeTransfer, shouldUseLayeredComposite } from "./hdrCompositor.js";import {@@ -3697,3 +3700,76 @@ describe("createCaptureObservabilityUpdater", () => {expect(observability.workerCount).toBe(4);});});++describe("parallel stall and encoder death: retry eligibility on default routing", () => {+ it("recognises the parallel stall by name or by message", () => {+ const named = Object.assign(new Error("anything"), { name: "ParallelCaptureStallError" });+ expect(isParallelCaptureStallError(named)).toBe(true);+ expect(+ isParallelCaptureStallError(+ new Error(+ "[Render] Parallel screenshot capture stalled: no frame progress for 60000ms (stuck at 0/9000).",+ ),+ ),+ ).toBe(true);+ expect(+ isParallelCaptureStallError(+ new Error(+ "[Render] Parallel BeginFrame capture stalled after 60000ms with no frame progress",+ ),+ ),+ ).toBe(true);+ // The sequential stall has its own predicate and its own fallback shape.+ expect(+ isParallelCaptureStallError(+ new Error("[Render] Sequential screenshot capture stalled: no frame progress for 60000ms"),+ ),+ ).toBe(false);+ expect(isParallelCaptureStallError("[Render] Parallel screenshot capture stalled")).toBe(false);+ });++ it("retries an encoder death only once frames had started, and never a host interruption", () => {+ const died = (frame: number) =>+ new Error(+ `Streaming encoder exited before frame ${frame} was written: FFmpeg exited with code 1`,+ );+ // Frames 0 and 1: the encoder rejected its own arguments or first input.+ // That reproduces on a fresh ffmpeg, so a retry only doubles the time.+ expect(isRetryableEncoderDeath(died(0))).toBe(false);+ expect(isRetryableEncoderDeath(died(1))).toBe(false);+ expect(isRetryableEncoderDeath(died(2))).toBe(true);+ expect(isRetryableEncoderDeath(died(4831))).toBe(true);+ // Same prefix, but typed as a host lifecycle interruption: the producer+ // that owns this render retries that, not the capture stage.+ expect(+ isRetryableEncoderDeath(+ new EncoderInterruptedError("Streaming encoder exited before frame 4831 was written", "x"),+ ),+ ).toBe(false);+ expect(isRetryableEncoderDeath(new Error("Segment 3 encode failed: boom"))).toBe(false);+ expect(isRetryableEncoderDeath("Streaming encoder exited before frame 9 was written")).toBe(+ false,+ );+ });++ it("routes both through the pinned fallback with no pinned routing", () => {+ const base = {+ isVerifyError: false,+ isCancellation: false,+ deWorkerInversion: undefined,+ deParallelRouter: undefined,+ };+ // Default routing alone retries nothing…+ expect(shouldRetryViaPinnedFallback(base)).toBe(false);+ // …so each of these has to be its own gate, like the sequential stall.+ expect(shouldRetryViaPinnedFallback({ ...base, isParallelCaptureStall: true })).toBe(true);+ expect(shouldRetryViaPinnedFallback({ ...base, isEncoderDeath: true })).toBe(true);+ // Cancellation and host interruption still win.+ expect(+ shouldRetryViaPinnedFallback({ ...base, isParallelCaptureStall: true, isCancellation: true }),+ ).toBe(false);+ expect(+ shouldRetryViaPinnedFallback({ ...base, isEncoderDeath: true, isEncoderInterrupted: true }),+ ).toBe(false);+ });+});diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.tsindex 02f4ee058f..87eb4dde46 100644--- a/packages/producer/src/services/renderOrchestrator.ts+++ b/packages/producer/src/services/renderOrchestrator.ts@@ -2358,6 +2358,14 @@ export function shouldRetryViaPinnedFallback(args: {isDeRendererStall?: boolean;/** The producer's no-progress watchdog tripped around a sequential capture call. */isSequentialCaptureStall?: boolean;+ /**+ * The parallel streaming stage's watchdog tripped. Routing-independent+ * like the sequential stall: on the non-drawElement router (default+ * routing) it used to fail the render hard while promising a fallback.+ */+ isParallelCaptureStall?: boolean;+ /** The streaming encoder died after frames had started — see isRetryableEncoderDeath. */+ isEncoderDeath?: boolean;/*** A transient browser failure around the capture call itself* (`classifyCaptureFailure` → `transient_browser`, e.g. a CDP@@ -2371,6 +2379,7 @@ export function shouldRetryViaPinnedFallback(args: {if (args.isCancellation || args.isEncoderInterrupted) return false;if (args.isVerifyError || args.isDeCaptureError) return true;if (args.isDeRendererStall === true || args.isSequentialCaptureStall === true) return true;+ if (args.isParallelCaptureStall === true || args.isEncoderDeath === true) return true;if (args.isTransientCaptureError === true) return true;return args.deWorkerInversion === "inverted" || args.deParallelRouter === "routed";}@@ -2401,6 +2410,36 @@ export function isSequentialCaptureStallError(err: unknown): boolean {);}+/**+ * The parallel streaming stage's no-progress watchdog tripped+ * (`ParallelCaptureStallError`). Matched on name+message like the sequential+ * one so it survives a boundary that rebuilds the error from its message.+ */+export function isParallelCaptureStallError(err: unknown): boolean {+ if (!(err instanceof Error)) return false;+ return (+ err.name === "ParallelCaptureStallError" ||+ /^\[Render\] Parallel \S+ capture stalled/.test(err.message)+ );+}++const ENCODER_DEATH_FRAME_RE = /^Streaming encoder exited before frame (\d+) was written/;++/**+ * The streaming encoder died mid-render in a way a fresh ffmpeg might not+ * repeat: an inactivity kill, a crash, an ENOSPC that cleared. Death before+ * frame 2 is the encoder rejecting its own arguments or input (bad codec+ * params, unsupported pixel format, a broken user ffmpeg) and reproduces+ * exactly, so retrying it only doubles the time to the same failure. A host+ * lifecycle interruption is typed separately and is never retried here — the+ * producer that owns the render is what retries that.+ */+export function isRetryableEncoderDeath(err: unknown): boolean {+ if (!(err instanceof Error) || err instanceof EncoderInterruptedError) return false;+ const match = ENCODER_DEATH_FRAME_RE.exec(err.message);+ return match !== null && Number(match[1]) >= 2;+}+/*** When a self-verify (or pinned-fallback) retry is triggered mid-capture, the* caller may still hold a live probe session that the failed stage was passed@@ -4544,6 +4583,15 @@ async function executeRenderPipeline(input: {assertNotAborted,onProgress,dedupPerfs,+ // The plan's forceScreenshot is per attempt (a retry forces+ // it), so it outranks the mode resolved before the router.+ parallelCaptureLabel: streamingPlan.forceScreenshot+ ? "screenshot"+ : cfg.useDrawElement+ ? "drawElement"+ : parallelCaptureMode === "beginframe"+ ? "BeginFrame"+ : "screenshot",}),);};@@ -4562,6 +4610,8 @@ async function executeRenderPipeline(input: {const isDeCaptureError = isDrawElementCaptureError(err);const isDeStall = isDeRendererStallError(err);const isSequentialStall = isSequentialCaptureStallError(err);+ const isParallelStall = isParallelCaptureStallError(err);+ const isEncoderDeath = isRetryableEncoderDeath(err);const isCancellation =err instanceof RenderCancelledError || executionSignal?.aborted === true;if (@@ -4574,6 +4624,8 @@ async function executeRenderPipeline(input: {deParallelRouter,isDeRendererStall: isDeStall,isSequentialCaptureStall: isSequentialStall,+ isParallelCaptureStall: isParallelStall,+ isEncoderDeath,isTransientCaptureError: isTransientBrowserError(err),}))@@ -4591,7 +4643,11 @@ async function executeRenderPipeline(input: {? "oom": isDeStall? "de_renderer_stall"- : "capture_error";+ : isEncoderDeath+ ? "encoder_death"+ : isParallelStall+ ? "parallel_stall"+ : "capture_error";}log.warn(isVerifyError@@ -4600,7 +4656,11 @@ async function executeRenderPipeline(input: {? "[Render] drawElement renderer stalled; re-rendering via screenshot": isSequentialStall? "[Render] sequential capture stalled; retrying on a fresh screenshot session"- : "[Render] capture failed; re-rendering via a fresh screenshot session",+ : isParallelStall+ ? "[Render] parallel capture stalled; retrying on a fresh single-worker screenshot session"+ : isEncoderDeath+ ? "[Render] streaming encoder died mid-render; retrying on a fresh screenshot session"+ : "[Render] capture failed; re-rendering via a fresh screenshot session",{ error: err instanceof Error ? err.message : String(err) },);observability.checkpoint(@@ -4611,7 +4671,11 @@ async function executeRenderPipeline(input: {? "drawElement renderer stalled; retrying with forceScreenshot": isSequentialStall? "sequential capture stalled; retrying with a fresh screenshot session"- : "capture failed; retrying with a fresh screenshot session",+ : isParallelStall+ ? "parallel capture stalled; retrying with a fresh single-worker screenshot session"+ : isEncoderDeath+ ? "streaming encoder died; retrying with a fresh screenshot session"+ : "capture failed; retrying with a fresh screenshot session",);const failedRouting = capturePlan.routing.kind;const failure = streamingCaptureFailure(