Skip to content

Commit 32fb2fc

Browse files
authored
fix(cli): render task flow JSON failures (#127080)
* fix(cli): render task flow JSON failures * test(cli): stabilize entry console capture * fix(cli): keep JSON terminal resets off stdout * fix(cli): keep task JSON resets off stdout
1 parent 5edc2a7 commit 32fb2fc

6 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎src/commands/flows.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,28 @@ describe("flows commands", () => {
318318
});
319319
});
320320

321+
it("keeps terminal reset bytes off stdout for JSON lookup failures", async () => {
322+
await withTaskFlowCommandStateDir(async () => {
323+
const runtime = createRuntime();
324+
325+
await flowsShowCommand({ lookup: "missing-flow", json: true }, runtime);
326+
327+
expect(runtime.error).not.toHaveBeenCalled();
328+
expect(runtime.writeJson).toHaveBeenCalledWith(
329+
{
330+
ok: false,
331+
error: {
332+
type: "cli_error",
333+
message:
334+
"TaskFlow not found: missing-flow. Run openclaw tasks flow list to see recent flow ids.",
335+
},
336+
},
337+
2,
338+
);
339+
expect(runtime.exit).toHaveBeenCalledWith(1, { resetStream: process.stderr });
340+
});
341+
});
342+
321343
it("shows one TaskFlow with linked task details in text mode", async () => {
322344
await withTaskFlowCommandStateDir(async () => {
323345
const flow = createManagedTaskFlow({

‎src/commands/flows.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text
77
import { isRich, theme } from "../../packages/terminal-core/src/theme.js";
88
import { formatCliCommand } from "../cli/command-format.js";
99
import { parseCliEnumFilter } from "../cli/enum-filter.js";
10+
import { formatCliJsonFailure } from "../cli/failure-output.js";
1011
import { getRuntimeConfig } from "../config/config.js";
1112
import { info } from "../globals.js";
12-
import type { RuntimeEnv } from "../runtime.js";
13-
import { writeRuntimeJson } from "../runtime.js";
13+
import { type RuntimeEnv, writeRuntimeJson } from "../runtime.js";
1414
import { listTasksForFlowId } from "../tasks/runtime-internal.js";
1515
import { cancelFlowById, getFlowTaskSummary } from "../tasks/task-executor.js";
1616
import {
@@ -211,8 +211,13 @@ export async function flowsShowCommand(
211211
) {
212212
const flow = resolveTaskFlowForLookupToken(opts.lookup);
213213
if (!flow) {
214-
runtime.error(formatFlowLookupMiss(opts.lookup));
215-
runtime.exit(1);
214+
const message = formatFlowLookupMiss(opts.lookup);
215+
if (opts.json) {
216+
writeRuntimeJson(runtime, formatCliJsonFailure(message));
217+
} else {
218+
runtime.error(message);
219+
}
220+
runtime.exit(1, opts.json ? { resetStream: process.stderr } : undefined);
216221
return;
217222
}
218223
const tasks = listTasksForFlowId(flow.flowId);

‎src/commands/tasks.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ describe("tasks commands", () => {
811811
message: expect.stringContaining("Task not found: missing"),
812812
},
813813
});
814+
expect(jsonLookupRuntime.exit).toHaveBeenCalledWith(1, { resetStream: process.stderr });
814815
});
815816
});
816817

‎src/commands/tasks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export async function tasksShowCommand(
330330
} else {
331331
runtime.error(message);
332332
}
333-
runtime.exit(1);
333+
runtime.exit(1, opts.json ? { resetStream: process.stderr } : undefined);
334334
return;
335335
}
336336

‎src/entry.run-main.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it, vi } from "vitest";
22
import { ExpectedCliError } from "./cli/failure-output.js";
33
import { runMainOrRootHelp } from "./entry.js";
4+
import { enableConsoleCapture } from "./logging.js";
45

56
describe("entry run-main boundary", () => {
67
it("retains JSON console routing through process finalization", async () => {
@@ -25,6 +26,7 @@ describe("entry run-main boundary", () => {
2526
humanOutput: message,
2627
machineOutput: message,
2728
});
29+
enableConsoleCapture();
2830
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
2931
process.exitCode = undefined;
3032

‎test/cli-json-stdout.e2e.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,27 @@ describe("cli json stdout contract", () => {
295295
);
296296
});
297297

298+
it("renders a missing TaskFlow as one canonical JSON document without stderr", async () => {
299+
await withTempHome(
300+
async (tempHome) => {
301+
const result = runBuiltCli(tempHome, ["tasks", "flow", "show", "missing-flow", "--json"]);
302+
303+
expect(result.status, result.stderr).toBe(1);
304+
expect(result.stdout, result.stderr).not.toBe("");
305+
expect(JSON.parse(result.stdout)).toEqual({
306+
ok: false,
307+
error: {
308+
type: "cli_error",
309+
message:
310+
"TaskFlow not found: missing-flow. Run openclaw tasks flow list to see recent flow ids.",
311+
},
312+
});
313+
expect(result.stderr).toBe("");
314+
},
315+
{ prefix: "openclaw-task-flow-json-failure-e2e-" },
316+
);
317+
});
318+
298319
it.each([
299320
{ name: "qr", command: ["qr"] },
300321
{ name: "clawbot qr", command: ["clawbot", "qr"] },

0 commit comments

Comments
 (0)