exit-code.ts
This file
Path
packages/protocol/src/exit-code.ts
120 lines · 4.1 KiB · TypeScript
Last changed by
Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
History of this fileTake it
Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 1–120why, one step back1/**2 * How the CLI's process ends, from sysexits(3), so a script or an agent can3 * tell what to do next from the exit code alone. A refusal's code follows4 * from its fixed `retryable`: a retryable one is `tempFail`, since running5 * its `next` and trying again can pass; a final one says what has to6 * change, which `final` fixes per tag: what was sent, what was named, or7 * who is asking.8 *9 * The CLI's projection of the union, kept apart from `cli.ts` so the10 * reference docs read it without effect/cli. A tag's code is public: change11 * one only as a tag is renamed, with notice.12 */13import type { Array } from "effect";1415import type { Refusal } from "./refusal.ts";1617export const ExitCode = {18 /** `EX_DATAERR`: what was sent was refused; change it, then try again. */19 dataErr: 65,20 /** Anything else that went wrong, said once on stderr. */21 failed: 1,22 /** `EX_NOINPUT`: what was named does not exist, or no longer stands open. */23 noInput: 66,24 /** `EX_NOPERM`: the caller may not do this: not signed in, or not let in. */25 noPerm: 77,26 /** It did what it was asked. */27 ok: 0,28 /** `EX_TEMPFAIL`: run the refusal's `next`, and the same thing can pass. */29 tempFail: 75,30 /** `EX_UNAVAILABLE`: no platform answered. */31 unavailable: 69,32 /** The command was misused (a parse error, a missing value, `--yes` left out). */33 usage: 2,34} as const;3536export type ExitCode = (typeof ExitCode)[keyof typeof ExitCode];3738type FinalTag = Extract<Refusal, { readonly retryable: false }>["_tag"];3940type FinalCode =41 | typeof ExitCode.dataErr42 | typeof ExitCode.noInput43 | typeof ExitCode.noPerm;4445// Each final refusal's code. A new final tag fails the typecheck until it46// has one here; a retryable tag can't be given any but `tempFail`.47const final = {48 ChecksFailed: ExitCode.dataErr,49 DecisionClosed: ExitCode.noInput,50 Denounced: ExitCode.noPerm,51 DirectPush: ExitCode.noPerm,52 HeldBackFailed: ExitCode.dataErr,53 IncompleteBrief: ExitCode.dataErr,54 LayerContentInPublicTree: ExitCode.dataErr,55 NoAsk: ExitCode.noInput,56 NotDecider: ExitCode.noPerm,57 NotRedFirst: ExitCode.dataErr,58 NotSensitive: ExitCode.dataErr,59 NotVouched: ExitCode.noPerm,60 OutOfScope: ExitCode.dataErr,61 // A declined decision, as a declined waiver is `Weakened`: the answer changes.62 PostureChange: ExitCode.dataErr,63 PrivatePathInPublicTree: ExitCode.dataErr,64 ProjectExists: ExitCode.dataErr,65 ProposalClosed: ExitCode.noInput,66 ReachedInternals: ExitCode.dataErr,67 SecretInOutput: ExitCode.dataErr,68 TouchedJudge: ExitCode.dataErr,69 UnknownAsk: ExitCode.noInput,70 UnknownCommit: ExitCode.noInput,71 UnknownDecision: ExitCode.noInput,72 UnknownEnvItem: ExitCode.noInput,73 UnknownPath: ExitCode.noInput,74 UnknownProject: ExitCode.noInput,75 UnknownProposal: ExitCode.noInput,76 UnknownRevision: ExitCode.noInput,77 UnstatedRequirement: ExitCode.dataErr,78 ValueTooShort: ExitCode.dataErr,79 WeakCheck: ExitCode.dataErr,80 Weakened: ExitCode.dataErr,81} as const satisfies Record<FinalTag, FinalCode>;8283/** The code one refusal ends the CLI with. */84export const exitCodeOf = (refusal: Refusal): ExitCode =>85 refusal.retryable ? ExitCode.tempFail : final[refusal._tag];8687const finalCodes: ReadonlyMap<string, FinalCode> = new Map(88 Object.entries(final)89);9091/**92 * The code a refusal with `tag`, one of the union's, ends the CLI with: for93 * the reference docs, which read the union's schemas rather than refusals.94 */95export const exitCodeOfTag = (tag: string): ExitCode =>96 finalCodes.get(tag) ?? ExitCode.tempFail;9798/**99 * The order a caller deals with refusals met together: who is asking, then100 * what was named, then what was sent; a retry helps only once none is left.101 */102export const precedence: readonly ExitCode[] = [103 ExitCode.noPerm,104 ExitCode.noInput,105 ExitCode.dataErr,106 ExitCode.tempFail,107];108109/**110 * The code refusals met together end the CLI with: the first of111 * `precedence` any of them has, so `tempFail` only when every one is112 * retryable.113 */114export const exitCodeFor = (115 refusals: Array.NonEmptyReadonlyArray<Refusal>116): ExitCode => {117 const codes = new Set(refusals.map(exitCodeOf));118119 return precedence.find((code) => codes.has(code)) ?? exitCodeOf(refusals[0]);120};