Skip to content

Write HTML. Render video. Built for agents.

fix(producer,cli): hash every byte-affecting segment input; pin the resume-flag plumbing

Vance Ingallse7c4eb52b9d2
diff --git a/packages/cli/src/commands/render/execute.test.ts b/packages/cli/src/commands/render/execute.test.ts
new file mode 100644
index 0000000000..82a94bb492
--- /dev/null
+++ b/packages/cli/src/commands/render/execute.test.ts
@@ -0,0 +1,42 @@
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { createRenderPlan } from "./plan.js";
+import { renderOptionsFromPlan } from "./execute.js";
+
+describe("renderOptionsFromPlan", () => {
+ let projectDir: string;
+
+ beforeEach(() => {
+ projectDir = mkdtempSync(join(tmpdir(), "hf-render-execute-"));
+ writeFileSync(
+ join(projectDir, "index.html"),
+ '<main data-composition-id="main" data-width="1920" data-height="1080" data-fps="24"></main>',
+ );
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ rmSync(projectDir, { recursive: true, force: true });
+ });
+
+ it("carries the segmented resume flags from the plan into the render options", () => {
+ // The hop that once dropped --resume / --keep-segments silently; the plan
+ // -> options -> request -> config chain is otherwise only exercised by a
+ // manual render gate.
+ const off = renderOptionsFromPlan(createRenderPlan({ dir: projectDir }), undefined, undefined);
+ expect(off.resumeSegments).toBe(false);
+ expect(off.keepSegments).toBe(false);
+ const on = renderOptionsFromPlan(
+ createRenderPlan({ dir: projectDir, resume: true, "keep-segments": true }),
+ "/usr/bin/chrome",
+ { title: "x" },
+ );
+ expect(on.resumeSegments).toBe(true);
+ expect(on.keepSegments).toBe(true);
+ expect(on.browserPath).toBe("/usr/bin/chrome");
+ expect(on.variables).toEqual({ title: "x" });
+ });
+});
diff --git a/packages/cli/src/commands/render/execute.ts b/packages/cli/src/commands/render/execute.ts
index b73ece9a12..a929c16ca7 100644
--- a/packages/cli/src/commands/render/execute.ts
+++ b/packages/cli/src/commands/render/execute.ts
@@ -116,6 +116,21 @@ export async function executeRenderPlan(
reportVariableIssues(issues, { strict: plan.strictVariables, quiet: plan.quiet });
}
+ const options = renderOptionsFromPlan(plan, browserPath, variables);
+ const execute = plan.useDocker ? dependencies.renderDocker : dependencies.renderLocal;
+ await execute(plan.project.dir, plan.outputPath, options, cancellation);
+}
+
+/**
+ * The plan -> RenderOptions hop for a single render. Pure and exported so the
+ * flags that cross it are pinned by a test: `--resume` / `--keep-segments`
+ * were once dropped exactly here and nothing but a manual gate noticed.
+ */
+export function renderOptionsFromPlan(
+ plan: RenderPlan,
+ browserPath: string | undefined,
+ variables: RenderOptions["variables"],
+): RenderOptions {
const options: RenderOptions = {
fps: plan.fps,
quality: plan.quality,
@@ -157,8 +172,7 @@ export async function executeRenderPlan(
options.pageSideCompositing = plan.pageSideCompositing;
options.experimentalFastCapture = plan.experimentalFastCapture;
}
- const execute = plan.useDocker ? dependencies.renderDocker : dependencies.renderLocal;
- await execute(plan.project.dir, plan.outputPath, options, cancellation);
+ return options;
}
function assertRenderActive(cancellation?: RenderCancellationScope): void {
@@ -345,6 +359,8 @@ async function executeBatchRender(
protocolTimeout: plan.protocolTimeout,
playerReadyTimeout: plan.playerReadyTimeout,
debug: plan.debug,
+ resumeSegments: plan.resumeSegments,
+ keepSegments: plan.keepSegments,
bestEffort: plan.bestEffort,
exitAfterComplete: false,
throwOnError: true,
diff --git a/packages/producer/src/renderRequest.test.ts b/packages/producer/src/renderRequest.test.ts
index fdb10c1944..9fb1cca947 100644
--- a/packages/producer/src/renderRequest.test.ts
+++ b/packages/producer/src/renderRequest.test.ts
@@ -345,3 +345,25 @@ describe("RenderRequest", () => {
});
});
});
+
+describe("segmented resume flags", () => {
+ it("carries resumeSegments and keepSegments from the request into the render config", () => {
+ const req = createRenderRequest({
+ projectDir: "/project",
+ outputPath: "/output/video.mp4",
+ engineConfig: { ...DEFAULT_CONFIG },
+ options: {
+ fps: { num: 30, den: 1 },
+ quality: "high",
+ format: "mp4",
+ gifLoop: 0,
+ strictness: "best-effort",
+ resumeSegments: true,
+ keepSegments: true,
+ },
+ });
+ const config = renderConfigFromRequest(req);
+ expect(config.resumeSegments).toBe(true);
+ expect(config.keepSegments).toBe(true);
+ });
+});
diff --git a/packages/producer/src/services/render/segmentManifest.test.ts b/packages/producer/src/services/render/segmentManifest.test.ts
index 26a1d997ef..1c2469671f 100644
--- a/packages/producer/src/services/render/segmentManifest.test.ts
+++ b/packages/producer/src/services/render/segmentManifest.test.ts
@@ -27,6 +27,10 @@ const hashInput: SegmentPlanHashInput = {
bitrate: undefined,
pixelFormat: "yuv420p",
imageFormat: "jpeg",
+ useGpu: false,
+ outputWidth: 1920,
+ outputHeight: 1080,
+ motionBlur: "",
};
describe("computeSegmentPlanHash", () => {
@@ -53,6 +57,17 @@ describe("computeSegmentPlanHash", () => {
expect(computeSegmentPlanHash({ ...hashInput, bitrate: "8M" })).not.toBe(a);
expect(computeSegmentPlanHash({ ...hashInput, pixelFormat: "yuv420p10le" })).not.toBe(a);
expect(computeSegmentPlanHash({ ...hashInput, imageFormat: "png" })).not.toBe(a);
+ // Review findings on the Phase 2b PR: three byte-affecting inputs that
+ // used to be missing. NVENC and libx264 both report codec "h264"; a DPR
+ // change captures at a different pixel size than the composition's; a
+ // motion-blur sample count changes every averaged frame.
+ expect(computeSegmentPlanHash({ ...hashInput, useGpu: true })).not.toBe(a);
+ expect(
+ computeSegmentPlanHash({ ...hashInput, outputWidth: 3840, outputHeight: 2160 }),
+ ).not.toBe(a);
+ expect(computeSegmentPlanHash({ ...hashInput, motionBlur: '{"samplesPerFrame":32}' })).not.toBe(
+ a,
+ );
});
it("does not collide across adjacent field boundaries", () => {
diff --git a/packages/producer/src/services/render/segmentManifest.ts b/packages/producer/src/services/render/segmentManifest.ts
index 52d94d4af5..5e7203fa77 100644
--- a/packages/producer/src/services/render/segmentManifest.ts
+++ b/packages/producer/src/services/render/segmentManifest.ts
@@ -41,6 +41,13 @@ export interface SegmentPlanHashInput {
bitrate: string | undefined;
pixelFormat: string | undefined;
imageFormat: string;
+ /** Hardware encoder or not: h264_nvenc and libx264 both report codec "h264" but produce different bytes. */
+ useGpu: boolean;
+ /** Device-scaled capture size; `width`/`height` are the composition's CSS size. */
+ outputWidth: number;
+ outputHeight: number;
+ /** Serialised motion-blur options, or "" when off; sample count and shutter change every pixel. */
+ motionBlur: string;
}
const MANIFEST_FILENAME = "segments.json";
@@ -60,6 +67,9 @@ export function computeSegmentPlanHash(input: SegmentPlanHashInput): string {
input.bitrate ?? "",
input.pixelFormat ?? "",
input.imageFormat,
+ input.useGpu ? "gpu" : "cpu",
+ `${input.outputWidth}x${input.outputHeight}`,
+ input.motionBlur,
].join(" ");
return createHash("sha256").update(ordered).digest("hex").slice(0, 16);
}
diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.ts
index e5a9c87738..e6eea620b9 100644
--- a/packages/producer/src/services/renderOrchestrator.ts
+++ b/packages/producer/src/services/renderOrchestrator.ts
@@ -4307,6 +4307,8 @@ async function executeRenderPipeline(input: {
const captureFrameStart = Date.now();
resetCaptureAttemptProgress(job);
const segmentFrames = resolveSegmentFrames(process.env);
+ // One binding for the hash and the encoder so the two cannot drift.
+ const segmentImageFormat = captureOptions.format || "jpeg";
const segmentPlanHash = computeSegmentPlanHash({
compositionHash: compositionHash ?? "",
cliVersion: process.env.npm_package_version ?? "dev",
@@ -4320,7 +4322,12 @@ async function executeRenderPipeline(input: {
quality: effectiveQuality,
bitrate: effectiveBitrate,
pixelFormat: preset.pixelFormat,
- imageFormat: captureOptions.format || "jpeg",
+ imageFormat: segmentImageFormat,
+ useGpu: job.config.useGpu === true,
+ // Device-scaled: the capture buffer, not the CSS composition size.
+ outputWidth: captureCompositionWidth ?? width,
+ outputHeight: captureCompositionHeight ?? height,
+ motionBlur: job.config.motionBlur ? JSON.stringify(job.config.motionBlur) : "",
});
const segmentDir = segmentDirFor(join(projectDir, "renders"), segmentPlanHash);
let completedSegments: ReadonlySet<number> = new Set<number>();
@@ -4377,7 +4384,7 @@ async function executeRenderPipeline(input: {
pixelFormat: preset.pixelFormat,
vp9CpuUsed: cfg.vp9CpuUsed,
useGpu: job.config.useGpu,
- imageFormat: captureOptions.format || "jpeg",
+ imageFormat: segmentImageFormat,
hdr: preset.hdr,
// No hlsEncoderGopLock here: each segment sets its own GOP to
// its own length, and hls never reaches this route.