Skip to content

fix: tighten CLI argument parsing and usage error classification #58

Closed

Description

@abruption

What happened

Several argument-parsing paths produce results that contradict the documented CLI/JSON contract:

  1. Invalid thread ids are reported as command_failed/3 instead of invalid_usage/64. parseArguments checks only that a thread id is present; format validation happens later in validateThreadId and the thrown error reaches the generic command_failed handler. Example: inspect ../../etc --json → exit 3, errorCode: "command_failed", suggestedAction: null. (Path traversal itself is blocked by the UUID pattern.)
  2. -h/-v are matched anywhere in argv. --json inspect --codex-home -h <id> prints the plain-text help and exits 0; --json --version prints plain 0.4.0 on stdout.
  3. Option values starting with - are accepted. list --codex-home --json sets the home to <cwd>/--json and exits 0 with non-JSON output.
  4. command is null for identified commands in usage errors. requestedCommand only inspects argv[0], so --json check-update --codex-home x reports command: null, although docs say null means the command could not be identified.
  5. --help crashes on EPIPE. process.stdout.write(HELP) has no error handling; with a closed stdout the process exits 1 with an unhandled 'error' stack trace.

What you expected

  • Malformed thread ids are invalid_usage with exit 64 and a suggested action.
  • --json consumers never receive non-JSON stdout with exit 0 due to a flag appearing as an option value.
  • Option values that look like flags are usage errors.
  • command is set whenever the command token was recognised.
  • Help output on a closed pipe exits cleanly.

Environment

  • codex-unlock 0.4.0 at 6227e6c
  • macOS 27.0 arm64, Node.js 22.14.0

Source evidence

  • Global -h/-v scan:

    codex-unlock/src/cli.ts

    Lines 111 to 113 in 6227e6c

    function parseArguments(argv: string[]): ParsedArguments | "help" | "version" {
    if (argv.includes("--help") || argv.includes("-h")) return "help";
    if (argv.includes("--version") || argv.includes("-v")) return "version";
  • --codex-home consumes the next token unchecked:

    codex-unlock/src/cli.ts

    Lines 126 to 130 in 6227e6c

    } else if (argument === "--codex-home") {
    doctorOptionSeen = true;
    const value = argv[++index];
    if (!value) throw new Error("--codex-home requires a path");
    options.codexHome = resolve(value);
  • Thread id presence-only check:

    codex-unlock/src/cli.ts

    Lines 157 to 161 in 6227e6c

    const threadId = positional.shift();
    if (!threadId || positional.length > 0) {
    throw new Error(`${command} requires exactly one thread id`);
    }
    return { command, threadId, json, noUpdateNotice, options };
  • Validation throws later:
    const threadId = validateThreadId(rawThreadId);
    ,
    const threadId = validateThreadId(rawThreadId);
  • Generic command_failed path:

    codex-unlock/src/cli.ts

    Lines 387 to 404 in 6227e6c

    } catch (error) {
    if (parsed.json) {
    console.log(
    JSON.stringify(
    withClientUpdate(
    cliError(parsed.command, "command_failed", 3, errorText(error)),
    advisory.clientUpdate,
    ),
    null,
    2,
    ),
    );
    } else {
    console.error(`codex-unlock: ${errorText(error)}`);
    }
    finishAutomaticAdvisory(advisory);
    return 3;
    }
  • requestedCommand uses argv[0] only:

    codex-unlock/src/cli.ts

    Lines 64 to 69 in 6227e6c

    function requestedCommand(argv: string[]): CommandName | null {
    const candidate = argv[0];
    return candidate === "list" || candidate === "inspect" || candidate === "unlock" || candidate === "check-update"
    ? candidate
    : null;
    }
  • Unhandled write:

    codex-unlock/src/cli.ts

    Lines 341 to 343 in 6227e6c

    if (parsed === "help") {
    process.stdout.write(HELP);
    return 0;
  • Exit code contract:
    | `3` | Termination/post-check failure or an unexpected command failure | `unlock` failure outcome or `errorCode: "command_failed"` |
    | `64` | Invalid command-line usage | `errorCode: "invalid_usage"` |

Acceptance criteria

  • Thread ids are validated during parsing and produce invalid_usage/64 with a suggested action.
  • Help/version are recognised only as standalone requests (not as option values), and with --json either produce JSON or a usage error.
  • Option values beginning with - are rejected as invalid_usage.
  • command is populated when a known command token is present anywhere in argv.
  • EPIPE on stdout exits without a stack trace.
  • Contract tests cover each case.

Scope and limitations

  • Reliability/contract issues only; none of these paths can send a signal.

Checked

Activity

  1. added
    bug
    on Sep 28, 2026
  2. added this to the v0.4.1 milestone on Sep 28, 2026
  3. added a commit that references this issue on Sep 29, 2026
    a15d32e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions