Repository navigation
Description
What happened
Several argument-parsing paths produce results that contradict the documented CLI/JSON contract:
- Invalid thread ids are reported as
command_failed/3 instead ofinvalid_usage/64.parseArgumentschecks only that a thread id is present; format validation happens later invalidateThreadIdand the thrown error reaches the genericcommand_failedhandler. Example:inspect ../../etc --json→ exit 3,errorCode: "command_failed",suggestedAction: null. (Path traversal itself is blocked by the UUID pattern.) -h/-vare matched anywhere in argv.--json inspect --codex-home -h <id>prints the plain-text help and exits 0;--json --versionprints plain0.4.0on stdout.- Option values starting with
-are accepted.list --codex-home --jsonsets the home to<cwd>/--jsonand exits 0 with non-JSON output. commandisnullfor identified commands in usage errors.requestedCommandonly inspectsargv[0], so--json check-update --codex-home xreportscommand: null, although docs saynullmeans the command could not be identified.--helpcrashes 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_usagewith exit 64 and a suggested action. --jsonconsumers 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.
commandis set whenever the command token was recognised.- Help output on a closed pipe exits cleanly.
Environment
codex-unlock0.4.0 at6227e6c- macOS 27.0 arm64, Node.js 22.14.0
Source evidence
- Global
-h/-vscan: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-homeconsumes the next token unchecked: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:
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: ,
codex-unlock/src/inspection.ts
Line 75 in 6227e6c
const threadId = validateThreadId(rawThreadId); Line 155 in 6227e6c
const threadId = validateThreadId(rawThreadId); - Generic
command_failedpath: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; } requestedCommandusesargv[0]only: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:
Lines 341 to 343 in 6227e6c
if (parsed === "help") { process.stdout.write(HELP); return 0; - Exit code contract:
Lines 131 to 132 in 6227e6c
| `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
--jsoneither produce JSON or a usage error. - Option values beginning with
-are rejected asinvalid_usage. commandis 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
- feat: publish a stable JSON error and exit-code compatibility contract #25 / feat: define the CLI JSON integration contract #31 define the JSON error and exit-code contract; no existing issue covers these cases.
Activity
fix: validate CLI usage and Codex home before inspection