From a06e04b4dfcd994a426cc6b6ab77082260b95a3f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:12:45 -0500 Subject: [PATCH] fix(cli): preserve doctor routing with empty output values (#13262) * fix(cli): preserve doctor routing with empty output values * fix(cli): keep configuration precedence before malformed readiness commands --- src/cliBuiltinArguments.ts | 9 +- src/cliOneShot.ts | 6 +- src/cliOutput.ts | 4 +- tests/boundary/cli/mcpDoctorHelpCli.test.ts | 388 ++++++++------------ 4 files changed, 152 insertions(+), 255 deletions(-) diff --git a/src/cliBuiltinArguments.ts b/src/cliBuiltinArguments.ts index 8b6c497fa1..136ca4b69b 100644 --- a/src/cliBuiltinArguments.ts +++ b/src/cliBuiltinArguments.ts @@ -12,8 +12,7 @@ export const VALUED_OUTPUT_FLAGS: ReadonlySet = new Set([ const spacedOutputValue = ( flag: string, next: string | undefined, -): string | undefined => - VALUED_OUTPUT_FLAGS.has(flag) && next ? next : undefined; +): string | undefined => (VALUED_OUTPUT_FLAGS.has(flag) ? next : undefined); export const OUTPUT_SWITCHES: ReadonlySet = new Set([ "--json", @@ -65,7 +64,7 @@ export interface CliGlobalArguments { readonly commandIndices: readonly number[]; /** Valueless global flags and timing spellings taken out before command parsing. */ readonly flags: ReadonlySet; - /** Positions of nonempty values consumed by valued output flags. */ + /** Positions of explicit values consumed by valued output flags, including empty values. */ readonly outputValues: readonly { readonly flag: string; readonly index: number; @@ -88,7 +87,7 @@ const normalizeCliArguments = (arguments_: readonly string[]): string[] => { const argument = arguments_[index]!; const outputValue = spacedOutputValue(argument, arguments_[index + 1]); if (outputValue !== undefined) { - // A nonempty spaced value is consumed before looking for more flags. + // A spaced value is consumed before looking for more flags. // Its contents belong to that option, even when they look like a flag. normalized.push(argument, outputValue); index += 1; @@ -118,7 +117,7 @@ const normalizeCliArguments = (arguments_: readonly string[]): string[] => { /** * Read one invocation's global arguments. Builtin switches are taken from any * position, including after `--`; a valued output flag consumes the next - * nonempty token as data. The timing global is then read from the remaining + * token as data even when it is empty. The timing global is then read from the remaining * tokens until `--`. The output argument reader reports malformed output * values from these same positions. */ diff --git a/src/cliOneShot.ts b/src/cliOneShot.ts index 75955fe92f..622835b87b 100644 --- a/src/cliOneShot.ts +++ b/src/cliOneShot.ts @@ -80,11 +80,11 @@ export const runOneShotCli = async ( host.stdoutIsTerminal, ); const configurationError = cliConfigurationError(host.environment); - // Doctor is the readiness report for this failure. Other commands still stop - // here so they do not run with an unusable log level. + // Only an admitted doctor can report this failure as readiness. A malformed + // command still stops at the same configuration boundary as other commands. if ( configurationError !== undefined && - (global === undefined || global.command[0] !== "doctor") + (global?.command[0] !== "doctor" || !outputArguments.ok) ) { host.writeStderr(`${configurationError}\n`); host.setExitCode(1); diff --git a/src/cliOutput.ts b/src/cliOutput.ts index 572f4a3e50..a5fc4a858b 100644 --- a/src/cliOutput.ts +++ b/src/cliOutput.ts @@ -111,7 +111,7 @@ interface CliOutputControls { /** Whether `--json` or `--format` selected the format, rather than the default. */ readonly formatExplicit: boolean; readonly fullOutput: boolean; - /** First valued output flag that Incur skips because it has no value. */ + /** First valued output flag with an absent or empty value. */ readonly missingValue: string | undefined; readonly invalidValue: string | undefined; readonly tokenCount: boolean; @@ -175,7 +175,7 @@ export const readCliOutputArguments = ( if (argument === "--token-count") tokenCount = true; if (!VALUED_OUTPUT_FLAGS.has(argument)) continue; const value = values.has(index + 1) ? tokens[index + 1]! : undefined; - if (value === undefined) { + if (value === undefined || value === "") { missingValue ??= argument; continue; } diff --git a/tests/boundary/cli/mcpDoctorHelpCli.test.ts b/tests/boundary/cli/mcpDoctorHelpCli.test.ts index b3255a46d9..bae1ab2448 100644 --- a/tests/boundary/cli/mcpDoctorHelpCli.test.ts +++ b/tests/boundary/cli/mcpDoctorHelpCli.test.ts @@ -1,267 +1,165 @@ -import { describe, expect } from "vitest"; +import { expect } from "vitest"; -import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js"; +import { cliTest } from "../../support/cli/cliFixture.js"; -describe("MCP doctor output routing boundary", () => { - workspaceCliTest( - "keeps output options from selecting the CLI-derived MCP doctor", - async ({ cli }) => { - for (const arguments_ of [ - ["mcp", "doctor", "--json"], - ["--json", "mcp", "doctor"], - ["mcp", "--json", "doctor"], - ["--format", "json", "mcp", "doctor"], - ["mcp", "--format", "json", "doctor"], - ["--format=json", "mcp", "doctor"], - ["mcp", "--format=json", "doctor"], - ["--full-output", "mcp", "--json", "doctor"], - ]) { - const result = await cli.run({ - arguments: arguments_, - environment: { REA_MCP_DOCTOR_DEADLINE_MS: "0" }, - }); - expect(result.exitCode, arguments_.join(" ")).toBe(1); - const report = { - healthy: false, - adapter: "production-stdio", - checks: [ - expect.objectContaining({ - name: "REA_MCP_DOCTOR_DEADLINE_MS", - ok: false, - }), - ], - }; - expect(result.json).toMatchObject( - arguments_.includes("--full-output") - ? { ok: false, error: report } - : report, - ); - expect(result.stderr).toBe(""); - } - }, - ); -}); +const invalidDeadline = { REA_MCP_DOCTOR_DEADLINE_MS: "0" }; +const deadlineReport = { + healthy: false, + adapter: "production-stdio", + checks: [ + expect.objectContaining({ name: "REA_MCP_DOCTOR_DEADLINE_MS", ok: false }), + ], +}; +const unknownOption = { + code: "VALIDATION_ERROR", + message: "Unknown mcp doctor option: --bogus", +}; +const missingFormat = { + code: "MISSING_OUTPUT_VALUE", + message: + "--format requires a value, as in --format or --format=.", +}; -workspaceCliTest( - "keeps output choices that appear after an MCP doctor argument error", +cliTest( + "routes output-prefixed and interleaved commands to the production doctor", async ({ cli }) => { - const unknownOption = { - code: "VALIDATION_ERROR", - message: "Unknown mcp doctor option: --bogus", - }; - const missingFormat = { - code: "MISSING_OUTPUT_VALUE", - message: - "--format requires a value, as in --format or --format=.", - }; - const invalidFormat = { - code: "VALIDATION_ERROR", - message: "Invalid output format: --full-output", - }; - for (const { args, expected } of [ - { args: ["--bogus", "--json"], expected: unknownOption }, - { - args: ["--json", "--bogus", "--full-output", "--format="], - expected: { ok: false, error: unknownOption }, - }, - { - args: ["--format=", "--json", "--full-output"], - expected: { ok: false, error: missingFormat }, - }, - { - args: ["--format", "--full-output", "--json"], - expected: invalidFormat, - }, - { - args: ["--json", "--bogus", "--format=jsonl", "--full-output"], - expected: { + for (const [arguments_, expected] of [ + [["--format", "json", "mcp", "doctor"], deadlineReport], + [["mcp", "--json", "doctor"], deadlineReport], + [["mcp", "--format", "", "doctor", "--json"], missingFormat], + [ + ["--full-output", "mcp", "--format=json", "doctor"], + { ok: false, error: deadlineReport }, + ], + ] as const) { + const result = await cli.run({ + arguments: arguments_, + environment: invalidDeadline, + }); + expect(result, arguments_.join(" ")).toMatchObject({ + exitCode: 1, + stderr: "", + json: expected, + }); + } + }, +); + +cliTest( + "advertises doctor help and honors both spellings over option errors", + async ({ cli }) => { + const parent = await cli.run({ arguments: ["mcp", "--help"] }); + expect(parent.exitCode).toBe(0); + expect(parent.stdout).toContain("doctor"); + + for (const arguments_ of [ + ["mcp", "doctor", "--bogus", "--help", "--json"], + ["--format=", "mcp", "doctor", "-h"], + ]) { + const result = await cli.run({ + arguments: arguments_, + environment: invalidDeadline, + }); + expect(result, arguments_.join(" ")).toMatchObject({ + exitCode: 0, + stderr: "", + }); + expect(result.stdout).toContain("Usage: rea mcp doctor [options]"); + expect(result.stdout).toContain("--format "); + expect(result.stdout).toContain("--full-output"); + expect(result.stdout).toContain("--help, -h"); + expect(result.stdout).toContain("current runtime"); + expect(result.stdout).toContain( + "registered command or launch environment", + ); + } + }, +); + +cliTest( + "preserves the first option error and still applies later output choices", + async ({ cli }) => { + for (const [args, expected] of [ + [ + ["--bogus", "--json", "--full-output", "--format="], + { ok: false, error: unknownOption }, + ], + [ + ["--format=", "--json", "--full-output"], + { ok: false, error: missingFormat }, + ], + [ + ["--json", "--bogus", "--format=jsonl", "--full-output"], + { $rea: "jsonl/v1", type: "error", ok: false, result: { ok: false, error: unknownOption }, }, - }, - ]) { + ], + ] as const) { const result = await cli.run({ arguments: ["mcp", "doctor", ...args] }); - expect(result, args.join(" ")).toMatchObject({ - exitCode: 1, - stderr: "", - }); + expect(result, args.join(" ")).toMatchObject({ exitCode: 1, stderr: "" }); expect(result.json, args.join(" ")).toEqual(expected); } + + const missing = await cli.run({ arguments: ["mcp", "doctor", "--format"] }); + expect(missing).toMatchObject({ + exitCode: 1, + stderr: "", + stdout: `code: ${missingFormat.code}\nmessage: "${missingFormat.message}"\n`, + }); }, ); -describe("MCP doctor help boundary", () => { - workspaceCliTest( - "answers the advertised help option instead of validating it", - async ({ cli }) => { - const parent = await cli.run({ arguments: ["mcp", "--help"] }); - expect(parent.exitCode).toBe(0); - expect(parent.stdout).toContain("doctor"); - for (const argument of ["--help", "-h"]) { - const result = await cli.run({ - arguments: ["mcp", "doctor", argument], - }); - expect(result).toMatchObject({ exitCode: 0 }); - expect(result.stdout).toContain("Usage: rea mcp doctor [options]"); - expect(result.stdout).toContain("--format "); - expect(result.stdout).toContain("--full-output"); - expect(result.stdout).toContain("current runtime"); - expect(result.stdout).toContain( - "registered command or launch environment", - ); - // Help returns before the diagnostic session starts, so no report - // envelope and no startup diagnostics are produced. - expect(result.stdout).not.toContain("healthy"); - expect(result.stderr).toBe(""); - } - }, - ); - - workspaceCliTest( - "answers help in option position regardless of surrounding output options", - async ({ cli }) => { - for (const arguments_ of [ - ["mcp", "doctor", "--json", "--help"], - ["mcp", "doctor", "--bogus", "--help"], - ["mcp", "doctor", "--format", "bogus", "--help"], - ["mcp", "doctor", "--help", "--format", "json"], - ]) { - const result = await cli.run({ arguments: arguments_ }); - expect(result).toMatchObject({ exitCode: 0 }); - expect(result.stdout).toContain("Usage: rea mcp doctor [options]"); - expect(result.stdout).toContain("--help, -h"); - expect(result.stdout).not.toContain("healthy"); - } - }, - ); - - workspaceCliTest( - "treats help consumed as --format value as an invalid format, not help", - async ({ cli }) => { - for (const arguments_ of [ - ["mcp", "doctor", "--format", "--help"], - ["mcp", "doctor", "--format", "-h"], - ["mcp", "doctor", "--format=--help"], - ]) { - const result = await cli.run({ arguments: arguments_ }); - expect(result).toMatchObject({ exitCode: 1 }); - expect(result.stdout).toContain("Invalid output format:"); - expect(result.stdout).not.toContain("Usage: rea mcp doctor"); - } - }, - ); - - workspaceCliTest( - "keeps the actionable error for a genuinely unknown option", - async ({ cli }) => { - const result = await cli.run({ arguments: ["mcp", "doctor", "--bogus"] }); - expect(result).toMatchObject({ - exitCode: 1, - stdout: - 'code: VALIDATION_ERROR\nmessage: "Unknown mcp doctor option: --bogus"\n', - }); - const invalid = await cli.run({ - arguments: ["mcp", "doctor", "--format", "bogus"], - }); - expect(invalid).toMatchObject({ - exitCode: 1, - stdout: - 'code: VALIDATION_ERROR\nmessage: "Invalid output format: bogus"\n', - }); - }, - ); - - workspaceCliTest( - "reports an absent or empty --format value as a missing output value", - async ({ cli }) => { - const message = - "--format requires a value, as in --format or --format=."; - for (const arguments_ of [ - ["mcp", "doctor", "--format"], - ["mcp", "doctor", "--format="], - ]) { - const result = await cli.run({ arguments: arguments_ }); - expect(result).toMatchObject({ - exitCode: 1, - stdout: `code: MISSING_OUTPUT_VALUE\nmessage: "${message}"\n`, - }); - } - for (const arguments_ of [ - ["mcp", "doctor", "--json", "--format"], - ["mcp", "doctor", "--json", "--format="], - ]) { - const result = await cli.run({ arguments: arguments_ }); - expect(result.exitCode).toBe(1); - expect(JSON.parse(result.stdout)).toEqual({ - code: "MISSING_OUTPUT_VALUE", - message, - }); - } - }, - ); -}); - -describe("MCP doctor timing boundary", () => { - workspaceCliTest( - "reports doctor timing on stderr while preserving JSON output", - async ({ cli }) => { - const result = await cli.run({ - arguments: ["mcp", "doctor", "--json", "--timings"], - }); - expect([0, 1]).toContain(result.exitCode); - expect(JSON.parse(result.stdout)).toHaveProperty("healthy"); - const lines = result.stderr.trim().split("\n"); - expect(lines).toHaveLength(1); - const record: unknown = JSON.parse(lines[0]!); - expect(record).toMatchObject({ - rea_timings: { version: 1, command_ms: expect.any(Number) }, - }); - const duration = (record as { rea_timings: { command_ms: number } }) - .rea_timings.command_ms; - expect(Number.isFinite(duration)).toBe(true); - expect(duration).toBeGreaterThanOrEqual(0); - }, - ); - - workspaceCliTest( - "does not treat a --format value as the timing flag", - async ({ cli }) => { - const result = await cli.run({ - arguments: ["mcp", "doctor", "--format", "--timings"], - }); - expect(result.exitCode).toBe(1); - expect(result.stdout).toContain("Invalid output format: --timings"); - expect(result.stderr).toBe(""); - }, - ); -}); - -workspaceCliTest( - "accepts the same timing spellings as the ordinary CLI", +cliTest( + "diagnoses flag-shaped format values without activating the flags", async ({ cli }) => { - for (const [flag, enabled] of [ - ["--no-timings", false], - ["--timings=false", false], - ["--timings=true", true], - ["--timings=other", false], - ["--timings=", false], - ] as const) { + for (const [value, ...format] of [ + ["--help", "--format=--help"], + ["-h", "--format", "-h"], + ["--timings", "--format", "--timings"], + ["--full-output", "--format", "--full-output"], + ]) { const result = await cli.run({ - arguments: ["mcp", "doctor", "--json", flag], - environment: { REA_MCP_DOCTOR_DEADLINE_MS: "0" }, + arguments: ["mcp", "doctor", ...format, "--json"], }); - expect(result.exitCode, result.stdout).toBe(1); - expect(JSON.parse(result.stdout)).toMatchObject({ - healthy: false, - adapter: "production-stdio", + expect(result, value).toMatchObject({ exitCode: 1, stderr: "" }); + expect(result.json, value).toEqual({ + code: "VALIDATION_ERROR", + message: `Invalid output format: ${value}`, }); - if (enabled) - expect(JSON.parse(result.stderr)).toMatchObject({ - rea_timings: { version: 1, command_ms: expect.any(Number) }, - }); - else expect(result.stderr).toBe(""); } }, ); + +cliTest( + "writes opt-in timing to stderr and leaves production doctor JSON unchanged", + async ({ cli }) => { + const args = ["mcp", "doctor", "--json", "--timings"]; + const timed = await cli.run({ + arguments: args, + environment: invalidDeadline, + }); + const disabled = await cli.run({ + arguments: [...args, "--no-timings"], + environment: invalidDeadline, + }); + expect(timed).toMatchObject({ exitCode: 1, json: deadlineReport }); + expect(disabled).toMatchObject({ + exitCode: timed.exitCode, + stdout: timed.stdout, + stderr: "", + }); + const lines = timed.stderr.trim().split("\n"); + expect(lines).toHaveLength(1); + const record = JSON.parse(lines[0]!) as { + rea_timings: { version: number; command_ms: number }; + }; + expect(record).toMatchObject({ + rea_timings: { version: 1, command_ms: expect.any(Number) }, + }); + expect(Number.isFinite(record.rea_timings.command_ms)).toBe(true); + expect(record.rea_timings.command_ms).toBeGreaterThanOrEqual(0); + }, +);