Skip to content

Write HTML. Render video. Built for agents.

fix(producer): concat directly on a full segmented resume and document the retry surface

Vance Ingalls7b65cb98e7c5
diff --git a/packages/producer/src/services/render/stages/captureSegmentedStage.test.ts b/packages/producer/src/services/render/stages/captureSegmentedStage.test.ts
index 8750330b40..5bd8bd16b4 100644
--- a/packages/producer/src/services/render/stages/captureSegmentedStage.test.ts
+++ b/packages/producer/src/services/render/stages/captureSegmentedStage.test.ts
@@ -355,6 +355,32 @@ describe("runCaptureSegmentedStage", () => {
);
});
+ it("concats without opening a browser when every segment is already complete", async () => {
+ const create = mock(async () => fakeSession(99));
+ const closeSession = mock(async () => {});
+ const concat = mock(async () => ({ success: true as const }));
+ const input = fakeStageInput({ totalFrames: 3 });
+ const stableDir = join(fixtureRoot, "stable-all");
+ const result = await runCaptureSegmentedStage({
+ ...input,
+ segmentFrames: 1,
+ segmentDir: stableDir,
+ completedSegments: new Set([0, 1, 2]),
+ sessionFactory: { create },
+ deps: { concat, closeSession },
+ });
+ expect(result.success).toBe(true);
+ expect(create).not.toHaveBeenCalled();
+ // The probe handed in by the orchestrator is still closed on this path.
+ expect(closeSession).toHaveBeenCalledWith(input.probeSession);
+ expect(concat).toHaveBeenCalledWith(
+ [0, 1, 2].map((i) => segmentOutputPath(stableDir, i)),
+ join(fixtureRoot, "video-only.mp4"),
+ undefined,
+ expect.anything(),
+ );
+ });
+
it("can fall back when the first PENDING segment fails to spawn on a resume", async () => {
// Segment 0 is already done, so segment 1 is the first one this run has to
// capture; nothing new is on disk yet, so the plain streaming path is
diff --git a/packages/producer/src/services/render/stages/captureSegmentedStage.ts b/packages/producer/src/services/render/stages/captureSegmentedStage.ts
index 83b556e77e..9b9700e10e 100644
--- a/packages/producer/src/services/render/stages/captureSegmentedStage.ts
+++ b/packages/producer/src/services/render/stages/captureSegmentedStage.ts
@@ -467,6 +467,9 @@ async function retryAfterTargetLoss(
{ worker: worker.id, error: err instanceof Error ? err.message : String(err) },
);
run.deps.removeFile(segmentPath);
+ // Side effect worth knowing: recycling resets the worker's sessionSegments,
+ // so a retry also restarts the cadence clock. Intended: the fresh browser
+ // is as new as a cadence restart would have made it.
await recycleWorkerSession(run, worker, "retry");
run.totals.encodeMs += await captureSegment(worker.ctx, segment);
}
@@ -494,6 +497,38 @@ async function runOneSegment(
return "done";
}
+/**
+ * A full resume hit: nothing to capture, so no browser round-trip. The probe
+ * the orchestrator handed in is still ours to close, as the worker finally
+ * would have done.
+ */
+async function concatResumedRun(
+ run: SegmentRun,
+ segmentPaths: string[],
+): Promise<CaptureSegmentedStageResult> {
+ const { input, deps } = run;
+ input.log.info("[Render] every segment already complete (resume); concatenating without capture");
+ if (input.probeSession) await deps.closeSession(input.probeSession);
+ await concatSegments(
+ deps.concat,
+ segmentPaths,
+ input.videoOnlyPath,
+ input.abortSignal,
+ input.cfg,
+ );
+ return {
+ success: true,
+ encodeMs: 0,
+ probeSession: null,
+ lastBrowserConsole: [],
+ workerCount: run.workerCount,
+ segments: run.segments.length,
+ segmentPaths,
+ segmentRetries: 0,
+ browserRecycles: 0,
+ };
+}
+
export async function runCaptureSegmentedStage(
input: CaptureSegmentedStageInput,
): Promise<CaptureSegmentedStageResult> {
@@ -505,11 +540,7 @@ export async function runCaptureSegmentedStage(
mkdirSync(segmentDir, { recursive: true });
const skip = input.completedSegments ?? new Set<number>();
const pending = segments.filter((s) => !skip.has(s.index));
- for (const segment of segments) {
- if (skip.has(segment.index)) {
- log.info("[Render] segment skipped (resume)", { index: segment.index });
- }
- }
+ for (const index of skip) log.info("[Render] segment skipped (resume)", { index });
const run: SegmentRun = {
input,
deps,
@@ -525,6 +556,7 @@ export async function runCaptureSegmentedStage(
// Built from the plan, not from completion order: workers finish out of
// order, so the concat list must not depend on who finished when.
const segmentPaths = segments.map((s) => segmentOutputPath(segmentDir, s.index));
+ if (pending.length === 0) return concatResumedRun(run, segmentPaths);
let fellBackToStreaming = false;
const workers: SegmentWorker[] = [];
diff --git a/packages/producer/tests/long-form/README.md b/packages/producer/tests/long-form/README.md
index 3e881be295..ec7a2f1069 100644
--- a/packages/producer/tests/long-form/README.md
+++ b/packages/producer/tests/long-form/README.md
@@ -132,6 +132,26 @@ streaming (both drawElement capture). Segmentation buys bounded scratch,
resumability and blast radius, not speed; threading the worker-encode loop
through the segmented stage is the follow-up that would close the gap.
+### Known cost: a narrower retry surface than plain streaming
+
+Segmented capture retries one thing: a Chrome target loss, once, on a fresh
+session (Phase 2c). It has no orchestrator-level fallback. A drawElement
+self-verify miss, a sequential stall, an encoder failure on a segment after the
+first, or a concat failure ends the render, where the plain streaming path would
+have re-run it on a fresh screenshot session. Opting a specific composition into
+`HF_SEGMENTED_CAPTURE=true` therefore trades some resilience for the bounded
+scratch and resume. Wiring the streaming path's failure classification into the
+segmented branch is the other follow-up before the R4 flip.
+
+### Operational note: `--resume` state lives in the project directory
+
+Segments are written to `<project>/renders/.hf-segments/<planHash>/` rather than
+the ephemeral work directory, and they are deliberately left in place after a
+failed render because that is what `--resume` reads. A project that iterates on
+a long render grows by one segment directory per distinct plan hash until a run
+succeeds without `--keep-segments`, which removes its own hash directory. Stale
+hash directories from earlier settings are not swept.
+
## PRINFRA-694 gate for the R3 flip
R3 turns the interleaved parallel-stream router on for screenshot capture, so