refactor(ghost): consistent CLI exit-code contract#198
Draft
nahiyankhan wants to merge 1 commit into
Draft
Conversation
Normalize exit codes so an agent can branch on them. Unexpected errors exit 1; caller mistakes exit 2. The wrinkle was that several genuine usage errors are surfaced by throwing from deep helpers (path validation, byte budgets, overwrite guards, bad --agent/--template), so a flat catch code mislabels them. Introduce a typed UsageError (ghost-core) carrying exitCode 2 and a shared failFromError() that exits with a thrown error's exitCode or 1 otherwise, then route every command catch through it. A missing package now throws UsageError with a 'ghost init' hint instead of leaking a raw ENOENT, and the exit-code contract is documented in the CLI reference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on
ghost/08-search-and-grounding.A focused cleanup of the CLI exit-code contract, prompted by the gather-ranking work in #197: an agent that branches on exit codes needs them to mean one thing.
The problem
The per-command
catchblocks had drifted — the same "something threw" event exited1ingather/checks/migratebut2ininit/skill/review/fingerprint. Worse, several genuine usage errors are surfaced bythrowfrom deep helpers (path validation,--max-diff-bytes, overwrite guards, bad--agent/--template), so any single catch code mislabels them: flat1calls a bad flag a crash; flat2calls a crash a usage error.The fix
UsageError(inghost-core, reachable from thescanlayer) carriesexitCode: 2. The genuine "you called it wrong" throw sites now throw it.failFromError()(shared) reports the message and exits with the error'sexitCode, or1otherwise. Every command catch routes through it, so the contract can't drift again.UsageErrorwith aghost inithint (exit2) instead of leaking a rawENOENT.Net contract:
0success ·1ran-but-unhappy (validate findings, or unexpected error) ·2called-wrong ·3command-specific refusal (skill already installed). Documented as a table in the CLI reference, including the deliberate split where reporting commands (validate/scan/signals) treat a missing package as a state to report rather than a usage error.Verification
pnpm test(115 passing; +2 contract tests: a thrown usage error exits 2, a missing package exits 2 with guidance)pnpm checkgreenpatchchangeset added.