perf(producer): distributed chunks download only the videos they show

A project video under compiled/ that chunks extract their own frames from
now goes only to the chunks that show it, like video-sources/ files. A
chunk skips the readiness wait for a deferred video it does not show, so
it no longer logs media_load_failed for a file it was never given.
This commit is contained in:
Miguel Angel Simon Sierra
2026-10-16 23:51:43 -07:00
parent cc9e1bb33a
commit 1cdc271cd6
4 changed files with 159 additions and 22 deletions
@@ -450,20 +450,43 @@ describe("Plan v2 manifest", () => {
);
});
it("keeps a deferred source the page itself loads in every chunk", () => {
it("ships each compiled/ video source only to the chunks that show it", () => {
const root = tempPath("hf-plan-v2-deferred-compiled-source-");
const v1 = createV1Plan(root, {
video: true,
videoStart: 1 / 30,
deferredSource: "compiled/hero.mp4",
});
// A second clip shown in both chunks, also shipped from compiled/.
const videosPath = join(v1, "meta", "videos.json");
const videos = JSON.parse(readFileSync(videosPath, "utf-8")) as PlanVideosJson;
const hero = videos.extracted[0]!;
videos.videos.push({ ...videos.videos[0]!, id: "long", src: "long.mp4", start: 0 });
videos.extracted.push({
...hero,
videoId: "long",
deferredRange: { ...hero.deferredRange!, sourcePath: "compiled/long.mp4" },
});
writeFileSync(videosPath, JSON.stringify(videos));
writeFileSync(join(v1, "compiled", "long.mp4"), "long source video");
refreshV1PlanHash(v1);
const manifest = readPlanV2Manifest(createPlanV2FromV1(v1, join(root, "v2")).planDir);
const chunksOf = (path: string) =>
manifest.artifacts.find((artifact) => artifact.path === path)?.chunks;
expect(manifest.limitations.videoDependencyMode).toBe("source-extract");
for (const chunkIndex of [0, 1]) {
const artifacts = listPlanV2ArtifactsForTarget(manifest, { role: "chunk", chunkIndex });
expect(artifacts.some((artifact) => artifact.path === "compiled/hero.mp4")).toBe(true);
}
expect(chunksOf("compiled/hero.mp4")).toEqual([1]);
expect(chunksOf("compiled/long.mp4")).toEqual([0, 1]);
expect(chunksOf("compiled/index.html")).toBe("all");
expect(chunksOf("compiled/asset.txt")).toBe("all");
const chunk0Dir = join(root, "chunk-0");
materializePlanV2Target(join(root, "v2"), { role: "chunk", chunkIndex: 0 }, chunk0Dir);
expect(existsSync(join(chunk0Dir, "compiled", "hero.mp4"))).toBe(false);
expect(existsSync(join(chunk0Dir, "compiled", "long.mp4"))).toBe(true);
expect(validatePlanV2MaterializedTarget(chunk0Dir, { role: "chunk", chunkIndex: 0 })).not.toBe(
null,
);
});
it("rejects a deferred video source outside the plan directory", () => {
@@ -288,8 +288,18 @@ function artifactTargets(
if (path === "plan.json" || path === "meta/chunks.json" || path === "meta/encoder.json") {
return { chunks: "all", assembler: true };
}
// Each chunk's page loads every <video> src at init (a missing one fails the
// chunk), so deferred sources under compiled/ still go to every chunk.
// A compiled/ file in the map is a deferred video source. Chunks inject its
// frames and skip its <video> readiness, so only chunks that show it get it.
if (
path.startsWith("video-frames/") ||
path.startsWith("video-sources/") ||
(path.startsWith("compiled/") && videoDependencies?.has(path))
) {
return {
chunks: videoDependencies === null ? "all" : (videoDependencies.get(path) ?? []),
assembler: false,
};
}
if (
path === "meta/composition.json" ||
path === "meta/videos.json" ||
@@ -297,12 +307,6 @@ function artifactTargets(
) {
return { chunks: "all", assembler: false };
}
if (path.startsWith("video-frames/") || path.startsWith("video-sources/")) {
return {
chunks: videoDependencies === null ? "all" : (videoDependencies.get(path) ?? []),
assembler: false,
};
}
// Unknown future execution files go to both roles. Over-including is safe;
// silently omitting a new execution dependency is not.
return { chunks: "all", assembler: true };
@@ -20,13 +20,24 @@
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import {
copyFileSync,
cpSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { type CaptureOptions, extractVideoFramesRange } from "@hyperframes/engine";
import { HOST_CHROME_FAILURE_PATTERNS } from "./__test_utils__/hostChromeFailures.js";
import { defaultLogger } from "../../logger.js";
import { RenderQualityError } from "../renderOrchestrator.js";
import { plan } from "./plan.js";
import { materializePlanV2Target, planV2, readPlanV2Manifest } from "./planV2.js";
import {
CHUNK_INDEX_OUT_OF_RANGE,
distributedCaptureSessionDependencies,
@@ -530,6 +541,98 @@ describe("renderChunk() — script failure", () => {
}, 60_000);
});
describe("renderChunk() — a video the chunk does not show", () => {
it("renders the same without the video's file, with no media warning", async () => {
if (!hasChrome) {
console.warn(
"[renderChunk.test] skipping unshown-video test — chrome-headless-shell not available on this host",
);
return;
}
const videoDir = join(runRoot, "project-two-videos");
mkdirSync(videoDir, { recursive: true });
for (const [name, seconds] of [
["short.mp4", 2],
["long.mp4", 3],
] as const) {
const synth = spawnSync("ffmpeg", [
"-v",
"error",
"-y",
"-f",
"lavfi",
"-i",
`testsrc2=s=64x36:r=30:d=${seconds}`,
"-c:v",
"libx264",
"-pix_fmt",
"yuv420p",
join(videoDir, name),
]);
if (synth.status !== 0) throw new Error(synth.stderr.toString());
}
writeFileSync(
join(videoDir, "index.html"),
// Clips over a second long, so the planner defers them to the chunks.
FIXTURE_HTML.replace('data-duration="0.16667"', 'data-duration="2.4"').replace(
"</div>",
'<video id="short" src="short.mp4" muted data-start="0" data-duration="1.2" style="position:absolute;left:0;top:0;width:64px"></video>' +
'<video id="long" src="long.mp4" muted data-start="0" data-duration="2.4" style="position:absolute;right:0;bottom:0;width:64px"></video></div>',
),
"utf-8",
);
const v2Dir = join(runRoot, "plan-two-videos");
await planV2(
videoDir,
{ fps: 30, width: 160, height: 120, format: "png-sequence", chunkSize: 36 },
v2Dir,
);
const manifest = readPlanV2Manifest(v2Dir);
expect(manifest.artifacts.find((a) => a.path === "compiled/short.mp4")?.chunks).toEqual([0]);
const without = join(runRoot, "chunk1-without-short");
const withShort = join(runRoot, "chunk1-with-short");
materializePlanV2Target(v2Dir, { role: "chunk", chunkIndex: 1 }, without);
cpSync(without, withShort, { recursive: true });
copyFileSync(join(videoDir, "short.mp4"), join(withShort, "compiled", "short.mp4"));
const warnings: unknown[] = [];
const skipped: (readonly string[] | undefined)[] = [];
const originalWarn = defaultLogger.warn;
const originalCreate = distributedCaptureSessionDependencies.createCaptureSession;
defaultLogger.warn = (message, meta) => {
warnings.push(meta);
originalWarn(message, meta);
};
distributedCaptureSessionDependencies.createCaptureSession = ((...args) => {
skipped.push(args[2].skipReadinessVideoIds);
return originalCreate(...args);
}) as typeof originalCreate;
let a, b;
try {
a = await renderChunk(without, 1, join(runRoot, "chunk1-out-without"));
b = await renderChunk(withShort, 1, join(runRoot, "chunk1-out-with"));
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (!HOST_CHROME_FAILURE_PATTERNS.test(message)) throw err;
console.warn(
"[renderChunk.test] skipping unshown-video test — host Chrome stack can't render. Diagnostic:",
message.slice(0, 240),
);
return;
} finally {
defaultLogger.warn = originalWarn;
distributedCaptureSessionDependencies.createCaptureSession = originalCreate;
}
expect(JSON.stringify(warnings)).not.toContain("media_load_failed");
// Only the unshown video is skipped; the shown one keeps today's readiness wait.
expect(new Set(skipped.map(String))).toEqual(new Set(["short"]));
expect(a.sha256).toBe(b.sha256);
}, 90_000);
});
describe("resolvePresetForLockedEncoder", () => {
// Tiny fast tests for the codec-override helper. No Chrome, no ffmpeg —
// exists so a refactor that moves the override (e.g. into
@@ -774,6 +774,16 @@ export async function renderChunk(
// its own injector hook because the hook remembers the last injected frame
// per video; sharing that state across a fresh retry page can suppress the
// first injection and produce a blank/stale frame.
const deferredVideoFrames = planVideos
? await extractDeferredVideoFramesForChunk({
planDir,
planVideos,
slice,
fps: { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
outputRoot: videoFramesDir,
cfg,
})
: [];
const videoFrameLookup =
planVideos && planVideos.extracted.length > 0
? createFrameLookupTable(
@@ -784,14 +794,7 @@ export async function renderChunk(
planVideos.extracted.filter((video) => !video.deferredRange),
v2Manifest === null ? "dense-v1" : "sparse-v2",
),
...(await extractDeferredVideoFramesForChunk({
planDir,
planVideos,
slice,
fps: { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
outputRoot: videoFramesDir,
cfg,
})),
...deferredVideoFrames,
],
resolveRenderFpsConfig(job.config.fps).value,
)
@@ -832,6 +835,10 @@ export async function renderChunk(
// engine skips the `evaluateOnNewDocument` injection.
variables: encoder.variables,
subTimelineWaitMemo: {},
// The plan ships a deferred source only to chunks that show it; don't wait on the rest.
skipReadinessVideoIds: deferredVideoFrames
.filter((video) => video.framePaths.size === 0)
.map((video) => video.videoId),
...(videoCaptureBeyondViewport !== undefined
? { captureBeyondViewport: videoCaptureBeyondViewport }
: {}),