Skip to content

Write HTML. Render video. Built for agents.

feat(producer): sdr_segmented capture plan kind

Vance Ingallsd7398517bd01
diff --git a/packages/producer/src/services/render/capturePlan.test.ts b/packages/producer/src/services/render/capturePlan.test.ts
index b9e09cb787..8ec1b0beb3 100644
--- a/packages/producer/src/services/render/capturePlan.test.ts
+++ b/packages/producer/src/services/render/capturePlan.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import {
+ capturePathForPlanKind,
createCapturePlan,
drawElementVerificationFailure,
replanAfterFailure,
@@ -374,3 +375,63 @@ describe("CapturePlan", () => {
);
});
});
+
+describe("sdr_segmented capture plan", () => {
+ const segmented = {
+ workerCount: 1,
+ forceScreenshot: false,
+ forceParallelStream: false,
+ useStreamingEncode: true,
+ useLayeredComposite: false,
+ usePageSideCompositing: false,
+ hasHdrContent: false,
+ needsAlpha: false,
+ useSegmentedCapture: true,
+ };
+
+ it("selects segmented capture only for single-worker streaming-eligible renders", () => {
+ const plan = createCapturePlan(segmented);
+ expect(plan).toMatchObject({
+ kind: "sdr_segmented",
+ workerCount: 1,
+ forceParallelStream: false,
+ });
+ expect(Object.isFrozen(plan)).toBe(true);
+ expect(createCapturePlan({ ...segmented, workerCount: 3 }).kind).toBe("sdr_streaming");
+ });
+
+ it("does not segment when streaming is off or the route is layered", () => {
+ expect(createCapturePlan({ ...segmented, useStreamingEncode: false }).kind).toBe("sdr_disk");
+ expect(createCapturePlan({ ...segmented, useLayeredComposite: true }).kind).toBe("hdr_layered");
+ });
+
+ it("reports its own capture path", () => {
+ expect(capturePathForPlanKind("sdr_segmented")).toBe("segmented");
+ });
+
+ it("falls back from segmented to plain streaming when the encoder is unavailable", () => {
+ const initial = createCapturePlan(segmented);
+ const next = replanAfterFailure(initial, { kind: "streaming_unavailable" });
+ expect(next.kind).toBe("sdr_streaming");
+ // The input plan is never mutated.
+ expect(initial.kind).toBe("sdr_segmented");
+ });
+
+ it("keeps segmenting but drops to screenshot after a drawElement failure", () => {
+ const next = replanAfterFailure(createCapturePlan(segmented), {
+ kind: "draw_element_capture",
+ });
+ expect(next).toMatchObject({ kind: "sdr_segmented", forceScreenshot: true });
+ });
+
+ it("drops segmentation after a plain capture failure instead of throwing", () => {
+ // Until Phase 2c adds per-segment retry, the whole render retries; it must
+ // reach a plan rather than the "cannot apply" throw that guards the
+ // non-streaming kinds.
+ const next = replanAfterFailure(createCapturePlan(segmented), {
+ kind: "capture_failure",
+ memoryExhaustion: false,
+ });
+ expect(next).toMatchObject({ kind: "sdr_streaming", forceScreenshot: true });
+ });
+});
diff --git a/packages/producer/src/services/render/capturePlan.ts b/packages/producer/src/services/render/capturePlan.ts
index 909d2f72b2..f61c4226ca 100644
--- a/packages/producer/src/services/render/capturePlan.ts
+++ b/packages/producer/src/services/render/capturePlan.ts
@@ -51,7 +51,21 @@ export interface HdrLayeredCapturePlan extends CapturePlanBase {
readonly forceParallelStream: false;
}
-export type CapturePlan = SdrStreamingCapturePlan | SdrDiskCapturePlan | HdrLayeredCapturePlan;
+/**
+ * Long-form capture (spec §5 Phase 2): the frame range is split into
+ * closed-GOP segments, each streamed into its own encoder and concat-copied,
+ * so scratch is encoded video and a crash costs one segment.
+ */
+export interface SdrSegmentedCapturePlan extends CapturePlanBase {
+ readonly kind: "sdr_segmented";
+ readonly forceParallelStream: false;
+}
+
+export type CapturePlan =
+ | SdrStreamingCapturePlan
+ | SdrDiskCapturePlan
+ | HdrLayeredCapturePlan
+ | SdrSegmentedCapturePlan;
/**
* Telemetry name for the stage a plan runs on. Exhaustive over `CapturePlan`,
@@ -66,6 +80,8 @@ export function capturePathForPlanKind(kind: CapturePlan["kind"]): CapturePath {
return "disk";
case "hdr_layered":
return "hdr_layered";
+ case "sdr_segmented":
+ return "segmented";
}
}
@@ -78,6 +94,8 @@ export interface CreateCapturePlanInput {
usePageSideCompositing: boolean;
hasHdrContent: boolean;
needsAlpha: boolean;
+ /** Opt in to segmented capture; honoured only for single-worker streaming renders. */
+ useSegmentedCapture?: boolean;
routing?: CaptureRouting;
}
@@ -178,6 +196,9 @@ export function createCapturePlan(input: CreateCapturePlanInput): CapturePlan {
forceParallelStream: false,
});
}
+ if (input.useStreamingEncode && input.useSegmentedCapture === true && input.workerCount === 1) {
+ return Object.freeze({ ...base, kind: "sdr_segmented", forceParallelStream: false });
+ }
if (input.useStreamingEncode) {
return Object.freeze({ ...base, kind: "sdr_streaming" });
}
@@ -190,7 +211,32 @@ function revertedRouting(routing: CaptureRouting): CaptureRouting {
}
/** Pure, exhaustive capture fallback transition. The input plan is never mutated. */
+/**
+ * Segmented capture keeps segmenting through a drawElement failure (only the
+ * capture engine changes) but drops to a plain streaming render for anything
+ * else: no encoder means no per-segment encoder either, and until Phase 2c
+ * adds per-segment retry a capture failure has to retry the whole render.
+ */
+function replanSegmentedAfterFailure(
+ plan: SdrSegmentedCapturePlan,
+ failure: CapturePlanFailure,
+): CapturePlan {
+ const keepSegmenting =
+ failure.kind === "draw_element_verification" || failure.kind === "draw_element_capture";
+ return createCapturePlan({
+ ...plan,
+ forceScreenshot: keepSegmenting || failure.kind === "capture_failure",
+ useStreamingEncode: true,
+ useLayeredComposite: false,
+ useSegmentedCapture: keepSegmenting,
+ forceParallelStream: false,
+ routing: revertedRouting(plan.routing),
+ });
+}
+
export function replanAfterFailure(plan: CapturePlan, failure: CapturePlanFailure): CapturePlan {
+ // Before the sdr_streaming guard below, which would otherwise throw.
+ if (plan.kind === "sdr_segmented") return replanSegmentedAfterFailure(plan, failure);
// Disk-path drawElement self-verification (parallel disk workers under the
// explicit fast-capture opt-in) can also trip — the retry stays on the disk
// path but forces the screenshot baseline.
diff --git a/packages/producer/src/services/render/motionBlurRoute.ts b/packages/producer/src/services/render/motionBlurRoute.ts
index 6d5eb95276..9886019548 100644
--- a/packages/producer/src/services/render/motionBlurRoute.ts
+++ b/packages/producer/src/services/render/motionBlurRoute.ts
@@ -24,6 +24,10 @@ import type { CapturePlan } from "./capturePlan.js";
const UNSUPPORTED_REASON: Record<CapturePlan["kind"], string | null> = {
sdr_streaming: null,
sdr_disk: null,
+ // Segmented capture runs the same per-frame capture loop as sdr_streaming;
+ // only the encoder lifetime and the frame range differ, and neither is
+ // visible to sub-frame accumulation.
+ sdr_segmented: null,
// Named for HDR but reached by shader transitions with no HDR content at all
// (`shouldUseLayeredComposite`), so the reason must not blame HDR for a composition
// that has none.