Skip to content

Commit 25f5ae3

Browse files
committed
Merge remote-tracking branch 'origin/main' into dslivers/far-scenes-skip-render
# Conflicts: # scripts/package-size-budgets.json
2 parents d4ff3fc + b2124b2 commit 25f5ae3

13 files changed

Lines changed: 577 additions & 46 deletions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import type { Page } from "puppeteer-core";
3+
import { seekDeVerifyTruth } from "./frameCapture.js";
4+
5+
const pageOf = () =>
6+
({
7+
evaluate: async (fn: (t: number, s: boolean) => void, t: number, s: boolean) => fn(t, s),
8+
}) as unknown as Page;
9+
10+
describe("seekDeVerifyTruth", () => {
11+
afterEach(() => vi.unstubAllGlobals());
12+
13+
it("runs timeline callbacks when asked, as the render's own seek does", async () => {
14+
const seek = vi.fn();
15+
vi.stubGlobal("window", { __hf: { seek } });
16+
17+
await seekDeVerifyTruth(pageOf(), 5, false);
18+
19+
expect(seek).toHaveBeenCalledWith(5, { suppressEvents: false });
20+
});
21+
22+
it("suppresses them on the render's own page, where a callback would change what it draws", async () => {
23+
const seek = vi.fn();
24+
vi.stubGlobal("window", { __hf: { seek } });
25+
26+
await seekDeVerifyTruth(pageOf(), 5, true);
27+
28+
expect(seek).toHaveBeenCalledWith(5, { suppressEvents: true });
29+
});
30+
31+
it("does nothing on a page without the seek protocol", async () => {
32+
vi.stubGlobal("window", {});
33+
34+
await expect(seekDeVerifyTruth(pageOf(), 5, false)).resolves.toBeUndefined();
35+
});
36+
});

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,6 +5093,26 @@ export function computeDeVerifySampleFractions(k: number): number[] {
50935093
return [...Array.from({ length: k - 1 }, (_, i) => (i + 1) / k), 0.95];
50945094
}
50955095

5096+
/** Seeks the page the truth is read from; callbacks run unless suppressed (suppressed, a count-up's text is stale). */
5097+
export async function seekDeVerifyTruth(
5098+
page: Page,
5099+
t: number,
5100+
suppressEvents: boolean,
5101+
): Promise<void> {
5102+
await page.evaluate(
5103+
(tt: number, suppress: boolean) => {
5104+
const hf = (
5105+
window as unknown as {
5106+
__hf?: { seek?: (x: number, options?: { suppressEvents?: boolean }) => void };
5107+
}
5108+
).__hf;
5109+
if (hf && typeof hf.seek === "function") hf.seek(tt, { suppressEvents: suppress });
5110+
},
5111+
t,
5112+
suppressEvents,
5113+
);
5114+
}
5115+
50965116
async function captureDeVerificationFrames(
50975117
session: CaptureSession,
50985118
page: Page,
@@ -5142,47 +5162,47 @@ async function captureDeVerificationFrames(
51425162
// would share the corruption, PSNR would pass on the damaged output.
51435163
// Seeking 0 → ascending reproduces the render's own seek order.
51445164
const fractions = computeDeVerifySampleFractions(k);
5145-
const seekTo = async (t: number): Promise<void> => {
5146-
await page.evaluate((tt: number) => {
5147-
const hf = (
5148-
window as unknown as {
5149-
__hf?: { seek?: (x: number, options?: { suppressEvents?: boolean }) => void };
5150-
}
5151-
).__hf;
5152-
if (hf && typeof hf.seek === "function") hf.seek(tt, { suppressEvents: true });
5153-
}, t);
5154-
};
5155-
await seekTo(quantizeTimeToFrame(0, fps));
5156-
// Force one frame so lazy tween initialization paints at t=0 state.
5157-
await pageScreenshotCapture(page, session.options);
5165+
// A video-free comp reads its truth on a disposable page: callbacks that run there
5166+
// cannot leave the render's own page changed. The video injector is tied to the render page.
5167+
const disposable = session.onBeforeCapture
5168+
? null
5169+
: await createStaticVerificationPage(session).catch(() => {
5170+
logInitPhase("self-verify truth page unavailable; reading it on the render page");
5171+
return null;
5172+
});
5173+
const isolated = disposable !== null;
5174+
const truthPage = disposable ?? page;
5175+
const seekTo = (t: number): Promise<void> => seekDeVerifyTruth(truthPage, t, !isolated);
51585176
const frames = new Map<number, Buffer>();
5159-
for (const f of fractions) {
5160-
let idx = Math.min(totalFrames - 1, Math.max(1, Math.round(totalFrames * f)));
5161-
// Nudge off clip-cut boundaries (±1-frame desync is legitimate there);
5162-
// if the nudge saturates on a boundary index, skip the sample entirely.
5163-
let guard = 0;
5164-
while (boundary.has(idx) && guard++ < 6) idx = Math.min(totalFrames - 1, idx + 2);
5165-
if (boundary.has(idx)) continue;
5166-
if (frames.has(idx)) continue;
5167-
const t = quantizeTimeToFrame(idx / fps, fps);
5168-
await seekTo(t);
5169-
// Video frame injection (same hook the real capture paths run) — without
5170-
// it, <video> elements screenshot black and every video comp would
5171-
// false-positive into the screenshot fallback.
5172-
if (session.onBeforeCapture) await session.onBeforeCapture(page, t);
5173-
// Double-capture: the first screenshot forces a frame, which is what runs
5174-
// rAF-driven callbacks (count-up text counters land a tick after seek()
5175-
// returns — a single immediate screenshot captures stale text and
5176-
// false-positives the verify: 3bea8c73 28.7dB vs a truth missing its stat
5177-
// values while the DE frame was correct). NOTE: waiting on rAF via
5178-
// evaluate instead deadlocks — headless only fires rAF when a frame is
5179-
// produced, and nothing produces one until a screenshot asks.
5180-
await pageScreenshotCapture(page, session.options);
5181-
frames.set(idx, await pageScreenshotCapture(page, session.options));
5177+
try {
5178+
await seekTo(quantizeTimeToFrame(0, fps));
5179+
// Force one frame so lazy tween initialization paints at t=0 state.
5180+
await pageScreenshotCapture(truthPage, session.options);
5181+
for (const f of fractions) {
5182+
let idx = Math.min(totalFrames - 1, Math.max(1, Math.round(totalFrames * f)));
5183+
// Nudge off clip-cut boundaries (±1-frame desync is legitimate there);
5184+
// if the nudge saturates on a boundary index, skip the sample entirely.
5185+
let guard = 0;
5186+
while (boundary.has(idx) && guard++ < 6) idx = Math.min(totalFrames - 1, idx + 2);
5187+
if (boundary.has(idx)) continue;
5188+
if (frames.has(idx)) continue;
5189+
const t = quantizeTimeToFrame(idx / fps, fps);
5190+
await seekTo(t);
5191+
// Video frame injection (same hook the real capture paths run) — without
5192+
// it, <video> elements screenshot black and every video comp would
5193+
// false-positive into the screenshot fallback.
5194+
if (session.onBeforeCapture) await session.onBeforeCapture(truthPage, t);
5195+
// The first screenshot forces a frame, which runs rAF-driven callbacks (a count-up lands
5196+
// a tick after seek()); waiting on rAF via evaluate deadlocks, headless fires it only
5197+
// when a frame is produced.
5198+
await pageScreenshotCapture(truthPage, session.options);
5199+
frames.set(idx, await pageScreenshotCapture(truthPage, session.options));
5200+
}
5201+
// The render's own page goes back to frame 0 so its first seek starts like an unverified render.
5202+
if (!isolated) await seekTo(quantizeTimeToFrame(0, fps));
5203+
} finally {
5204+
if (isolated) await truthPage.close().catch(() => {});
51825205
}
5183-
// Leave the page at frame 0 so the render's first seek starts from the
5184-
// same state as an unverified render.
5185-
await seekTo(quantizeTimeToFrame(0, fps));
51865206
session.deVerifyFrames = frames;
51875207
logInitPhase(
51885208
`drawElement self-verify armed: ${frames.size} ground-truth frame(s) @ [${[...frames.keys()].join(", ")}] of ${totalFrames}`,

‎packages/engine/src/services/videoFrameExtractor.concurrency.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ vi.mock("os", async (importOriginal) => ({
1515

1616
const { extractAllVideoFrames, extractVideoFramesRange } = await import("./videoFrameExtractor.js");
1717
const { extractMediaMetadata } = await import("../utils/ffprobe.js");
18+
const { isFfmpegFilterAvailable } = await import("../utils/psnrFilterAvailability.js");
1819

1920
describe.skipIf(process.platform === "win32")("extractAllVideoFrames ffmpeg concurrency", () => {
2021
const dir = mkdtempSync(join(tmpdir(), "hf-extract-concurrency-"));
@@ -124,6 +125,8 @@ describe.skipIf(process.platform === "win32")("extractAllVideoFrames ffmpeg conc
124125
);
125126
const run = join(dir, "tonemap");
126127
mkdirSync(join(run, "running"), { recursive: true });
128+
// macOS checks for zscale once per process; take that check before the shim counts runs.
129+
await isFfmpegFilterAvailable("zscale");
127130
process.env.HF_TEST_SHIM_DIR = run;
128131
process.env[FFMPEG_PATH_ENV] = shim;
129132
try {

‎packages/producer/src/renderRequest.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface RenderRequestOptions {
4545
gifLoop?: number;
4646
hlsSegmentSeconds?: number;
4747
workers?: number;
48+
maxRenderMs?: number;
4849
useGpu?: boolean;
4950
debug?: boolean;
5051
/** Segmented capture: reuse segments recorded in a matching manifest. */
@@ -172,6 +173,7 @@ function assertRequestOptionScalars(options: Record<string, unknown>): void {
172173
assertOptionalInteger(options, "gifLoop");
173174
assertOptionalInteger(options, "hlsSegmentSeconds", 1);
174175
assertOptionalInteger(options, "workers", 1);
176+
assertOptionalInteger(options, "maxRenderMs");
175177
assertOptionalInteger(options, "crf");
176178
for (const field of ["useGpu", "debug", "outputResolutionAspectAgnostic"] as const) {
177179
assertOptionalBoolean(options, field);
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Hono } from "hono";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
const createdConfigs = vi.hoisted(() => [] as Record<string, unknown>[]);
5+
vi.mock("./services/renderOrchestrator.js", async () => {
6+
const { RenderTooSlowError } = await import("./services/render/renderEtaGuard.js");
7+
class RenderCancelledError extends Error {}
8+
return {
9+
RenderCancelledError,
10+
createRenderJob: (config: Record<string, unknown>) => {
11+
createdConfigs.push(config);
12+
return {
13+
config,
14+
progress: 0,
15+
currentStage: "capture",
16+
framesRendered: 0,
17+
totalFrames: 0,
18+
warnings: [],
19+
};
20+
},
21+
executeRenderJob: async () => {
22+
throw new RenderTooSlowError("capture", {
23+
maxRenderMs: 120_000,
24+
elapsedMs: 70_000,
25+
framesCompleted: 127,
26+
totalFrames: 21_540,
27+
msPerFrame: 472,
28+
etaMs: 10_000_000,
29+
});
30+
},
31+
};
32+
});
33+
34+
import { createRenderHandlers } from "./server.js";
35+
36+
function post(path: string, extra: Record<string, unknown> = {}): Promise<Response> {
37+
const app = new Hono();
38+
const handlers = createRenderHandlers({
39+
getRequestId: () => "too-slow-test",
40+
maxConcurrentRenders: 1,
41+
});
42+
app.post("/v1/render", handlers.render);
43+
app.post("/v1/render-stream", handlers.renderStream);
44+
return app.request(path, {
45+
method: "POST",
46+
headers: { "content-type": "application/json" },
47+
body: JSON.stringify({ html: "<html><body></body></html>", maxRenderMs: 120_000, ...extra }),
48+
});
49+
}
50+
51+
describe("server render too slow", () => {
52+
it("forwards maxRenderMs into the render config", async () => {
53+
await post("/v1/render");
54+
expect(createdConfigs.at(-1)?.maxRenderMs).toBe(120_000);
55+
});
56+
57+
it("emits RENDER_TOO_SLOW as non-retryable in blocking JSON", async () => {
58+
const response = await post("/v1/render");
59+
const body = await response.json();
60+
expect(response.status).toBe(500);
61+
expect(body).toMatchObject({
62+
success: false,
63+
errorCode: "RENDER_TOO_SLOW",
64+
retryable: false,
65+
errorOwner: "user",
66+
errorMetadata: {
67+
stage: "capture",
68+
framesCompleted: 127,
69+
totalFrames: 21_540,
70+
msPerFrame: 472,
71+
maxRenderMs: 120_000,
72+
},
73+
});
74+
expect(body.error).toMatch(/^render too slow:/);
75+
});
76+
77+
it("emits the same event over SSE", async () => {
78+
const body = await (await post("/v1/render-stream")).text();
79+
expect(body).toContain('"errorCode":"RENDER_TOO_SLOW"');
80+
expect(body).toContain('"retryable":false');
81+
expect(body).toContain('"error":"render too slow:');
82+
});
83+
84+
it.each([-1, 1.5, "90000", null])("rejects maxRenderMs=%p with a 400", async (value) => {
85+
const response = await post("/v1/render", { maxRenderMs: value });
86+
expect(response.status).toBe(400);
87+
expect((await response.json()).error).toContain("maxRenderMs");
88+
});
89+
});

‎packages/producer/src/server.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ interface RenderInput {
8888
pageMotionBlur?: RenderConfig["pageMotionBlur"];
8989
outputDynamicRange?: "auto" | "hdr" | "sdr";
9090
workers?: number;
91+
maxRenderMs?: number;
9192
crf?: number;
9293
videoBitrate?: string;
9394
useGpu: boolean;
@@ -130,6 +131,7 @@ const SAFE_RENDER_ERROR_CODES = new Set<string>([
130131
"VIDEO_SOURCE_UNRENDERABLE",
131132
"VIDEO_EXTRACTION_FAILED",
132133
"ENCODER_INTERRUPTED",
134+
"RENDER_TOO_SLOW",
133135
]);
134136

135137
/**
@@ -224,6 +226,7 @@ export function parseRenderOptions(body: Record<string, unknown>): Omit<RenderIn
224226
const fps = parseServerFps(body.fps);
225227
const quality = parseServerQuality(body.quality);
226228
const workers = typeof body.workers === "number" ? body.workers : undefined;
229+
const maxRenderMs = typeof body.maxRenderMs === "number" ? body.maxRenderMs : undefined;
227230
const useGpu = body.gpu === true;
228231
const debug = body.debug === true;
229232
// Preserve the pre-structured-warning HTTP contract for callers that do
@@ -248,6 +251,7 @@ export function parseRenderOptions(body: Record<string, unknown>): Omit<RenderIn
248251
fps,
249252
quality,
250253
workers,
254+
maxRenderMs,
251255
crf,
252256
videoBitrate,
253257
useGpu,
@@ -309,6 +313,7 @@ function buildRenderJobConfig(input: RenderInput, outputPath: string, log: Produ
309313
quality: input.quality,
310314
format: input.format ?? "mp4",
311315
workers: input.workers,
316+
maxRenderMs: input.maxRenderMs,
312317
crf: input.crf,
313318
videoBitrate: input.videoBitrate,
314319
useGpu: input.useGpu,
@@ -352,6 +357,14 @@ function resolvePreparedRenderOutput(
352357
function validateRenderOverrides(body: Record<string, unknown>): string | undefined {
353358
const encoder = parseEncoderOptions(body, parseServerFormat(body.format) ?? "mp4");
354359
if (!encoder.ok) return encoder.error;
360+
if (
361+
body.maxRenderMs !== undefined &&
362+
(typeof body.maxRenderMs !== "number" ||
363+
!Number.isInteger(body.maxRenderMs) ||
364+
body.maxRenderMs < 0)
365+
) {
366+
return "maxRenderMs must be a non-negative integer number of milliseconds";
367+
}
355368
if (body.variables !== undefined && !isPlainObject(body.variables)) {
356369
return 'variables must be a JSON object keyed by variable id (e.g. {"title":"Hello"})';
357370
}

0 commit comments

Comments
 (0)