Skip to content

Commit ea95b7d

Browse files
fix(cli): stop a caught post-render throw reporting a valid render as failed (#3409)
A render that produced and validated its artifact still exited 1. Reported again from the field on 0.8.7: the MP4 was on disk and an independent ffprobe and full decode both passed, and the CLI exited 1 immediately after logging `artifact validated`. `render-success-state.ts` exists for exactly this and documents three earlier cases, so the sentinel was already there. Its gap is which paths read it: the uncaughtException and unhandledRejection handlers both consult `isRenderSucceeded()`, but a post-render throw that the command wrapper CATCHES never reaches either. It becomes an ordinary non-zero CommandResult, and `finalizeCli` wrote that straight to `process.exitCode`. The result was a run that disagreed with itself: `commandSucceededForTelemetry()` already lets a validated render override a failure, so telemetry recorded success while the shell saw exit 1. Sanitize once in `finalizeCli`, where every command result funnels through, rather than wrapping the individual steps. Which step threw does not matter; that the artifact is committed does. The throw is still printed, so it stays visible for diagnosis without being fatal. Reproduced first as a failing test (`expected 1 to be +0`) on macOS, so this is not Windows-specific — the field reports are one instance of it. A second test pins the other side: a command that throws with no validated render still exits non-zero, so the sanitizer cannot swallow a genuine failure.
1 parent 9c73e64 commit ea95b7d

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎packages/cli/src/cli.lifecycle.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,57 @@ describe("CLI lifecycle", () => {
165165
}
166166
});
167167

168+
it("does not let a post-validation throw doom a render whose artifact is on disk", async () => {
169+
// The gap the field reports land in: a teardown step throws AFTER the
170+
// artifact validated, the command wrapper catches it, and the result
171+
// carries exitCode 1. The uncaughtException / unhandledRejection handlers
172+
// both consult isRenderSucceeded(), but a caught throw never reaches them,
173+
// so nothing sanitized the code and a valid MP4 was reported as a failure.
174+
const trackCommandResult = vi.fn();
175+
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);
176+
try {
177+
const successState = await import("./utils/render-success-state.js");
178+
mockInitCommand(() => {
179+
successState.markRenderSucceeded();
180+
throw new Error("post-render teardown blew up");
181+
});
182+
mockTelemetry({ trackCommandResult });
183+
184+
process.argv = ["node", "cli.ts", "init", "--json"];
185+
await import("./cli.js");
186+
187+
expect(process.exitCode).toBe(0);
188+
expect(trackCommandResult).toHaveBeenCalledWith(
189+
expect.objectContaining({ success: true, exitCode: 0 }),
190+
);
191+
successState._resetRenderSuccessForTests();
192+
} finally {
193+
exitSpy.mockRestore();
194+
}
195+
});
196+
197+
it("still exits non-zero when a command throws and no render ever validated", async () => {
198+
// The guard on the sanitizer above: it must key off a validated artifact,
199+
// not merely off the command having finished. Without this, every caught
200+
// throw would silently become exit 0.
201+
const trackCommandResult = vi.fn();
202+
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);
203+
try {
204+
mockInitCommand(() => {
205+
throw new Error("genuine failure");
206+
});
207+
mockTelemetry({ trackCommandResult });
208+
209+
process.argv = ["node", "cli.ts", "init", "--json"];
210+
await import("./cli.js");
211+
212+
expect(process.exitCode).not.toBe(0);
213+
expect(trackCommandResult).toHaveBeenCalledWith(expect.objectContaining({ success: false }));
214+
} finally {
215+
exitSpy.mockRestore();
216+
}
217+
});
218+
168219
it("still scores an EPIPE before the artifact is validated as a failure", async () => {
169220
const trackCommandResult = vi.fn();
170221
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never);

‎packages/cli/src/cli.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,21 @@ let finalized = false;
283283
async function finalizeCli(result: CommandResult): Promise<void> {
284284
if (finalized) return;
285285
finalized = true;
286-
commandFailed ||= result.exitCode !== 0;
286+
// Once the artifact has validated and been committed to disk, the run
287+
// delivered — anything recorded as a failure after that is teardown noise.
288+
// The uncaughtException / unhandledRejection handlers already consult
289+
// isRenderSucceeded(), but a post-render throw that the command wrapper
290+
// CATCHES never reaches them: it becomes an ordinary non-zero
291+
// CommandResult, and a valid render is reported as a failure. Sanitizing
292+
// here, once, is what those handlers cannot cover, and it keeps the exit
293+
// code and the telemetry record from disagreeing about the same run.
294+
const exitCode = isRenderSucceeded() ? 0 : result.exitCode;
295+
commandFailed ||= exitCode !== 0;
287296
await telemetryReady.catch(() => {});
288297
_trackCommandResult?.({
289298
command,
290-
success: result.exitCode === 0 && commandSucceededForTelemetry(),
291-
exitCode: result.exitCode,
299+
success: exitCode === 0 && commandSucceededForTelemetry(),
300+
exitCode,
292301
durationMs: Date.now() - commandStart,
293302
runId,
294303
});
@@ -298,7 +307,7 @@ async function finalizeCli(result: CommandResult): Promise<void> {
298307
_printStalePinNotice?.();
299308
_printSkillsUpdateNotice?.();
300309
}
301-
process.exitCode = result.exitCode;
310+
process.exitCode = exitCode;
302311
}
303312

304313
registerRootExitRequester((exitCode) => {

0 commit comments

Comments
 (0)