@opengsd/gsd-core 1.7.0-rc.6 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/agents/gsd-executor.md +2 -1
  4. package/agents/gsd-security-auditor.md +13 -15
  5. package/bin/install.js +5 -4
  6. package/commands/gsd/mempalace-capture.md +27 -1
  7. package/commands/gsd/surface.md +6 -6
  8. package/gsd-core/bin/gsd-tools.cjs +63 -2
  9. package/gsd-core/bin/lib/api-coverage.cjs +3 -4
  10. package/gsd-core/bin/lib/audit.cjs +7 -6
  11. package/gsd-core/bin/lib/capability-registry.cjs +57 -57
  12. package/gsd-core/bin/lib/check-command-router.cjs +1 -1
  13. package/gsd-core/bin/lib/clock.cjs +19 -0
  14. package/gsd-core/bin/lib/commands.cjs +48 -9
  15. package/gsd-core/bin/lib/config-loader.cjs +6 -2
  16. package/gsd-core/bin/lib/config.cjs +12 -0
  17. package/gsd-core/bin/lib/core-utils.cjs +8 -2
  18. package/gsd-core/bin/lib/drift.cjs +4 -4
  19. package/gsd-core/bin/lib/frontmatter.cjs +22 -0
  20. package/gsd-core/bin/lib/gsd2-import.cjs +2 -1
  21. package/gsd-core/bin/lib/init.cjs +27 -13
  22. package/gsd-core/bin/lib/install-engine.cjs +3 -2
  23. package/gsd-core/bin/lib/installer-migration-authoring.cjs +2 -1
  24. package/gsd-core/bin/lib/installer-migrations.cjs +1 -1
  25. package/gsd-core/bin/lib/markdown-sectionizer.cjs +342 -0
  26. package/gsd-core/bin/lib/markdown-table.cjs +698 -0
  27. package/gsd-core/bin/lib/milestone.cjs +217 -31
  28. package/gsd-core/bin/lib/phase-command-router.cjs +50 -2
  29. package/gsd-core/bin/lib/phase-lifecycle.cjs +62 -36
  30. package/gsd-core/bin/lib/phase-locator.cjs +23 -2
  31. package/gsd-core/bin/lib/phase.cjs +436 -61
  32. package/gsd-core/bin/lib/plan-scan.cjs +3 -0
  33. package/gsd-core/bin/lib/roadmap-parser.cjs +218 -13
  34. package/gsd-core/bin/lib/roadmap.cjs +100 -49
  35. package/gsd-core/bin/lib/runtime-artifact-conversion.cjs +7 -6
  36. package/gsd-core/bin/lib/runtime-artifact-install-plan.cjs +3 -2
  37. package/gsd-core/bin/lib/runtime-artifact-layout.cjs +2 -1
  38. package/gsd-core/bin/lib/runtime-hooks-surface.cjs +25 -17
  39. package/gsd-core/bin/lib/schema-detect.cjs +2 -1
  40. package/gsd-core/bin/lib/security.cjs +1 -1
  41. package/gsd-core/bin/lib/shell-command-projection.cjs +61 -25
  42. package/gsd-core/bin/lib/smart-entry.cjs +4 -3
  43. package/gsd-core/bin/lib/state-transition.cjs +100 -45
  44. package/gsd-core/bin/lib/state.cjs +391 -126
  45. package/gsd-core/bin/lib/surface.cjs +2 -2
  46. package/gsd-core/bin/lib/template.cjs +2 -1
  47. package/gsd-core/bin/lib/uat.cjs +54 -8
  48. package/gsd-core/bin/lib/ui-safety-gate.cjs +23 -1
  49. package/gsd-core/bin/lib/verify.cjs +4 -3
  50. package/gsd-core/bin/lib/workstream.cjs +3 -2
  51. package/gsd-core/bin/lib/worktree-safety.cjs +1 -1
  52. package/gsd-core/bin/lib/write-set.cjs +38 -0
  53. package/gsd-core/bin/shared/config-schema.manifest.json +2 -0
  54. package/gsd-core/references/checkpoints.md +12 -0
  55. package/gsd-core/workflows/add-tests.md +1 -1
  56. package/gsd-core/workflows/debug.md +2 -0
  57. package/gsd-core/workflows/execute-phase.md +5 -3
  58. package/gsd-core/workflows/fast.md +8 -22
  59. package/gsd-core/workflows/progress.md +2 -2
  60. package/gsd-core/workflows/quick.md +2 -0
  61. package/gsd-core/workflows/review.md +42 -3
  62. package/gsd-core/workflows/secure-phase.md +1 -1
  63. package/gsd-core/workflows/ship.md +8 -2
  64. package/gsd-core/workflows/spec-phase.md +1 -1
  65. package/gsd-core/workflows/transition.md +1 -1
  66. package/hooks/dist/gsd-statusline.js +164 -14
  67. package/hooks/gsd-statusline.js +164 -14
  68. package/package.json +4 -2
  69. package/scripts/ci-test-scope.cjs +39 -1
  70. package/scripts/gen-golden-install-parity-zcode.cjs +35 -45
  71. package/scripts/gen-install-tree-fixtures.cjs +75 -0
  72. package/scripts/lint-allow-test-rule-refs.allowlist.json +0 -1
  73. package/scripts/lint-table-schema-drift.cjs +157 -0
  74. package/skills/gsd-mempalace-capture/SKILL.md +27 -1
  75. package/skills/gsd-surface/SKILL.md +6 -6
  76. package/vscode/package.json +1 -1
@@ -12,13 +12,17 @@
12
12
  */
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.stripFencedCode = stripFencedCode;
15
+ exports.extractFencedBlock = extractFencedBlock;
15
16
  exports.tokenizeHeadings = tokenizeHeadings;
16
17
  exports.collectSections = collectSections;
17
18
  exports.collectSection = collectSection;
18
19
  exports.iterateBullets = iterateBullets;
20
+ exports.updateBullet = updateBullet;
19
21
  exports.extractTaggedBlocks = extractTaggedBlocks;
20
22
  exports.stripTaggedBlocks = stripTaggedBlocks;
21
23
  exports.replaceSection = replaceSection;
24
+ exports.withSection = withSection;
25
+ exports.deleteSection = deleteSection;
22
26
  // ─── stripFencedCode ──────────────────────────────────────────────────────────
23
27
  /**
24
28
  * CommonMark-correct fenced-code-block stripper.
@@ -80,6 +84,98 @@ function stripFencedCode(content) {
80
84
  }
81
85
  return { text: kept.join('\n'), unterminatedFence: openFence !== null };
82
86
  }
87
+ /**
88
+ * Shared low-level fence-scanning engine. Walks `lines` and returns every
89
+ * fenced block found, applying the EXACT SAME CommonMark delimiter rules as
90
+ * `stripFencedCode` (≥3 backticks/tildes, ≤3-space indent tolerance, a closer
91
+ * must be the same delimiter char with run length ≥ the opener and no
92
+ * trailing non-whitespace text; a mismatched delimiter char — or a same-char
93
+ * run that is too short or carries trailing text — encountered while a fence
94
+ * is already open is fence CONTENT, not a new open/close event). This is the
95
+ * "engine" `extractFencedBlock` reuses instead of an ad-hoc regex, so a
96
+ * different-info-string fence, a fence nested/indented inside another fence,
97
+ * and a `~~~` fence are all classified exactly as `stripFencedCode` would.
98
+ *
99
+ * Tracked duplication (same status as `tokenizeHeadings`'s copy, see its
100
+ * comment above): this is a second independent copy of the fence state
101
+ * machine, pending a T-tier consolidation.
102
+ */
103
+ function scanFencedBlocks(lines) {
104
+ const delimRe = /^( {0,3})(`{3,}|~{3,})(.*)$/;
105
+ const blocks = [];
106
+ let open = null;
107
+ for (let i = 0; i < lines.length; i++) {
108
+ const line = lines[i].replace(/\r$/, '');
109
+ const m = delimRe.exec(line);
110
+ if (!m)
111
+ continue;
112
+ const char = m[2][0];
113
+ const len = m[2].length;
114
+ const trailing = m[3];
115
+ if (open === null) {
116
+ // CommonMark §4.5: backtick fence info string must not contain a backtick.
117
+ if (char === '`' && trailing.includes('`'))
118
+ continue; // not a valid opener — ordinary content
119
+ open = { char, len, infoString: trailing.trim(), openLineIdx: i };
120
+ }
121
+ else if (char === open.char && len >= open.len && /^\s*$/.test(trailing)) {
122
+ blocks.push({
123
+ char: open.char,
124
+ len: open.len,
125
+ infoString: open.infoString,
126
+ openLineIdx: open.openLineIdx,
127
+ closeLineIdx: i,
128
+ });
129
+ open = null;
130
+ }
131
+ // else: mismatched/insufficient delimiter while a fence is open — content, not a boundary.
132
+ }
133
+ if (open !== null) {
134
+ blocks.push({
135
+ char: open.char,
136
+ len: open.len,
137
+ infoString: open.infoString,
138
+ openLineIdx: open.openLineIdx,
139
+ closeLineIdx: -1,
140
+ });
141
+ }
142
+ return blocks;
143
+ }
144
+ /**
145
+ * Return the INNER text (the lines between the delimiters, joined by `\n`) of
146
+ * the FIRST fenced code block whose opening info string — trimmed,
147
+ * case-insensitive — equals `infoString`. Returns `null` when no such block
148
+ * exists, including when the only matching-name fence is left unterminated
149
+ * (EOF inside the fence — there is no well-defined inner span to return,
150
+ * matching a non-greedy `\n```-anchored` regex's behaviour of also failing to
151
+ * match an unclosed fence).
152
+ *
153
+ * Built on `scanFencedBlocks`, the same CommonMark fence-tracking engine
154
+ * `stripFencedCode` uses — so a fence of a DIFFERENT info string, a fence
155
+ * nested/indented inside another fence, and a `~~~` fence are all handled
156
+ * exactly as `stripFencedCode` would classify them; this is not a fresh
157
+ * ad-hoc regex.
158
+ *
159
+ * Migrated from `api-coverage.cts`'s bespoke
160
+ * `` /```coverage\s*\n([\s\S]*?)\n```/i `` (ADR-1372 tier migration, #2143 audit).
161
+ */
162
+ function extractFencedBlock(content, infoString) {
163
+ if (typeof content !== 'string' || content.length === 0)
164
+ return null;
165
+ if (typeof infoString !== 'string')
166
+ return null;
167
+ const target = infoString.trim().toLowerCase();
168
+ const lines = content.split('\n');
169
+ const blocks = scanFencedBlocks(lines);
170
+ for (const block of blocks) {
171
+ if (block.closeLineIdx === -1)
172
+ continue; // unterminated — no well-defined inner span
173
+ if (block.infoString.trim().toLowerCase() !== target)
174
+ continue;
175
+ return lines.slice(block.openLineIdx + 1, block.closeLineIdx).join('\n');
176
+ }
177
+ return null;
178
+ }
83
179
  // ─── tokenizeHeadings ─────────────────────────────────────────────────────────
84
180
  /**
85
181
  * Extract all ATX headings from `content` in document order.
@@ -391,6 +487,137 @@ function iterateBullets(sectionText) {
391
487
  flush();
392
488
  return items;
393
489
  }
490
+ // ─── updateBullet ─────────────────────────────────────────────────────────────
491
+ /**
492
+ * Locate the FIRST top-level bullet-opening line — checkbox (`- [ ]`/`- [x]`),
493
+ * dash/asterisk/plus (`- `/`* `/`+ `), or numbered (`1. `) — whose bullet text
494
+ * satisfies `match(bulletText, rawLine)`, replace that ONE physical line with
495
+ * `transform(rawLine)`, and return the resulting full content string. Every
496
+ * other byte in `content` — surrounding bullets, indentation, EOL style — is
497
+ * left untouched: this is a pure single-line splice, not a document-wide
498
+ * regex `.replace()`.
499
+ *
500
+ * Unlike `iterateBullets` (read-only, no offsets, and not itself fence-aware
501
+ * — callers pre-strip fences when that matters), `updateBullet` tracks
502
+ * character offsets itself so it can splice the transformed line back into
503
+ * the ORIGINAL `content`, and is fence-aware on its own: a bullet-shaped line
504
+ * inside a fenced code block (``` / ~~~, same CommonMark delimiter rules as
505
+ * `stripFencedCode`) is never offered to `match`/`transform`. (Tracked
506
+ * duplication of the fence state machine — same status as `tokenizeHeadings`'s
507
+ * copy, see its doc comment — pending a T-tier consolidation.)
508
+ *
509
+ * `rawLine` (second argument to both `match` and `transform`) is the
510
+ * UNMODIFIED physical line exactly as it appears between `\n` separators — so
511
+ * on a CRLF document its trailing `\r` is included, matching what a
512
+ * hand-rolled `^...[^\n]*`-shaped, `m`-flagged regex applied to the whole
513
+ * document would have seen. `bulletText` (first argument to `match`) is the
514
+ * bullet's own text with marker/checkbox stripped and any trailing `\r`
515
+ * removed — the same extraction `iterateBullets` uses for `BulletItem.text`.
516
+ *
517
+ * Only the OPENING line of a (possibly multi-line) bullet is ever matched or
518
+ * replaced — indented continuation lines are never presented to `match` or
519
+ * `transform`.
520
+ *
521
+ * The gap between the marker and its content tolerates 1 or more spaces — not
522
+ * only exactly one — mirroring CommonMark/GFM's 1–4-space allowance for
523
+ * list-marker spacing (`checkboxRe`/`numberedRe`/`dashRe`'s own dedicated
524
+ * quantifier caps at 4 per GFM; a wider run still recognises the line as a
525
+ * bullet opener via the uncapped `dashRe` fallback catching the excess as
526
+ * ordinary bullet text). So `- [ ] text` (two spaces), `1. text` (three
527
+ * spaces), and even a pathologically wide run are all recognised bullet
528
+ * openers, just as the canonical single-space `- [ ] text` / `1. text` are.
529
+ *
530
+ * Bounded no-op: if no bullet-opening line satisfies `match`, or `transform`
531
+ * returns a non-string, `content` is returned completely unchanged.
532
+ */
533
+ function updateBullet(content, match, transform) {
534
+ if (typeof content !== 'string' || content.length === 0)
535
+ return content;
536
+ const lines = content.split('\n');
537
+ // Marker-to-content gap: CommonMark/GFM tolerates 1–4 spaces between a list
538
+ // marker and its content (5+ pushes the content into indented-code-block
539
+ // territory) — so `- [ ] Phase 1: Foo` (two spaces) is still a valid
540
+ // bullet opener, not just the single-space `- [ ] …` shape. A hand-rolled
541
+ // single-space-only regex (e.g. the OLD `mutateMilestonePhase` checkbox
542
+ // regex before its `updateBullet` migration, which used `-\s*\[` — no cap,
543
+ // but at least 0+) would flip such a line; matching that requires this
544
+ // primitive's own bullet-opening recognition to tolerate the same gap,
545
+ // otherwise a wider-spaced bullet is silently never offered to `match`.
546
+ // F5 (#2245 review, nit): the gap also tolerates a literal TAB (`\t`), not
547
+ // only spaces — the OLD `-\s*\[` regex's `\s` class matched a tab too, so a
548
+ // `-\t[ ] text` bullet (tab-separated marker) must still be recognised here.
549
+ // Checkbox bullet: `<indent>- [ ] text` or `<indent>- [x] text`
550
+ const checkboxRe = /^(\s*)-[ \t]{1,4}\[([xX ])\] (.*)$/;
551
+ // Plain dash/asterisk/plus bullet: `<indent>- text`, `<indent>* text`, `<indent>+ text`
552
+ const dashRe = /^(\s*)[-*+][ \t]{1,4}(.*)$/;
553
+ // Numbered bullet: `<indent>1. text`
554
+ const numberedRe = /^(\s*)\d+\.[ \t]{1,4}(.*)$/;
555
+ // Fence tracking — same CommonMark delimiter rules as stripFencedCode
556
+ // (tracked duplication, see doc comment above).
557
+ const delimRe = /^( {0,3})(`{3,}|~{3,})(.*)$/;
558
+ let openFence = null;
559
+ let offset = 0;
560
+ for (let i = 0; i < lines.length; i++) {
561
+ const rawLine = lines[i];
562
+ const line = rawLine.replace(/\r$/, '');
563
+ const dm = delimRe.exec(line);
564
+ if (dm) {
565
+ const char = dm[2][0];
566
+ const len = dm[2].length;
567
+ const trailing = dm[3];
568
+ if (openFence === null) {
569
+ // CommonMark §4.5: backtick fence info string must not contain a backtick.
570
+ if (!(char === '`' && trailing.includes('`'))) {
571
+ // Valid opener — record fence state; this delimiter line is not a bullet.
572
+ openFence = { char, len };
573
+ offset += rawLine.length + 1;
574
+ continue;
575
+ }
576
+ // else: not a valid opener — falls through to the bullet check below.
577
+ }
578
+ else if (char === openFence.char && len >= openFence.len && /^\s*$/.test(trailing)) {
579
+ // Closing delimiter — close the fence; this line is not a bullet.
580
+ openFence = null;
581
+ offset += rawLine.length + 1;
582
+ continue;
583
+ }
584
+ else {
585
+ // Mismatched/insufficient delimiter while a fence is open — fence content.
586
+ offset += rawLine.length + 1;
587
+ continue;
588
+ }
589
+ }
590
+ if (openFence !== null) {
591
+ // Inside a fence — never a bullet candidate.
592
+ offset += rawLine.length + 1;
593
+ continue;
594
+ }
595
+ let bulletText = null;
596
+ const cbm = checkboxRe.exec(line);
597
+ if (cbm) {
598
+ bulletText = cbm[3];
599
+ }
600
+ else {
601
+ const dm2 = dashRe.exec(line);
602
+ if (dm2) {
603
+ bulletText = dm2[2];
604
+ }
605
+ else {
606
+ const nm = numberedRe.exec(line);
607
+ if (nm)
608
+ bulletText = nm[2];
609
+ }
610
+ }
611
+ if (bulletText !== null && match(bulletText, rawLine)) {
612
+ const newLine = transform(rawLine);
613
+ if (typeof newLine !== 'string')
614
+ return content;
615
+ return content.slice(0, offset) + newLine + content.slice(offset + rawLine.length);
616
+ }
617
+ offset += rawLine.length + 1;
618
+ }
619
+ return content;
620
+ }
394
621
  // ─── extractTaggedBlocks ──────────────────────────────────────────────────────
395
622
  /**
396
623
  * Return the inner text of every `<tagName>…</tagName>` block in `content`,
@@ -506,5 +733,120 @@ function replaceSection(content, section, newBody) {
506
733
  return content;
507
734
  return content.slice(0, section.bodyStart) + newBody + content.slice(section.bodyEnd);
508
735
  }
736
+ // ─── withSection ──────────────────────────────────────────────────────────────
737
+ /**
738
+ * Locate the section whose heading matches `target`, run `edit` against ONLY
739
+ * that section's body, and splice the result back into `content`.
740
+ *
741
+ * `target` is either an exact (trimmed) heading-text match or a predicate
742
+ * function over `HeadingToken`. `edit` receives ONLY the section body — so any
743
+ * regex it runs is physically confined to that section — an edit cannot cross
744
+ * a section boundary (ADR-2143 §4, structurally retires the #2130/#2067/#2080
745
+ * boundary-crossing class, where a hand-rolled regex escaped its intended
746
+ * section and mutated a sibling/shipped/backticked-literal occurrence instead).
747
+ *
748
+ * Bounded no-op behaviour (Phase 3 of ADR-2143 adds fail-loud diagnostics on
749
+ * top of this):
750
+ * - No heading matches `target` → `content` is returned unchanged.
751
+ * - `edit` returns a non-string, or returns the same string it was given →
752
+ * `content` is returned unchanged (no-op splice avoided).
753
+ *
754
+ * `opts` is forwarded verbatim to `collectSection` (see its doc comment for
755
+ * `levelBounded` / `stopAtLevel` / `stripFences` semantics) — it lets a caller
756
+ * whose heading levels are non-uniform (e.g. a mix of `###`/`####` phase
757
+ * headings) choose the correct section-end rule instead of relying on the
758
+ * `levelBounded: true` default.
759
+ */
760
+ function withSection(content, target, edit, opts = {}) {
761
+ if (typeof content !== 'string')
762
+ return content;
763
+ const predicate = typeof target === 'function'
764
+ ? target
765
+ : (h) => h.text.trim() === target.trim();
766
+ const section = collectSection(content, predicate, opts);
767
+ if (!section)
768
+ return content; // bounded no-op on miss (Phase 3 adds fail-loud)
769
+ const newBody = edit(section.body);
770
+ if (typeof newBody !== 'string' || newBody === section.body)
771
+ return content;
772
+ return replaceSection(content, section, newBody);
773
+ }
774
+ // ─── deleteSection ────────────────────────────────────────────────────────────
775
+ /**
776
+ * Delete an entire section — the matching heading line ITSELF plus its body —
777
+ * and return the resulting full content string.
778
+ *
779
+ * Locates the target heading via the SAME machinery `collectSection` uses
780
+ * (`tokenizeHeadings` + `headingPredicate`), then determines the stop boundary
781
+ * with the SAME level-bounding rule (`levelBounded` / `stopAtLevel`, see
782
+ * `CollectSectionOptions`): the deleted range runs from the target heading's
783
+ * OWN start offset up to (but not including) the next heading whose level is
784
+ * the same-or-higher (lower level number) than the target's — so a level-3
785
+ * `### Phase N` section deletes through any nested `####` content but STOPS at
786
+ * the next `##`/`###` sibling, whatever that heading's text is (unlike a
787
+ * hand-rolled regex anchored to a specific heading TEXT pattern, which keeps
788
+ * scanning past an unrelated heading and can run away to EOF when no further
789
+ * heading of that specific text shape follows — the whole-section-deletion
790
+ * data-loss class this primitive retires).
791
+ *
792
+ * Unlike `collectSection`/`withSection` (which operate on a section's BODY
793
+ * only, leaving the heading line untouched), `deleteSection` removes the
794
+ * heading line too — the counterpart for "delete section" call sites that
795
+ * `withSection` structurally cannot serve.
796
+ *
797
+ * Collapses at most one resulting blank-line seam: if removing the section
798
+ * leaves 2+ blank lines immediately at the splice point (e.g. the original
799
+ * document already had a double-blank separator immediately before the
800
+ * deleted heading), the seam is normalized down to a single blank line so no
801
+ * double-blank gap accumulates where the section used to sit. Content
802
+ * elsewhere in the document is never touched.
803
+ *
804
+ * Returns `content` unchanged when no heading matches `headingPredicate`
805
+ * (bounded no-op, mirroring `withSection`'s miss behaviour).
806
+ */
807
+ function deleteSection(content, headingPredicate, opts = {}) {
808
+ if (typeof content !== 'string')
809
+ return content;
810
+ const { levelBounded = true, stopAtLevel } = opts;
811
+ const headings = tokenizeHeadings(content);
812
+ const targetIdx = headings.findIndex(headingPredicate);
813
+ if (targetIdx === -1)
814
+ return content;
815
+ const target = headings[targetIdx];
816
+ const lines = content.split('\n');
817
+ // Determine the stop line using the SAME level-bounding rule collectSection uses.
818
+ let stopLine = lines.length + 1; // 1-based, exclusive (default: EOF+1)
819
+ for (let j = targetIdx + 1; j < headings.length; j++) {
820
+ const next = headings[j];
821
+ let isStop;
822
+ if (stopAtLevel !== undefined) {
823
+ isStop = next.level <= stopAtLevel;
824
+ }
825
+ else {
826
+ isStop = levelBounded ? next.level <= target.level : true;
827
+ }
828
+ if (isStop) {
829
+ stopLine = next.line;
830
+ break;
831
+ }
832
+ }
833
+ // Character offsets — same line-offset table collectSection builds.
834
+ const lineOffsets = new Array(lines.length);
835
+ let acc = 0;
836
+ for (let i = 0; i < lines.length; i++) {
837
+ lineOffsets[i] = acc;
838
+ acc += lines[i].length + 1; // +1 for the '\n' separator
839
+ }
840
+ const eofOffset = acc;
841
+ const sectionStart = lineOffsets[target.line - 1]; // start of the target heading LINE itself
842
+ const sectionEnd = stopLine <= lines.length ? lineOffsets[stopLine - 1] : eofOffset;
843
+ const before = content.slice(0, sectionStart);
844
+ const after = content.slice(sectionEnd);
845
+ // Collapse a resulting blank-line seam to at most one blank line (2 newlines).
846
+ // Only the tail of `before` (immediately at the splice point) is touched —
847
+ // this never reaches into unrelated content elsewhere in the document.
848
+ const collapsedBefore = before.replace(/(?:\r\n|\n){3,}$/, (m) => (m.includes('\r\n') ? '\r\n\r\n' : '\n\n'));
849
+ return collapsedBefore + after;
850
+ }
509
851
  // Consumers: require('../gsd-core/bin/lib/markdown-sectionizer.cjs')
510
852
  // Named CJS exports are the canonical surface (ADR-457 .cts → .cjs build-at-publish).