Skip to content

Write HTML. Render video. Built for agents.

fix(producer): plan hash no longer depends on the scratch folder (#5782)

Miguel Ángeldeeb8d437c64
diff --git a/packages/producer/src/services/distributed/plan.test.ts b/packages/producer/src/services/distributed/plan.test.ts
index 14750e1040..d7322cc5bf 100644
--- a/packages/producer/src/services/distributed/plan.test.ts
+++ b/packages/producer/src/services/distributed/plan.test.ts
@@ -15,8 +15,8 @@
* inside the regression harness (`bun run --cwd packages/producer docker:test`).
*/
-import { spawnSync } from "node:child_process";
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
+import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -788,10 +788,11 @@ describe("plan() — golden planDir + planHash determinism", () => {
// variant generates a 1s silent wav via `ffmpeg`, references it from
// the composition, and runs plan() — exercising the audio-mix path
// that produced the original bug.
- const HAS_FFMPEG = (() => {
- const { spawnSync } = require("node:child_process") as typeof import("node:child_process");
- return spawnSync("ffmpeg", ["-version"]).status === 0;
- })();
+ const HAS_FFMPEG = spawnSync("ffmpeg", ["-version"]).status === 0;
+ const ffmpegOrThrow = (args: string[]) => {
+ const ffmpeg = spawnSync("ffmpeg", ["-nostdin", "-loglevel", "error", ...args]);
+ if (ffmpeg.status !== 0) throw new Error(`ffmpeg fixture failed: ${ffmpeg.stderr?.toString()}`);
+ };
it.skipIf(!HAS_FFMPEG)(
"plan.json.planHash matches recompute on an audio-bearing composition",
@@ -801,11 +802,7 @@ describe("plan() — golden planDir + planHash determinism", () => {
// Generate a 1s mono silent wav. PCM keeps the file tiny without
// pulling in an audio asset fixture.
const audioPath = join(audioProjectDir, "silence.wav");
- const { spawnSync } = require("node:child_process") as typeof import("node:child_process");
- const ffmpeg = spawnSync("ffmpeg", [
- "-nostdin",
- "-v",
- "error",
+ ffmpegOrThrow([
"-f",
"lavfi",
"-i",
@@ -816,9 +813,6 @@ describe("plan() — golden planDir + planHash determinism", () => {
"pcm_s16le",
audioPath,
]);
- if (ffmpeg.status !== 0) {
- throw new Error(`ffmpeg silence-wav generation failed: ${ffmpeg.stderr?.toString()}`);
- }
writeFileSync(
join(audioProjectDir, "index.html"),
`<!doctype html>
@@ -852,6 +846,46 @@ describe("plan() — golden planDir + planHash determinism", () => {
},
TIMEOUT_MS,
);
+
+ it.skipIf(!HAS_FFMPEG)(
+ "plans a video-bearing project to the same planHash from different plan folders",
+ async () => {
+ const videoProjectDir = join(runRoot, "project-with-video");
+ mkdirSync(videoProjectDir, { recursive: true });
+ ffmpegOrThrow([
+ "-f",
+ "lavfi",
+ "-i",
+ "testsrc=size=320x240:rate=30:duration=1",
+ "-pix_fmt",
+ "yuv420p",
+ "-c:v",
+ "libx264",
+ join(videoProjectDir, "clip.mp4"),
+ ]);
+ writeFileSync(
+ join(videoProjectDir, "index.html"),
+ `<!doctype html>
+<div data-composition-id="root" data-width="320" data-height="240" data-duration="1">
+ <video id="clip" src="clip.mp4" data-start="0" muted></video>
+</div>`,
+ "utf-8",
+ );
+
+ const config = { fps: 30 as const, width: 320, height: 240, format: "mp4" as const };
+ const dirA = join(runRoot, "plan-video-a");
+ const dirB = join(runRoot, "nested", "plan-video-b");
+ for (const dir of [dirA, dirB]) mkdirSync(dir, { recursive: true });
+ const a = await plan(videoProjectDir, config, dirA);
+ const b = await plan(videoProjectDir, config, dirB);
+
+ expect(a.planHash).toBe(b.planHash);
+ const videosJson = (dir: string) => readFileSync(join(dir, "meta", "videos.json"), "utf-8");
+ expect(videosJson(dirB)).toBe(videosJson(dirA));
+ expect(JSON.parse(videosJson(dirA)).extracted[0].srcPath).toBe("compiled/clip.mp4");
+ },
+ TIMEOUT_MS,
+ );
});
describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("stitched mp4 plans", () => {
diff --git a/packages/producer/src/services/distributed/plan.ts b/packages/producer/src/services/distributed/plan.ts
index 1a9edb3982..441083ac51 100644
--- a/packages/producer/src/services/distributed/plan.ts
+++ b/packages/producer/src/services/distributed/plan.ts
@@ -998,6 +998,18 @@ function pathInside(root: string, path: string): string | null {
return rel === "" || rel.startsWith("..") || isAbsolute(rel) ? null : rel;
}
+/**
+ * Name a video source without the planner's scratch folder, so `meta/videos.json`
+ * (and the planHash over it) is the same on every run with the same inputs.
+ */
+function stableSourcePath(roots: ReadonlyArray<[root: string, prefix: string]>, srcPath: string) {
+ for (const [root, prefix] of roots) {
+ const rel = pathInside(root, srcPath);
+ if (rel !== null) return [prefix, ...rel.split(sep)].filter(Boolean).join("/");
+ }
+ return srcPath;
+}
+
/**
* Return a planDir-relative path chunks can extract a deferred video from.
* Sources the plan already ships under `compiled/` are reused; anything else
@@ -1329,12 +1341,18 @@ export async function buildLocalExecutionPlan(
workCompiledDir: compiledDir,
workVideoFramesDir: stagedVideoFrames,
});
+ // Staged frames live inside compiledDir, so they must match first.
+ const sourceRoots: Array<[string, string]> = [
+ [stagedVideoFrames, "video-frames"],
+ [compiledDir, "compiled"],
+ [projectDir, ""],
+ ];
const planVideosJson: PlanVideosJson = buildPlanVideosJson({
videos: composition.videos,
compositionEnd: job.duration ?? Number.NaN,
extracted: (extractResult.extractionResult?.extracted ?? []).map((ext) => ({
videoId: ext.videoId,
- srcPath: ext.srcPath,
+ srcPath: stableSourcePath(sourceRoots, ext.srcPath),
framePattern: ext.framePattern,
fps: ext.fps,
totalFrames: ext.totalFrames,