* enhance(#3912): gsd-tools declares outcomes, pinned at v1 ADR-3889 §4. Phase 6 already moved error()'s terminator onto the seam, so what remained was the declaration — and the pin that makes it invisible today. The census corrected two documented figures before any code changed. ERROR_REASON has exactly 25 members (the ADR and epic were right; an earlier note of mine claiming 23 was wrong and is corrected). And output({error}) is **64 sites across 9 files, not the 60 ADR-2980 ratified** — the module shape holds but the total drifted +4: frontmatter 7 not 6, phase 4 not 2, roadmap 3 not 2. That matters because this phase's criterion demands the pin be asserted over the enumerated population rather than sampled; asserting over a stale 60 would leave four sites unpinned while claiming full coverage, which is the shape of failure this epic exists to remove. The issue does not state the fact that shapes the design: output() never touches the exit code. Confirmed by reading it — it writes fd 1 and returns. So a declared outcome for those 64 sites had nowhere to be READ. The mapping was never the work; wiring somewhere for the declaration to land was. The seam already existed twice over. cli-exit.cts holds two globalThis-Symbol cells, each because the module is emitted to three locations and a module-level `let` would let instances disagree, and runMain already maps a code returned by main(). A third cell inherits that solution. output() records DEGRADED for any {error} payload — key-order agnostic, which is exactly why the "42 sites" figure undercounts — and runMain projects the cell only when main() returns nothing, so an explicit return still wins. error() maps its reason through a table over the closed 25-member enum, leaving all 278 call sites untouched; 226 of them pass no reason at all. The version gate lives in error(), NOT in projectOutcome: registered names are version-invariant there, so mapping a reason straight through would make USAGE project to 64 under v1 and break the pin on its first line. projectOutcome is left exactly as Phase 2 shipped it, DEGRADED's 0/80 asymmetry included. Proven rather than asserted. v1 is byte-identical across three real CLI paths — config-get plain, config-get --json-errors, and an output({error}) path — matching exit code and exact bytes against the pre-change build. Under GSD_EXIT_CONTRACT=v2 the same commands now exit 66 (CONFIG_KEY_NOT_FOUND -> NO_INPUT) and 80 (DEGRADED), both looked up through the registry. An anti-vacuity test pins that v1 and v2 genuinely differ for at least one reason, because without it a mapping where everything projects to 1 under both versions would satisfy every other assertion and the declaration would be theatre. A1 iterates all 25 enum members and A3 asserts over the measured 64-site population, so a 26th reason or a 65th site fails until it is given a mapping — the drift guard this phase needs, given ADR-2980's own count had drifted +4 unnoticed. Verification runs on the remote runner. Refs #3912 * fix(#3912): the outcome cell must never lower an exit code The remote run caught a fail-open that this phase introduced, in the phase whose entire purpose is removing fail-opens. `state validate --strict` on a missing STATE.md exited **0** where it must exit 1. Mechanism: `runMain` projected the pending outcome whenever `main()` returned void, and under v1 DEGRADED projects to 0 — so a `process.exitCode` already set non-zero by the command was clobbered down to success. Confirmed live against a fixture, before and after. This refutes a review conclusion recorded earlier in this phase, that the cell was "fail-closed and can never mask a failure as success". It could, and did. Recording that plainly so the assumption is not repeated: the cell's danger was never only that it might add a failure — it was that projecting it unconditionally overwrites whatever decision came before. Projection is now guarded: it may set a code only when none is set, and an already-non-zero exit code always wins. The full precedence — explicit `main()` return, then an existing non-zero exitCode, then the declared outcome — is written at the projection site. A regression test drives a void return with a pre-set non-zero code and a pending DEGRADED, and fails against the pre-fix build. The second failure was my test encoding the wrong contract, not a code defect. It asserted `output({found:false, error: undefined})` records DEGRADED because the KEY is present. `JSON.stringify` drops undefined, so the payload the user receives is `{"found":false}` — carrying no error at all, and calling that degraded would hand back exit 80 under v2 for output that reads as clean. The discriminator is a serializable error VALUE, not key presence. The test now pins `{error: undefined}` as explicitly NOT degraded, and the design doc's wording is tightened to match. Verification runs on the remote runner. Refs #3912 * docs(#3912): the versioned exit contract, and a flag defect the docs found Diataxis pass for Phase 8, plus a real fix that only surfaced because writing the how-to meant running its own examples. The docs. ADR-2980's "Revisit if" clause asked for exactly the versioned projection this phase provides, so it gets an amendment naming #3912 / ADR-3889 section 4 as that boundary: v1 stays 0 byte-for-byte, v2 projects DEGRADED to 80. The amendment also records the count drift rather than restating a stale figure — the ADR ratified 60 output({error}) sites in 9 modules; the AST-measured population is 64 across the same 9 (frontmatter 7 not 6, phase 4 not 2, roadmap 3 not 2). The pin is asserted over the enumerated 64. json-errors.md gains the outcome-declaration reference, including the precedence order a review pass got wrong and the suite refuted: an explicit main() return, then an already-set non-zero process.exitCode, then the declared outcome. Projection may only ever set a code, never lower one. A how-to is owed here and is written, not skipped. Under v1 nothing changes, so the audience is an operator opting into v2 and needing to know what the codes mean for a CI gate — a migration, which is how-to shaped. It covers turning v2 on, the code table, why 80 is "ran and reported a condition" rather than a crash, and how to split a gate that treats any non-zero as fatal. No tutorial: there is no new entry point to learn, and under the default contract a reader would be walked through observing nothing. The defect. Running the how-to's own Step 1 example returned $ gsd-tools --exit-contract=v2 state validate --strict Error: Unknown command: --exit-contract=v2 (exit 64) while the same flag trailing the subcommand worked and exited 80. The flag half-worked, by argv position. resolveContractVersion scans argv non-destructively, so the token survived into the dispatcher, which treats argv[2] as the command name. --json-errors had already solved precisely this at gsd-tools.cjs:4455, under a comment naming the hazard verbatim: "The argv splice must happen here too, otherwise the dispatcher below sees --json-errors as an unknown command." The later flag never got the same treatment. Fixed rather than documented around: the version is resolved first — which memoizes the cell and makes an invalid value throw early — and then every --exit-contract= token is spliced out of the dispatcher's argv copy. --exit-contract is now listed in TOP_LEVEL_USAGE, where it never was. The regression test pins leading position, trailing position, agreement between the two, and a loud failure on v3 rather than a silent fall back to v1. Neither review engine would have caught this: the defect is invisible in the diff, because the diff does not touch argv handling. It surfaced only from running the documentation's own example. Writing a how-to is an execution pass. Verification runs on the remote runner. Refs #3912 * fix(#3912): the flag splice has to run before the run-with-timeout return An isolated review of the previous commit found that the fix did not deliver what it claimed, and that two of its own tests were weak. All three findings reproduced by execution before any change was made. The fix was placed below a return. main() intercepts `run-with-timeout` at gsd-tools.cjs:4436 and returns from there — above both the --json-errors block and the --exit-contract splice added in the previous commit. So the flag still died in leading position for that one command: $ gsd-tools --exit-contract=v2 run-with-timeout 5 -- node -e "..." Error: Unknown command: run-with-timeout (exit 64, child never ran) The previous commit message and the test's describe-block both claimed position-independence unconditionally. That was an overclaim, not a gap left open, and it is the part worth naming: the fix was verified by hand on the commands I happened to think of, and `run-with-timeout` returns before the code I was verifying. Both global-flag blocks now run above the interception, with a comment naming it so a later edit cannot slide them back down. Moving --json-errors up fixes the identical pre-existing bug for that flag, verified failing beforehand (exit 1, sdk_unknown_command). Fixing the sibling is deliberate: same defect, same block, and a known-broken twin next to a fixed one is not a resting state. Two tests were not pulling their weight. The invalid-value test was vacuous — it passed against the pre-fix build, because `--exit-contract=v3` already exited 1 there and already printed the resolve error lazily through error() -> getContractVersion. Both its assertions held before the fix, so it pinned nothing. The real discriminator is that the pre-fix build emits BOTH "Unknown command: --exit-contract=v3" and the resolve error, while the fixed build emits only the latter; the test now asserts that absence. The leading-position and leading==trailing tests asserted proxies — "not 64", "no Unknown command", "the two agree" — none of which pin a value, and all of which would survive both positions being identically broken. With a .planning directory and no STATE.md, state-snapshot exits exactly 80 under v2 and 0 under v1 in both positions. Those numbers are pinned now. The multi-token case the descending splice loop exists for is covered too, and run-with-timeout has regression tests for both flags. The lesson is narrower than "test more". Hand-verifying the production behavior does not verify that the test would have caught its absence. The pre-fix binary has to be run against the test's own assertions. Investigated and deliberately not changed: splicing before --cwd parsing degrades one diagnostic from "Missing value for --cwd" to "Invalid --cwd: <path>", but that is pre-existing — verified on the pre-fix build via --json-errors, which already did it. This change joins the pattern rather than creating it, and both forms exit 64 on malformed input either way. Verification runs on the remote runner. Refs #3912 * chore(#3912): backfill changeset pr numbers to 3983 * test(#3912): pin the reason-table invariant as set equality, not a count A graph-backed review flagged the unchecked lookup in expectedErrorCode3912. Investigated by execution: the drift guard DOES hold — for an unmapped reason under v2 the production error() yields 1 while the table yields undefined, so the assertion fails. Not a correctness defect, and deliberately NOT made tolerant, since a tolerant lookup would destroy the guard. Two real problems remained. The guard asserted the wrong invariant: it counted the TABLE's keys at 25 rather than checking they match the ENUM's values, so a renamed member keeps the count at 25 and slips past, and a 26th member leaves the table at 25 and slips past too. Both were then caught only indirectly, by an undefined mismatch producing 'must exit undefined'. It is now a sorted set equality, so the failure names the specific missing or extra reason. And the comment above it described a '?? FAIL' fallback that does not exist anywhere in the function. It now states what the code actually does, verified by running it rather than by reading it. Refs #3912 --------- Co-authored-by: sim <sim@local>
15 KiB
JSON Error Mode — gsd-tools Structured Errors
Overview
gsd-tools supports a JSON error mode that emits most errors as structured
JSON objects on stderr instead of free-form text. This is the recommended
surface for tests and tooling that need to assert on error types without
grepping raw text (see CONTRIBUTING.md — "Prohibited: Raw Text Matching on
Test Outputs"). Usage errors are an intentional exception — see the
ExitError carve-out below.
This page describes one of two failure channels. A second, equally intentional one reports conditions in the result payload on stdout with exit 0. A caller that branches on exit status alone will not see it. Read Degraded results vs faults before writing anything that consumes
gsd-toolsoutput.
Activating
Either flag or env var activates the mode:
# Flag (preferred in test code):
node gsd-tools.cjs --json-errors <command> [args]
# Env var (preferred for shell wrappers and CI):
GSD_JSON_ERRORS=1 node gsd-tools.cjs <command> [args]
Wire format
On any error, exactly one JSON line is written to stderr and the process exits with code 1:
{ "ok": false, "reason": "<error_code>", "message": "<human text>" }
Fields:
| Field | Type | Description |
|---|---|---|
ok |
false |
Always false for error objects. |
reason |
string | Typed reason code from the taxonomy below. |
message |
string | Human-readable description (may change; do not assert on it). |
ExitError carve-out (plain text, not JSON)
Usage errors and explicit exit-code signals take a different path: they
throw ExitError (src/cli-exit.cts), which runMain catches before the
JSON-envelope branch. An ExitError writes its message as plain text
to stderr (not a JSON object) and exits with the error's own code (which
may differ from 1). This is intentional — usage messages are operator-facing
prose, not structured failures.
If you are testing a usage/flag error, do not parse stderr as JSON;
assert on the exit code and (if needed) the plain-text message. The
"parse stderr as JSON" guidance below applies only to the structured-envelope
branch (non-ExitError failures).
Which tools honor this. Both surfaces that run
runMaindo: the compiledgsd-core/bin/lib/cli-exit.cjsand thescripts/lib/cli-exit.cjsthat the repo's ownscripts/**tooling requires. Before #3904 the latter was a separate hand-written copy that never gained the structured-envelope branch, so ascripts/-side tool failing unexpectedly printed a raw stack trace even under--json-errors. It is now generated from the same source and byte-compared bynpm run lint:generated-sync, so the two cannot answer differently again.
Degraded results vs faults — read this before writing a caller
gsd-tools has two ways of telling you something went wrong, and they use different exit
codes. The wire format above describes only one of them. If you write a caller that branches on
exit status alone, you will silently miss the other.
| Fault | Degraded result | |
|---|---|---|
| Produced by | error(message, reason) |
output({ error: … }) |
| Stream | stderr | stdout |
| Exit code | 1 | 0 |
| Shape | { "ok": false, "reason": …, "message": … } |
the command's ordinary result object, with an added error key |
Honors --json-errors |
yes | no — it is a payload, not an error envelope |
| How a caller detects it | exit code | inspect the payload |
A degraded result means: the command ran to completion and is reporting a condition through its result. It is not a process failure. The command succeeded at the job of determining that, for example, the artifact you asked about is absent.
$ gsd-tools state-snapshot # in a project with no STATE.md
{
"error": "STATE.md not found"
}
$ echo $?
0
Some verbs return a companion result alongside the key, which is the shape that makes the intent clearest:
$ gsd-tools roadmap get-phase --phase 1 # no ROADMAP.md
{
"found": false,
"error": "ROADMAP.md not found"
}
$ echo $?
0
This is a ratified contract, not an accident — see
ADR-2980 for the decision and the blast
radius that drove it. It applies to 64 call sites across nine modules — state, verify,
workstream, frontmatter, commands, template, phase, roadmap, and gsd2-import.
(Issues #2966 and #2980 record this as "42 sites"; that figure counts only the sites where error
happens to be the object's first key. ADR-2980 itself re-derived the population as "60" by
brace-matching; a further AST re-measure for #3912
found the true current count is 64 — the same nine modules, with frontmatter, phase, and
roadmap each having grown since. See ADR-2980's amendment for the breakdown.)
Writing a correct caller
The obvious shell form is wrong for a degraded result:
# WRONG — the process exits 0, so this branch never runs
if ! gsd-tools state-snapshot > snap.json; then
echo "failed"
fi
Check both channels — the exit code for faults, the payload for degraded results:
if ! out=$(gsd-tools state-snapshot); then
echo "fault (exit non-zero)" >&2 # error() path
exit 1
fi
if err=$(printf '%s' "$out" | jq -er '.error // empty'); then
echo "degraded: $err" >&2 # output({error}) path
fi
Four things that will surprise you
--json-errorsdoes nothing here. It governserror()only. A degraded result is byte-identical with and without the flag, and still exits 0.--rawis not uniform on this path. Most sites pass no raw value, so--rawstill yields the JSON object rather than bare text — but eleven sites do pass one and behave differently. Do not infer either behavior from--rawalone; check the verb.- Not every degraded result is an absent artifact. A missing required argument is reported the
same way —
gsd-tools state add-blockerwith no--textreturns{"error":"text required"}and exits 0. So is unusable input:gsd-tools state advance-planagainst a STATE.md it cannot parse returns{"error":"Cannot parse Current Plan or Total Plans in Phase from STATE.md"}, also exit 0. The exit code does not distinguish absent from malformed from misinvoked — see ADR-2980's Consequences, where this is recorded as a known cost. message/errortext is not stable. Assert on structure and on typedreasoncodes, never on prose. The rule in "Writing tests" below applies to both paths.
Which one should new code use?
Prefer the fault path, or a result with a named field. ADR-2980 ratifies an existing population;
it is not a license to add a 61st output({ error: … }) site. Where a verb needs to report a
non-fatal condition in its payload, prefer the shape state update-progress already uses — a named
field plus a reason, with no overloaded error key:
$ gsd-tools state update-progress # STATE.md present, no Progress field
{
"updated": false,
"reason": "Progress field not found in STATE.md"
}
Outcome declaration and the versioned exit contract (ADR-3889 §4, #3912)
Both failure channels above now declare an outcome on every terminating path, per ADR-3889. Declaration is unconditional; whether it changes the observed exit code depends on which exit-contract version the process is running under.
Turn on v2 with either --exit-contract=v2 or GSD_EXIT_CONTRACT=v2 (a flag beats the env var if
both are given). Absent either, the process runs v1 — today's default and, for every existing
caller, byte-identical to pre-#3912 behavior. See
Adopt the v2 exit contract for a worked migration.
error(message, reason)
error()'s reason argument now maps onto a declared outcome name (USAGE, NO_INPUT,
UNAVAILABLE, INTERNAL, or FAIL) via a fixed table over all 25 ERROR_REASON members.
- Under
v1, the mapping is recorded but never projected.error()still throwsExitError(1)unconditionally, exactly as before — stderr and the exit code are byte-identical to every prior release. - Under
v2, the mapping is projected through the exit-code registry.error()throwsExitError(exitCodeFor(<mapped outcome>))instead of a hardcoded1— so, for example, a call withERROR_REASON.SDK_MISSING_ARGorERROR_REASON.SDK_UNKNOWN_COMMANDexits64(USAGE) underv2, and one withERROR_REASON.CONFIG_KEY_NOT_FOUNDexits66(NO_INPUT). - Most call sites are unaffected either way. 226 of the 278
error()call sites in the repo pass noreasonat all, defaulting toERROR_REASON.UNKNOWN, which maps to the genericFAILoutcome (exit1) under both versions.
output({ error: … }) — a degraded result is also a declared outcome
The degraded-result idiom above now declares the outcome DEGRADED whenever output()'s payload
carries a serializable error value — any key order, and regardless of that value's own
truthiness (0/null/'' all count). The one exclusion: { error: undefined } does not
declare DEGRADED, because JSON.stringify (the exact serializer output() uses) drops an
object property whose value is undefined before it ever reaches the wire — a payload built that
way reaches the caller as {}, with nothing to be degraded about.
- Under
v1,DEGRADEDprojects to0— deliberately: this is ADR-2980's compatibility boundary, pinned so all 64 ratified sites keep exiting0byte-for-byte. - Under
v2,DEGRADEDprojects to80— looked up from the exit-code registry, never hardcoded, so a future re-allocation ofDEGRADED's number cannot silently desync this doc from the shipped table.
Precedence — what code a void-returning command actually exits with
A command's main() can end up producing a code from more than one source. The order, highest
precedence first, is:
- An explicit
main()return (a number or a registered outcome-name string) — always wins. - A non-zero
process.exitCodemain()already set directly before returning — wins over anything declared throughoutput(). This is what keepsstate validate --strictcorrect: it setsprocess.exitCode = 1itself on a missingSTATE.md, and aDEGRADEDdeclared earlier in the same call must not clobber that1back down toDEGRADED'sv1projection of0. - The declared outcome pending from
output()— consulted only when neither of the above set anything. - Otherwise the process exits
0.
Projection may only ever set a code, never lower one. A prior review pass concluded the pending
declaration was fail-closed by construction; it was not — without rule 2 above, state validate --strict briefly exited 0 on a case that must exit 1. If you add a new call path that sets
process.exitCode directly, check it still wins over a later output({error}) in the same
invocation.
The declaration does not accumulate across calls. output()'s declaration follows
last-write-wins: a clean payload clears a prior DEGRADED declaration in the same invocation, and
runMain clears the cell on every exit regardless of which branch produced the final code, so a
later runMain call in the same process never inherits a stale declaration.
Error code taxonomy
Codes are frozen constants in gsd-core/bin/lib/core.cjs under
ERROR_REASON. Tests must assert on reason values (stable), not message
text (unstable).
Dispatch errors (gsd-tools routing layer)
| Code | When emitted |
|---|---|
sdk_unknown_command |
Unknown top-level command (gsd-tools bogus-cmd) |
sdk_unknown_command |
Unknown dotted command (gsd-tools foo.bar where foo is not a known command) |
sdk_unknown_command |
Unknown subcommand within a domain (e.g. gsd-tools intel bogus-sub) |
sdk_missing_arg |
Required argument omitted by an SDK-level guard |
sdk_fail_fast |
SDK fail-fast policy triggered |
Usage / flag errors
| Code | When emitted |
|---|---|
usage |
--pick flag used without a following value |
usage |
Version flag (--version, -v) which gsd-tools never accepts |
usage |
Top-level no-args invocation (usage text) |
--pick <field> errors (ADR-3473 §8.4, #3884)
| Code | When emitted |
|---|---|
pick_field_absent |
--pick <field> names a field that does not exist in the command's JSON output (missing key, out-of-range index, a partially-missing dotted path, or a non-object JSON root) — see CLI-TOOLS.md's --pick contract |
pick_output_not_json |
--pick <field> is combined with a command whose output is not JSON (including --raw output) |
Config errors (config-get, config-set, config-ensure-section)
| Code | When emitted |
|---|---|
config_key_not_found |
config-get for a key that is absent from the config file |
config_no_file |
Config operation when .planning/config.json does not exist |
config_parse_failed |
Config file exists but is not valid JSON |
config_invalid_key |
config-set for a key outside the allowed whitelist |
Phase / workflow errors
| Code | When emitted |
|---|---|
phase_not_found |
Phase directory lookup returns no match |
summary_no_planning |
Summary operation when no .planning/ directory exists |
Estimate errors
| Code | When emitted |
|---|---|
estimate_phases_unreadable |
estimate-calibrate when .planning/phases/ exists but could not be read (EACCES/EIO) — refused rather than silently rebuilding calibration from a phantom empty sample set (#3882, ADR-3473 §8.5) |
Graphify errors
| Code | When emitted |
|---|---|
graphify_no_graph |
Graphify query or diff when no graph has been built |
graphify_invalid_query |
Graphify query with a malformed query string |
Hook / security errors
| Code | When emitted |
|---|---|
hooks_opt_out |
Hooks are disabled via opt-out config |
security_scan_failed |
Security scan produced a finding that blocks the operation |
Fallback
| Code | When emitted |
|---|---|
unknown |
All other errors without a specific reason code assigned |
Writing tests
For non-usage errors (the structured-envelope branch), parse stderr with
JSON.parse and assert on typed fields. Never use .includes(), .match(),
or regex on the raw error string.
// CORRECT: parse then assert on typed field
const result = runGsdTools(['--json-errors', 'bogus-command'], tmpDir);
assert.strictEqual(result.success, false);
const err = JSON.parse(result.error);
assert.strictEqual(err.ok, false);
assert.strictEqual(err.reason, 'sdk_unknown_command');
// WRONG: text matching (banned by lint-no-source-grep policy)
// assert.ok(result.error.includes('Unknown command'));
Adding a new error code
- Add the constant to
ERROR_REASONingsd-core/bin/lib/core.cjs(snake_case, prefixed by subsystem). - Pass it as the second argument to
error()at the call site. - Add a row to this document.
- Add a test asserting the new
reasoncode viaJSON.parse.