From c5cbb212d7a3e370ffbd47df9fcb9d1393cae0ab Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Sat, 28 Nov 2026 07:12:36 +0000 Subject: [PATCH] fix(cli): report argv parse failures as PARSE_ERROR Unknown flags, missing flag values and surplus positional arguments reported the catch-all UNKNOWN code, while a misspelled flag already reported PARSE_ERROR. Every argv parse failure now reports PARSE_ERROR with retryable false; UNKNOWN stays reserved for unexpected failures. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- src/cli/argumentErrors.ts | 7 ++++--- tests/acceptance/jsonLinesCli.test.ts | 6 +++++- .../boundary/cli/cliLazyRegistration.test.ts | 3 ++- .../cli/explicitSensitiveArgumentsCli.test.ts | 20 ++++++++++--------- tests/boundary/cli/jsonLinesOutput.test.ts | 12 +++++++---- .../cli-parity/error-extra-positional.txt | 3 ++- .../cli-parity/error-missing-flag-value.txt | 3 ++- .../cli-parity/error-sensitive-positional.txt | 3 ++- tests/fixtures/cli-parity/error-separator.txt | 3 ++- .../cli-parity/error-unknown-flag.txt | 3 ++- .../terminal-error-unknown-flag.txt | 2 +- 11 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/cli/argumentErrors.ts b/src/cli/argumentErrors.ts index 8928ad5eaa3..1f306107544 100644 --- a/src/cli/argumentErrors.ts +++ b/src/cli/argumentErrors.ts @@ -49,16 +49,17 @@ export const cliArgumentFailure = ( command: string, failure: CliArgvFailure, ): CliCommandFailure => { - if (failure.kind === "dry_run" || failure.kind === "flag_spelling") - return { code: "PARSE_ERROR", message: failure.message, retryable: false }; + // Unknown flags, missing values and surplus positionals are argv parse + // failures like a misspelled flag; UNKNOWN stays reserved for unexpected errors. if (failure.kind !== "validation") return { - code: "UNKNOWN", + code: "PARSE_ERROR", message: failure.message + (failure.kind === "surplus" ? ` Use '${cli} ${command} --help' for the declared argument shape.` : ""), + retryable: false, }; return { code: "VALIDATION_ERROR", diff --git a/tests/acceptance/jsonLinesCli.test.ts b/tests/acceptance/jsonLinesCli.test.ts index 31f86af756e..cdab76c0073 100644 --- a/tests/acceptance/jsonLinesCli.test.ts +++ b/tests/acceptance/jsonLinesCli.test.ts @@ -217,7 +217,11 @@ for (const full of [false, true]) { }), ], } - : { code: "UNKNOWN", message: "Unknown flag: --zzz-bogus" }; + : { + code: "PARSE_ERROR", + message: "Unknown flag: --zzz-bogus", + retryable: false, + }; expect(JSON.parse(invalid.stdout)).toMatchObject({ $rea: "jsonl/v1", type: "error", diff --git a/tests/boundary/cli/cliLazyRegistration.test.ts b/tests/boundary/cli/cliLazyRegistration.test.ts index 345f497da3b..0356ad21376 100644 --- a/tests/boundary/cli/cliLazyRegistration.test.ts +++ b/tests/boundary/cli/cliLazyRegistration.test.ts @@ -237,8 +237,9 @@ describe("lazy CLI execution", () => { expect(result.stderr, name).not.toContain(COMPLETE_CLI_IMPORT); const diagnostic: unknown = JSON.parse(result.stdout); expect(diagnostic, name).toMatchObject({ - code: "UNKNOWN", + code: "PARSE_ERROR", message: expect.stringContaining("--rea-test-unknown-option"), + retryable: false, }); diagnostics.set(name, diagnostic); }), diff --git a/tests/boundary/cli/explicitSensitiveArgumentsCli.test.ts b/tests/boundary/cli/explicitSensitiveArgumentsCli.test.ts index aca0601ed1e..fdddaa7cf47 100644 --- a/tests/boundary/cli/explicitSensitiveArgumentsCli.test.ts +++ b/tests/boundary/cli/explicitSensitiveArgumentsCli.test.ts @@ -45,7 +45,6 @@ describe("CLI --sensitive-value parser diagnostics", () => { // The parser refuses the schema key spelling; its value stays redacted. message: "Unknown flag: --sensitiveValue. Did you mean --sensitive-value?", - failure: { code: "PARSE_ERROR", retryable: false }, }, { // Incur removes builtin output flags before reading the declared value. @@ -62,14 +61,15 @@ describe("CLI --sensitive-value parser diagnostics", () => { }, ])( "excludes declarations from a JSON $name rejection", - async ( - { arguments: arguments_, message, failure = { code: "UNKNOWN" } }, - { cli }, - ) => { + async ({ arguments: arguments_, message }, { cli }) => { const result = await cli.run({ arguments: [...arguments_, "--json"] }); expectExcluded(result); - expect(JSON.parse(result.stdout)).toEqual({ ...failure, message }); + expect(JSON.parse(result.stdout)).toEqual({ + code: "PARSE_ERROR", + message, + retryable: false, + }); }, ); @@ -80,7 +80,7 @@ describe("CLI --sensitive-value parser diagnostics", () => { arguments: [ ...CAPTURE, "har", - ...["flag", "code", "UNKNOWN"].flatMap((literal) => [ + ...["flag", "code", "PARSE_ERROR"].flatMap((literal) => [ "--sensitive-value", literal, ]), @@ -91,8 +91,9 @@ describe("CLI --sensitive-value parser diagnostics", () => { expect(result.exitCode).toBe(1); expect(JSON.parse(result.stdout)).toEqual({ - code: "UNKNOWN", + code: "PARSE_ERROR", message: "Unknown [REDACTED]: --no-x", + retryable: false, }); }, ); @@ -129,8 +130,9 @@ describe("CLI --sensitive-value parser diagnostics", () => { }); expect(undeclared.exitCode).toBe(1); expect(JSON.parse(undeclared.stdout)).toEqual({ - code: "UNKNOWN", + code: "PARSE_ERROR", message: `Unknown flag: --no-${LITERAL}`, + retryable: false, }); // An accepted output value is honored, so the service failure is JSON. diff --git a/tests/boundary/cli/jsonLinesOutput.test.ts b/tests/boundary/cli/jsonLinesOutput.test.ts index 45d514e3f6f..c1cc95f0231 100644 --- a/tests/boundary/cli/jsonLinesOutput.test.ts +++ b/tests/boundary/cli/jsonLinesOutput.test.ts @@ -506,10 +506,14 @@ describe("Command-owned JSON Lines parser diagnostics", () => { }); await stream.output.finish(); expect(status).toBe(1); - const diagnostic = { - code: extra.length === 0 ? "VALIDATION_ERROR" : "UNKNOWN", - message: expect.any(String), - }; + const diagnostic = + extra.length === 0 + ? { code: "VALIDATION_ERROR", message: expect.any(String) } + : { + code: "PARSE_ERROR", + message: expect.any(String), + retryable: false, + }; expect(lines(stream.text())).toEqual([ expect.objectContaining({ $rea: "jsonl/v1", diff --git a/tests/fixtures/cli-parity/error-extra-positional.txt b/tests/fixtures/cli-parity/error-extra-positional.txt index e45dc5a2193..6045fbd529e 100644 --- a/tests/fixtures/cli-parity/error-extra-positional.txt +++ b/tests/fixtures/cli-parity/error-extra-positional.txt @@ -1,8 +1,9 @@ $ rea inspect-byte-entropy /sample.bin extra exit: 1 --- stdout -code: UNKNOWN +code: PARSE_ERROR message: "Unexpected positional arguments: \"extra\". Use 'rea inspect-byte-entropy --help' for the declared argument shape." +retryable: false --- stderr diff --git a/tests/fixtures/cli-parity/error-missing-flag-value.txt b/tests/fixtures/cli-parity/error-missing-flag-value.txt index ace92157d27..da37cee77fa 100644 --- a/tests/fixtures/cli-parity/error-missing-flag-value.txt +++ b/tests/fixtures/cli-parity/error-missing-flag-value.txt @@ -1,8 +1,9 @@ $ rea inspect-byte-entropy /sample.bin --block-size exit: 1 --- stdout -code: UNKNOWN +code: PARSE_ERROR message: "Missing value for flag: --block-size" +retryable: false --- stderr diff --git a/tests/fixtures/cli-parity/error-sensitive-positional.txt b/tests/fixtures/cli-parity/error-sensitive-positional.txt index 6e22c1892bc..8b69fc6eae0 100644 --- a/tests/fixtures/cli-parity/error-sensitive-positional.txt +++ b/tests/fixtures/cli-parity/error-sensitive-positional.txt @@ -1,8 +1,9 @@ $ rea inspect-web-network-capture /capture.har har secret-literal --sensitive-value secret-literal exit: 1 --- stdout -code: UNKNOWN +code: PARSE_ERROR message: "Unexpected positional arguments: \"[REDACTED]\". Use 'rea inspect-web-network-capture --help' for the declared argument shape." +retryable: false --- stderr diff --git a/tests/fixtures/cli-parity/error-separator.txt b/tests/fixtures/cli-parity/error-separator.txt index 7ff270c2adc..a04c0af3b6f 100644 --- a/tests/fixtures/cli-parity/error-separator.txt +++ b/tests/fixtures/cli-parity/error-separator.txt @@ -1,8 +1,9 @@ $ rea inspect-byte-entropy /sample.bin -- --block-size exit: 1 --- stdout -code: UNKNOWN +code: PARSE_ERROR message: "Unknown flag: --" +retryable: false --- stderr diff --git a/tests/fixtures/cli-parity/error-unknown-flag.txt b/tests/fixtures/cli-parity/error-unknown-flag.txt index d56ccf66372..a3e619d43fd 100644 --- a/tests/fixtures/cli-parity/error-unknown-flag.txt +++ b/tests/fixtures/cli-parity/error-unknown-flag.txt @@ -1,8 +1,9 @@ $ rea inspect-byte-entropy /sample.bin --no-such-flag exit: 1 --- stdout -code: UNKNOWN +code: PARSE_ERROR message: "Unknown flag: --no-such-flag" +retryable: false --- stderr diff --git a/tests/fixtures/cli-parity/terminal-error-unknown-flag.txt b/tests/fixtures/cli-parity/terminal-error-unknown-flag.txt index 441ec42ed53..f0798162eb7 100644 --- a/tests/fixtures/cli-parity/terminal-error-unknown-flag.txt +++ b/tests/fixtures/cli-parity/terminal-error-unknown-flag.txt @@ -1,7 +1,7 @@ $ rea inspect-byte-entropy /sample.bin --no-such-flag # stdout is a terminal exit: 1 --- stdout -Error: Unknown flag: --no-such-flag +Error (PARSE_ERROR): Unknown flag: --no-such-flag --- stderr