Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.
TypeScript agentic-aiaiai-agentsai-videocompilerdslffmpeggenerative-aillmmarkup-languagemonorepoplugin-systemprogramming-languagetext-to-videotypescriptvideovideo-automationvideo-clonevideo-editingvideo-generation
fix: let successful render workers exit naturally
kashorin7373835663bf
diff --git a/packages/provider-hyperframes-local/README.md b/packages/provider-hyperframes-local/README.mdindex 595b742f..128dfbca 100644--- a/packages/provider-hyperframes-local/README.md+++ b/packages/provider-hyperframes-local/README.md@@ -96,11 +96,20 @@ execution earlier. ResourceStore I/O and Surface probes receive the cancellationResourceStore must implement the port's cancellation behavior, including streaming reads and writes.Browser launch, source extraction, capture and encoding run in one disposable child process per-render. At cancellation it receives a stop request and has up to five seconds to clean up. The owner-then terminates any remaining process tree, including Chrome's separate process groups, and awaits-the child exit before removing temporary files and returning failure. This also covers engine calls-that do not accept a signal. The deadline initiates shutdown; the call may spend additional time-closing resources. Completed Outputs in the Build remain available for a new Run and Build.+render. After successful capture closes its resources, the child sends its completion message,+flushes that message and disconnects IPC so it can exit normally. The owner awaits exit and drains+diagnostics before returning. Normal completion does not enumerate or forcibly terminate processes.+On failure, cleanup may be incomplete: the child reports the error and keeps IPC open while the owner+discovers and terminates the remaining process tree, before it can become orphaned.++At cancellation the child receives a stop request and has up to five seconds to clean up. A child+that remains after cancellation or its completion message is forcibly terminated along with its discovered+process tree, including Chrome's separate process groups. Cleanup problems are reported through the+existing diagnostic callback; they do not discard a render already reported as completed. If process+enumeration fails, the owner still terminates the direct child but cannot confirm descendant cleanup.+This also covers engine calls that do not accept a signal. The deadline initiates shutdown; the call+may spend additional time closing resources. Completed Outputs in the Build remain available for a+new Run and Build.Deployments may additionally set `initializationTimeoutMs` or `frameTimeoutMs` when they have ameasured stage deadline. Initialization here means initializing an already created browser session;diff --git a/packages/provider-hyperframes-local/src/capture-process.ts b/packages/provider-hyperframes-local/src/capture-process.tsindex a2720d5c..e7d6f3b6 100644--- a/packages/provider-hyperframes-local/src/capture-process.ts+++ b/packages/provider-hyperframes-local/src/capture-process.ts@@ -75,6 +75,7 @@ export async function runCaptureProcess(});let failure: Error | undefined;let completed = false;+ let closed = false;let outputBytes = 0;let stderr = "";let diagnostics = Promise.resolve();@@ -84,14 +85,27 @@ export async function runCaptureProcess(if (grace !== undefined) clearTimeout(grace);killing ??= (child.pid === undefined ? Promise.resolve() : killRenderTree(child.pid)).catch((error) => {if (!completed) failure = new Error(`${failure?.message ?? "Render cleanup failed"}; ${String(error)}`);+ diagnostic({ level: "warning", message: `Render process-tree cleanup could not be confirmed: ${String(error)}` });child.kill("SIGKILL");});return killing;};const stop = (error: Error) => {failure ??= error;+ if (closed) return;if (child.connected) child.send({ type: "abort", error: failure.message }, () => {});- grace ??= setTimeout(() => { void kill(); }, cleanupMs);+ awaitExit();+ };+ const diagnostic = (value: ExecutionDiagnostic) => {+ if (onDiagnostic === undefined) return;+ diagnostics = diagnostics.then(() => onDiagnostic(value))+ .catch((error) => stop(error instanceof Error ? error : new Error(String(error))));+ };+ const awaitExit = () => {+ grace ??= setTimeout(() => {+ diagnostic({ level: "warning", message: `Render process did not exit within ${cleanupMs} ms; terminating its remaining process tree` });+ void kill();+ }, cleanupMs);};const abort = () => stop(signal.reason instanceof Error ? signal.reason : new Error(String(signal.reason)));signal.addEventListener("abort", abort, { once: true });@@ -104,10 +118,7 @@ export async function runCaptureProcess(pipe?.setEncoding("utf8");pipe?.on("data", (text: string) => {log(Buffer.from(text), stream === "stderr");- if (onDiagnostic !== undefined && text.trim()) {- diagnostics = diagnostics.then(() => onDiagnostic({ stream, level: "info", message: text.trimEnd() }))- .catch((error) => stop(error instanceof Error ? error : new Error(String(error))));- }+ if (text.trim()) diagnostic({ stream, level: "info", message: text.trimEnd() });});}child.on("message", (value: { type: string; event?: HyperframesRenderProgress; error?: string }) => {@@ -117,13 +128,20 @@ export async function runCaptureProcess(stop(new Error(value.error));} else if (value.type === "completed" || value.type === "failed") {completed = value.type === "completed";- if (!completed) failure ??= new Error(value.error);- // The worker has finished cleanup. Terminate any leftover descendants before releasing capacity.- void kill();+ if (completed) {+ // Successful capture has closed its resources and will disconnect after this message.+ awaitExit();+ } else {+ failure ??= new Error(value.error);+ // Resource cleanup may itself have failed. Keep the worker alive until+ // its remaining descendants have been discovered and terminated.+ void kill();+ }}});child.on("error", (error) => { failure ??= error; void kill(); });child.on("close", () => {+ closed = true;if (grace !== undefined) clearTimeout(grace);signal.removeEventListener("abort", abort);void (killing ?? Promise.resolve()).then(async () => {diff --git a/packages/provider-hyperframes-local/src/capture-worker.ts b/packages/provider-hyperframes-local/src/capture-worker.tsindex 12e230d3..23c467e7 100644--- a/packages/provider-hyperframes-local/src/capture-worker.ts+++ b/packages/provider-hyperframes-local/src/capture-worker.ts@@ -3,15 +3,34 @@ import type { CaptureInput } from "./capture.js";const controller = new AbortController();const message = (error: unknown) => error instanceof Error ? error.message : String(error);+const disconnected = () => controller.abort(new Error("Render owner disconnected"));+const report = (value: object) => {+ if (process.connected) process.send?.(value, undefined, undefined, (error) => { if (error) controller.abort(error); });+};+const finish = (value: { type: "completed" } | { type: "failed"; error: string }) => {+ // Failure can leave descendants behind. Keep the owner connected until it has+ // discovered and stopped that tree, rather than orphaning it by exiting first.+ if (value.type === "failed") { report(value); return; }+ // Successful capture has closed its resources. Flush the result before closing our IPC handle;+ // our own disconnect is not a cancellation by the owner.+ process.off("disconnect", disconnected);+ process.off("message", receive);+ if (!process.connected) return;+ process.send?.(value, undefined, undefined, (error) => {+ if (error) { console.error(message(error)); process.exitCode = 1; }+ if (process.connected) process.disconnect();+ });+};controller.signal.addEventListener("abort", () => {- process.send?.({ type: "stopping", error: message(controller.signal.reason) });+ report({ type: "stopping", error: message(controller.signal.reason) });}, { once: true });-process.on("message", (value: { type: "start"; input: CaptureInput } | { type: "abort"; error: string }) => {+const receive = (value: { type: "start"; input: CaptureInput } | { type: "abort"; error: string }) => {if (value.type === "abort") { controller.abort(new Error(value.error)); return; }void captureStagedVisual(value.input, controller,- (event) => process.send?.({ type: "progress", event })).then(- () => process.send?.({ type: "completed" }),- (error) => process.send?.({ type: "failed", error: message(error) }),+ (event) => report({ type: "progress", event })).then(+ () => finish({ type: "completed" }),+ (error) => finish({ type: "failed", error: message(error) }),);-});-process.on("disconnect", () => controller.abort(new Error("Render owner disconnected")));+};+process.on("message", receive);+process.on("disconnect", disconnected);diff --git a/packages/provider-hyperframes-local/test/capture-process.test.ts b/packages/provider-hyperframes-local/test/capture-process.test.tsindex 17ac52ab..a414b1ac 100644--- a/packages/provider-hyperframes-local/test/capture-process.test.ts+++ b/packages/provider-hyperframes-local/test/capture-process.test.ts@@ -1,5 +1,5 @@import assert from "node:assert/strict";-import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { pathToFileURL } from "node:url";@@ -9,6 +9,53 @@ import { runCaptureProcess } from "../src/capture-process.js";import { resolveExecutionOptions } from "../src/render.js";import type { CaptureInput } from "../src/capture.js";+test("a capture failure still terminates a detached process left by failed resource cleanup", async () => {+ const root = await mkdtemp(join(tmpdir(), "hypit-worker-failed-cleanup-"));+ let descendant: number | undefined;+ try {+ const engine = join(root, "engine.mjs");+ const pidFile = join(root, "descendant");+ await writeFile(engine, `import { spawn } from 'node:child_process';+import { writeFileSync } from 'node:fs';+const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { detached: true, stdio: 'ignore' });+child.unref();+writeFileSync(${JSON.stringify(pidFile)}, String(child.pid));+throw new Error('engine initialization failed after starting a child');`);+ await assert.rejects(runCaptureProcess({+ document: { frameRate: { numerator: 30, denominator: 1 } },+ range: { startFrame: 0, endFrameExclusive: 1 }, config: resolveExecutionOptions({}),+ directory: root, engineModule: pathToFileURL(engine).href,+ } as CaptureInput, new AbortController().signal, () => {}), /engine initialization failed/);+ descendant = Number(await readFile(pidFile, "utf8"));+ try {+ process.kill(descendant, 0);+ assert.notEqual(process.platform, "win32");+ assert.match(execFileSync("ps", ["-p", String(descendant), "-o", "stat="], { encoding: "utf8" }).trim(), /^Z/u);+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error;+ }+ } finally {+ if (descendant !== undefined) { try { process.kill(descendant, "SIGKILL"); } catch {} }+ await rm(root, { recursive: true, force: true });+ }+});++test("a completed renderer finishes its IPC shutdown and exits naturally", async () => {+ const root = await mkdtemp(join(tmpdir(), "hypit-render-exit-"));+ try {+ const marker = join(root, "exited");+ const entry = join(root, "worker.mjs");+ await writeFile(entry, `import { writeFileSync } from 'node:fs';+process.on('exit', () => writeFileSync(${JSON.stringify(marker)}, 'natural exit'));+process.once('message', () => {+ process.send({ type: 'completed' }, () => setTimeout(() => process.disconnect(), 100));+});`);+ await runCaptureProcess({ config: resolveExecutionOptions({}) } as CaptureInput,+ new AbortController().signal, () => {}, pathToFileURL(entry));+ assert.equal(await readFile(marker, "utf8"), "natural exit");+ } finally { await rm(root, { recursive: true, force: true }); }+});+test("a stuck renderer and its detached child both stop before the call rejects", async () => {const root = await mkdtemp(join(tmpdir(), "hypit-stuck-render-"));const controller = new AbortController();@@ -68,7 +115,7 @@ process.stdout.write("resolved:" + Object.keys(module).join(",") + "\\n");`);await writeFile(worker, `process.once("message", async () => {const module = await import("@hypit/hyperframes/project");process.stdout.write("resolved:" + Object.keys(module).join(",") + "\\n");- process.send({ type: "completed" });+ process.send({ type: "completed" }, () => process.disconnect());});`);const diagnostics: string[] = [];await runCaptureProcess({ config: resolveExecutionOptions({}) } as CaptureInput,@@ -86,7 +133,7 @@ test("renderer stdout and stderr diagnostics are drained before reporting succesawait writeFile(entry, `process.once('message', () => {process.stdout.write('browser ready\\n');process.stderr.write('render diagnostic\\n');- process.send({ type: 'completed' });+ process.send({ type: 'completed' }, () => process.disconnect());});`);await runCaptureProcess({ config: resolveExecutionOptions({}) } as CaptureInput,new AbortController().signal, () => {}, pathToFileURL(entry), async (message) => {@@ -96,3 +143,52 @@ test("renderer stdout and stderr diagnostics are drained before reporting succesassert.ok(messages.some((item) => item.stream === "stderr" && item.message.includes("render diagnostic")));} finally { await rm(root, { recursive: true, force: true }); }});++test("normal exit needs no process enumeration and forced-cleanup failures remain visible", async (t) => {+ const root = await mkdtemp(join(tmpdir(), "hypit-render-no-process-tools-"));+ try {+ const runner = join(root, "runner.mjs");+ // Isolate PATH in another Node process: no pgrep/taskkill is available, and other+ // tests retain their normal environment. The capture child uses an absolute Node path.+ await writeFile(runner, `+import { runCaptureProcess } from ${JSON.stringify(new URL("../src/capture-process.ts", import.meta.url).href)};+import { resolveExecutionOptions } from ${JSON.stringify(new URL("../src/render.ts", import.meta.url).href)};+import { pathToFileURL } from 'node:url';+process.env.PATH = ${JSON.stringify(root)};+const diagnostics = [];+const controller = new AbortController();+let error;+try {+ await runCaptureProcess({ config: resolveExecutionOptions({}) }, controller.signal,+ () => controller.abort(new Error('cancel this render')), pathToFileURL(process.argv[2]),+ async (event) => { diagnostics.push(event); });+} catch (caught) { error = caught.message; }+process.stdout.write(JSON.stringify({ error, diagnostics }));+`);+ for (const scenario of ["completed", "failed", "stuck-completed", "cancelled"] as const) {+ await t.test(scenario, async () => {+ const entry = join(root, `${scenario}.mjs`);+ const stuck = scenario === "stuck-completed" || scenario === "cancelled";+ const result = scenario === "failed" ? { type: "failed", error: "capture failed" }+ : scenario === "cancelled" ? { type: "progress", event: { phase: "encoding" } }+ : { type: "completed" };+ await writeFile(entry, `process.once('message', () => {+ process.send(${JSON.stringify(result)}, () => { ${scenario !== "completed" ? "setInterval(() => {}, 1000);" : "process.disconnect();"} });+});`);+ const run = spawnSync(process.execPath, ["--import", import.meta.resolve("tsx"), runner, entry],+ { encoding: "utf8", timeout: 20_000 });+ assert.equal(run.status, 0, `${run.error ?? ""}\n${run.stderr}`);+ const outcome = JSON.parse(run.stdout) as { error?: string; diagnostics: { level: string; message: string }[] };+ if (scenario === "failed") assert.match(outcome.error!, /capture failed/);+ else if (scenario === "cancelled") assert.match(outcome.error!, /cancel this render/u);+ else assert.equal(outcome.error, undefined);+ if (stuck) {+ assert.ok(outcome.diagnostics.some((d) => d.level === "warning" && /did not exit/u.test(d.message)));+ }+ if (scenario !== "completed") {+ assert.ok(outcome.diagnostics.some((d) => d.level === "warning" && /cleanup could not be confirmed/u.test(d.message)));+ } else assert.deepEqual(outcome.diagnostics, []);+ });+ }+ } finally { await rm(root, { recursive: true, force: true }); }+});diff --git a/packages/provider-hyperframes-local/test/range.test.ts b/packages/provider-hyperframes-local/test/range.test.tsindex 45eea5b3..ca2b9ce3 100644--- a/packages/provider-hyperframes-local/test/range.test.ts+++ b/packages/provider-hyperframes-local/test/range.test.ts@@ -103,8 +103,11 @@ test("real selected renders sample video correctly across loop, hold and stretchconst events: HyperframesRenderProgress[] = [];const render = async (name: string, range: { startFrame: number; endFrameExclusive: number } | undefined,workers: number) => {+ const warnings: string[] = [];const visual = await renderHyperframesVisual({ document, ...(range === undefined ? {} : { range }) },- { resources, workers, quality: "high", processTimeoutMs: 120000, onProgress: (e) => events.push(e) });+ { resources, workers, quality: "high", processTimeoutMs: 120000, onProgress: (e) => events.push(e),+ onDiagnostic: async (event) => { if (event.level === "warning") warnings.push(event.message); } });+ assert.deepEqual(warnings, [], "a completed render should close its resources and exit without forced cleanup");const file = join(root, `${name}.mp4`);await writeFile(file, (await resources.get(visual.artifact.resource))!);const decoded = spawnSync("ffmpeg", ["-v", "error", "-i", file, "-f", "rawvideo", "-pix_fmt", "rgb24", "pipe:1"]);