Skip to content

Write HTML. Render video. Built for agents.

fix(capture): bound static dedup verification time (#2457)

Miguel Ángeld047d28bb402
diff --git a/packages/engine/src/services/frameCapture-staticDedupVerifyDensity.test.ts b/packages/engine/src/services/frameCapture-staticDedupVerifyDensity.test.ts
index 43824272f1..3035654ad0 100644
--- a/packages/engine/src/services/frameCapture-staticDedupVerifyDensity.test.ts
+++ b/packages/engine/src/services/frameCapture-staticDedupVerifyDensity.test.ts
@@ -183,4 +183,35 @@ describe("verifyStaticFramesSafe catches drift the old fixed-point density would
expect(seekCalls.map((call) => Math.round(call.t * fps))).toEqual([0, 1, 2, 0]);
expect(seekCalls.every((call) => call.options?.suppressEvents === true)).toBe(true);
});
+
+ it("disarms within a wall-clock budget instead of spending minutes verifying a long static run", async () => {
+ const fps = 24;
+ let nowMs = 0;
+ const nowSpy = vi.spyOn(Date, "now").mockImplementation(() => nowMs);
+ const page = {
+ evaluate: vi.fn(async () => undefined),
+ };
+ vi.mocked(pageScreenshotCapture).mockImplementation(async () => {
+ nowMs += 8_000;
+ return Buffer.from("same");
+ });
+
+ // Mirrors the reported 350.35s / 23.976fps alpha composition closely:
+ // ~8,400 predicted-static frames used to schedule ~352 full-page PNG
+ // screenshots before capture could begin.
+ const staticFrames = new Set<number>();
+ for (let frame = 1; frame < 8_400; frame++) staticFrames.add(frame);
+
+ const result = await verifyStaticFramesSafe(
+ { options: {} } as unknown as CaptureSession,
+ page as unknown as Parameters<typeof verifyStaticFramesSafe>[1],
+ staticFrames,
+ fps,
+ 24,
+ );
+ nowSpy.mockRestore();
+
+ expect(result?.budgetExhausted).toBe(true);
+ expect(pageScreenshotCapture).toHaveBeenCalledTimes(2);
+ });
});
diff --git a/packages/engine/src/services/frameCapture.ts b/packages/engine/src/services/frameCapture.ts
index a443e8abed..56cff28fc4 100644
--- a/packages/engine/src/services/frameCapture.ts
+++ b/packages/engine/src/services/frameCapture.ts
@@ -2359,6 +2359,13 @@ export async function computeStaticFrameSet(
// sampleCount, so that knob's effect on density stays monotonic (see below).
const STATIC_VERIFY_REFERENCE_STRIDE = 24;
+// Verification uses full-page screenshots, whose cost scales with canvas size
+// and page complexity rather than frame count. Bound wall time as well as the
+// capture count so a long composition cannot spend minutes proving an
+// optimization before the real render starts. Exhaustion fails closed: dedup is
+// disabled and normal capture proceeds.
+const STATIC_VERIFY_MAX_MS = 15_000;
+
/**
* Interior verification points for a run [a..b], plus the always-included end `b`.
* Density used to be a flat point-count cap (min(sampleCount, 8)), so a run's
@@ -2414,6 +2421,7 @@ export async function verifyStaticFramesSafe(
): Promise<{ badFrame: number; budgetExhausted: boolean } | null> {
const frames = [...staticFrames].sort((a, b) => a - b);
if (frames.length === 0) return null;
+ const deadline = Date.now() + STATIC_VERIFY_MAX_MS;
// Runs are maximal-contiguous (adjacent frames merge), so a run's anchor a-1 is
// guaranteed NOT static — always a freshly-captured frame.
const runs: Array<{ a: number; b: number }> = [];
@@ -2460,9 +2468,11 @@ export async function verifyStaticFramesSafe(
for (const { a, b } of runs) {
const anchor = a - 1;
if (anchor < 0) continue;
+ if (Date.now() >= deadline) return { badFrame: a, budgetExhausted: true };
const anchorBuf = await seekCapture(anchor);
spent++;
for (const f of computeStaticVerificationPoints(a, b, sampleCount)) {
+ if (Date.now() >= deadline) return { badFrame: f, budgetExhausted: true };
const cur = await seekCapture(f);
spent++;
if (!anchorBuf.equals(cur)) return { badFrame: f, budgetExhausted: false };