Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(engine): pin the pool's worker-failure contract and survive a throwing hook
Vance Ingalls439bdbd77443
diff --git a/packages/engine/src/services/parallelCoordinator.test.ts b/packages/engine/src/services/parallelCoordinator.test.tsindex ef2595edc1..414902db5a 100644--- a/packages/engine/src/services/parallelCoordinator.test.ts+++ b/packages/engine/src/services/parallelCoordinator.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect, vi } from "vitest";import {calculateOptimalWorkers,computeWorkerSizing,+ createPoolFailureHandler,distributeFrames,expectedFramesForTask,flagSilentWorkerExits,@@ -536,3 +537,75 @@ describe("isPoolFatalWorkerFailure", () => {expect(isPoolFatalWorkerFailure(failure("cancelled"), true)).toBe(false);});});++describe("createPoolFailureHandler", () => {+ const fatal = () => new CaptureFailure({ kind: "transient_browser", message: "Target closed" });++ it("delivers the original failure to the hook before the peers are aborted", () => {+ const peerController = new AbortController();+ const seen: Array<{ failure: CaptureFailure; abortedYet: boolean }> = [];+ const handler = createPoolFailureHandler({+ streaming: true,+ peerController,+ hooks: {+ onWorkerFailure: (failure) =>+ seen.push({ failure, abortedYet: peerController.signal.aborted }),+ },+ });+ const failure = fatal();+ handler.onFailure(failure);+ expect(seen).toHaveLength(1);+ expect(seen[0]?.failure).toBe(failure);+ expect(seen[0]?.abortedYet).toBe(false);+ expect(peerController.signal.aborted).toBe(true);+ expect(peerController.signal.reason).toBe(failure);+ expect(handler.firstFatalFailure()).toBe(failure);+ });++ it("still aborts the peers when the hook throws, and keeps the classified failure", () => {+ const peerController = new AbortController();+ const handler = createPoolFailureHandler({+ streaming: true,+ peerController,+ hooks: {+ onWorkerFailure: () => {+ throw new Error("hook exploded");+ },+ },+ });+ const failure = fatal();+ expect(() => handler.onFailure(failure)).not.toThrow();+ expect(peerController.signal.aborted).toBe(true);+ expect(peerController.signal.reason).toBe(failure);+ expect(handler.firstFatalFailure()).toBe(failure);+ });++ it("ignores every failure after the first, which owns the abort reason", () => {+ const peerController = new AbortController();+ const hook = vi.fn();+ const handler = createPoolFailureHandler({+ streaming: true,+ peerController,+ hooks: { onWorkerFailure: hook },+ });+ const first = fatal();+ handler.onFailure(first);+ handler.onFailure(fatal());+ expect(hook).toHaveBeenCalledTimes(1);+ expect(peerController.signal.reason).toBe(first);+ });++ it("leaves the peers running for a cancellation on the streaming path", () => {+ const peerController = new AbortController();+ const hook = vi.fn();+ const handler = createPoolFailureHandler({+ streaming: true,+ peerController,+ hooks: { onWorkerFailure: hook },+ });+ handler.onFailure(new CaptureFailure({ kind: "cancelled", message: "aborted" }));+ expect(hook).not.toHaveBeenCalled();+ expect(peerController.signal.aborted).toBe(false);+ expect(handler.firstFatalFailure()).toBeUndefined();+ });+});diff --git a/packages/engine/src/services/parallelCoordinator.ts b/packages/engine/src/services/parallelCoordinator.tsindex 991db3ab26..f01deb7e18 100644--- a/packages/engine/src/services/parallelCoordinator.ts+++ b/packages/engine/src/services/parallelCoordinator.ts@@ -1008,6 +1008,43 @@ export interface ParallelCaptureHooks {onWorkerFailure?: (failure: CaptureFailure) => void;}+/**+ * The pool's single failure gate. Both halves of the contract the streaming+ * stage relies on live here so they can be pinned without a browser: the+ * first pool-fatal failure reaches `hooks.onWorkerFailure` with the ORIGINAL+ * `CaptureFailure` before the peers are aborted, and a hook that throws cannot+ * skip that abort (the pool still rejects with its own classified failure,+ * not the hook's error). Later failures are ignored: the first one owns the+ * abort reason. `onFailure` runs synchronously at the tail of a worker's+ * catch, before that worker's promise settles, so the hook always precedes+ * the pool's rejection. Exported for tests.+ */+export function createPoolFailureHandler(args: {+ streaming: boolean;+ peerController: AbortController;+ hooks?: ParallelCaptureHooks;+}): {+ onFailure: (failure: CaptureFailure) => void;+ firstFatalFailure: () => CaptureFailure | undefined;+} {+ let firstFatalFailure: CaptureFailure | undefined;+ return {+ firstFatalFailure: () => firstFatalFailure,+ onFailure: (failure) => {+ if (firstFatalFailure || !isPoolFatalWorkerFailure(failure, args.streaming)) return;+ firstFatalFailure = failure;+ try {+ args.hooks?.onWorkerFailure?.(failure);+ } catch {+ // A caller-supplied hook must not be able to disable the pool abort;+ // the worker's classified failure is what the pool reports.+ } finally {+ args.peerController.abort(failure);+ }+ },+ };+}+export async function executeParallelCapture(serverUrl: string,workDir: string,@@ -1074,13 +1111,12 @@ export async function executeParallelCapture(const workerSignal = signal? AbortSignal.any([signal, peerController.signal]): peerController.signal;- let firstFatalFailure: CaptureFailure | undefined;- const onFailure = (failure: CaptureFailure): void => {- if (firstFatalFailure || !isPoolFatalWorkerFailure(failure, Boolean(onFrameBuffer))) return;- firstFatalFailure = failure;- hooks?.onWorkerFailure?.(failure);- peerController.abort(failure);- };+ const failureHandler = createPoolFailureHandler({+ streaming: Boolean(onFrameBuffer),+ peerController,+ hooks,+ });+ const onFailure = failureHandler.onFailure;const results = await Promise.all(tasks.map((task) =>executeWorkerTask(@@ -1104,7 +1140,8 @@ export async function executeParallelCapture(const errors = results.filter((r) => r.failure || r.error);if (errors.length > 0) {const errorMessages = errors.map(formatWorkerFailure).join("; ");- const representative = firstFatalFailure ?? errors.find((result) => result.failure)?.failure;+ const representative =+ failureHandler.firstFatalFailure() ?? errors.find((result) => result.failure)?.failure;const workerDiagnostics = errors.flatMap((result) => result.failure?.workerDiagnostics ?? []);throw new CaptureFailure({kind: representative?.kind ?? "io",