diff --git a/src/cli/discovery.ts b/src/cli/discovery.ts index 20e04c1eb8..67fd581585 100644 --- a/src/cli/discovery.ts +++ b/src/cli/discovery.ts @@ -12,6 +12,7 @@ import { CLI_OUTPUT_FORMATS, readCliOutputArguments, renderCliDocument, + renderCliOutputArgumentError, type CliOutputFormat, } from "../cliOutput.js"; import { formatCliOutput } from "./cliOutputFormatter.js"; @@ -227,7 +228,9 @@ const llmsAnswer = ( global: CliGlobalArguments, full: boolean, ): DiscoveryAnswer => { - const { format, formatExplicit } = readCliOutputArguments(global); + const { format, formatExplicit, validation } = readCliOutputArguments(global); + if (!validation.ok) + return renderedAnswer(renderCliOutputArgumentError(validation), 1); const scoped = cliCommands(cli).find( ({ name }) => name === global.command[0], ); @@ -269,7 +272,9 @@ const schemaAnswer = ( }); return answer(`Error: ${failure.message}`, 1); } - const { format, formatExplicit } = readCliOutputArguments(global); + const { format, formatExplicit, validation } = readCliOutputArguments(global); + if (!validation.ok) + return renderedAnswer(renderCliOutputArgumentError(validation), 1); return documentAnswer( commandSchemaDocument(entry, CLI_CUSTOM_GLOBAL_OPTIONS), formatExplicit ? format : "toon", diff --git a/src/cliOutput.ts b/src/cliOutput.ts index 9cd430e938..d08e12ac3d 100644 --- a/src/cliOutput.ts +++ b/src/cliOutput.ts @@ -221,7 +221,7 @@ export const readCliOutputArguments = ( }; return { ...controls, - validation: validateCliOutputControls(controls, rootVersion), + validation: validateCliOutputControls(controls, rootVersion, global), versionReportFormat: rootVersion && versionEligible && @@ -240,6 +240,20 @@ export const readCliOutputArguments = ( export const missingCliOutputValueMessage = (flag: string): string => `${flag} requires a value, as in ${flag} or ${flag}=.`; +const discoveryRowFormatFlag = ( + global: CliGlobalArguments, +): "--llms" | "--llms-full" | "--schema" | undefined => { + const { command, flags } = global; + const help = flags.has("--help") || flags.has("-h"); + if ((flags.has("--update") && !help) || flags.has("--mcp")) return undefined; + if (flags.has("--llms-full")) return "--llms-full"; + if (flags.has("--llms")) return "--llms"; + if (flags.has("--version") && !help) return undefined; + if (command.length === 0 || help) return undefined; + if (flags.has("--schema")) return "--schema"; + return undefined; +}; + /** Reject output controls that cannot safely produce a complete result. */ const validateCliOutputControls = ( { @@ -253,6 +267,7 @@ const validateCliOutputControls = ( tokenCount, }: CliOutputControls, rootVersion: boolean, + global: CliGlobalArguments, ): CliOutputArgumentValidation => { if (invalidValue !== undefined) return { @@ -279,6 +294,14 @@ const validateCliOutputControls = ( code: "MISSING_OUTPUT_VALUE", message: missingCliOutputValueMessage(missingValue), }; + const discoveryFlag = delimited ? discoveryRowFormatFlag(global) : undefined; + if (delimited && discoveryFlag !== undefined) + return { + ok: false, + format: "json", + code: "UNSUPPORTED_OUTPUT_COMBINATION", + message: `${discoveryFlag} returns a complete document and does not support --format ${format}. Use --format json, yaml, md, toon, or jsonl; CSV/TSV output is available only for selected row collections.`, + }; if (delimited && rootVersion) return { ok: false, diff --git a/tests/boundary/cli/builtinDispatch.test.ts b/tests/boundary/cli/builtinDispatch.test.ts index ca9dce5aa7..abe3a47878 100644 --- a/tests/boundary/cli/builtinDispatch.test.ts +++ b/tests/boundary/cli/builtinDispatch.test.ts @@ -188,3 +188,51 @@ cliTest( expect(reply.stdout).toContain("MCP_MODE_REMOVED"); }, ); + +it.for([ + [["--llms", "--format", "csv"], "--llms", "csv"], + [["--llms", "--format", "tsv"], "--llms", "tsv"], + [["--llms-full", "--format", "csv"], "--llms-full", "csv"], + [["--llms-full", "--format", "tsv"], "--llms-full", "tsv"], + [["fixture", "--schema", "--format", "csv"], "--schema", "csv"], + [["fixture", "--schema", "--format", "tsv"], "--schema", "tsv"], +] as const)( + "refuses row-only output formats on discovery manifests and schemas: %j", + async ([arguments_, flag, format]) => { + const reply = await serveInProcessCli(shell(), arguments_); + expect(reply.exitCode).toBe(1); + expect(JSON.parse(reply.stdout)).toMatchObject({ + ok: false, + error: { + code: "UNSUPPORTED_OUTPUT_COMBINATION", + message: expect.stringMatching( + new RegExp(`${flag}.*--format ${format}`, "u"), + ), + }, + }); + }, +); + +cliTest( + "refuses bare CSV/TSV discovery requests through the public CLI without suggesting row flags", + async ({ cli }) => { + for (const [arguments_, flag, format] of [ + [["--llms", "--format", "csv"], "--llms", "csv"], + [["--llms-full", "--format", "tsv"], "--llms-full", "tsv"], + [["doctor", "--schema", "--format", "csv"], "--schema", "csv"], + ] as const) { + const reply = await cli.run({ arguments: arguments_ }); + expect(reply.exitCode).toBe(1); + expect(reply.json).toMatchObject({ + ok: false, + error: { + code: "UNSUPPORTED_OUTPUT_COMBINATION", + message: expect.stringMatching( + new RegExp(`${flag}.*--format ${format}`, "u"), + ), + }, + }); + expect(reply.stdout).not.toContain("--filter-output"); + } + }, +);