Skip to content

Write HTML. Render video. Built for agents.

feat(producer): target-loss classifier and browser recycle cadence

Vance Ingalls61223223d40f
diff --git a/packages/producer/src/services/render/segmentRecycle.test.ts b/packages/producer/src/services/render/segmentRecycle.test.ts
new file mode 100644
index 0000000000..970f95d43b
--- /dev/null
+++ b/packages/producer/src/services/render/segmentRecycle.test.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+import {
+ DEFAULT_SEGMENT_BROWSER_RECYCLE,
+ isTargetLossError,
+ resolveSegmentBrowserRecycle,
+} from "./segmentRecycle.js";
+
+describe("isTargetLossError", () => {
+ it("matches the Chrome target-loss signatures seen in the field", () => {
+ for (const msg of [
+ "Protocol error (Page.captureScreenshot): Target closed",
+ "Protocol error (Runtime.callFunctionOn): Target closed",
+ "Attempted to use detached Frame 'B2CD611B9F8EF71674BEF3ABB0A9E0FD'.",
+ "Protocol error: Session closed. Most likely the page has been closed.",
+ "Target.closeTarget: Target closed",
+ ]) {
+ expect(isTargetLossError(new Error(msg))).toBe(true);
+ }
+ });
+
+ it("does not match authoring or encoder errors", () => {
+ // These reproduce on a fresh browser, so a retry only doubles the time
+ // spent reaching the same failure.
+ expect(isTargetLossError(new Error("Chunk concat failed: Invalid data"))).toBe(false);
+ expect(isTargetLossError(new Error("media_start_out_of_range"))).toBe(false);
+ expect(isTargetLossError(new Error("Segment 2 encode failed: broken pipe"))).toBe(false);
+ expect(isTargetLossError(new Error("Sequential screenshot capture stalled"))).toBe(false);
+ expect(isTargetLossError("Target closed")).toBe(false); // must be an Error
+ expect(isTargetLossError(null)).toBe(false);
+ expect(isTargetLossError(undefined)).toBe(false);
+ });
+});
+
+describe("resolveSegmentBrowserRecycle", () => {
+ it("defaults to 3 and accepts 0 as never", () => {
+ expect(resolveSegmentBrowserRecycle({})).toBe(DEFAULT_SEGMENT_BROWSER_RECYCLE);
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: "0" })).toBe(0);
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: "1" })).toBe(1);
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: "-2" })).toBe(
+ DEFAULT_SEGMENT_BROWSER_RECYCLE,
+ );
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: "x" })).toBe(
+ DEFAULT_SEGMENT_BROWSER_RECYCLE,
+ );
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: "1.5" })).toBe(
+ DEFAULT_SEGMENT_BROWSER_RECYCLE,
+ );
+ expect(resolveSegmentBrowserRecycle({ HF_SEGMENT_BROWSER_RECYCLE: " " })).toBe(
+ DEFAULT_SEGMENT_BROWSER_RECYCLE,
+ );
+ });
+});
diff --git a/packages/producer/src/services/render/segmentRecycle.ts b/packages/producer/src/services/render/segmentRecycle.ts
new file mode 100644
index 0000000000..e03ee81aba
--- /dev/null
+++ b/packages/producer/src/services/render/segmentRecycle.ts
@@ -0,0 +1,32 @@
+/**
+ * Browser recycling and retry classification for segmented capture
+ * (spec §5 Phase 2 item 4).
+ *
+ * A long render's failures cluster in one class: Chrome loses the target
+ * mid-capture after tens of thousands of frames. Segmentation makes that
+ * survivable — the segment boundary is a safe point to hand the next segment
+ * a fresh browser, and a lost target costs one segment's retry rather than
+ * the render.
+ */
+
+/** Recycle the browser every N segments; 0 disables. ~3 x 3000 frames keeps a session under the 10k-frame band (spec §2.5). */
+export const DEFAULT_SEGMENT_BROWSER_RECYCLE = 3;
+
+export function resolveSegmentBrowserRecycle(env: NodeJS.ProcessEnv): number {
+ const raw = env.HF_SEGMENT_BROWSER_RECYCLE;
+ if (raw === undefined || raw.trim() === "") return DEFAULT_SEGMENT_BROWSER_RECYCLE;
+ const parsed = Number(raw);
+ return Number.isInteger(parsed) && parsed >= 0 ? parsed : DEFAULT_SEGMENT_BROWSER_RECYCLE;
+}
+
+const TARGET_LOSS_PATTERNS = [/target closed/i, /detached frame/i, /session closed/i];
+
+/**
+ * The mid-capture Chrome target-loss class from spec §2.5 — the only class a
+ * segment retry can fix. Encoder failures and authoring errors reproduce on a
+ * fresh browser, so retrying them just doubles the time to the same failure.
+ */
+export function isTargetLossError(error: unknown): boolean {
+ if (!(error instanceof Error)) return false;
+ return TARGET_LOSS_PATTERNS.some((re) => re.test(error.message));
+}