linearis icon indicating copy to clipboard operation
linearis copied to clipboard

Argument-parse errors bypass the JSON output contract and give agents no recovery path

Open JulienJBO opened this issue 4 months ago • 2 comments

Summary

When a command is malformed, linearis emits a plain-text error on stderr, while every valid-command error emits the documented JSON envelope. Both exit with code 1. An LLM agent therefore cannot distinguish "I invoked the CLI wrong" from "the entity does not exist" — neither by parsing stdout/stderr nor by exit code — and the message contains no pointer to the correct command.

This is filed against the error channel, not as a request for command aliases (see "Relationship to #48" below).

Measured on linearis 2026.6.0 (commander 14.0.3), macOS, Node 24.18.0.

Measured behaviour

$ linearis issues read ABC-123          # valid command, entity absent
{
  "error": "Issue with identifier \"ABC-123\" not found"
}                                        # stderr, JSON, exit 1

$ linearis issues get ABC-123           # unknown subcommand
error: too many arguments for 'issues'. Expected 0 arguments but got 2.
                                         # stderr, plain text, exit 1

$ linearis issue read ABC-123           # unknown domain
error: too many arguments. Expected 0 arguments but got 3.
                                         # stderr, plain text, exit 1

Two problems:

  1. Contract violation. AGENTS.md states linearis "outputs JSON only (except help/usage text)", and docs/development.md documents the error contract as { "error": "message" } on stderr. Parse errors are neither JSON nor help/usage text.
  2. Exit-code collision. 1 for both malformed invocation and legitimate application error, so the two are indistinguishable programmatically. Contrast outputAuthError, which already uses a distinct code (42) precisely to make a class of error detectable.

Root cause

src/main.ts registers an action handler on the root program, and domain commands do the same:

program.action(() => console.log(formatOverview(pkg.version, allMetas)));

With an action handler present and zero declared arguments, Commander routes an unrecognised token to the excess-arguments path instead of unknownCommand(). Consequence: .showSuggestionAfterError() can never fire — it hooks onto unknownCommand() / unknownOption(). Simply enabling that option would change nothing.

Standalone Commander 14.0.3 repro, same version as shipped:

### with action handlers (current linearis shape)
  issue read X     -> error: too many arguments. Expected 0 arguments but got 3.
  issues get X     -> error: too many arguments for 'issues'. Expected 0 arguments but got 2.
  issues raed X    -> error: too many arguments for 'issues'. Expected 0 arguments but got 2.

### without action handlers
  issue read X     -> error: unknown command 'issue' / (Did you mean issues?)
  issues get X     -> error: unknown command 'get'
  issues raed X    -> error: unknown command 'raed' / (Did you mean read?)

Note the second column: even with suggestions active, get → read yields no hint (edit distance too large). Only typos are covered.

Relationship to #48

#48 ("Add command aliases for common alternative names") was closed NOT_PLANNED. This issue does not re-litigate aliases — that call is the maintainer's and is respected here. It targets the part of #48 that was never separately addressed: the error output itself, plus the JSON-contract angle that #48 did not raise.

Suggested direction (for discussion)

Intercept at the parse boundary rather than relying on Commander's own messaging:

  • program.exitOverride() + try/catch around program.parseAsync()
  • On a CommanderError, emit a JSON envelope mirroring the existing outputAuthError shape in src/common/output.ts, with a distinct exit code
  • Optionally include the valid command names for that scope — allMetas and program.commands already expose them at runtime, and src/common/usage.ts already formats them

This stays inside the existing output-layer pattern, restores the JSON contract, and does not depend on Commander's suggestion feature firing.

Happy to open a PR if the direction is agreeable.

JulienJBO avatar Jul 20 '26 11:07 JulienJBO

LIN-156

linear[bot] avatar Jul 20 '26 11:07 linear[bot]

Thanks for raising this — the writeup did most of the diagnostic work for us. Pinning it on the parse boundary, and specifically on _excessArguments() firing instead of unknownCommand() because the root and every domain register an action handler with zero declared arguments, was exactly right. That also explained why .showSuggestionAfterError() would have been a no-op, which saved us from shipping a fix that did nothing.

How it was addressed

Fixed in #288 (merged into next), essentially along the direction you proposed. Every malformed invocation now emits a JSON envelope on stderr and exits 2:

{
  "error": "UNKNOWN_COMMAND",
  "message": "Unknown command \"get\" for \"linearis issues\".",
  "command": "linearis issues",
  "available_commands": ["relations", "list", "search", "read", "…", "usage"],
  "instruction": "Run 'linearis issues usage' to list valid subcommands.",
  "exit_code": 2
}

Codes: UNKNOWN_COMMAND, UNKNOWN_OPTION, MISSING_ARGUMENT, TOO_MANY_ARGUMENTS, INVALID_USAGE.

Details worth calling out:

  • Per-command exitOverride. Rather than a single try/catch around parseAsync(), we walk the finished command tree and install an exitOverride on each command, so the callback closes over the command that actually failed. That yields the exact failing scope without re-parsing argv (the single-catch variant needs option-arity heuristics to tell an option value from a subcommand name).
  • available_commands comes from cmd.commands — the same source formatDomainUsage reads, so the recovery hint cannot drift from USAGE.md.
  • Exit code 2 is the conventional Unix usage-error code, and it keeps 1 meaning "well-formed request that could not be fulfilled". 42 for auth is unchanged.
  • No behaviour was removed. Bare linearis, linearis issues, --help and --version still print to stdout with exit 0, byte-identical to before.
  • Along the way it also fixed an unreported defect on the same line: program.parseAsync() was neither awaited nor caught, so a rejection during parse surfaced as a raw Node unhandled-rejection stack trace.

The contract is documented in README.md and docs/development.md, and the bundled agent skill now tells agents to branch on exit 2 vs 1.

Trying the pre-release

It is on the next dist-tag, in 2026.7.0-next.4:

npm install -g linearis@next
# or, without installing:
npx linearis@next issues get ABC-123

Quick check:

linearis issues get ABC-123 ; echo "exit=$?"   # JSON UNKNOWN_COMMAND, exit=2
linearis issues read         ; echo "exit=$?"  # JSON MISSING_ARGUMENT, exit=2
linearis issues list --bogus ; echo "exit=$?"  # JSON UNKNOWN_OPTION,   exit=2
linearis issues read ABC-123 ; echo "exit=$?"  # {"error": "… not found"}, exit=1 (unchanged)

Feedback wanted

Before this goes to the stable channel, it would help to hear from you — especially on:

  1. Whether the envelope carries what your agent loop actually needs to recover, or whether a field is missing.
  2. Whether available_commands truncation on wide domains (issues) is a problem in practice.
  3. Any invocation that still escapes as plain text or exits 1 when it should be 2.

Reopen this issue or file a new one if you hit anything — and thanks again for the level of detail, including the standalone Commander repro.

iamfj avatar Aug 06 '26 10:08 iamfj