Files
msd-core/src/adr-parser.cts
Tom Boucher d98b55562c enhance(#3910): the raw terminator is banned by construction (#3980)
* enhance(#3910): move the last src/ terminators onto the seam

Phase 6 bans the raw terminator by construction, which it cannot do while
violations stand. A census found 12 sites the rule would flag; nine of the ten
unsanctioned ones were owned by no phase of the epic at all — a coverage hole
in the decomposition, since P0-P2 are infra, P3 the gate modules, P4 the
scanners, P5 the fragments, P7 the hooks, P8 io.cts, and P6 itself only adds
the rule. `src/**/*.cts` now holds exactly 2 raw exits, both inside
`terminateNow`, the single sanctioned site.

`io.cts`'s `error()` is the interesting one. It was first called substantive on
"dozens of callers, contract risk" — asserted, not measured, and the
measurement refuted it: 289 call sites, zero inside a try whose catch would
swallow a throw. The real obstacle was structural instead: `terminateNow`
cannot emit exit 1, because ADR-3889 §1 makes 0 and 1 unallocatable and
`nameForExitCode(1)` throws. So the only route is `ExitError` under `runMain`,
which sets exitCode and writes stderr only when the error carries a user
message — keeping the existing stderr write and throwing a message-less
ExitError is observably identical.

That census was still too narrow, and running the CLI proved it. It asked
whether the CALL sits in a try/catch; the two regressions that surfaced were
interceptors elsewhere on the stack:

- `command-routing-hub.cts`'s `dispatch()` swallowed the ExitError into a
  HandlerFailure, so the caller emitted a duplicated, wrong stderr line on
  every Hub-routed path. It now rethrows ExitError explicitly — the same shape
  `gsd-tools.cjs` already used at two dispatch sites, so this follows an
  established idiom rather than inventing one.
- the profile-pipeline router's deliberately un-awaited `.catch(e => error(...))`
  turned an ExitError rejection into an uncaught exception; it now mirrors
  runMain's handling.

`edge-probe` and `ui-consideration-probe` gained `runMain` wrappers because
probe-core's new throwing default would otherwise have escaped them.

A follow-up sweep of every dispatcher — 19 command routers, the Hub, the
gsd-tools dispatch seams — found no further swallowing catch. The admitted
bound: ~1260 non-rethrowing catches repo-wide were scanned structurally but not
individually classified. Both real regressions were found by execution, not by
reading, so the suite is the detector that matters here.

`gsd-tools.cjs:253` stays a raw exit deliberately: it is the ensureRuntimeBuild
bootstrap, which runs before cli-exit is required, so the seam does not yet
exist. It needs a second allowlist entry, which means #3910's "single allowlist
entry" criterion is unachievable as written.

Verification runs on the remote runner.

Refs #3910

* enhance(#3910): ban the raw terminator by construction

Adds local/require-registered-exit and registers it on all four globs:
src/**/*.cts, scripts/**/*.cjs, hooks/**/*.js, gsd-core/bin/**/*.cjs.

Registering on the .cts glob is load-bearing, not redundant — the emitted .cjs
mirrors are globally eslint-ignored, so a rule registered only on the emitted
globs is blind to the sources. That is the #3496 lesson, and it is how the
previous guard became invisible: n/no-process-exit was 'error' in one block yet
fired zero times on all three surfaces that mattered.

The dead n/no-process-exit: 'off' block for hooks is deleted in the same PR.
Phase 7 migrated every hook, so the exemption now protects nothing.

Two allowlist entries, not the one #3910 anticipated. terminateNow's body is
detected STRUCTURALLY — a process.exit lexically inside a function of that name
— rather than by a path and line number that rots. The second is
gsd-tools.cjs's ensureRuntimeBuild bootstrap, an inline disable with its reason
at the call site: it runs before ./lib/cli-exit.cjs is required, so the seam
does not exist yet and no migration is possible. #3910's 'single allowlist
entry' criterion is therefore unachievable as written, and is amended with the
measurement rather than quietly missed.

The rule is proven able to FAIL, per glob: four positive controls, one for each
registered glob. A guard that cannot be shown to fire is not a guard. Four
matching negative controls pin process.exitCode as never-flagged — conflating
it with process.exit is what inflated this epic's original census 2x. An
allowlist case and a near-miss (same shape, different function name) fix the
structural detection in place.

Verification runs on the remote runner.

Refs #3910

* fix(#3910): stop the detached catch from throwing, and scope the allowlist

Review findings, one of them a regression the previous fix introduced.

_handlePipelineRejection called error() from inside a DETACHED .catch().
error() now throws, so that throw became an unhandled promise rejection — and
on Node >=15 with --unhandled-rejections=throw, Node dumps a raw stack trace
with absolute paths on top of the clean Error: line. That was impossible before
this branch, because process.exit(1) terminated synchronously before any
rejection machinery could observe it. The handler now writes byte-identical
stderr itself, in both plain and --json-errors form, and sets exitCode in
place. This was the THIRD interceptor found, and like the first two it surfaced
by running the CLI rather than by reading code.

The rule's terminateNow allowlist had no path constraint, so any function
anywhere named terminateNow across all four globs inherited it. It now requires
the structural nesting check AND a cli-exit.cts basename — still no line
numbers to rot.

The four per-glob positive controls only varied a filename inside RuleTester,
which never resolves eslint.config.mjs. Since the rule is filename-agnostic,
all four exercised identical logic and none proved the rule was WIRED — this
epic's own failure mode. A registration test now asserts the rule resolves for
a real path in each glob, and it is proven able to fail: removing one glob's
registration flips the resolved value from [2] to undefined.

Three evasions the rule cannot catch (computed member, aliasing, .call/.apply)
are documented in its header and pinned by tests, labelled as known limits
rather than endorsed, so a future change that starts catching them is a
deliberate diff.

Refs #3910

* docs(#3910): document the raw-terminator ban

Reference and Explanation via a new docs/features fragment (FEATURES.md is
generated from it, not hand-edited). How-To:
docs/how-to/resolve-a-raw-terminator-finding.md, indexed from docs/README.md —
a contributor whose code trips the rule picks among three replacements by
surface (runMain/ExitError for a CLI path, terminateNow for a hook,
process.exitCode where the process should drain), and needs to know why
process.exitCode is correct and never flagged, since conflating the two is what
inflated this epic's original census 2x.

The page also names the three patterns the rule cannot catch and says plainly
that using one to dodge it is a review finding, not a fix — documenting them
without that sentence would read as a sanctioned workaround.

docs/INVENTORY.md deliberately untouched: eslint-rules/ is not a tracked family
in the manifest (verified — a regen produced a zero diff), so a hand-written row
would desync the table from the family it claims to belong to.

Refs #3910

* fix(#3910): a catch that sniffs the message swallows an ExitError

The remote run returned 41 failures, and one of them was a live production
regression rather than a test artifact.

`cmdMilestoneComplete`'s unstarted-phase guard re-threw only when
`e.message.startsWith('Cannot mark milestone complete:')`. `error()` used to
`process.exit(1)`, uncatchable, so the guard always fired. It now throws an
ExitError carrying no message, the string test fails, and the ExitError was
silently swallowed — the guard stopped blocking milestone completion entirely.
Proven against the real CLI: pre-fix, a milestone with an unstarted phase
archived at exit 0; post-fix it is blocked at exit 1 with the intended message.

That is a guard that silently stopped guarding, which is this epic's thesis
appearing inside the phase meant to enforce it. Worth stating plainly: an
earlier census DID examine this site, saw a `throw e`, and classified it as
rethrowing. It was wrong — the rethrow is conditional, and a conditional
rethrow on an inspected message is indistinguishable from an unconditional one
unless you read the predicate.

So the class was swept rather than patched where it was tripped over. An AST
census of every CatchClause across src/, gsd-core/bin/ and scripts/ found 38
conditional rethrows. Two more had the same defect and are fixed the same way:
`config.cts`'s `'No config.json'` sniff and `gsd-tools.cjs`'s
`e.name === 'WindowsError'`. The remaining 25 are provably unreachable — every
one wraps a bare fs, YAML, manifest-require or git-exec primitive that cannot
throw ExitError — and two were scanner false positives, both explained. Each
fix is an unconditional `instanceof ExitError` rethrow placed BEFORE any
inspection, matching the idiom command-routing-hub and gsd-tools already used.

Residual bound, stated rather than implied: zero known-reachable unfixed sites,
contingent only on error() never later being called inside one of those 25
primitive try blocks.

The remaining failures were harness artifacts, and the harnesses were corrected
to the new contract rather than the assertions weakened. Tests that mocked
`process.exit` to observe termination now catch ExitError and assert its code;
tests parsing stderr as a single JSON object still assert exactly that, with
their ad-hoc `node -e` scripts wrapped in runMain so it is true. milestone and
phase-resolution-parity needed no test change — they were correctly written
against the real bug and are what caught it.

Verification runs on the remote runner.

Refs #3910

* chore(#3910): backfill the changeset PR number

Also reframes the fragment to lead with the user-visible change — the
milestone guard blocking again — rather than the narrowest of the three fixes.

Refs #3910

---------

Co-authored-by: sim <sim@local>
2026-08-28 03:15:39 -04:00

485 lines
13 KiB
TypeScript

/**
* ADR Markdown parser — parses Architecture Decision Record documents into
* structured objects for downstream processing (adr command, gap checker, etc.).
*
* ADR-457 build-at-publish: the hand-written bin/lib/adr-parser.cjs collapsed
* to a TypeScript source of truth. Behaviour is preserved byte-for-behaviour
* from the prior hand-written .cjs; only types are added.
*/
import fs from 'node:fs';
import path from 'node:path';
import { requireSafePath } from './security.cjs';
import { collectSections } from './markdown-sectionizer.cjs';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import cliExitModule = require('./cli-exit.cjs');
const { ExitError, runMain } = cliExitModule;
const STATUS_REJECT_SET = new Set(['superseded', 'rejected', 'deprecated']);
type CanonicalHeader =
| 'status'
| 'goal'
| 'decisions'
| 'considered_options'
| 'risks'
| 'success_criteria'
| 'plan_sequence'
| 'key_files'
| 'out_of_scope'
| 'deferred'
| 'dependencies'
| 'update'
| 'consequences';
const CANONICAL_HEADERS: Record<CanonicalHeader, string[]> = {
status: ['status', 'state', 'lifecycle', 'stage'],
goal: [
'context',
'background',
'problem statement',
'problem',
'situation',
'forces',
'motivation',
'issue',
'drivers',
'pain points',
'story',
'setting',
'premise',
'status quo',
'context and problem statement',
],
decisions: [
'decision',
'decisions',
'resolution',
'conclusion',
'choice',
'we decided',
'direction',
'approach',
'solution',
'outcome',
'selected option',
'recommendation',
'strategy',
'decision outcome',
],
considered_options: [
'considered options',
'alternatives',
'options',
'choices',
'candidates',
'approaches considered',
'variants',
'pros and cons of the options',
'discussion',
],
risks: [
'risks',
'trade-offs',
'drawbacks',
'cost',
'tensions',
'liabilities',
'negative consequences',
'side effects',
],
success_criteria: [
'success criteria',
'acceptance criteria',
'validation',
"how we'll know",
'metrics',
'kpis',
'verification',
'test strategy',
'compliance',
'definition of done',
'exit criteria',
'positive consequences',
],
plan_sequence: [
'implementation plan',
'implementation notes',
'steps',
'tasks',
'roadmap',
'sequence',
'migration plan',
'plan',
'action items',
'work breakdown',
'phases',
'milestones',
'stages',
],
key_files: [
'affected files',
'files touched',
'surface area',
'modules affected',
'code locations',
'file changes',
'diff summary',
'touched code',
],
out_of_scope: [
'out of scope',
'non-goals',
'excluded',
'not in this adr',
'out of bounds',
"won't do",
"won't have",
'beyond scope',
'anti-goals',
],
deferred: [
'future work',
'deferred',
'future',
'later',
'follow-up',
'next steps',
],
dependencies: [
'dependencies',
'depends on',
'prerequisites',
'sequencing',
'order',
'blocked by',
'cross-cuts',
'related adrs',
'links',
'references',
'see also',
'upstream',
'inbound',
],
update: [
'update',
'revision',
'amendment',
'locked design',
'final decision',
'post-grilling',
'addendum',
],
consequences: [
'consequences',
'implications',
'impact',
'what this means',
'result',
],
};
const CONSEQUENCE_NEGATIVE_HINTS: string[] = [
'negative',
'drawback',
'risk',
'cost',
'liability',
'trade-off',
'tension',
'side effect',
];
const CONSEQUENCE_POSITIVE_HINTS: string[] = [
'positive',
'success',
'metric',
'kpi',
'verification',
'acceptance',
'benefit',
];
function normalizeAdrHeader(raw: unknown): string {
const s = typeof raw === 'string' ? raw : '';
return s
.trim()
.toLowerCase()
.replace(/[\s:._-]+/g, ' ')
.replace(/[^\w\s]/g, '')
.trim();
}
// Normalized synonym index (audit M7). classifyHeader receives an ALREADY-normalized
// header (via normalizeAdrHeader), but historically compared it against the RAW synonym
// strings. Because normalizeAdrHeader collapses [\s:._-]+ to a space and strips [^\w\s],
// any synonym carrying a hyphen/apostrophe/etc. ('trade-offs', "won't do", 'post-grilling')
// could never match a normalized header — it was silently dead, and its ADR section went
// unmapped. Normalizing BOTH sides closes that abstraction asymmetry once, so every synonym
// (current and future) is reachable regardless of punctuation. Precomputed at module load to
// avoid re-normalizing the whole table per call; insertion order is preserved so first-match-
// wins and the exact-then-prefix precedence stay identical to the prior raw-compare loop.
const _NORMALIZED_SYNONYM_INDEX: Array<[string, CanonicalHeader]> = (() => {
const index: Array<[string, CanonicalHeader]> = [];
for (const [canonical, synonyms] of Object.entries(CANONICAL_HEADERS) as Array<[CanonicalHeader, string[]]>) {
for (const synonym of synonyms) {
index.push([normalizeAdrHeader(synonym), canonical]);
}
}
return index;
})();
function classifyHeader(normalizedHeader: string): CanonicalHeader | null {
for (const [synonym, canonical] of _NORMALIZED_SYNONYM_INDEX) {
if (normalizedHeader === synonym) return canonical;
if (normalizedHeader.startsWith(`${synonym} `)) return canonical;
}
return null;
}
function splitEntries(blockText: unknown): string[] {
return (typeof blockText === 'string' ? blockText : '')
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
.map((line) => line.replace(/^[-*+]\s+/, '').trim())
.filter(Boolean);
}
interface MarkdownSection {
heading: string | null;
body: string[];
}
/**
* Thin adapter: wraps the seam's `collectSections` to produce the same
* `{ heading: string | null, body: string[] }` shape the rest of adr-parser
* consumes. ADR-1372 T2 migration.
*/
function parseSections(markdown: unknown): MarkdownSection[] {
const content = typeof markdown === 'string' ? markdown : '';
// collectSections(content, () => true) collects every heading as a stop
// boundary — mirrors the old line-by-line heading walk exactly.
const sections = collectSections(content, () => true);
// Map seam Section → MarkdownSection. The seam's HeadingToken.text is the
// heading text after trimming (same as the old m[1].trim() capture).
// The body is a trimEnd()-ed joined string; split it back to lines to match
// the old string[] shape consumed by parseStatusFromSections / parseAdrMarkdown.
//
// Note: the old parseSections emitted a leading { heading: null, body: [...] }
// entry for preamble text before the first heading. Both consumers skip it
// immediately (parseAdrMarkdown: `if (!heading) continue`; parseStatusFromSections:
// `classifyHeader(normalizeAdrHeader(null))` → null ≠ 'status' → continue), so
// the preamble entry was dead code and is not reconstructed here.
return sections.map((sec) => ({
heading: sec.heading.text,
body: sec.body === '' ? [] : sec.body.split('\n'),
}));
}
function parseStatusFromSections(sections: MarkdownSection[]): string {
for (const section of sections) {
const canonical = classifyHeader(normalizeAdrHeader(section.heading));
if (canonical !== 'status') continue;
const line = splitEntries(section.body.join('\n'))[0] || '';
const norm = normalizeAdrHeader(line);
if (!norm) return '';
if (norm.includes('accepted')) return 'accepted';
if (norm.includes('proposed')) return 'proposed';
if (norm.includes('superseded')) return 'superseded';
if (norm.includes('rejected')) return 'rejected';
if (norm.includes('deprecated')) return 'deprecated';
return norm;
}
return '';
}
function pushUnique(target: string[], values: string[]): void {
const seen = new Set(target);
for (const value of values) {
if (!seen.has(value)) {
target.push(value);
seen.add(value);
}
}
}
interface AdrOut {
title: string;
status: string;
context: string;
decisions: string[];
options_considered: string[];
consequences_positive: string[];
consequences_negative: string[];
out_of_scope: string[];
deferred: string[];
dependencies: string[];
updates: Array<{ heading: string; entries: string[] }>;
source_path: string;
key_files: string[];
plan_sequence: string[];
format: string;
unmapped_headers: string[];
}
function parseConsequences(lines: string[], out: AdrOut): void {
for (const entry of lines) {
const lower = entry.toLowerCase();
if (CONSEQUENCE_NEGATIVE_HINTS.some((hint) => lower.includes(hint))) {
out.consequences_negative.push(entry);
continue;
}
if (CONSEQUENCE_POSITIVE_HINTS.some((hint) => lower.includes(hint))) {
out.consequences_positive.push(entry);
continue;
}
out.consequences_positive.push(entry);
}
}
interface ParseAdrMarkdownOptions {
sourcePath?: string;
format?: string;
}
function parseAdrMarkdown(markdown: unknown, { sourcePath = '', format = 'auto' }: ParseAdrMarkdownOptions = {}): AdrOut {
const sections = parseSections(markdown);
const titleLine = (typeof markdown === 'string' ? markdown : '').split(/\r?\n/).find((line) => /^#\s+/.test(line)) || '';
const title = titleLine.replace(/^#\s+/, '').trim();
const out: AdrOut = {
title,
status: parseStatusFromSections(sections) || 'accepted',
context: '',
decisions: [],
options_considered: [],
consequences_positive: [],
consequences_negative: [],
out_of_scope: [],
deferred: [],
dependencies: [],
updates: [],
source_path: sourcePath,
key_files: [],
plan_sequence: [],
format,
unmapped_headers: [],
};
for (const section of sections) {
const heading = section.heading || '';
if (!heading) continue;
const canonical = classifyHeader(normalizeAdrHeader(heading));
const entries = splitEntries(section.body.join('\n'));
const prose = section.body.join('\n').trim();
if (!canonical) {
out.unmapped_headers.push(heading);
continue;
}
switch (canonical) {
case 'goal':
if (!out.context && prose) out.context = prose;
break;
case 'decisions':
pushUnique(out.decisions, entries);
break;
case 'considered_options':
pushUnique(out.options_considered, entries);
break;
case 'risks':
pushUnique(out.consequences_negative, entries);
break;
case 'success_criteria':
pushUnique(out.consequences_positive, entries);
break;
case 'plan_sequence':
pushUnique(out.plan_sequence, entries);
break;
case 'key_files':
pushUnique(out.key_files, entries);
break;
case 'out_of_scope':
pushUnique(out.out_of_scope, entries);
break;
case 'deferred':
pushUnique(out.deferred, entries);
break;
case 'dependencies':
pushUnique(out.dependencies, entries);
break;
case 'update':
out.updates.push({ heading, entries });
break;
case 'consequences':
parseConsequences(entries, out);
break;
default:
break;
}
}
return out;
}
function shouldRejectAdrStatus(status: string): boolean {
return STATUS_REJECT_SET.has(normalizeAdrHeader(status));
}
interface CliOpts {
input: string | null;
format: string;
projectDir: string;
}
function parseCliArgs(argv: string[]): CliOpts {
const opts: CliOpts = { input: null, format: 'auto', projectDir: process.cwd() };
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--input') {
opts.input = argv[++i] || null;
} else if (arg === '--format') {
opts.format = argv[++i] || 'auto';
} else if (arg === '--project-dir') {
opts.projectDir = argv[++i] || process.cwd();
} else {
throw new Error(`Unknown argument: ${arg}`);
}
}
if (!opts.input) {
throw new Error('Missing required --input <path>');
}
return opts;
}
function main(argv: string[]): void {
const opts = parseCliArgs(argv);
const safePath = requireSafePath(opts.input, path.resolve(opts.projectDir), 'ADR input path', { allowAbsolute: true });
const content = fs.readFileSync(safePath, 'utf8');
const parsed = parseAdrMarkdown(content, { sourcePath: opts.input ?? undefined, format: opts.format });
process.stdout.write(JSON.stringify(parsed, null, 2));
}
if (require.main === module) {
runMain(() => {
try {
main(process.argv.slice(2));
} catch (err) {
// ExitError with a message so runMain's catch writes it verbatim
// (byte-identical to the prior `Error: ${message}\n` process.exit(1)).
throw new ExitError(1, `Error: ${(err as Error).message}`);
}
});
}
export = {
CANONICAL_HEADERS,
normalizeAdrHeader,
parseAdrMarkdown,
shouldRejectAdrStatus,
};