Skip to content

Write HTML. Render video. Built for agents.

fix(producer): long renders with small frames no longer fail the up-front disk estimate (#6117)

Miguel Ángelaeecf38345f0
diff --git a/packages/producer/src/services/render/stages/captureStage.test.ts b/packages/producer/src/services/render/stages/captureStage.test.ts
index 169098437a..c8b4d94432 100644
--- a/packages/producer/src/services/render/stages/captureStage.test.ts
+++ b/packages/producer/src/services/render/stages/captureStage.test.ts
@@ -41,7 +41,6 @@ vi.mock("node:fs", async (importOriginal) => {
});
import {
- assertDiskCaptureHeadroom,
createDiskCaptureProjection,
diskCaptureRemedy,
diskCaptureShortfallError,
@@ -87,18 +86,6 @@ describe("disk capture capacity", () => {
expect(estimateDiskCaptureBytes(10, { ...captureOptions, quality: 100 })).toBe(200_000);
});
- it("fails before capture when estimated frames exceed available headroom", () => {
- expect(() =>
- assertDiskCaptureHeadroom(
- "/render/captured-frames",
- 10,
- pngOptions,
- "Free up disk space.",
- () => 800_000,
- ),
- ).toThrow(/may need ~0\.8 MB.*0\.8 MB is free.*Free up disk space\./s);
- });
-
it("exposes the same 90% headroom decision to fallback planning", () => {
const estimatedBytes = estimateDiskCaptureBytes(10, pngOptions);
@@ -298,6 +285,29 @@ describe("runCaptureStage measured disk projection (both branches)", () => {
expect(createCaptureSession).toHaveBeenCalledTimes(1);
});
+ it("sequential: a png render whose raw estimate exceeds free space still captures when frames are small", async () => {
+ const { workDir, framesDir } = makeDirs();
+ // 64x64 png estimates 20 * 16 KB = 327 KB raw; 100 KB free, but the real frames are 100 bytes.
+ freeBytes.mockReturnValue({ bavail: 100_000, bsize: 1 });
+ createCaptureSession.mockResolvedValue({
+ isInitialized: false,
+ browserConsoleBuffer: [],
+ options: { captureBeyondViewport: false, format: "png" },
+ workerEncodeEnabled: false,
+ outputDir: framesDir,
+ });
+ captureFrame.mockImplementation(async (_s: unknown, index: number) => {
+ writeFileSync(join(framesDir, formatCaptureFrameName(index, "png")), Buffer.alloc(100));
+ });
+ const input = makeInput(1, workDir, framesDir);
+
+ await runCaptureStage({
+ ...input,
+ buildCaptureOptions: () => ({ ...input.buildCaptureOptions(), format: "png" as const }),
+ });
+ expect(captureFrame).toHaveBeenCalledTimes(20);
+ });
+
it("a measured shortfall is never classified as a retryable browser death", () => {
const shortfall = diskCaptureShortfallError(10e9, 1e9, "/frames", "measured", "");
const kind = classifyCaptureFailure(shortfall);
diff --git a/packages/producer/src/services/render/stages/captureStage.ts b/packages/producer/src/services/render/stages/captureStage.ts
index c949ec1faa..c12482a8a7 100644
--- a/packages/producer/src/services/render/stages/captureStage.ts
+++ b/packages/producer/src/services/render/stages/captureStage.ts
@@ -26,13 +26,9 @@
* - `job.framesRendered` is updated at every per-frame / per-progress
* point; `Capturing frame N/M` goes through `reportFrameProgress`.
*
- * Known follow-up: this stage imports `executeDiskCaptureWithAdaptiveRetry`
- * from `renderOrchestrator.ts`, which itself imports the stage — a runtime
- * cycle that resolves at module-init time because no stage function is
- * invoked during load. A subsequent PR will consolidate the capture
- * helpers (`executeDiskCaptureWithAdaptiveRetry`, `countFrameRanges`,
- * `safeCleanup`, `sampleDirectoryBytes`, etc.) into a shared module so
- * the stages can import them without reaching back into the orchestrator.
+ * Known follow-up: this stage imports `executeDiskCaptureWithAdaptiveRetry` from
+ * `renderOrchestrator.ts`, which imports the stage (a cycle that resolves at module init). The
+ * capture helpers should move to a shared module so stages stop reaching back into the orchestrator.
*/
import { existsSync, readdirSync, statfsSync } from "node:fs";
@@ -150,7 +146,6 @@ export function shouldAllowAdaptiveCaptureRetry(
const JPEG_RAW_RGBA_DIVISOR = 8;
/** Above draft's quality 80; quality-100 jpeg of grainy 4K footage measures about 1/6 of raw RGBA. */
const HIGH_QUALITY_JPEG_RAW_RGBA_DIVISOR = 4;
-/** Frames captured before the measured projection replaces the static estimate. */
const DISK_PROJECTION_SAMPLE_FRAMES = 10;
export function estimateDiskCaptureBytes(
@@ -255,29 +250,6 @@ export function diskCaptureShortfallError(
);
}
-export function assertDiskCaptureHeadroom(
- framesDir: string,
- totalFrames: number,
- captureOptions: CaptureOptions,
- remedy: string,
- freeDiskBytes?: (path: string) => number | null,
-): void {
- const headroom = inspectDiskCaptureHeadroom(
- framesDir,
- totalFrames,
- captureOptions,
- freeDiskBytes,
- );
- if (headroom.available) return;
- throw diskCaptureShortfallError(
- headroom.estimatedBytes,
- headroom.freeBytes,
- framesDir,
- "estimate",
- remedy,
- );
-}
-
/** Frame bytes written so far: the merged frames dir plus every capture attempt's worker dirs. */
export function measureCaptureFrameBytes(workDir: string, framesDir: string): number {
let total = sampleDirectoryBytes(framesDir);
@@ -380,7 +352,6 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
const captureOptions = buildCaptureOptions();
const remedy = diskCaptureRemedy(cfg, job.config.format ?? "mp4", job.duration ?? 0, workerCount);
- assertDiskCaptureHeadroom(framesDir, totalFrames, captureOptions, remedy);
const checkDiskProjection = createDiskCaptureProjection({
framesDir,
totalFrames,