* docs(#3881): answer §8.1's open question and correct three wrong premises ADR-3473 §8.1 carries a blocking open question with a forcing function: it must be answered before any implementation PR for the rule opens. Answered here as (a), a string-coercing adapter, with the measurement that settles it. The sequencing note bet that §8.8's schema would make (b) tractable. Measured against merged reality it does not: only 33 of extractFrontmatter's 78 non-test call sites read STATE.md, and two of the five compensating mechanisms §8.1 lists survive real types, leaving ~31 lines across 3 call sites as the actual prize. Also corrects three claims verified false while answering it. §8.1's justifying sentence names #3349 and #3360 as defects a real parser would fix; both are already fixed on next, confirmed by executing the compiled parser rather than reading it. The guard roster calls lint-frontmatter-scalar-broad-grep.cjs an expected casualty of this rule, but it guards shell grep idioms in workflow bash fences and never touches our parser. The same roster calls lint-vendored-deps.cjs reusable as-is; it is hardcoded to re2js throughout. The last two were caught by applying the rule this amendment records -- a factual claim in this ADR is a hypothesis until the implementing phase executes it -- on its first use. Refs #3881 * docs(#3881): record that §8.1's fork is ill-posed and (a) is not implementable An adversarial pass on the Phase 4 design established by execution that extractFrontmatter is not a YAML parser but a line-oriented scanner whose output is a function of raw source text. Four spellings of the same value collapse to one js-yaml tree but produce four distinct legacy strings, one of them mangled. No adapter over a tree can choose among outputs the tree does not distinguish, so fork (a) -- keep a string-coercing adapter so the existing contract holds -- cannot be built. For any document with a non-scalar value, (a) collapses into (b); about 26 percent of frontmatter-carrying documents have one. Also records three design defects and one new attack surface, all confirmed by execution: catching a parse failure and returning {} would delete the frontmatter block on the next write at eight call sites that conflate empty with unparseable; an empty value yields null where legacy yields {}, and reconstructFrontmatter omits null-valued keys, so the shipped state template's empty progress key would vanish; the #1882 truncation probe is parseYamlRegion itself rather than a pre-parse heuristic, so it cannot both stay unchanged and survive that deletion; and FAILSAFE_SCHEMA still resolves aliases, expanding seven lines to 22.8 MB. The rule is not deferred. The measurement is the deliverable and the re-scoping is recorded as an open question with a forcing function, per section 8's own rule. Refs #3881 * test(#3881): failing-first rows for block scalars, unicode keys and the missing #3594 matrix Creates tests/feat-3594-parser-adversarial-frontmatter.test.cjs, the file the fixture README instructs contributors to register fixtures in but which never existed. Section C: table-driven ownership check over tests/fixtures/adversarial/frontmatter/ so a fixture with no matrix entry fails loudly; six existing fixtures (duplicate-keys, crlf-mixed, unclosed-block, unicode-keys-and-values, null-byte-value, huge-bounded) each get the invariant its README states. B1 blockScalarValueIsNotTheBlockIndicator: parsing commands/gsd/add-tests.md must give argument-instructions the instruction text, not the literal '|'. RED today. B2 blockScalarDoesNotInventATopLevelKey: same parse must not produce a top-level Example key scraped from inside the block body. RED today. B3 unicodeKeyRoundTripsAsIs: the 相 key in unicode-keys-and-values.md must survive parsing; today it is silently dropped. RED today. Refs #3881 * chore(#3881): vendor js-yaml and generalize the vendored-deps guard to a manifest Packaging step for ADR-3473 §8.1: makes js-yaml available to gsd-core/bin/** without promoting it out of devDependencies (promoting broke every installed tree, #3496). gsd-core/bin/lib/vendor/js-yaml.cjs is a verbatim copy of node_modules/js-yaml/dist/js-yaml.js (the self-contained UMD dist bundle, not index.js), exposing load/dump/FAILSAFE_SCHEMA/YAMLException with zero require() calls of its own. src/vendor/js-yaml.d.cts is hand-authored, not copied, because js-yaml ships no upstream .d.ts and @types/js-yaml is not installed. It is deliberately narrow, declaring only the four symbols in use, so anchors/aliases/custom types/loadAll are unreachable from typed code -- a compile-time enforcement of ADR-3473 §8.1's refusal to expand alias resolution for security reasons. Because it has no upstream counterpart it is excluded from the byte-compare. scripts/lint-vendored-deps.cjs is refactored from a script hardcoded to re2js into a table-driven VENDORED manifest (one row per package: upstream/vendored .cjs paths, optional .d.cts paths, twin kind upstream-verbatim vs hand-authored) so a second vendored package does not require a second hardcoded check block, per ADR-3473 §8.3 'one implementation per rule'. The four existing re2js checks (vendored .cjs vs node_modules, vendored .d.cts vs node_modules, src/vendor twin vs bin-side twin, devDependency version pin vs installed version) are preserved unchanged; verified pass/fail identical before and after the refactor, and the guard's ability to fail was re-proven with a deliberate one-byte append to both re2js.cjs and js-yaml.cjs, then restored. docs/INVENTORY.md and docs/INVENTORY-MANIFEST.json (via gen-inventory-manifest.cjs --write, run after build:lib) register vendor/js-yaml.cjs. gsd-core/bin/lib/vendor/README.md documents both vendored packages and the two twin kinds. Refs #3881 * feat(#3881): parse .planning frontmatter with the vendored js-yaml ADR-3473 §8.1: extractFrontmatter's read path is no longer a hand-rolled line scanner. parseYamlRegion, escapeDoubleQuoted, unescapeDoubleQuoted and parseQuotedScalar are deleted (not patched); parsing now goes through the vendored js-yaml (./vendor/js-yaml.cjs) under { schema: FAILSAFE_SCHEMA, json: true }. Everything js-yaml does not do is layered on top, in one place, carrying the seven design-doc consequences: 1. Empty value: a null js-yaml value is coerced to {} (matching legacy's own empty-value contract) so reconstructFrontmatter — which omits null-valued keys — still round-trips a bare `key:` line instead of deleting it. Verified live: progress: with no value survives parse -> reconstruct -> re-parse. 2. Unparseable no longer collapses to a bare {}: a new FRONTMATTER_UNPARSEABLE Symbol (exported), keyed exactly like the existing #3257 FULL_LINE_COMMENTS channel, is carried on the {} returned for malformed/refused YAML. Invisible to Object.keys/entries/JSON.stringify/for-in, so the 70 call sites that never inspect it are unaffected; wiring the 8 hasFrontmatter sites to consult it is a separate change, not done here. 3. Non-scalar object-list items (the four spellings of `- test: a b` that js-yaml collapses into one tree shape) are rendered as a canonical `key: value[, key2: value2]` string per item, keeping the existing array-of-strings value SHAPE. A full corpus differential over all 1702 tracked markdown files found 11 residual divergences from the legacy parser (enumerated in the PR/report), most of them the parser now being MORE correct (a dropped quoted top-level key, the block-scalar/phantom-key defect, a dropped Unicode key). 4. The #1882 truncation probe still runs the one real parser, but derives its key count from js-yaml's own thrown error and mark.line when the whole region doesn't parse cleanly (the dominant real truncation shape: fence opened, well-formed keys, no closing fence). Verified against both the clean-parse and the exception-fallback path. 5. The #3257 comment channel now attributes each pending column-0 comment against js-yaml's own parsed top-level key list (matched by literal key text, in document order) instead of the legacy ASCII-only key regex, so a comment above a Unicode key attaches correctly. 6. Anchors, aliases and merge keys are refused outright (a raw-text pre-scan, since FAILSAFE_SCHEMA still resolves them) — corpus occurrences today: zero. A 7-line billion-laughs fixture is verified refused rather than expanded. 7. A literal U+0000 is swapped for a private-use sentinel before the parse and restored in every resulting string afterward, since js-yaml rejects NUL unconditionally under every schema. escapeDoubleQuoted is deleted and reimplemented via js-yaml's dump() (forced double-quoted style), with control-char hex escapes lowercased to keep serialized output byte-stable (#1779 emitted lowercase); it keeps its exported name and signature for its two other call sites (commands.cts, runtime-artifact-conversion.cts), which need no change. frontmatterDeepEqual, the comment channel, sliceTopLevelFrontmatterSegments, regenerateFrontmatterKey's guard, noOpObjectListSetError and parseMustHavesBlock are all unchanged — retiring them is fork (b) and is not this phase. Refs #3881 * fix(#3881): quote template placeholders and preserve unparseable frontmatter SECURITY.md/UI-SPEC.md/VALIDATION.md wrote frontmatter placeholders as bare {N}/{phase-slug}/{date}, which is valid YAML flow-mapping syntax under the vendored js-yaml parser, not the literal placeholder text intended. Quote them so they parse as strings. Wire the FRONTMATTER_UNPARSEABLE Symbol (exported but unused) at the 8 call sites in state.cts/state-transition.cts that compute hasFrontmatter via Object.keys(extractFrontmatter(...)).length > 0 and reassemble the document without a frontmatter block when false. That check conflated 'no frontmatter' with 'unparseable frontmatter' (both parse to {}), so a document with a merge-conflict marker or refused alias in its frontmatter had that block silently dropped on write. Each site now preserves the exact raw bytes stripFrontmatter removed when the marker is set, leaving the genuinely-empty case unchanged. Refs #3881 * test(#3881): consequence and boundary coverage for the js-yaml migration Rows: A1 emptyValuedKeySurvivesAWrite, A2 unparseableDocumentKeepsItsFrontmatterBlock, A3 unparseableIsDistinguishableFromEmpty, A4 nonScalarValuesCanonicalize, A5 truncationProbeStillFiresOnAnOpenFence, A6 commentsStayOnTheirOwnKey, A7 anchorsAndAliasesAreRefused, A8 aliasExpansionCannotExhaustMemory, F1 UNTERMINATED_KEY_THRESHOLD boundary, F2 alias/nesting refusal bound, F3 frontmatter size boundary (huge-bounded.md + larger). Adds tests/fixtures/adversarial/frontmatter/anchor-alias-bomb.md and its entry in the feat-3594 fixture matrix. Refs #3881 * docs(#3881): document the vendored parser, correct a stale rationale, add a vendoring how-to Refs #3881 * docs(#3881): correct the frontmatter glossary entry Two errors in the entry as first written: it named parseYamlRegion as part of the read path when that function is deleted, and it recorded the eight hasFrontmatter call sites as unwired follow-on work when they were wired in e35ac2a2c. Also records the scope caveat that the CLI write path rebuilds the frontmatter block independently, so the marker binds at the transform layer. Refs #3881 * docs(#3881): record the semantic-migration decision and the counted guard ledger The maintainer chose the full semantic migration over splitting the rule into its own epic or patching the scanner, so section 8.1 is answered as "the fork was ill-posed and the migration is semantic" rather than as (a) or (b). Also replaces the pre-implementation guess that this phase would shrink the guard surface with the counted result: excluding vendored third-party lines the hand-maintained surface is net +307, and frontmatter.cts grew by 68 lines despite four functions being deleted, because the compatibility layer over js-yaml is larger than the scanner it replaced. Section 8.1's stated benefit is therefore not delivered as written; what improved is the kind of code maintained, not the amount. Decision 6 requires recording that rather than netting it away. Refs #3881 * chore(#3881): changeset for the vendored YAML parser migration Refs #3881 * test(#3881): golden parity, round-trip property and packaging coverage Refs #3881 * fix(#3881): refuse anchors structurally and fold in review findings ADR-3473 §8.1 review findings, addressed inline: Finding 1 (BLOCKER): refuseAnchorsAndAliases was a raw-line regex that matched only the bare-key spelling (key: &x). A quoted key ("a": &x), a flow mapping ({b: &x}) and a flow sequence ([&x, *x]) all define/use the SAME anchor mechanics while never matching that line shape, so the exact expansion the guard exists to stop went straight through unrefused (a 303-byte quoted-key bomb expanded to ~35.8MB). Replaced with js-yaml's own `load` `listener` callback, which reports `state.anchor` for every event belonging to an anchored node in every spelling, and throws from inside the callback to abort before any expansion (~1-2ms vs full expand-then-discard). A merge key with an alias is still refused (merge always requires a previously anchored node, so the alias itself trips the listener); a bare merge key with NO alias is no longer separately refused, documented as intentional: FAILSAFE_SCHEMA never resolves `!!merge`, so it carries no expansion risk. Table-driven tests added for all four bypass spellings + merge key, plus a quoted-key-spelled billion-laughs fixture registered in the adversarial matrix and README. Finding 2: src/vendor/js-yaml.d.cts's docblock falsely claimed anchors/ aliases were "simply UNREACHABLE from typed code" through the twin. Corrected to state the truth: anchor/alias resolution is document-level `load` mechanics reachable through exactly the declared surface, and refusal is enforced at RUNTIME (Finding 1's listener), not by the type surface. Finding 3 (MAJOR): the null-byte sentinel (U+E000) round-trip was non-injective — restoreNullBytesDeep rewrote every U+E000 in the parsed tree back to NUL, including one the document author legitimately wrote, silently corrupting it. Now refuses outright whenever the raw region already contains U+E000 (consistent with the existing anchor/merge-key refusal path), making the substitution provably injective. Tests added for a real NUL alone (preserved), a pre-existing U+E000 alone (refused, not corrupted), and both together (refused, not merged into one byte). Finding 4 (MAJOR): scripts/lint-vendored-deps.cjs's `srcTwin` field was dead for a hand-authored row (only read inside the upstream-verbatim branch) — exactly how Finding 2's stale docblock drifted unnoticed. Added checkHandAuthoredTwin: every value-level export the twin DECLARES must be an actual own property of the vendored runtime module at require-time. Tests added, including a sensor that a declared-but-nonexistent export IS caught. Finding 5: the existingFm/hasFrontmatter/stripFrontmatter/fmPrefix/ unparseableFm/reassemble preamble, copy-pasted at 7 sites in state-transition.cts plus a sixth hand-inlined copy in state.cts's cmdStateCompletePhase, is now one exported helper (beginFrontmatterReassembly) every site routes through, including the hand-inlined one. Three call sites (beginPhaseCore, patchCore, updateCore) keep a literal `body = stripFrontmatter(content)` assignment alongside the helper call so scripts/lint-state-write-path-drift.cjs's single-hop backward scan (which does not chase aliases) still sees the strip; stripFrontmatter is pure/idempotent so the extra call changes nothing observable. Finding 6: corrected the frontmatter.cts docblock's stale "wiring is a separate change" claim (the 8 call sites are wired on this branch) and the changeset's backlink from (#3473) to (#3881). Finding 7: fixed the lint:ci failures blocking the gate — an @typescript-eslint/only-throw-error violation from throwing a bare Symbol as the anchor-detected signal (now a real Error subclass), unused-var warnings left over from the Finding 5 refactor, a lint-test-file-count cap exceeded by two migration-specific test files (allowlisted with justification), and the lint-state-write-path-drift false positive from Finding 5's helper (fixed above). tests/frontmatter-golden-parity.test.cjs:117's execFileSync already carried an explicit timeout; no change was needed there. Golden fixture: added a golden entry for the new anchor-alias-bomb-quoted.md fixture ({} — matches what the legacy line scanner would also produce, since it independently dropped every quoted top-level key). No other corpus document diverges: real .planning/ documents carry zero anchors/aliases/merge keys/U+E000 today. Refs #3881 * fix(#3881): fold in second-round review findings Finding 1 (BLOCKER): tests/frontmatter.test.cjs pinned the pre-migration ASCII-only key regex for the Unicode fixture; updated to require the 相 key's value now that js-yaml has no such restriction. Audited the rest of the file for other pre-migration pins (block scalars, quoted keys, flattened values, empty values, duplicate keys, unclosed blocks, null bytes) by execution against real fixtures; found none regressed. Finding 2: parseYamlRegion and escapeDoubleQuoted renamed to parseGuardedYamlRegion and escapeDoubleQuotedScalar in src/frontmatter.cts so no function still answers to the deleted hand-rolled scanner's name (ADR-3473 §8.1 "deleted, not patched"). escapeDoubleQuotedScalar's three external call sites (src/commands.cts, src/runtime-artifact-conversion.cts) updated in the same change — a mechanical rename, not an ADR-amendment matter. Finding 3 (BLOCKER): fixed a real crash and a silent data-loss bug found by execution. A top-level key named constructor/__proto__/toString/ valueOf/hasOwnProperty crashed reconstructFrontmatter (bracket read resolving an inherited Object.prototype member); a key literally named __proto__ was silently DROPPED entirely (bracket assignment on an ordinary {} invoked the inherited __proto__ setter instead of creating a data property). Fixed by building every parsed Frontmatter object with Object.create(null), and replacing an `in` check with hasOwnProperty.call in propagateCommentChannel. Added round-trip tests for all five hostile keys, each with its own leading comment. Finding 4 (MAJOR): escapeDoubleQuotedScalar's docstring falsely claimed full byte-stability across the migration. Verified by execution: BEL/NUL/ NEL/NBSP/LS/PS/BOM now emit YAML-named escapes instead of the old hex/raw- literal forms. Proved round-trip equivalence (each escape re-parses to the exact source codepoint) and corrected the docstring. Found and fixed a related real defect while verifying: a lone UTF-16 surrogate was emitted BARE (scalarNeedsDoubleQuoting didn't trigger), producing genuinely unparseable YAML that silently collapsed to {} on re-read — extended scalarNeedsDoubleQuoting to route surrogates through the quoted+escaped path. Finding 5 (MAJOR): countKeysBeforeTruncation went silent on 4 real truncation shapes (unquoted colon, open flow collection, mis-indented sibling key, refused anchor). Root cause: the mark-based prefix recovery excluded the very line whose key needed counting, and a mark-less refusal never entered the recovery branch at all. Fixed by taking the max of two lower bounds: the longest parser-verified line-prefix, and a raw-text count of key-shaped lines (reusing the same key-shape pattern this file already uses for isFrontmatterShaped). Extended test-matrix row A5 table-driven over all 4 regressed shapes. Finding 6: the design doc's claim that no test owned the #3594 adversarial fixture corpus was false — consolidation epic #1969 had already folded it into tests/frontmatter.test.cjs. An earlier commit on this branch re-created a standalone duplicate under that false premise; folded its genuinely-new coverage (fixture-ownership check, anchor-bomb fixtures, block-scalar B1/B2 rows) into frontmatter.test.cjs and deleted the duplicate file. Corrected the false claims in 40-design.md §3.3.1 and the ADR's §8.1 note, including the roadmap-sibling claim (no such file exists). Finding 7: the golden serializer sorted object keys, making it structurally blind to the key-order-parity invariant ADR-3473 §8.1 actually claims. Made it order-preserving and regenerated the golden fixture from a standalone compile of the legacy (pre-#3881) parser at ddde001af; the current parser matches it with zero undocumented divergences, confirming key-order parity genuinely holds. Extended row A2 table-driven across 6 of the remaining 7 transitionCore kinds (all pass) plus documented, by execution, a newly-discovered 8th-site regression: state.cts's cmdStateCompletePhase calls the same preservation helper but its result is clobbered by a later unconditional resync — filed as a distinct finding rather than fixed here (touches syncAndPreserveStateMd, outside this change's verified scope). Refs #3881 * fix(#3881): preserve unparseable frontmatter through the CLI write path Characterization (executed, before/after shown): case (b), not (a). The frontmatter FENCE survives — `state complete-phase` on a conflict-marked STATE.md returns success and a well-formed, freshly-derived frontmatter block, not a document with no frontmatter at all. But the block's actual content (the merge-conflict markers, and with them any signal to a human that the document was in conflict) is silently discarded and replaced. Root cause was two clobber sites, not one: 1. syncStateFrontmatter (src/state.cts) re-parses the already-preserved `transformedContent` from readModifyWriteStateMd, finds {} + the FRONTMATTER_UNPARSEABLE marker, and unconditionally rebuilt a fresh frontmatter block from the body anyway. 2. Even after (1) is fixed, applyPostSyncPreservation's own postFm/applyStatePreservation/authoritativeFm-reassertion machinery re-extracts frontmatter from syncedContent, restores curated fields from the pre-write snapshot, and reconstructs a NEW block again — confirmed live via `state begin-phase`, which still lost the markers after fixing (1) alone. Both are now guarded by the same predicate (isUnparseableFrontmatter, checking FRONTMATTER_UNPARSEABLE): when the ORIGINAL frontmatter did not parse and the caller is not on ADR-3408 §8.3's closed "body wins" list, both functions return their input content unchanged rather than re-deriving over it. The closed list (cmdStateSync #905, /gsd-health --repair's REGENERATE_STATE, both routed only through writeStateMd, which never reaches applyPostSyncPreservation and passes sanctionedPermanentEmptyFallback=true to syncStateFrontmatter) is untouched — neither widened nor narrowed; verified by execution that `state sync` still overwrites the conflict-marked block exactly as before. Other verbs sharing the same readModifyWriteStateMd path were checked and were equally affected before this fix: state update, query state.patch, and state begin-phase all lost the conflict markers (RED, shown by execution), and all three now preserve them (GREEN). Covered table-driven in tests/feat-3881-yaml-parser-consequences.test.cjs's new A2b describe block, which drives the real CLI verbs via runGsdTools — not just the pure transitionCore layer the earlier A2 rows exercised — plus a control asserting state sync's body-wins contract is unchanged. Refs #3881 * fix(#3881): restore the parse surface's prototype and fix remote-runner failures Root cause of the bulk of the 88 remote-runner failures: extractFrontmatter/parseGuardedYamlRegion handed back Object.create(null) trees for prototype-pollution safety, but assert.deepStrictEqual compares prototypes, so every assertion against a plain object literal failed (57 frontmatter.unit.test.cjs + 5 frontmatter.test.cjs + others). Fixed by keeping the internal construction null-prototype (unchanged) and converting to a plain-prototype tree via Object.defineProperty (never bracket assignment, so __proto__/constructor/toString keys stay safe) at the parseGuardedYamlRegion/unparseableResult return boundary only; the internal FULL_LINE_COMMENTS Symbol channel is copied by reference, not recursed, so its own __proto__-safety is untouched. Per-class fixes: (1) bomAcrossArtifactTypes was the same prototype bug, no separate code change needed. (2) frontmatter-cli #1660: added objectListFieldWouldLoseData, a broader lossy-field detector alongside the existing byte-identical noOpObjectListSetError -- js-yaml's flattenObjectListItem now correctly includes every sub-key of an object-list item (a real bug fix over the legacy scanner, which silently dropped every field but the first), so a set that drops that now-included data is no longer byte-identical to the original and needs its own guard. (3) uat.test.cjs: updated the pinned expectation for the human_verification quote-stripping artifact -- js-yaml resolves quoting correctly where the legacy regex left an unbalanced quote; documented as an intentional, non-lossy behavior change. (4) smart-entry: added a fallback-only loadWithAmbiguousColonRepair so a column-0 key: value line whose value itself contains an unquoted colon (the #2571 hand-edited-STATE.md shape) round-trips instead of failing the whole frontmatter block closed. (5) frontmatter.unit.test.cjs bracket-array leniency: added a second fallback, repairMalformedInlineArrays, restoring the legacy scanner's tolerant inline-array handling (consecutive/blank commas, unclosed bracket) -- both repairs run ONLY after the primary parse already threw, so well-formed documents are unaffected. (6) prompt-injection-scan: src/frontmatter.cts had a literal U+FEFF BOM embedded in a comment illustrating the #2977 fix; replaced with the U+FEFF text escape. (7) eslint-glob-coverage: allowlisted the new src/vendor/js-yaml.d.cts vendored type declaration, same precedent as the existing re2js.d.cts entry. (8) frontmatter-golden-parity: git ls-files *.md now runs with -c safe.directory=* (process-scoped) so it survives the remote runner's dubious-ownership check without a persistent git config write. Refs #3881 * chore(#3881): backfill changeset PR number Refs #3881 * test(#3881): make golden parity resistant to unrelated tree churn A corpus-wide snapshot keyed to every tracked *.md file was coupled to mutable-by-design files: .changeset/*.md's pr:0 -> real-PR-number backfill is a required workflow step, not a parser change, yet it turned this suite red. Training people to 'just regenerate the golden' on that kind of failure defeats the point of the snapshot. Exclude .changeset/** from the golden corpus entirely, tolerate tracked *.md files with no golden entry (they postdate the capture) instead of failing on them, keep hard failures for a golden entry whose file has vanished from the tree and for any real parity divergence, and add a coverage floor so the enumeration cannot quietly degrade to comparing a handful of files. Golden regenerated by recompiling the legacy pre-migration parser (git show ddde001af:src/frontmatter.cts) standalone, independent of the current parser, over the same non-changeset corpus. Refs #3881 * test(#3881): make the parser golden hermetic instead of tree-keyed This repo merges ~21 commits/day; a 14-day sample measured 937 touches of the exact files (commands/gsd/*.md, gsd-core/workflows/*.md, agents/*.md, docs/*.md) the prior golden pinned by tracked path. Any PR editing one of those files' frontmatter for reasons unrelated to the parser (an argument-hint addition, an allowed-tools tweak) turned the suite red, and the reflex fix -- "regenerate the golden" -- overwrote the very snapshot meant to catch a real regression. Excluding .changeset/** was not enough; the design itself was wrong: a regression fixture must not be keyed to mutable repo paths, and a single 376-entry JSON every such PR touches is also a guaranteed merge-conflict surface. Rebuilt the fixture to carry its own documents: each of 51 entries stores a stable id, literal documentText (shrunk from a real ddde001af-era corpus document), and an expectedParse captured independently from the pre-migration legacy parser (git show ddde001af:src/frontmatter.cts, compiled standalone against its byte-identical sibling modules). The test reads no tracked path, shells out to no git command, and enumerates no tree -- a PR editing commands/gsd/help.md cannot affect it. Every entry's reconstruction was verified at capture time to reproduce both the current and legacy parser's output on the original document; 0 of 51 candidates were dropped by that check (1, the deliberately-unterminated unclosed-block.md adversarial fixture, has no closing fence to truncate at and is stored unshrunk). Kept the 5 documented DIVERGENCES rows (now diverges:true entries) and the D2 order-preserving structural serializer that keeps the comparison from passing vacuously; dropped the tree-enumeration helpers, the coverage floor, the post-capture-skip logic, and the vanished-file check -- all artifacts of the path-keyed design. Refs #3881 * fix(#3881): resolve vendored-deps paths independently of cwd shape Five rows in tests/lint-vendored-deps-manifest.test.cjs failed on windows-latest CI: the test passed absolute scratch-file paths into compareFiles()/checkRow(), whose helpers joined every input onto ROOT via path.join(ROOT, rel), producing garbage when the input was already absolute. It surfaced on windows-latest specifically because GitHub's Windows runners checkout the repo on a different drive than TEMP, so path.relative(REPO_ROOT, tmpFile) returned the absolute path unchanged (no relative traversal is representable across drives) rather than the relative form the test assumed. The remote gsd-test runner this repo gates pushes on is Linux-only and could never have caught this; GitHub CI's windows-latest job is the only signal that does, and it did. Fixed the helper itself (scripts/lint-vendored-deps.cjs's new resolvePath()) to treat an already-absolute input as absolute-in, absolute-out instead of silently mis-joining it, and updated the test to pass the scratch file's absolute path directly rather than relying on a relative conversion that is not always representable. Kept every mutation-sensor assertion intact and added coverage proving resolvePath is a no-op for relative inputs and correctly passes absolute ones through unchanged. Refs #3881 * fix(#3881): warn when state sync regenerates over unparseable frontmatter state sync (ADR-3408 §8.3's sanctioned regenerate path) correctly overwrites an unparseable frontmatter block per its 'body wins' contract — that overwrite behavior is unchanged here. The defect was the silence: synced:true/exit 0 gave no signal that the existing block (including git merge-conflict markers) could not be parsed and was destroyed, per ADR-3473 §8.5 ('a derived conclusion may not be reported as authoritative when the derivation dropped input it could not resolve') and §8.4 ('failure is a value'). Adds a gsd: warning — ... (#3881) line on stderr, matching the existing #3573 precedent, and surfaces the same disclosure in the JSON result's existing changes[] array so a machine consumer sees it too. Exit code and synced:true are left unchanged — sync did what its contract says. REGENERATE_STATE (/gsd-health --repair's sibling on the same sanctioned-regenerate list) is DESTRUCTIVE-risk and unconditionally refused by applyRepairs's dispatcher before runRepairAction ever runs (src/health-diagnostic.cts), so it is not a live path today and is not in scope for this fix. Refs #3881 * fix(#3881): exit non-zero when a state command returns an error Refs #3881 * chore(#3881): changeset for the state exit-code fix Refs #3881 * fix(#3881): honor the documented --project-dir flag Refs #3881 * revert(#3881): restore exit-0 result envelopes for state errors Reverts 9638f2936 and its changeset. The change was wrong and the revert is the correction. This repo distinguishes two error mechanisms deliberately. error() in src/io.cts writes to stderr and calls process.exit(1) -- the hard-failure path. output({error: ...}) writes a JSON result envelope to stdout and returns normally with exit 0. The reverted commit converted 23 result-envelope sites into hard failures, which is a different contract, not a bug fix. tests/state-contract.test.cjs's errorPathDoesNotPublish asserts the envelope contract directly -- a failing command exits 0 with a JSON error envelope and must not publish state.json -- and the remote matrix run caught it along with four cases in the QA scenario walk. Thirteen tests in tests/state.test.cjs that the original commit rewrote were encoding that real contract, not the bug it claimed; they are restored. Whether an error envelope on stdout with exit 0 is the right CLI design is a genuine question, and it is section 8.4's rule ('failure is a value') with its own phase. It is not something to flip inside this PR. Refs #3881 * chore(#3881): backfill changeset PR number for the project-dir fix Refs #3881 * test(#3881): keep the frontmatter mutation shard inside its time budget The Stryker (frontmatter) shard hit the documented 15-minute (900s) shard cap. Root cause is NOT row-level spawn overhead (contrast the #2790/ core-utils precedent): the three shard test files' own logic runs in ~413ms total (356+30+27ms) with all 392 assertions passing. Instead, src/frontmatter.cts grew from ~825 to 1496 lines (+671/-187) migrating to the vendored YAML parser, proportionally growing the mutant count Stryker generates for gsd-core/bin/lib/frontmatter.cjs. Stryker's command runner bills the full 'node --test <3 files>' invocation once per mutant, and node:test's default per-file process isolation forks a child process for each of the three files on every one of those invocations — pure fork overhead multiplied by a much larger mutant population. Fix: scripts/mutation-matrix.cjs COVERED.frontmatter now declares isolation: 'none', and .github/workflows/mutation.yml passes --test-isolation=${{ matrix.isolation }} (defaulting to 'process' — i.e. unchanged behavior — for the other 8 shards, which were not individually audited for cross-file state leakage under shared-process execution). Measured locally via node:test's run() API on the exact 3-file set: isolation:'process' took ~593ms vs isolation:'none' ~478ms for the same 392 passing assertions. The true CI-shard number can only be confirmed on the GitHub Actions run (Stryker cannot run locally, and 'node --test' is hard-blocked in this environment). Refs #3881 * test(#3881): register the vendored-parser tests in the frontmatter mutation shard stryker.config.mjs's own rule ("Keep this list in sync with the tests arrays in scripts/mutation-matrix.cjs COVERED") was violated: #3881 grew src/frontmatter.cts from ~825 to 1496 lines but its new tests (tests/feat-3881-yaml-parser-consequences.test.cjs, tests/frontmatter-golden-parity.test.cjs, tests/frontmatter-roundtrip.property.test.cjs, and +167 lines in tests/frontmatter.test.cjs) were never added to the frontmatter shard's tests array, so Stryker's mutants in the new vendored-js-yaml adapter had nothing constraining them. PR #3888 measured 55.8% against the 65 floor (748 killed / 593 survived / 17 timeout) and the shard was separately cancelled at 15m04s against the 15-minute per-shard cap. Registers all four files (each earns its slot on evidence of a unique constraining assertion, documented inline), gives the shard a measured/projected 180-minute budget via a new per-module timeoutMinutes field threaded through mutation.yml's job-level timeout-minutes the same way isolation is threaded, and removes the prior isolation:'none' override (re-measured at this file-set size, its savings are within run-to-run noise, not worth the unaudited cross-file-state-leakage risk). Refs #3881 * feat(#3881): derive the mutation test list and ratchet the score floor Refs #3881 * test(#3881): ratchet five stale mutation floors and close the frontmatter gap Raised five module minScore floors per CI run 33012034388 (floor(achieved)-1): config-schema 75.51%->74, prompt-budget 88.95%->87, context-composer 79.92%->78, context-utilization 92.31%->91, active-workstream-store 87.42%->86. Updated both scripts/mutation-matrix.cjs COVERED entries and tests/mutation-matrix-ratchet.test.cjs RATCHET_BASELINE in the same diff per the ratchet's own contract. Closed the frontmatter shard's 63.03%-vs-65 gap with new behavioral tests in tests/feat-3881-yaml-parser-consequences.test.cjs, each paired with a documented near-miss: frontmatterDeepEqual's array-order/length/type-mismatch/key-order semantics (via spliceFrontmatter's no-op guard), scalarNeedsDoubleQuoting's leading/trailing-whitespace and dash/surrogate triggers (via reconstructFrontmatter), repairAmbiguousColonValues' already-quoted vs ambiguous-colon repair paths (via extractFrontmatter), and the null-byte sentinel round-trip surviving at region offset 1. Did not lower minScore. Refs #3881 * test(#3881): decouple the ratchet test from real module floors The CLI end-to-end rows in tests/mutation-score-ratchet.test.cjs hardcoded config-schema's real floor (52), which commit 973321541 legitimately ratcheted to 74 -- breaking a test pinned to the exact value the mechanism under test exists to change. Add an injectable --matrix seam to scripts/check-mutation-score-ratchet.cjs and point the CLI rows at a synthetic module + synthetic floor built via a temp fixture, so the rows are indifferent to any real module's floor moving while still exercising the same fail/pass behaviour. Refs #3881 * refactor(#3881): parse must_haves with the vendored parser and drop re-implemented leniency Refs #3881 * fix(#3881): restore the ambiguous-colon repair its hand-edited-STATE.md contract needs A tracked-document sweep of 910 *.md files cannot see this dependent: repairAmbiguousColonValues's one real caller is user hand-edited STATE.md content that never lives in this repo's tree, only on end users' machines, and is pinned by tests/smart-entry.unit.test.cjs. Restores the function plus its post-throw fallback path (loadWithAmbiguousColonRepair) only; repairMalformedInlineArrays and splitLegacyInlineArrayItems stay deleted, reverified against the full frontmatter test shard. Adds a frontmatter-level regression row in tests/feat-3881-yaml-parser-consequences.test.cjs so the dependency is visible where the function lives. Closes #2571 Refs #3881 --------- Co-authored-by: sim <sim@local>
805 lines
41 KiB
JavaScript
805 lines
41 KiB
JavaScript
#!/usr/bin/env node
|
||
'use strict';
|
||
|
||
/**
|
||
* scripts/mutation-matrix.cjs
|
||
*
|
||
* Single source of truth for the ADR-457 Stryker mutation gate dynamic matrix.
|
||
*
|
||
* Computes which covered modules changed vs a base ref and emits a GitHub
|
||
* Actions matrix JSON so CI can run one Stryker shard per changed module in
|
||
* parallel rather than a single serial run over all modules.
|
||
*
|
||
* Usage:
|
||
* node scripts/mutation-matrix.cjs --base origin/next
|
||
* printf 'src/config-schema.cts\n' | node scripts/mutation-matrix.cjs
|
||
* node scripts/mutation-matrix.cjs --base origin/next --print
|
||
*
|
||
* Output (stdout, default): JSON object
|
||
* {
|
||
* "has_work": "true"|"false",
|
||
* "matrix": {
|
||
* "include": [
|
||
* { "name": "<module>", "mutate": "gsd-core/bin/lib/<module>.cjs", "tests": "<space-joined test files>" },
|
||
* ...
|
||
* ]
|
||
* }
|
||
* }
|
||
*
|
||
* Exit codes: 0 always (empty matrix is not an error, has_work "false").
|
||
*/
|
||
|
||
const { execFileSync } = require('child_process');
|
||
const fs = require('fs');
|
||
|
||
const { ExitError, runMain } = require('./lib/cli-exit.cjs');
|
||
|
||
// ── Resilient stdin reader ────────────────────────────────────────────────────
|
||
// On macOS, libuv sets the stdin pipe fd to non-blocking mode. A synchronous
|
||
// readFileSync(process.stdin.fd) can therefore throw EAGAIN ("resource
|
||
// temporarily unavailable") when the writer hasn't yet filled the pipe — this
|
||
// is intermittent under heavy CI shard load and causes a spurious status 2
|
||
// exit. We work around it by calling fs.readSync in a loop and retrying on
|
||
// EAGAIN with a 1 ms synchronous pause (Atomics.wait on a fresh SharedArrayBuffer
|
||
// — no hot spin, no real-clock dependency, works under --experimental-vm-modules).
|
||
/**
|
||
* Read all of stdin synchronously, retrying on EAGAIN.
|
||
*
|
||
* @returns {string} UTF-8 decoded full stdin content.
|
||
*/
|
||
function readStdinSync() {
|
||
const BUF_SIZE = 64 * 1024; // 64 KB chunks
|
||
const buf = Buffer.allocUnsafe(BUF_SIZE);
|
||
const chunks = [];
|
||
|
||
for (;;) {
|
||
let bytesRead;
|
||
try {
|
||
bytesRead = fs.readSync(process.stdin.fd, buf, 0, BUF_SIZE, null);
|
||
} catch (err) {
|
||
if (err.code === 'EAGAIN') {
|
||
// Non-blocking pipe not yet ready — yield for ~1 ms then retry.
|
||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1);
|
||
continue;
|
||
}
|
||
if (err.code === 'EOF') {
|
||
break;
|
||
}
|
||
throw err;
|
||
}
|
||
if (bytesRead === 0) {
|
||
break; // Clean EOF
|
||
}
|
||
chunks.push(Buffer.from(buf.slice(0, bytesRead)));
|
||
}
|
||
|
||
return Buffer.concat(chunks).toString('utf8');
|
||
}
|
||
|
||
// ── Per-module mutation score ratchet ─────────────────────────────────────────
|
||
// ADR-456 / issue #1187: every covered module declares a minScore floor.
|
||
//
|
||
// HOW THE RATCHET WORKS:
|
||
// • minScore locks in the current measured mutation score (minus a 1–2 pt
|
||
// margin for run-to-run timeout variance).
|
||
// • CI fails a shard if the module's live score drops below its minScore.
|
||
// • Raise minScore (never lower) as a module's tests improve.
|
||
// • The goal is every module reaching TARGET_MUTATION_SCORE (80).
|
||
//
|
||
// GOODHART SAFETY: scores are improved by writing genuine behavioural
|
||
// assertions that kill real mutants — never by adding brittle exact-string
|
||
// matches on incidental output. A justified `// Stryker disable` on a
|
||
// confirmed equivalent mutant is acceptable.
|
||
//
|
||
// HOW TO UPDATE:
|
||
// 1. The per-module Stryker shard CANNOT be run locally: Stryker's command
|
||
// runner invokes `node --test` once per mutant (see stryker.config.mjs),
|
||
// and this repo hard-blocks local `node --test` via
|
||
// .claude/hooks/block-local-node-test.sh. Push the branch instead and
|
||
// let CI run the shard for the changed module.
|
||
// 2. Read the measured score from the CI shard's output.
|
||
// 3. Set minScore = floor(measured) - 1 (never lower than current value)
|
||
// and update the matching RATCHET_BASELINE entry in the same diff.
|
||
// 4. Open/update the PR — the CI gate will enforce the new floor on every
|
||
// future run.
|
||
|
||
/** Long-run target for all modules (ADR-456). */
|
||
const TARGET_MUTATION_SCORE = 80;
|
||
|
||
// ── Derived test-list engine (#3881 follow-up, "one YAML parser" mutation-matrix
|
||
// piece 2) ─────────────────────────────────────────────────────────────────
|
||
//
|
||
// PROBLEM THIS REPLACES: `tests: [...]` used to be a hand-maintained array per
|
||
// module, and `stryker.config.mjs`'s DEFAULT_TEST_CMD hand-duplicated the union
|
||
// of every such array in a second literal. The two drifted independently — PR
|
||
// #3888 shipped four new frontmatter test files that were never added to either
|
||
// list, so their mutants had zero constraining coverage and the shard's score
|
||
// silently fell (see the frontmatter entry's own PR #3888 note below, kept for
|
||
// history). A hand list can be forgotten; a derivation cannot forget a file that
|
||
// exists on disk.
|
||
//
|
||
// SIGNAL: a test file that directly `require()`s a covered module's built
|
||
// artifact (`gsd-core/bin/lib/<name>.cjs`) is declaring, by that require, that
|
||
// it constrains that module. That signal alone is far too broad to feed a
|
||
// per-mutant re-run budget: measured directly (no filter) against this tree,
|
||
// config-schema alone picks up 22 files most of this repo's spawn-heavy
|
||
// integration suites require incidentally for fixture setup — including a file
|
||
// literally named `graphify-auto-update.slow.test.cjs`. Stryker's command
|
||
// runner re-runs the WHOLE test command once per mutant, so an incidental
|
||
// integration require would multiply that module's shard cost by 10-20x for
|
||
// zero mutation-killing benefit (those suites do not assert on config-schema's
|
||
// internals; they merely load it as a dependency of something else under test).
|
||
//
|
||
// NARROWING RULE: a file is auto-derived into a module's shard only when BOTH
|
||
// hold:
|
||
// 1. it directly requires that module's `gsd-core/bin/lib/<name>.cjs`, and
|
||
// 2. its own filename starts with the module's name followed by `.` or `-`
|
||
// (i.e. `<name>.test.cjs`, `<name>.unit.test.cjs`, `<name>.property.test.cjs`,
|
||
// `<name>-anything.test.cjs`) — the file DECLARES itself, by its own name,
|
||
// to be that module's dedicated test surface. This is the exact naming
|
||
// shape every entry in this registry already used before this change
|
||
// (`*.property.test.cjs` / `*.unit.test.cjs`, or `<name>.test.cjs`), now
|
||
// made load-bearing instead of merely conventional.
|
||
// Measured effect of narrowing config-schema this way: 22 candidates -> 1
|
||
// (config-schema.property.test.cjs, the file already in the shard) — the
|
||
// naming filter is what keeps the derivation from silently tripling that
|
||
// shard's cost, per the piece-2 "watch the cost consequence" requirement.
|
||
//
|
||
// ESCAPE HATCHES (both REQUIRED to be explicit, reasoned per-module entries —
|
||
// never a silent list):
|
||
// - `extraTests`: files that constrain this module (genuinely, by writing
|
||
// assertions against its behaviour) but do not match the naming rule
|
||
// above — either because the module was extracted from another file's
|
||
// tests (context-composer) or because the file's own name follows a
|
||
// different, still-legible convention (feat-3881-yaml-parser-consequences.test.cjs).
|
||
// - `excludeTests`: files that DO match the naming + require signal above
|
||
// (so the derivation would otherwise auto-include them) but are
|
||
// deliberately withheld from the per-mutant shard for a measured,
|
||
// documented reason (almost always: they are integration-shaped and
|
||
// spawn a subprocess per case, so Stryker's per-mutant re-run of the
|
||
// whole file cannot finish inside the shard's timeout — the exact #2790
|
||
// planning-inspect.test.cjs precedent this file already documented before
|
||
// this change; the derivation engine now enforces that precedent by
|
||
// construction instead of leaving it to reviewer memory).
|
||
// `computeModuleTests` combines all three into the final `tests` array; the
|
||
// guard `scripts/lint-mutation-test-derivation-drift.cjs` independently
|
||
// verifies every SIGNAL-matching file (require + naming rule, unfiltered by
|
||
// this module's own excludeTests) has an explicit disposition — auto-derived,
|
||
// named in extraTests, or named in excludeTests — so a file that newly starts
|
||
// matching the naming rule (like #3888's four files would have, had they been
|
||
// named `frontmatter*`) cannot silently fall through the cracks again.
|
||
const TESTS_DIR = require('node:path').join(__dirname, '..', 'tests');
|
||
let _testRequireCache = null;
|
||
|
||
/**
|
||
* Scan every `tests/*.test.cjs` file once and cache, per covered module name,
|
||
* which files directly `require('gsd-core/bin/lib/<name>.cjs')` (or a relative
|
||
* equivalent — `../gsd-core/bin/lib/<name>` etc. — the require path always
|
||
* ends in the literal segment matched below). Pure w.r.t. process lifetime;
|
||
* the tests/ directory does not change while this process runs.
|
||
*
|
||
* @returns {Map<string, Set<string>>} module name -> Set of basenames (e.g. 'frontmatter.test.cjs')
|
||
*/
|
||
function scanTestRequires() {
|
||
if (_testRequireCache) return _testRequireCache;
|
||
const REQUIRE_RE = /require\(\s*['"](?:[./]*)?gsd-core\/bin\/lib\/([a-zA-Z0-9_-]+)(?:\.cjs)?['"]\s*\)/g;
|
||
const cache = new Map();
|
||
let entries;
|
||
try {
|
||
entries = fs.readdirSync(TESTS_DIR).filter((f) => f.endsWith('.test.cjs'));
|
||
} catch {
|
||
entries = [];
|
||
}
|
||
for (const file of entries) {
|
||
const text = fs.readFileSync(require('node:path').join(TESTS_DIR, file), 'utf8');
|
||
let m;
|
||
REQUIRE_RE.lastIndex = 0;
|
||
while ((m = REQUIRE_RE.exec(text))) {
|
||
const mod = m[1];
|
||
if (!cache.has(mod)) cache.set(mod, new Set());
|
||
cache.get(mod).add(file);
|
||
}
|
||
}
|
||
_testRequireCache = cache;
|
||
return cache;
|
||
}
|
||
|
||
/**
|
||
* Every test file that directly requires `<moduleName>`'s built artifact —
|
||
* the FULL, unfiltered signal set (used by the derivation-drift guard, which
|
||
* must see every candidate regardless of naming, so it can demand an explicit
|
||
* disposition for each one).
|
||
*
|
||
* @param {string} moduleName
|
||
* @returns {string[]} sorted basenames
|
||
*/
|
||
function findRequiringTestFiles(moduleName) {
|
||
const set = scanTestRequires().get(moduleName);
|
||
return set ? [...set].sort() : [];
|
||
}
|
||
|
||
/** True when `file`'s own name declares it a dedicated test surface for `moduleName`
|
||
* (`<moduleName>.test.cjs`, or starts with `<moduleName>.` / `<moduleName>-`). */
|
||
function matchesModuleNamingRule(moduleName, file) {
|
||
return file === `${moduleName}.test.cjs`
|
||
|| file.startsWith(`${moduleName}.`)
|
||
|| file.startsWith(`${moduleName}-`);
|
||
}
|
||
|
||
/**
|
||
* Auto-derived candidates for `moduleName`: requires the module's artifact AND
|
||
* matches the naming rule. Does NOT apply that module's own `excludeTests` —
|
||
* callers combine that separately (`computeModuleTests` for the real shard,
|
||
* the guard for candidate enumeration).
|
||
*/
|
||
function deriveNamedTests(moduleName) {
|
||
return findRequiringTestFiles(moduleName).filter((f) => matchesModuleNamingRule(moduleName, f));
|
||
}
|
||
|
||
/**
|
||
* Final `tests` array for a COVERED entry: auto-derived (require + naming
|
||
* rule) UNION `extraTests` MINUS `excludeTests`, sorted, each prefixed
|
||
* `tests/`. Throws if `excludeTests` names a file that isn't actually a
|
||
* derived candidate (an exclusion of nothing is a stale/typo'd entry, not a
|
||
* real decision) or if `extraTests` names a file already auto-derived (that
|
||
* would silently mask which mechanism is responsible for its presence).
|
||
*/
|
||
function computeModuleTests(moduleName, entry) {
|
||
const derived = new Set(deriveNamedTests(moduleName));
|
||
const extra = entry.extraTests || [];
|
||
const exclude = entry.excludeTests || [];
|
||
for (const f of extra) {
|
||
if (derived.has(f)) {
|
||
throw new Error(`mutation-matrix: COVERED['${moduleName}'].extraTests names '${f}', which is already auto-derived — remove it from extraTests (it is redundant and hides which mechanism includes it)`);
|
||
}
|
||
}
|
||
for (const f of exclude) {
|
||
if (!derived.has(f)) {
|
||
throw new Error(`mutation-matrix: COVERED['${moduleName}'].excludeTests names '${f}', which is not an auto-derived candidate for this module — remove the stale exclusion`);
|
||
}
|
||
}
|
||
const excludeSet = new Set(exclude);
|
||
const final = new Set();
|
||
for (const f of derived) if (!excludeSet.has(f)) final.add(f);
|
||
for (const f of extra) final.add(f);
|
||
return [...final].sort().map((f) => `tests/${f}`);
|
||
}
|
||
|
||
// ── Single source of truth: covered modules ───────────────────────────────────
|
||
// Each entry: { cjs: '<built artifact>', extraTests: [...], excludeTests: [...], minScore: N }
|
||
// `tests` is no longer hand-written — computeModuleTests() derives it below
|
||
// from direct `require()`s of the module's artifact (see the derivation-engine
|
||
// header above). extraTests/excludeTests are the two REQUIRED, reasoned escape
|
||
// hatches; leave both `[]` (omit the key) when a module needs neither.
|
||
//
|
||
// minScore is the CI break threshold for this module's shard.
|
||
// Floors are measured scores minus 1–2 pts for run-to-run variance.
|
||
// Measured CI scores 2026-06-14 (issue #1187, timeout-free — source of truth):
|
||
// context-utilization 92.31% → floor 80 (target already met)
|
||
// prompt-budget 68.33% → floor 66 (local was 99.6% — TIMEOUT INFLATION; CI is the truth)
|
||
// frontmatter 63.35% → floor 62 (SUPERSEDED — see 2026-08-25 below)
|
||
// adr-parser 69.30% → floor 68
|
||
// config-schema 54.55% → floor 52 (local was 69.7% — TIMEOUT INFLATION; CI is the truth)
|
||
// active-workstream-store 81.91% → floor 80
|
||
// core-utils 77.52% → floor 75
|
||
//
|
||
// Measured CI score 2026-08-25 (#3706, PR 3867):
|
||
// frontmatter 66.67% → floor 65
|
||
// #3706 added agentScalarNeedsDoubleQuoting to frontmatter.cts and exported
|
||
// escapeDoubleQuoted, but the tests constraining them lived in
|
||
// tests/runtime-converters.test.cjs, which this lane does NOT run — the same trap the
|
||
// #1882 note on the frontmatter entry describes. The shard fell to 60.58 and broke the
|
||
// floor. Direct unit tests for both were added to tests/frontmatter.unit.test.cjs, each
|
||
// clause paired with a near-miss that must answer the opposite way, which took the module
|
||
// above its pre-change score. Floor ratcheted per the HOW TO UPDATE formula above, and
|
||
// RATCHET_BASELINE — which lives in tests/mutation-matrix-ratchet.test.cjs, not here — is
|
||
// updated in the same diff as that procedure requires.
|
||
//
|
||
// PR #3888 (#3881 follow-up): the frontmatter shard's new tests were never registered here
|
||
// (only the pre-existing frontmatter.property/unit + unusable-input ran), so Stryker's
|
||
// mutants in the new vendored-parser adapter code had nothing constraining them. Score fell
|
||
// to 55.8% against the 65 floor (748 killed / 593 survived / 17 timeout) and the shard also
|
||
// blew the 15-minute cap. Fixed by registering the branch's four new/changed frontmatter
|
||
// test files in the tests array above (see that entry's inline comment for which files and
|
||
// why) and giving the shard a measured 180-minute budget via timeoutMinutes. minScore left
|
||
// at 65 pending a fresh CI measurement with the corrected test list.
|
||
//
|
||
// LESSON: floors MUST be calibrated from CI mutation runs (CI runs with
|
||
// timeout≈0, deterministic). Local runs count timeouts as kills and
|
||
// inflate scores significantly (prompt-budget: 99.6% local vs 68.3% CI;
|
||
// config-schema: 69.7% local vs 54.55% CI). Never set a floor from a
|
||
// local run without CI cross-check.
|
||
const COVERED = {
|
||
'context-utilization': {
|
||
cjs: 'gsd-core/bin/lib/context-utilization.cjs',
|
||
// Derived: context-utilization.property.test.cjs (pre-existing) +
|
||
// context-utilization.test.cjs (piece-2 derivation find: it directly requires and
|
||
// matches the naming rule, but was never hand-added to the old literal list —
|
||
// exactly the #3888 drift class this derivation exists to stop. Measured cost:
|
||
// +50ms over the property-only baseline (58ms -> 108ms, in-process, 0 subprocess
|
||
// spawns) — negligible for a shard whose floor is already at TARGET.
|
||
// After mutation-killer assertions added in #1187: measured 92.31% (2026-06-14).
|
||
// 3 survivors are __esModule boilerplate (genuinely equivalent CJS interop mutants).
|
||
// minScore raised to TARGET (80) — module now meets ADR-456 goal. Not yet
|
||
// re-measured against the wider (derived) test list; the added file only adds
|
||
// assertions, never removes any, so the floor cannot have fallen.
|
||
// CI run 33012034388 (2026-08-25, #3881 ratchet): measured 92.31% (unchanged from
|
||
// the #1187 measurement above — same test list, re-confirmed by the mutation
|
||
// ratchet's own audit). Floor = floor(92.31) - 1 = 91.
|
||
minScore: 91,
|
||
},
|
||
// context-composer: extracted from prompt-budget by #2929. Needs its own entry because
|
||
// mutation coverage does not migrate with relocated code — scoring only prompt-budget.cjs
|
||
// would leave the extracted ladder unmeasured. Its own filename never matches the
|
||
// "context-composer*" naming rule for prompt-budget-parity.test.cjs / prompt-budget.unit.test.cjs
|
||
// — both genuinely constrain context-composer.cjs (the ladder was relocated INTO it), so
|
||
// both are declared via extraTests rather than silently missing from the derivation.
|
||
'context-composer': {
|
||
cjs: 'gsd-core/bin/lib/context-composer.cjs',
|
||
extraTests: [
|
||
'prompt-budget-parity.test.cjs',
|
||
'prompt-budget.unit.test.cjs',
|
||
],
|
||
// CI run 33012034388 (2026-08-25, #3881 ratchet): measured 79.92%. Floor =
|
||
// floor(79.92) - 1 = 78.
|
||
minScore: 78,
|
||
},
|
||
'prompt-budget': {
|
||
cjs: 'gsd-core/bin/lib/prompt-budget.cjs',
|
||
// Derived: property + unit (pre-existing) plus two piece-2 derivation finds that
|
||
// directly require prompt-budget.cjs and match the naming rule but were never in the
|
||
// old hand list — prompt-budget-parity.test.cjs and prompt-budget.test.cjs. Measured
|
||
// cost: 475ms (2-file) -> 527ms (4-file), in-process, 0 subprocess spawns; +52ms is
|
||
// negligible next to this module's own mutant count.
|
||
// CI 68.33% timeout-free (164 killed / 1 timeout / 240 total) 2026-06-14;
|
||
// local was 99.6% — timeout inflation. Floor = 68 - 2 margin. Not yet re-measured
|
||
// against the wider (derived) test list; both added files only add assertions, never
|
||
// remove any, so the floor cannot have fallen.
|
||
// CI run 33012034388 (2026-08-25, #3881 ratchet): re-measured against the wider
|
||
// (derived) test list at 88.95%. Floor = floor(88.95) - 1 = 87.
|
||
minScore: 87,
|
||
},
|
||
frontmatter: {
|
||
cjs: 'gsd-core/bin/lib/frontmatter.cjs',
|
||
// extraTests: files that genuinely constrain frontmatter.cjs but do not match the
|
||
// "frontmatter*" naming rule, so the derivation cannot find them on its own —
|
||
// each earns its slot on evidence, not blanket inclusion (verified no two duplicate
|
||
// the same constraining assertion):
|
||
// - unusable-input.test.cjs: #1882 added the unterminated-fence detection to
|
||
// frontmatter.cjs, and the tests that constrain it live here. Without this entry
|
||
// the mutants in that branch are covered by no test in the shard.
|
||
// - feat-3881-yaml-parser-consequences.test.cjs: consequence/boundary matrix for the
|
||
// #3881 parser swap (state-transition interop, unusable-input counters, and — as of
|
||
// the piece-1 mutation-matrix fix below — the relocated anchor-alias-bomb + B1/B2
|
||
// block-scalar assertions). Nothing else in the shard drives
|
||
// extractFrontmatter/reconstructFrontmatter through those seams.
|
||
extraTests: [
|
||
'unusable-input.test.cjs',
|
||
'feat-3881-yaml-parser-consequences.test.cjs',
|
||
],
|
||
// excludeTests: files the derivation WOULD auto-include (require frontmatter.cjs
|
||
// directly AND match the "frontmatter*" naming rule) but are deliberately withheld:
|
||
// - frontmatter-cli.test.cjs: 778-line CLI-integration file, 39 subprocess-spawn
|
||
// references (spawnSync/execFileSync/runGsdTools) — the same #2790
|
||
// planning-inspect.test.cjs shape (a `node --test <file>` invocation Stryker's
|
||
// command runner re-runs whole, once per mutant, at whatever its slowest spawn
|
||
// case costs). Never measured in a shard; excluded up front on the same evidence
|
||
// class rather than discovered by a timeout.
|
||
// - frontmatter.test.cjs: mutation-matrix piece 1 (#3881 follow-up). This
|
||
// 2932-line integration file cost 3132ms of the shard's ~4800ms per-run
|
||
// (measured via node:test's run() API — node --test is hard-blocked locally,
|
||
// this is the sanctioned substitute), which at ~1850 mutants (source grew 1.8x
|
||
// for #3881) projected to ~96 of the shard's 140-minute total. Its two
|
||
// genuinely-unique assertion classes — anchor-alias-bomb refusal (billion-laughs
|
||
// -style anchor/alias expansion must be rejected, not expanded) and the B1/B2
|
||
// block-scalar assertions (parsing commands/gsd/add-tests.md must not invent a
|
||
// phantom "Example" key) — were relocated verbatim into
|
||
// feat-3881-yaml-parser-consequences.test.cjs (already in this shard via
|
||
// extraTests above) rather than deleted, so the mutants they kill stay killed.
|
||
// frontmatter.test.cjs itself is UNCHANGED and keeps running in the normal
|
||
// (non-mutation) suite — only the mutation shard drops it.
|
||
excludeTests: [
|
||
'frontmatter-cli.test.cjs',
|
||
'frontmatter.test.cjs',
|
||
],
|
||
minScore: 65,
|
||
// Wall-time projection, re-derived after piece 1 (dropping frontmatter.test.cjs) using
|
||
// this file's own documented method. Mutant-count factor is unchanged: source grew 1.8x
|
||
// for #3881 (1030 -> ~1850 mutants; see the #3888-era note this superseded for that
|
||
// derivation). Per-run test-command cost is re-measured on the CURRENT 6-file derived
|
||
// set (frontmatter.property/.unit/-golden-parity/-roundtrip.property + unusable-input +
|
||
// feat-3881-yaml-parser-consequences — the shard minus frontmatter.test.cjs and minus
|
||
// frontmatter-cli.test.cjs, neither of which was ever in a measured baseline): 1520ms,
|
||
// vs the documented OLD 3-file baseline of 593ms — a 2.56x per-run cost increase (down
|
||
// from the pre-piece-1 8x, since the file responsible for 3132ms of the old 4669ms
|
||
// 7-file run is gone). Applying both factors the same way the prior note did: 586s
|
||
// (documented 3-file/1030-mutant CI baseline) * 1.8 (mutants) * 2.56 (test cost) ~=
|
||
// 2700s (~45 minutes). Set to 60 minutes for margin above that projection (the same
|
||
// ~1.3x margin ratio the prior 180-minute budget used over its own 140-minute
|
||
// projection), well under GitHub Actions' 360-minute job ceiling and a 3x cut from the
|
||
// previous 180. Scoped to this shard only via timeoutMinutes below — every other shard
|
||
// keeps the 15-minute default.
|
||
timeoutMinutes: 60,
|
||
// isolation: intentionally NOT set (defaults to 'process' below) — unchanged from the
|
||
// prior audit: 'none' showed no reliable win once the test set grew past 3 files
|
||
// (overlapping distributions), and dropping frontmatter.test.cjs only shrinks the set
|
||
// further, so there is no new basis to revisit that call.
|
||
},
|
||
// adr-parser / config-schema / active-workstream-store / core-utils: derivation reproduces
|
||
// their prior hand lists exactly (every constraining file's own name already matched the
|
||
// "<module>*" rule) — no extraTests/excludeTests needed. Note config-schema in particular:
|
||
// an UNFILTERED require-scan finds 22 files that require config-schema.cjs, but only
|
||
// config-schema.property.test.cjs matches the naming rule — the naming filter is what
|
||
// keeps this shard from silently ballooning to include spawn-heavy integration suites
|
||
// (e.g. graphify-auto-update.slow.test.cjs) that merely load config-schema as a fixture
|
||
// dependency of something else under test.
|
||
'adr-parser': {
|
||
cjs: 'gsd-core/bin/lib/adr-parser.cjs',
|
||
minScore: 68,
|
||
},
|
||
'config-schema': {
|
||
cjs: 'gsd-core/bin/lib/config-schema.cjs',
|
||
// CI 54.55% timeout-free (18 killed / 0 timeout / 33 total) 2026-06-14;
|
||
// local was 69.7% — timeout inflation. Floor = 54 - 2 margin.
|
||
// CI run 33012034388 (2026-08-25, #3881 ratchet): measured 75.51%. Floor =
|
||
// floor(75.51) - 1 = 74.
|
||
minScore: 74,
|
||
},
|
||
'active-workstream-store': {
|
||
cjs: 'gsd-core/bin/lib/active-workstream-store.cjs',
|
||
// CI run 33012034388 (2026-08-25, #3881 ratchet): measured 87.42%. Floor =
|
||
// floor(87.42) - 1 = 86.
|
||
minScore: 86,
|
||
},
|
||
'core-utils': {
|
||
cjs: 'gsd-core/bin/lib/core-utils.cjs',
|
||
minScore: 75, // measured 77.52% (2026-06-14, issue #1187); floor = 77 - 2
|
||
},
|
||
// planning-inspect / plan-document / planning-command-router: net-new modules
|
||
// added by #2790. Registered here so the Stryker gate stops SKIPPING them
|
||
// (previously has_work: "false" — ~1000 LOC entirely outside mutation scoring).
|
||
//
|
||
// WHY THESE SHARDS POINT AT tests/planning-inspect.unit.test.cjs, NOT
|
||
// tests/planning-inspect.test.cjs. CI evidence: two shards pointed at the
|
||
// integration file were CANCELLED at the workflow's 15-minute cap —
|
||
// "Mutation testing 4% (elapsed: ~3m, remaining: ~1h 19m) 27/640 tested".
|
||
// tests/planning-inspect.test.cjs is INTEGRATION-shaped (91 cases, most
|
||
// spawning a `gsd-tools` child process via `runGsdTools`); Stryker's command
|
||
// runner treats the whole `node --test <file>` invocation as ONE test costing
|
||
// whatever the slowest case costs (measured ~20s), and re-runs that entire
|
||
// file once per mutant — 640 mutants x 20s cannot finish in 15 minutes.
|
||
// tests/planning-inspect.unit.test.cjs is the dedicated, spawn-free,
|
||
// in-process mutation surface for exactly these three modules (measured
|
||
// locally: the whole file runs in well under a second) — the same shape
|
||
// every other entry in this registry already uses (*.property.test.cjs /
|
||
// *.unit.test.cjs). The integration suite is UNAFFECTED by this change: it
|
||
// keeps running in full in the normal (non-mutation) test job, and remains
|
||
// the source of truth for spawn-boundary/CLI-dispatch/read-only-proof
|
||
// behaviour that an in-process unit file cannot exercise.
|
||
//
|
||
// Measured CI scores (GitHub Actions run 32392791843, all three shards
|
||
// PASSED — not a local run; mutation shards run `node --test`, hard-blocked
|
||
// in this repo's local environment):
|
||
// planning-command-router 95.65% → floor 94 (already exceeds TARGET_MUTATION_SCORE (80))
|
||
// plan-document 76.58% → floor 75
|
||
// planning-inspect 57.03% → floor 56 (well below TARGET (80) — ratchet
|
||
// candidate; comfortably clears its own floor but has real room to grow.
|
||
// Raise as its tests improve, never lower it.)
|
||
//
|
||
// All three shards point at tests/planning-inspect.unit.test.cjs (in-process,
|
||
// spawn-free, ~0.3s dry run), not tests/planning-inspect.test.cjs — that is
|
||
// what made measurement possible at all. The integration file spawns a
|
||
// subprocess per case via runGsdTools; Stryker's command runner treats the
|
||
// whole `node --test <file>` invocation as one test costing whatever the
|
||
// slowest case costs (measured ~20s), and re-runs that entire file once per
|
||
// mutant, so 640 mutants x 20s could not finish inside the 15-minute shard
|
||
// cap. The integration suite is unaffected by this change: it keeps running
|
||
// in full in the normal (non-mutation) test job.
|
||
// planning-inspect's own name matches "planning-inspect.unit.test.cjs" via the naming
|
||
// rule, so that file is auto-derived. planning-inspect.test.cjs (the excluded integration
|
||
// file the comment above names) ALSO matches the naming rule and directly requires the
|
||
// module, so it must be an explicit excludeTests entry now — the derivation would
|
||
// otherwise auto-include it and reproduce the exact 15-minute-cap cancellation the
|
||
// comment above documents.
|
||
'planning-inspect': {
|
||
cjs: 'gsd-core/bin/lib/planning-inspect.cjs',
|
||
excludeTests: ['planning-inspect.test.cjs'],
|
||
minScore: 56,
|
||
},
|
||
// plan-document / planning-command-router: their own names never appear in any test
|
||
// filename (the shared dedicated unit file is named after planning-inspect, the module
|
||
// #2790 extracted them alongside), so the naming-rule derivation finds nothing — same
|
||
// cross-cutting shape as context-composer above. Declared via extraTests.
|
||
'plan-document': {
|
||
cjs: 'gsd-core/bin/lib/plan-document.cjs',
|
||
extraTests: ['planning-inspect.unit.test.cjs'],
|
||
minScore: 75,
|
||
},
|
||
'planning-command-router': {
|
||
cjs: 'gsd-core/bin/lib/planning-command-router.cjs',
|
||
extraTests: ['planning-inspect.unit.test.cjs'],
|
||
minScore: 94,
|
||
},
|
||
// model-catalog: net-new registration by #3007. The module was entirely
|
||
// outside mutation scoring (has_work: "false") before this entry, so the
|
||
// #3007 per-model Codex effort rewrite (renderEffortForRuntime's
|
||
// CODEX_MODEL_EFFORT lookup, the 'ultra' policy rejection, the ladder
|
||
// walk-up clamp) had zero mutation coverage.
|
||
//
|
||
// Same #2790 precedent as planning-inspect above: this shard points at a
|
||
// dedicated tests/model-catalog.unit.test.cjs, NOT tests/model-resolver.test.cjs
|
||
// — that integration file uses runGsdTools heavily and would hit the same
|
||
// 15-minute shard-cap cancellation #2790 documented (a `node --test <file>`
|
||
// invocation is ONE test costing whatever its slowest case costs, re-run
|
||
// per mutant). tests/model-catalog.unit.test.cjs is spawn-free, in-process,
|
||
// and runs in well under a second.
|
||
//
|
||
// Measured CI score (GitHub Actions run 32605073352, job 97108869486):
|
||
// model-catalog 59.62% → floor 58 (248 killed, 168 survived, 0 timeouts,
|
||
// 0 errors; below TARGET_MUTATION_SCORE (80) — ratchet candidate like
|
||
// planning-inspect (56): comfortably clears its own floor but has real
|
||
// room to grow. Raise as its tests improve, never lower it.)
|
||
// Floor follows this file's documented rule, minScore = floor(measured) - 1,
|
||
// matching the sibling precedent exactly (57.03 → 56, 76.58 → 75, 95.65 → 94).
|
||
//
|
||
// The shard completed in 57 seconds — concrete evidence the spawn-free
|
||
// unit-file design above worked: the #2790 precedent's 15-minute shard-cap
|
||
// cancellations do not apply here, and for comparison the `frontmatter`
|
||
// shard in the same run took 9m46s.
|
||
// model-catalog: derivation finds two files never in the old hand list —
|
||
// model-catalog-runtime-defaults.test.cjs and model-catalog-valid-tiers.test.cjs — both
|
||
// directly require model-catalog.cjs and match the "model-catalog*" naming rule. Measured
|
||
// cost: 50ms (1-file) -> 196ms (3-file), in-process, 0 subprocess spawns; still far under
|
||
// the 57s the shard already measured for the single-file set.
|
||
'model-catalog': {
|
||
cjs: 'gsd-core/bin/lib/model-catalog.cjs',
|
||
minScore: 58,
|
||
},
|
||
// state-contract: net-new module from #3227. Without this entry the
|
||
// Stryker gate reports has_work: "false" and SKIPS it entirely — the
|
||
// exact gap #2790 (planning-inspect / plan-document / planning-command-router)
|
||
// and #3007 (model-catalog) each had to fix after the fact.
|
||
//
|
||
// Same #2790 precedent as planning-inspect / model-catalog above: this
|
||
// shard points at tests/state-contract.unit.test.cjs, NOT
|
||
// tests/state-contract.test.cjs — the latter spawns a `gsd-tools` child
|
||
// process per case via runGsdTools, and Stryker's command runner treats
|
||
// the whole `node --test <file>` invocation as ONE test costing whatever
|
||
// its slowest case costs, re-run once per mutant, so it cannot finish
|
||
// inside the 15-minute shard cap. tests/state-contract.unit.test.cjs is
|
||
// spawn-free and in-process.
|
||
//
|
||
// Measured CI score (GitHub Actions run 32769289750, job 97565813640,
|
||
// `Stryker (state-contract)`, PASSED in 2m23s):
|
||
// state-contract 66.25% → floor 65 (below TARGET_MUTATION_SCORE (80) —
|
||
// ratchet candidate like planning-inspect (56) and model-catalog (58):
|
||
// comfortably clears its own floor but has real room to grow. Raise as
|
||
// its tests improve, never lower it.)
|
||
// Floor follows this file's documented rule, minScore = floor(measured) - 1,
|
||
// matching the sibling precedent exactly (57.03 → 56, 76.58 → 75,
|
||
// 95.65 → 94, 59.62 → 58, 66.25 → 65).
|
||
//
|
||
// The floor MUST come from a CI shard, never a local run: local runs count
|
||
// timeouts as kills and inflate scores badly (this file already records
|
||
// prompt-budget 99.6% local vs 68.33% CI, and config-schema 69.7% local vs
|
||
// 54.55% CI).
|
||
// state-contract.test.cjs matches the naming rule and directly requires state-contract.cjs
|
||
// but is the same spawn-heavy integration shape as planning-inspect.test.cjs (446 lines,
|
||
// 16 subprocess-spawn references) — excluded explicitly rather than left to fall through.
|
||
'state-contract': {
|
||
cjs: 'gsd-core/bin/lib/state-contract.cjs',
|
||
excludeTests: ['state-contract.test.cjs'],
|
||
minScore: 65,
|
||
},
|
||
};
|
||
|
||
// Compute the final, derived `tests` array for every COVERED entry. Done once, after the
|
||
// full COVERED literal above is built, so every entry's extraTests/excludeTests declarations
|
||
// are visible to computeModuleTests regardless of source order.
|
||
for (const [moduleName, entry] of Object.entries(COVERED)) {
|
||
entry.tests = computeModuleTests(moduleName, entry);
|
||
}
|
||
|
||
// ── Files that, when changed, invalidate ALL modules ─────────────────────────
|
||
// Changes to the Stryker config, this script itself, or any covered test file
|
||
// affect all mutation scores and must force a full re-run.
|
||
const GLOBAL_TRIGGERS = new Set([
|
||
'stryker.config.mjs',
|
||
'scripts/mutation-matrix.cjs',
|
||
]);
|
||
|
||
// Also flag all test files that belong to any covered module as global triggers.
|
||
for (const mod of Object.values(COVERED)) {
|
||
for (const t of mod.tests) {
|
||
GLOBAL_TRIGGERS.add(t);
|
||
}
|
||
}
|
||
|
||
// ── Argument parsing ──────────────────────────────────────────────────────────
|
||
function parseArgs(argv) {
|
||
const out = { base: null, print: false };
|
||
for (let i = 0; i < argv.length; i++) {
|
||
const arg = argv[i];
|
||
if (arg === '--base') {
|
||
out.base = argv[++i];
|
||
if (!out.base || out.base.startsWith('--')) {
|
||
throw new Error('--base requires a value');
|
||
}
|
||
} else if (arg.startsWith('--base=')) {
|
||
out.base = arg.slice('--base='.length);
|
||
if (!out.base) throw new Error('--base requires a value');
|
||
} else if (arg === '--print') {
|
||
out.print = true;
|
||
} else if (arg === '--help' || arg === '-h') {
|
||
console.log([
|
||
'Usage:',
|
||
' node scripts/mutation-matrix.cjs --base <ref> [--print]',
|
||
' printf "src/foo.cts\\n" | node scripts/mutation-matrix.cjs [--print]',
|
||
'',
|
||
'Options:',
|
||
' --base <ref> Git ref to diff against (default: origin/${GITHUB_BASE_REF:-next})',
|
||
' --print Human-readable output instead of JSON',
|
||
].join('\n'));
|
||
throw new ExitError(0);
|
||
} else {
|
||
throw new Error(`unknown argument: ${arg}`);
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// ── Changed-file resolution ───────────────────────────────────────────────────
|
||
function resolveChangedFiles(args) {
|
||
// When --base is provided, always use git diff (regardless of stdin).
|
||
// When --base is absent AND stdin is not a TTY (isTTY is falsy / undefined),
|
||
// read a newline-delimited file list from stdin.
|
||
if (!args.base && process.stdin.isTTY !== true) {
|
||
const raw = readStdinSync();
|
||
return raw.split('\n').map(l => l.trim()).filter(Boolean);
|
||
}
|
||
|
||
// Otherwise (--base given, or stdin is a real TTY), diff against the base ref.
|
||
const defaultBase = `origin/${process.env.GITHUB_BASE_REF || 'next'}`;
|
||
const base = args.base || defaultBase;
|
||
const stdout = execFileSync('git', ['diff', '--name-only', `${base}...HEAD`], {
|
||
encoding: 'utf8',
|
||
});
|
||
return stdout.split('\n').map(l => l.trim()).filter(Boolean);
|
||
}
|
||
|
||
// ── Module classification ─────────────────────────────────────────────────────
|
||
function computeMatrix(changedFiles) {
|
||
// Check for global triggers first — if any hit, include every covered module.
|
||
const allModuleNames = Object.keys(COVERED);
|
||
for (const f of changedFiles) {
|
||
if (GLOBAL_TRIGGERS.has(f)) {
|
||
return allModuleNames;
|
||
}
|
||
}
|
||
|
||
// Otherwise find which modules have their src/*.cts changed.
|
||
const changed = new Set();
|
||
for (const f of changedFiles) {
|
||
// Match src/<module>.cts (top-level src/, not nested)
|
||
const m = f.match(/^src\/([^/]+)\.cts$/);
|
||
if (m && COVERED[m[1]]) {
|
||
changed.add(m[1]);
|
||
}
|
||
}
|
||
return [...changed];
|
||
}
|
||
|
||
// ── Output formatting ─────────────────────────────────────────────────────────
|
||
function buildResult(moduleNames) {
|
||
const include = moduleNames.map(name => ({
|
||
name,
|
||
mutate: COVERED[name].cjs,
|
||
tests: COVERED[name].tests.join(' '),
|
||
minScore: COVERED[name].minScore,
|
||
// node:test's default per-file process isolation; only modules that document a
|
||
// measured, audited need for 'none' opt out.
|
||
isolation: COVERED[name].isolation || 'process',
|
||
// Per-shard CI job timeout in minutes. Defaults to 15 (the shared per-shard budget);
|
||
// only a module that documents a measured need for more (see the frontmatter entry
|
||
// above) sets a higher value. Threaded through mutation.yml's job-level
|
||
// `timeout-minutes: ${{ matrix.timeoutMinutes }}` the same way `isolation` is threaded
|
||
// through the test-runner env.
|
||
timeoutMinutes: COVERED[name].timeoutMinutes || 15,
|
||
}));
|
||
|
||
return {
|
||
has_work: include.length > 0 ? 'true' : 'false',
|
||
matrix: { include },
|
||
};
|
||
}
|
||
|
||
function printHuman(result, changedFiles) {
|
||
console.log(`Changed files (${changedFiles.length}):`);
|
||
for (const f of changedFiles) console.log(` ${f}`);
|
||
console.log('');
|
||
console.log(`has_work: ${result.has_work}`);
|
||
console.log(`Shards (${result.matrix.include.length}):`);
|
||
for (const shard of result.matrix.include) {
|
||
console.log(` [${shard.name}]`);
|
||
console.log(` mutate: ${shard.mutate}`);
|
||
console.log(` tests: ${shard.tests}`);
|
||
console.log(` minScore: ${shard.minScore}`);
|
||
console.log(` isolation:${shard.isolation}`);
|
||
console.log(` timeoutMinutes:${shard.timeoutMinutes}`);
|
||
}
|
||
}
|
||
|
||
// ── Main ──────────────────────────────────────────────────────────────────────
|
||
function main() {
|
||
try {
|
||
const args = parseArgs(process.argv.slice(2));
|
||
const changedFiles = resolveChangedFiles(args);
|
||
const moduleNames = computeMatrix(changedFiles);
|
||
const result = buildResult(moduleNames);
|
||
|
||
if (args.print) {
|
||
printHuman(result, changedFiles);
|
||
} else {
|
||
console.log(JSON.stringify(result, null, 2));
|
||
}
|
||
} catch (err) {
|
||
if (err instanceof ExitError) throw err;
|
||
console.error(`mutation-matrix: ${err.message}`);
|
||
throw new ExitError(2);
|
||
}
|
||
}
|
||
|
||
// ── MUTATION_BREAK resolver ───────────────────────────────────────────────────
|
||
/**
|
||
* Resolves the per-shard mutation break threshold from the MUTATION_BREAK env var.
|
||
*
|
||
* Fail-closed contract:
|
||
* - undefined → 60 (local run: no env set, documented backstop)
|
||
* - set but empty (e.g. CI matrix.minScore missing) → throws (wiring error)
|
||
* - non-numeric or out-of-range [1, 100] → throws (invalid config)
|
||
* - valid integer string → returns that number
|
||
*
|
||
* This function is the single call site for reading MUTATION_BREAK.
|
||
* stryker.config.mjs imports and calls it so CI shards with a bad
|
||
* MUTATION_BREAK fail immediately rather than silently falling back to 60
|
||
* and bypassing a per-module floor above 60 (e.g. prompt-budget: 90).
|
||
*
|
||
* @param {string|undefined} raw - value of process.env.MUTATION_BREAK
|
||
* @returns {number}
|
||
*/
|
||
function resolveMutationBreak(raw) {
|
||
if (raw === undefined) {
|
||
// Local run with no MUTATION_BREAK set — use documented backstop.
|
||
return 60;
|
||
}
|
||
if (typeof raw !== 'string' || raw.trim() === '') {
|
||
throw new Error(
|
||
'MUTATION_BREAK is set but empty — CI shard wiring is broken (matrix.minScore missing?)'
|
||
);
|
||
}
|
||
const n = Number(raw);
|
||
if (!Number.isFinite(n) || n < 1 || n > 100) {
|
||
throw new Error(
|
||
`MUTATION_BREAK invalid: "${raw}" (expected a per-module minScore 1-100)`
|
||
);
|
||
}
|
||
return n;
|
||
}
|
||
|
||
// Export internals for programmatic use (tests/mutation-matrix-ratchet.test.cjs).
|
||
// The require.main guard prevents main() from running when this file is require()d.
|
||
module.exports = {
|
||
COVERED,
|
||
TARGET_MUTATION_SCORE,
|
||
resolveMutationBreak,
|
||
readStdinSync,
|
||
// Derivation-engine internals — exported for tests/mutation-test-derivation-drift.test.cjs
|
||
// and scripts/lint-mutation-test-derivation-drift.cjs.
|
||
findRequiringTestFiles,
|
||
matchesModuleNamingRule,
|
||
deriveNamedTests,
|
||
computeModuleTests,
|
||
};
|
||
|
||
if (require.main === module) runMain(main);
|