Skip to content

Commit e027220

Browse files
fix(engine): isolate static dedup verification seeks (#3768)
Co-authored-by: heygengenesis[bot] <262951085+heygengenesis[bot]@users.noreply.github.com> Co-authored-by: miguel.sierra <[email protected]>
1 parent e5d89f7 commit e027220

8 files changed

Lines changed: 197 additions & 1 deletion

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { createStaticVerificationPage, type CaptureSession } from "./frameCapture.js";
3+
4+
describe("createStaticVerificationPage", () => {
5+
it("uses a fresh, ready page so verification seeks cannot affect capture", async () => {
6+
const verificationPage = {
7+
evaluateOnNewDocument: vi.fn().mockResolvedValue(undefined),
8+
setViewport: vi.fn().mockResolvedValue(undefined),
9+
goto: vi.fn().mockResolvedValue(undefined),
10+
evaluate: vi.fn().mockResolvedValue(true),
11+
close: vi.fn().mockResolvedValue(undefined),
12+
};
13+
const session = {
14+
browser: { newPage: vi.fn().mockResolvedValue(verificationPage) },
15+
page: {},
16+
serverUrl: "http://127.0.0.1:3000",
17+
options: {
18+
width: 1920,
19+
height: 1080,
20+
deviceScaleFactor: 1,
21+
format: "mp4",
22+
skipReadinessVideoIds: [],
23+
},
24+
} as unknown as CaptureSession;
25+
26+
await expect(createStaticVerificationPage(session)).resolves.toBe(verificationPage);
27+
28+
expect(session.browser.newPage).toHaveBeenCalledOnce();
29+
expect(verificationPage.goto).toHaveBeenCalledWith("http://127.0.0.1:3000/index.html", {
30+
waitUntil: "domcontentloaded",
31+
timeout: expect.any(Number),
32+
});
33+
expect(verificationPage.close).not.toHaveBeenCalled();
34+
});
35+
36+
it("closes a failed verification page before static dedup fails closed", async () => {
37+
const verificationPage = {
38+
evaluateOnNewDocument: vi.fn().mockResolvedValue(undefined),
39+
setViewport: vi.fn().mockRejectedValue(new Error("viewport failed")),
40+
close: vi.fn().mockResolvedValue(undefined),
41+
};
42+
const session = {
43+
browser: { newPage: vi.fn().mockResolvedValue(verificationPage) },
44+
serverUrl: "http://127.0.0.1:3000",
45+
options: { width: 1920, height: 1080, format: "mp4" },
46+
} as unknown as CaptureSession;
47+
48+
await expect(createStaticVerificationPage(session)).rejects.toThrow("viewport failed");
49+
expect(verificationPage.close).toHaveBeenCalledOnce();
50+
});
51+
});

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,62 @@ interface StaticVerificationDependencies {
30453045
capture?: typeof pageScreenshotCapture;
30463046
}
30473047

3048+
/**
3049+
* Prepare an isolated page for static-frame verification. Verification seeks
3050+
* intentionally visit frames out of capture order, so they must never mutate
3051+
* the page that will later be captured sequentially.
3052+
*/
3053+
export async function createStaticVerificationPage(session: CaptureSession): Promise<Page> {
3054+
const page = await session.browser.newPage();
3055+
const pageNavigationTimeout =
3056+
session.config?.pageNavigationTimeout ?? DEFAULT_CONFIG.pageNavigationTimeout;
3057+
const pageReadyTimeout = session.config?.playerReadyTimeout ?? DEFAULT_CONFIG.playerReadyTimeout;
3058+
3059+
try {
3060+
await page.evaluateOnNewDocument(() => {
3061+
const w = window as unknown as { __name?: <T>(fn: T, _name: string) => T };
3062+
if (typeof w.__name !== "function") {
3063+
w.__name = <T>(fn: T, _name: string): T => fn;
3064+
}
3065+
});
3066+
if (session.options.variables && Object.keys(session.options.variables).length > 0) {
3067+
const variablesJson = JSON.stringify(session.options.variables);
3068+
await page.evaluateOnNewDocument((json: string) => {
3069+
(window as Window & { __hfVariables?: Record<string, unknown> }).__hfVariables =
3070+
JSON.parse(json);
3071+
}, variablesJson);
3072+
}
3073+
await page.setViewport({
3074+
width: session.options.width,
3075+
height: session.options.height,
3076+
deviceScaleFactor: session.options.deviceScaleFactor || 1,
3077+
});
3078+
await page.goto(`${session.serverUrl}/index.html`, {
3079+
waitUntil: "domcontentloaded",
3080+
timeout: pageNavigationTimeout,
3081+
});
3082+
await page.evaluate(`window.__hfFlushSync?.()`);
3083+
await pollHfReady(page, pageReadyTimeout);
3084+
await pollSubCompositionTimelines(page, pageReadyTimeout);
3085+
await applyVideoMetadataHints(page, session.options.videoMetadataHints);
3086+
3087+
const skipVideoIds = session.options.skipReadinessVideoIds ?? [];
3088+
await Promise.all([
3089+
pollVideosReady(page, skipVideoIds, pageReadyTimeout),
3090+
pollImagesReady(page, pageReadyTimeout).then((ready) =>
3091+
ready ? decodeAllImages(page) : undefined,
3092+
),
3093+
page.evaluate(`document.fonts?.ready`),
3094+
waitForOptionalTailwindReady(page, pageReadyTimeout),
3095+
]);
3096+
if (session.options.format === "png") await initTransparentBackground(page);
3097+
return page;
3098+
} catch (error) {
3099+
await page.close().catch(() => {});
3100+
throw error;
3101+
}
3102+
}
3103+
30483104
/**
30493105
* Empirically verify the predicted-static set before trusting it. Group static frames
30503106
* into runs; each run [a..b] reuses anchor a-1. CRITICAL: compare against the ANCHOR,
@@ -3236,7 +3292,24 @@ async function armStaticDedup(
32363292
);
32373293
return;
32383294
}
3239-
const verdict = await verifyStaticFramesSafe(session, page, stats.staticFrameSet, fps, samples);
3295+
let verificationPage: Page | undefined;
3296+
let verdict: StaticVerificationResult;
3297+
try {
3298+
verificationPage = await createStaticVerificationPage(session);
3299+
verdict = await verifyStaticFramesSafe(
3300+
session,
3301+
verificationPage,
3302+
stats.staticFrameSet,
3303+
fps,
3304+
samples,
3305+
);
3306+
} catch {
3307+
session.staticDedupSkipReason = "verification_failed";
3308+
logInitPhase("static-frame dedup: disabled (verification infrastructure failure)");
3309+
return;
3310+
} finally {
3311+
await verificationPage?.close().catch(() => {});
3312+
}
32403313
session.staticDedupVerification = verdict;
32413314
if (verdict.outcome === "mismatch" || verdict.outcome === "infrastructure") {
32423315
session.staticDedupSkipReason = "verification_failed";

‎packages/producer/tests/shard-schedule.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"raf-ball-render-compat": 22,
3636
"render-symlinked-assets": 6,
3737
"static-volume-future-set": 120,
38+
"static-dedup-nested-visibility": 8,
3839
"style-1-prod": 156,
3940
"style-10-prod": 232,
4041
"style-11-prod": 324,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Static dedup nested visibility",
3+
"description": "Regression for #3767: static-frame verification must not leave a mounted sub-composition hidden after seeking backward across its own visibility kill.",
4+
"tags": ["regression", "static-dedup", "sub-composition", "visibility"],
5+
"minPsnr": 30,
6+
"maxFrameFailures": 0,
7+
"minAudioCorrelation": 0,
8+
"maxAudioLagWindows": 1,
9+
"renderConfig": {
10+
"fps": 30,
11+
"workers": 1
12+
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:ca4fdf520c4961eeaee542b9ea0f7eed4b2e3f3b8ab54b0860ede1c8b15dad5c
3+
size 89000
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:a75f90109f9387a9850e5443db1c3562a4713fd57513a8dc86b97ad0b1fc83c5
3+
size 21880
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<template>
6+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
7+
<style>
8+
#sub-a-root { position: relative; width: 640px; height: 360px; overflow: hidden; }
9+
#sub-a-square { position: absolute; top: 80px; left: 220px; width: 200px; height: 200px; background: #fff; opacity: 0; }
10+
#sub-a-clip { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
11+
</style>
12+
<div id="sub-a-root" data-composition-id="sub-a" data-start="0" data-duration="3" data-width="640" data-height="360">
13+
<div id="sub-a-clip" class="clip" data-start="0" data-duration="3"></div>
14+
<div id="sub-a-square"></div>
15+
</div>
16+
<script>
17+
window.__timelines = window.__timelines || {};
18+
const tl = gsap.timeline({ paused: true });
19+
tl.to("#sub-a-square", { autoAlpha: 1, duration: 0.6, ease: "none" }, 0.2);
20+
tl.set("#sub-a-root", { visibility: "hidden" }, 2.98);
21+
window.__timelines["sub-a"] = tl;
22+
</script>
23+
</template>
24+
</head>
25+
<body></body>
26+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=640, height=360" />
6+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
7+
<style>
8+
* { box-sizing: border-box; }
9+
html, body { width: 640px; height: 360px; margin: 0; overflow: hidden; background: #101010; }
10+
#root { position: relative; width: 640px; height: 360px; overflow: hidden; background: #101010; }
11+
#dot { position: absolute; top: 32px; left: 32px; width: 24px; height: 24px; border-radius: 50%; background: #ff4040; }
12+
</style>
13+
</head>
14+
<body>
15+
<div id="root" data-composition-id="main" data-start="0" data-duration="10" data-width="640" data-height="360">
16+
<div id="dot"></div>
17+
<div id="sub-a-host" data-composition-id="sub-a" data-composition-src="compositions/sub-a.html" data-start="5.5" data-duration="3" data-track-index="2" data-width="640" data-height="360"></div>
18+
</div>
19+
<script>
20+
window.__timelines = window.__timelines || {};
21+
const tl = gsap.timeline({ paused: true });
22+
tl.to("#dot", { x: 100, duration: 1, ease: "none" }, 0);
23+
window.__timelines.main = tl;
24+
</script>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)