fix(cli): return nonzero status for operation failures (#121)
This commit is contained in:
@@ -25,19 +25,6 @@ export interface ProcessCliErrorOutput {
|
||||
readonly message: string;
|
||||
}
|
||||
|
||||
/** Distinguish a safe process-command failure from successful Evidence. */
|
||||
export const isProcessCliErrorOutput = (
|
||||
value: unknown,
|
||||
): value is ProcessCliErrorOutput =>
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
"error" in value &&
|
||||
value.error === "Process command failed" &&
|
||||
"category" in value &&
|
||||
typeof value.category === "string" &&
|
||||
"message" in value &&
|
||||
typeof value.message === "string";
|
||||
|
||||
/** Capture one JSON scenario through the same policy and evidence contract as MCP. */
|
||||
export const captureProcessScenarioFile = async (path: string) => {
|
||||
try {
|
||||
|
||||
+21
-3
@@ -1,5 +1,21 @@
|
||||
import type { Logger } from "./logger.js";
|
||||
|
||||
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
|
||||
/** Identify caller-visible CLI outcomes that mean the requested operation failed. */
|
||||
export const isCliOperationFailure = (value: unknown): boolean => {
|
||||
if (!isRecord(value)) return false;
|
||||
if (typeof value.error === "string") return true;
|
||||
if (value.healthy === false) return true;
|
||||
return (
|
||||
value.status === "failed" ||
|
||||
value.status === "needs_confirmation" ||
|
||||
value.status === "needs_human" ||
|
||||
value.status === "planned"
|
||||
);
|
||||
};
|
||||
|
||||
/** Log one CLI command with duration and a stable success or failure status. */
|
||||
export const logCliCommand = async <Value>(
|
||||
logger: Logger,
|
||||
@@ -9,14 +25,16 @@ export const logCliCommand = async <Value>(
|
||||
const startedAt = performance.now();
|
||||
try {
|
||||
const value = await execute();
|
||||
logger.info(
|
||||
const failed = isCliOperationFailure(value);
|
||||
logger[failed ? "error" : "info"](
|
||||
{
|
||||
command,
|
||||
durationMs: Math.round((performance.now() - startedAt) * 100) / 100,
|
||||
status: "ok",
|
||||
status: failed ? "error" : "ok",
|
||||
},
|
||||
"CLI command completed",
|
||||
failed ? "CLI command failed" : "CLI command completed",
|
||||
);
|
||||
if (failed) process.exitCode = 1;
|
||||
return value;
|
||||
} catch (cause: unknown) {
|
||||
logger.error(
|
||||
|
||||
@@ -1,26 +1,12 @@
|
||||
import { Cli, Errors, z } from "incur";
|
||||
import { Cli, z } from "incur";
|
||||
|
||||
import {
|
||||
captureProcessScenarioFile,
|
||||
compareProcessEvidenceFiles,
|
||||
isProcessCliErrorOutput,
|
||||
} from "./application/ProcessCli.js";
|
||||
import { logCliCommand } from "./cliLogging.js";
|
||||
import type { Logger } from "./logger.js";
|
||||
|
||||
const requireProcessCommandSuccess = async <Value>(
|
||||
execute: () => Promise<Value>,
|
||||
): Promise<Value> => {
|
||||
const value = await execute();
|
||||
if (isProcessCliErrorOutput(value))
|
||||
throw new Errors.IncurError({
|
||||
code: "PROCESS_COMMAND_FAILED",
|
||||
message: value.message,
|
||||
retryable: false,
|
||||
});
|
||||
return value;
|
||||
};
|
||||
|
||||
/** Register Process Capture v4 one-shot commands through shared application services. */
|
||||
export const registerProcessCommands = (
|
||||
cli: ReturnType<typeof Cli.create>,
|
||||
@@ -31,9 +17,7 @@ export const registerProcessCommands = (
|
||||
args: z.object({ scenario: z.string().describe("Scenario JSON path") }),
|
||||
run: ({ args }) =>
|
||||
logCliCommand(logger, "capture-process", () =>
|
||||
requireProcessCommandSuccess(() =>
|
||||
captureProcessScenarioFile(args.scenario),
|
||||
),
|
||||
captureProcessScenarioFile(args.scenario),
|
||||
),
|
||||
});
|
||||
cli.command("compare-process-captures", {
|
||||
@@ -44,9 +28,7 @@ export const registerProcessCommands = (
|
||||
}),
|
||||
run: ({ args }) =>
|
||||
logCliCommand(logger, "compare-process-captures", () =>
|
||||
requireProcessCommandSuccess(() =>
|
||||
compareProcessEvidenceFiles(args.left, args.right),
|
||||
),
|
||||
compareProcessEvidenceFiles(args.left, args.right),
|
||||
),
|
||||
});
|
||||
};
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ const logLevelSchema = z
|
||||
export const parseLogLevel = (value: unknown): LogLevel =>
|
||||
logLevelSchema.parse(value);
|
||||
|
||||
/** Create a JSON logger whose destination cannot corrupt MCP stdout frames. */
|
||||
/** Create a JSON logger on stderr so protocol and CLI result stdout stay valid. */
|
||||
export const createLogger = (mode: "mcp" | "cli", level: LogLevel): Logger =>
|
||||
pino(
|
||||
{
|
||||
@@ -32,7 +32,7 @@ export const createLogger = (mode: "mcp" | "cli", level: LogLevel): Logger =>
|
||||
censor: "[Redacted]",
|
||||
},
|
||||
},
|
||||
pino.destination({ dest: mode === "mcp" ? 2 : 1, sync: false }),
|
||||
pino.destination({ dest: 2, sync: false }),
|
||||
);
|
||||
|
||||
/** Logger used by embedders that have not opted into process output. */
|
||||
|
||||
Reference in New Issue
Block a user