Skip to content

Write HTML. Render video. Built for agents.

fix(cli): stop a caught post-render throw reporting a valid render as failed (#3409)

Miguel Ángelea95b7d44e6d
diff --git a/packages/cli/src/cli.lifecycle.test.ts b/packages/cli/src/cli.lifecycle.test.ts
index 5f00fc31c1..c1edb4c6d7 100644
--- a/packages/cli/src/cli.lifecycle.test.ts
+++ b/packages/cli/src/cli.lifecycle.test.ts
@@ -165,6 +165,57 @@ describe("CLI lifecycle", () => {
}
});
+ it("does not let a post-validation throw doom a render whose artifact is on disk", async () => {
+ // The gap the field reports land in: a teardown step throws AFTER the
+ // artifact validated, the command wrapper catches it, and the result
+ // carries exitCode 1. The uncaughtException / unhandledRejection handlers
+ // both consult isRenderSucceeded(), but a caught throw never reaches them,
+ // so nothing sanitized the code and a valid MP4 was reported as a failure.
+ const trackCommandResult = vi.fn();
+ const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);
+ try {
+ const successState = await import("./utils/render-success-state.js");
+ mockInitCommand(() => {
+ successState.markRenderSucceeded();
+ throw new Error("post-render teardown blew up");
+ });
+ mockTelemetry({ trackCommandResult });
+
+ process.argv = ["node", "cli.ts", "init", "--json"];
+ await import("./cli.js");
+
+ expect(process.exitCode).toBe(0);
+ expect(trackCommandResult).toHaveBeenCalledWith(
+ expect.objectContaining({ success: true, exitCode: 0 }),
+ );
+ successState._resetRenderSuccessForTests();
+ } finally {
+ exitSpy.mockRestore();
+ }
+ });
+
+ it("still exits non-zero when a command throws and no render ever validated", async () => {
+ // The guard on the sanitizer above: it must key off a validated artifact,
+ // not merely off the command having finished. Without this, every caught
+ // throw would silently become exit 0.
+ const trackCommandResult = vi.fn();
+ const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);
+ try {
+ mockInitCommand(() => {
+ throw new Error("genuine failure");
+ });
+ mockTelemetry({ trackCommandResult });
+
+ process.argv = ["node", "cli.ts", "init", "--json"];
+ await import("./cli.js");
+
+ expect(process.exitCode).not.toBe(0);
+ expect(trackCommandResult).toHaveBeenCalledWith(expect.objectContaining({ success: false }));
+ } finally {
+ exitSpy.mockRestore();
+ }
+ });
+
it("still scores an EPIPE before the artifact is validated as a failure", async () => {
const trackCommandResult = vi.fn();
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);
diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts
index 44aceb12e8..66c67ccdb8 100644
--- a/packages/cli/src/cli.ts
+++ b/packages/cli/src/cli.ts
@@ -283,12 +283,21 @@ let finalized = false;
async function finalizeCli(result: CommandResult): Promise<void> {
if (finalized) return;
finalized = true;
- commandFailed ||= result.exitCode !== 0;
+ // Once the artifact has validated and been committed to disk, the run
+ // delivered — anything recorded as a failure after that is teardown noise.
+ // The uncaughtException / unhandledRejection handlers already consult
+ // isRenderSucceeded(), but a post-render throw that the command wrapper
+ // CATCHES never reaches them: it becomes an ordinary non-zero
+ // CommandResult, and a valid render is reported as a failure. Sanitizing
+ // here, once, is what those handlers cannot cover, and it keeps the exit
+ // code and the telemetry record from disagreeing about the same run.
+ const exitCode = isRenderSucceeded() ? 0 : result.exitCode;
+ commandFailed ||= exitCode !== 0;
await telemetryReady.catch(() => {});
_trackCommandResult?.({
command,
- success: result.exitCode === 0 && commandSucceededForTelemetry(),
- exitCode: result.exitCode,
+ success: exitCode === 0 && commandSucceededForTelemetry(),
+ exitCode,
durationMs: Date.now() - commandStart,
runId,
});
@@ -298,7 +307,7 @@ async function finalizeCli(result: CommandResult): Promise<void> {
_printStalePinNotice?.();
_printSkillsUpdateNotice?.();
}
- process.exitCode = result.exitCode;
+ process.exitCode = exitCode;
}
registerRootExitRequester((exitCode) => {