wachiman 1.14.0 → 1.16.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 (72) hide show
  1. package/README.md +2 -1
  2. package/package.json +4 -2
  3. package/src/accounts/commands.ts +3 -2
  4. package/src/accounts/confirm.ts +63 -4
  5. package/src/accounts/profile-list.ts +24 -0
  6. package/src/cli.ts +161 -142
  7. package/src/config.ts +19 -6
  8. package/src/connectors/local-git.ts +47 -0
  9. package/src/doctor/checks.ts +106 -1
  10. package/src/doctor/run.ts +9 -1
  11. package/src/engine.ts +294 -97
  12. package/src/help.ts +637 -0
  13. package/src/init/args.ts +5 -2
  14. package/src/init/github-login.ts +13 -0
  15. package/src/init/options.ts +31 -3
  16. package/src/init/prompt.ts +46 -15
  17. package/src/init/run.ts +238 -32
  18. package/src/init/settings.ts +127 -0
  19. package/src/init/write.ts +16 -102
  20. package/src/ledger-read/command.ts +106 -0
  21. package/src/ledger-read/credentials.ts +103 -0
  22. package/src/ledger-read/files.ts +141 -0
  23. package/src/ledger-read/pull.ts +69 -0
  24. package/src/ledger-read/rows.ts +60 -0
  25. package/src/ledger-read/types.ts +127 -0
  26. package/src/ledger.ts +34 -18
  27. package/src/local-ask.ts +5 -4
  28. package/src/local.ts +55 -10
  29. package/src/loop/command.ts +5 -5
  30. package/src/pr-template/adopt.ts +434 -0
  31. package/src/pr-template/catalog.ts +429 -0
  32. package/src/pr-template/check-run.ts +328 -0
  33. package/src/pr-template/check.ts +338 -0
  34. package/src/pr-template/describe.ts +368 -0
  35. package/src/pr-template/format.ts +399 -0
  36. package/src/pr-template/gh.ts +159 -0
  37. package/src/pr-template/install.ts +288 -0
  38. package/src/pr-template/legacy-guides.ts +50 -0
  39. package/src/pr-template/pr-risk.ts +426 -0
  40. package/src/pr-template/render.ts +302 -0
  41. package/src/pr-template/resolve.ts +65 -0
  42. package/src/pr-template/run.ts +88 -0
  43. package/src/pr-template/strip.ts +171 -0
  44. package/src/pr-template/write.ts +139 -0
  45. package/src/providers/claude-code.ts +7 -8
  46. package/src/providers/codex.ts +4 -4
  47. package/src/providers/spawn.ts +95 -0
  48. package/src/providers/types.ts +5 -2
  49. package/src/repo-settings.ts +159 -0
  50. package/src/setup/prompt.ts +49 -33
  51. package/src/setup/run.ts +58 -16
  52. package/src/sinks/common.ts +10 -3
  53. package/src/sinks/markdown.ts +3 -2
  54. package/src/sinks/terminal.ts +3 -2
  55. package/src/spend/report.ts +349 -0
  56. package/src/status/command.ts +150 -36
  57. package/src/store/command.ts +20 -3
  58. package/src/store/html.ts +205 -8
  59. package/src/store/index.ts +64 -5
  60. package/src/store/rows.ts +103 -0
  61. package/src/upgrade/command.ts +286 -0
  62. package/src/upgrade/pin.ts +112 -0
  63. package/src/wizard/interactive.ts +11 -0
  64. package/src/wizard/launcher.ts +340 -0
  65. package/src/wizard/load.ts +30 -0
  66. package/src/wizard/mascot.ts +228 -0
  67. package/src/wizard/prompt-fancy.ts +135 -0
  68. package/src/wizard/prompt.ts +108 -61
  69. package/src/wizard/style.ts +93 -13
  70. package/src/wizard/tone.ts +11 -0
  71. package/src/wizard/ui.ts +80 -0
  72. package/templates/pr/sections.json +163 -0
@@ -0,0 +1,434 @@
1
+ /**
2
+ * Adopting an unmanaged `.github/pull_request_template.md` (ADR 0052). The file is read in blocks
3
+ * (a table row, a heading, a `<details>` element, an HTML comment, or a run of lines up to a blank
4
+ * line) and split into the template's own structure, which the catalog replaces, and everything
5
+ * else, which moves into `.github/pr-template.json`:
6
+ *
7
+ * - what the catalog knows (guide comments, the status table, its `##` sections, its `<details>`
8
+ * blocks, the Wachiman block, the footer) is dropped, because the catalog generates it again;
9
+ * - an unknown status row, `##` section or `<details>` block becomes a `custom` entry;
10
+ * - any other block goes to `preamble`, in order.
11
+ *
12
+ * What the loader (`loadRepoChoice`) would reject never reaches the output: a name it refuses stays
13
+ * in the preamble as ordinary lines, a Wachiman marker (any `<!-- wachiman:` comment) is dropped
14
+ * (the catalog writes its own), and a comment that is opened and never closed (or closed and never
15
+ * opened) or a fenced code block that is never closed refuses the adoption with a `UsageError`,
16
+ * before anything is written.
17
+ *
18
+ * The report lists every block that moved and every comment taken for a guide, so a repository
19
+ * comment misread as guidance is visible (and still in the backup).
20
+ */
21
+ import { UsageError } from "../config";
22
+ import { checkCustomName, commentProblem, CUSTOM_NAME_KEY, entryName, fenceProblem, type Catalog, type CatalogEntry, type CustomEntry, type CustomKind } from "./catalog";
23
+ import { LEGACY_HEADER_PREFIX, collapseSpace, isLegacyGuide } from "./legacy-guides";
24
+ import { STATUS_GUIDE_LEAD, normalizeForStamp } from "./render";
25
+
26
+ export interface Adoption {
27
+ custom: CustomEntry[];
28
+ /** Blocks and lines for `preamble`; an empty string is the blank line between two blocks that were apart. */
29
+ preamble: string[];
30
+ /** Catalog entries that are not in the default template but were in the old file. */
31
+ add: string[];
32
+ report: string[];
33
+ }
34
+
35
+ const same = (a: string, b: string) => a.trim().toLowerCase() === b.trim().toLowerCase();
36
+ const clip = (text: string, n = 90) => (text.length > n ? `${text.slice(0, n - 1)}…` : text);
37
+ const LIST_ITEM = /^\s{0,3}([-*+]|\d+[.)])\s/;
38
+ const HEADING = /^(#{1,6})\s+(.*?)\s*$/;
39
+ const DETAILS_OPEN = /^\s*<details\b[^>]*>\s*<summary>(.*?)<\/summary>\s*$/i;
40
+ const STATUS_ROW = /^\|\s*\*\*(.+?)\*\*\s*\|(.*)\|\s*$/;
41
+ const TABLE_HEADER = /^\|\s*\|\s*\|\s*$/;
42
+ const TABLE_SEPARATOR = /^\|(\s*:?-+:?\s*\|)+\s*$/;
43
+ const WACHIMAN_MARKER = /<!--\s*wachiman:|wachiman:confidence:|wachiman:pr-template/i;
44
+ const FOOTER = /^<sub>(.*)<\/sub>\s*$/;
45
+
46
+ /** Whether `line` starts a block of its own, so a text run ends before it. */
47
+ const startsBlock = (line: string) => /^\s*<!--/.test(line) || /^#{1,6}\s/.test(line) || /^\s*<\/?details\b/i.test(line) || /^\s*\|/.test(line) || /^<sub>/.test(line);
48
+
49
+ const slug = (name: string) =>
50
+ name
51
+ .toLowerCase()
52
+ .replace(/[^a-z0-9]+/g, "-")
53
+ .replace(/^-+|-+$/g, "") || "custom";
54
+
55
+ export interface AdoptContext {
56
+ catalog: Catalog;
57
+ /** Custom ids and names the repository's JSON already has (to avoid a duplicate when merging). */
58
+ existingCustom?: CustomEntry[];
59
+ /** The `drop` list of the repository's JSON: a section it dropped is not added back. */
60
+ existingDrop?: string[];
61
+ }
62
+
63
+ export function analyzeUnmanaged(fileText: string, { catalog, existingCustom = [], existingDrop = [] }: AdoptContext): Adoption {
64
+ const lines = fileText.replace(/\r\n?/g, "\n").split("\n");
65
+ const result: Adoption = { custom: [], preamble: [], add: [], report: [] };
66
+ const report = (line: string) => result.report.push(line);
67
+
68
+ const lockedNames = catalog.sections.filter((e) => e.locked).map((e) => entryName(e)).filter((n): n is string => n !== undefined);
69
+ const catalogGuides = new Set(catalog.sections.flatMap((e) => (e.guide === undefined ? [] : [collapseSpace(e.guide)])).concat(collapseSpace(catalog.intro)));
70
+ const usedIds = new Set([...catalog.sections.map((e) => e.id), ...existingCustom.map((c) => c.id)]);
71
+ const knownCount = { rows: 0, sections: 0, details: 0 };
72
+
73
+ const findKnown = (kind: CatalogEntry["kind"], name: string) => catalog.sections.find((e) => e.kind === kind && entryName(e) !== undefined && same(entryName(e)!, name));
74
+ // A table line is compared as a formatter leaves it equal: the padding of a cell and the width of a separator do not count.
75
+ const canon = (line: string) => normalizeForStamp(line).trim();
76
+ const bodyOf = (e: CatalogEntry): Set<string> => new Set(("body" in e ? e.body : []).map(canon).filter((l) => l !== ""));
77
+
78
+ type Ctx = { known: CatalogEntry } | { custom: CustomEntry } | null;
79
+ let ctx: Ctx = null;
80
+ let afterKnown = false;
81
+ let prevRowId: string | undefined;
82
+ let prevOtherId: string | undefined;
83
+ let sawHeader = false;
84
+ let lastPreambleEnd = -1;
85
+ // One entry per `<details>` that is open: "moved" when its lines go to the preamble (so its closing tag goes too), "structure" when the catalog regenerates it.
86
+ const openDetails: ("moved" | "structure")[] = [];
87
+
88
+ const noteKnown = (e: CatalogEntry) => {
89
+ if (e.kind === "status-row") prevRowId = e.id;
90
+ else if (e.kind !== "block" && e.kind !== "footer") prevOtherId = e.id;
91
+ if (!e.default && !result.add.includes(e.id)) {
92
+ if (existingDrop.includes(e.id)) report(`"${entryName(e)}" is in the old file, but .github/pr-template.json drops it; it stays dropped.`);
93
+ else result.add.push(e.id);
94
+ }
95
+ };
96
+
97
+ const toPreamble = (blockLines: string[], start: number, end: number) => {
98
+ const items: string[] = [];
99
+ for (const raw of blockLines) {
100
+ let line = raw.replace(/<(model|sha)>/gi, (_m, tag: string) => `{${tag.toLowerCase()}}`);
101
+ if (line !== raw) report("rewrote a <model> or <sha> tag as a {model} or {sha} placeholder; GitHub hides HTML tags.");
102
+ // The catalog writes the Wachiman markers itself, and the loader refuses them in the preamble.
103
+ if (WACHIMAN_MARKER.test(line)) {
104
+ const cleaned = line.replace(/<!--\s*wachiman:[^]*?-->/gi, "");
105
+ report(`dropped a Wachiman marker from a line that was kept: ${clip(line.trim())}`);
106
+ if (WACHIMAN_MARKER.test(cleaned) || cleaned.trim() === "") continue;
107
+ line = cleaned.trimEnd();
108
+ }
109
+ if (items.length === 0 || LIST_ITEM.test(line)) items.push(line);
110
+ else items[items.length - 1] += `\n${line}`;
111
+ }
112
+ for (const item of items) {
113
+ const problem = commentProblem(item);
114
+ if (problem !== undefined) {
115
+ throw new UsageError(
116
+ `The old template ${problem} near "${clip(item.trim(), 60)}", which would hide or break the generated template. Close the comment with "-->" (or remove it) in the old file and run install again; nothing was written.`,
117
+ );
118
+ }
119
+ }
120
+ if (items.length === 0) return;
121
+ if (result.preamble.length > 0 && lastPreambleEnd !== start) result.preamble.push("");
122
+ result.preamble.push(...items);
123
+ lastPreambleEnd = end;
124
+ report(`moved to preamble: ${blockLines.map((l, i) => (i === 0 ? l.trim() : `\n ${l.trim()}`)).join("")}`);
125
+ };
126
+
127
+ const newCustom = (kind: CustomKind, name: string): CustomEntry | null => {
128
+ if (lockedNames.some((n) => same(n, name))) return null;
129
+ // The loader checks the name as it is stored; an entry it would refuse stays as ordinary lines in the preamble.
130
+ try {
131
+ checkCustomName(name.trim(), CUSTOM_NAME_KEY[kind], kind, "");
132
+ } catch (err) {
133
+ const kindName = kind === "status-row" ? "status row" : kind === "section" ? "section" : "collapsed block";
134
+ report(`"${clip(name.trim(), 40)}" cannot be a custom ${kindName} (${(err as Error).message.replace(/^:\s*/, "")}); its lines stay in the preamble as they are.`);
135
+ return null;
136
+ }
137
+ if (result.custom.some((c) => c.kind === kind && same(c.name, name))) return null;
138
+ const existing = existingCustom.find((c) => c.kind === kind && same(c.name, name));
139
+ if (existing) {
140
+ // Already a custom entry of the repository: it stays as it is; its place in the old file only moves the anchor.
141
+ if (kind === "status-row") prevRowId = existing.id;
142
+ else prevOtherId = existing.id;
143
+ report(`"${existing.name}" is already a custom entry in .github/pr-template.json; kept as it is.`);
144
+ return { ...existing };
145
+ }
146
+ let id = slug(name);
147
+ for (let n = 2; usedIds.has(id); n++) id = `${slug(name)}-${n}`;
148
+ usedIds.add(id);
149
+ const after = kind === "status-row" ? prevRowId : prevOtherId;
150
+ const entry: CustomEntry = { id, kind, name: name.trim(), required: false, ...(after !== undefined ? { after } : {}) };
151
+ result.custom.push(entry);
152
+ if (kind === "status-row") prevRowId = id;
153
+ else prevOtherId = id;
154
+ report(`kept the ${kind === "status-row" ? "status row" : kind === "section" ? "section" : "collapsed block"} "${entry.name}" as a custom entry${after ? ` after "${after}"` : ""} in .github/pr-template.json.`);
155
+ return entry;
156
+ };
157
+
158
+ const bodyCovered = (run: string[]) => {
159
+ if (ctx === null || !("known" in ctx)) return false;
160
+ const body = bodyOf(ctx.known);
161
+ return run.every((l) => l.trim() === "" || body.has(canon(l)));
162
+ };
163
+
164
+ let i = 0;
165
+ while (i < lines.length) {
166
+ const line = lines[i]!;
167
+ if (line.trim() === "") {
168
+ i++;
169
+ continue;
170
+ }
171
+
172
+ // An HTML comment of its own.
173
+ if (/^\s*<!--/.test(line)) {
174
+ let j = i;
175
+ while (j < lines.length && !lines[j]!.includes("-->", j === i ? line.indexOf("<!--") + 4 : 0)) j++;
176
+ const last = lines[Math.min(j, lines.length - 1)]!;
177
+ if (j < lines.length && last.trim().endsWith("-->")) {
178
+ const raw = lines.slice(i, j + 1);
179
+ const inner = collapseSpace(raw.join("\n").trim().replace(/^<!--/, "").replace(/-->$/, ""));
180
+ if (inner === "wachiman:confidence:start") {
181
+ let k = j + 1;
182
+ while (k < lines.length && collapseSpace(lines[k]!.replace(/^\s*<!--/, "").replace(/-->\s*$/, "")) !== "wachiman:confidence:end") k++;
183
+ if (k < lines.length) {
184
+ report("replaced the Wachiman block by the catalog's.");
185
+ ctx = null;
186
+ afterKnown = false;
187
+ i = k + 1;
188
+ continue;
189
+ }
190
+ // No end marker: nothing says where the block stops, so only the marker line goes (the catalog writes its own) and everything after it is kept.
191
+ report("a Wachiman start marker has no end marker; the marker line was dropped (the catalog writes its own) and everything after it was kept.");
192
+ ctx = null;
193
+ afterKnown = false;
194
+ i = j + 1;
195
+ continue;
196
+ }
197
+ if (inner === "wachiman:confidence:end" || inner.startsWith("wachiman:pr-template")) {
198
+ ctx = null;
199
+ afterKnown = false;
200
+ i = j + 1;
201
+ continue;
202
+ }
203
+ const isHeader = raw[0]!.trimStart().startsWith(LEGACY_HEADER_PREFIX);
204
+ const matches = isHeader || isLegacyGuide(inner) || catalogGuides.has(inner) || inner.startsWith(collapseSpace(STATUS_GUIDE_LEAD));
205
+ if (isHeader || matches || afterKnown) {
206
+ if (!isHeader && ctx !== null && "custom" in ctx && !matches && ctx.custom.guide === undefined && !/<!--|-->/.test(inner)) {
207
+ if (result.custom.includes(ctx.custom)) {
208
+ ctx.custom.guide = inner;
209
+ report(`kept the comment under "${ctx.custom.name}" as its guide: ${clip(inner)}`);
210
+ } else {
211
+ report(`the comment under "${ctx.custom.name}" was not kept as a guide: the entry is already in .github/pr-template.json and stays as it is (the comment is in the backup): ${clip(inner)}`);
212
+ }
213
+ } else {
214
+ report(`treated as a guide comment and replaced: ${clip(inner)}`);
215
+ }
216
+ afterKnown = false;
217
+ i = j + 1;
218
+ continue;
219
+ }
220
+ toPreamble(raw, i, j + 1);
221
+ afterKnown = false;
222
+ i = j + 1;
223
+ continue;
224
+ }
225
+ // An opening `<!--` with no clean end: it is ordinary text; fall through.
226
+ }
227
+
228
+ const heading = HEADING.exec(line);
229
+ if (heading) {
230
+ const level = heading[1]!.length;
231
+ const name = heading[2]!.replace(/(?:^|\s+)#+\s*$/, "");
232
+ if (level === 2) {
233
+ const known = findKnown("section", name);
234
+ if (known) {
235
+ ctx = { known };
236
+ knownCount.sections++;
237
+ noteKnown(known);
238
+ afterKnown = true;
239
+ i++;
240
+ continue;
241
+ }
242
+ const entry = newCustom("section", name);
243
+ if (entry) {
244
+ ctx = { custom: entry };
245
+ afterKnown = true;
246
+ i++;
247
+ continue;
248
+ }
249
+ }
250
+ toPreamble([line], i, i + 1);
251
+ ctx = null;
252
+ afterKnown = false;
253
+ i++;
254
+ continue;
255
+ }
256
+
257
+ const details = DETAILS_OPEN.exec(line);
258
+ if (details) {
259
+ const summary = details[1]!;
260
+ const bold = /^\s*<b>(.*?)<\/b>/i.exec(summary);
261
+ const name = (bold ? bold[1]! : summary.replace(/<[^>]+>/g, "")).trim();
262
+ const known = findKnown("details", name);
263
+ if (known) {
264
+ openDetails.push("structure");
265
+ ctx = { known };
266
+ knownCount.details++;
267
+ noteKnown(known);
268
+ afterKnown = true;
269
+ i++;
270
+ continue;
271
+ }
272
+ const entry = newCustom("details", name);
273
+ if (entry) {
274
+ ctx = { custom: entry };
275
+ openDetails.push("structure");
276
+ afterKnown = true;
277
+ i++;
278
+ continue;
279
+ }
280
+ // A locked or duplicate name: the block is the repository's, so it moves whole, with its closing tag.
281
+ openDetails.push("moved");
282
+ toPreamble([line], i, i + 1);
283
+ ctx = null;
284
+ afterKnown = false;
285
+ i++;
286
+ continue;
287
+ }
288
+ if (/^\s*<details\b/i.test(line)) {
289
+ // Any other opening tag is ordinary text, but its closing tag has to travel with it.
290
+ openDetails.push("moved");
291
+ toPreamble([line], i, i + 1);
292
+ ctx = null;
293
+ afterKnown = false;
294
+ i++;
295
+ continue;
296
+ }
297
+ if (/^\s*<\/details>\s*$/i.test(line)) {
298
+ const closed = openDetails.pop();
299
+ if (closed === "moved") {
300
+ toPreamble([line], i, i + 1);
301
+ afterKnown = false;
302
+ i++;
303
+ continue;
304
+ }
305
+ ctx = null;
306
+ afterKnown = false;
307
+ i++;
308
+ continue;
309
+ }
310
+
311
+ if (/^\s*\|/.test(line)) {
312
+ const trimmed = line.trim();
313
+ if (TABLE_HEADER.test(trimmed)) {
314
+ sawHeader = true;
315
+ i++;
316
+ continue;
317
+ }
318
+ if (TABLE_SEPARATOR.test(trimmed) && sawHeader) {
319
+ sawHeader = false;
320
+ i++;
321
+ continue;
322
+ }
323
+ sawHeader = false;
324
+ const row = STATUS_ROW.exec(trimmed);
325
+ if (row) {
326
+ const label = row[1]!;
327
+ const known = findKnown("status-row", label);
328
+ if (known) {
329
+ knownCount.rows++;
330
+ noteKnown(known);
331
+ ctx = null;
332
+ afterKnown = true;
333
+ i++;
334
+ continue;
335
+ }
336
+ const entry = newCustom("status-row", label);
337
+ if (entry) {
338
+ const cell = row[2]!.trim();
339
+ const text = collapseSpace(/^<!--(.*)-->$/.exec(cell)?.[1] ?? cell);
340
+ if (/<!--|-->|\|/.test(text) && result.custom.includes(entry)) {
341
+ // More than one value, or markup a guide cannot carry: dropping it would lose the repository's text.
342
+ throw new UsageError(
343
+ `The old template has a row of "${entry.name}" on line ${i + 1} with more than one value or markup in its cell: "${clip(trimmed, 60)}". A row keeps one plain value as its guide. Reduce the row to one value (or move the rest out of the table) in the old file and run install again; nothing was written.`,
344
+ );
345
+ } else if (text !== "" && !result.custom.includes(entry)) {
346
+ report(`the cell of "${entry.name}" was not kept as a guide: the entry is already in .github/pr-template.json and stays as it is (the cell is in the backup).`);
347
+ } else if (text !== "") {
348
+ entry.guide = text;
349
+ report(`kept the cell of "${entry.name}" as its guide: ${clip(text)}`);
350
+ }
351
+ ctx = null;
352
+ afterKnown = false;
353
+ i++;
354
+ continue;
355
+ }
356
+ }
357
+ if (bodyCovered([trimmed])) {
358
+ i++;
359
+ continue;
360
+ }
361
+ toPreamble([line], i, i + 1);
362
+ afterKnown = false;
363
+ i++;
364
+ continue;
365
+ }
366
+
367
+ const footer = FOOTER.exec(line.trim());
368
+ if (footer && /Describes commit|<sha>|\{sha\}/i.test(footer[1]!)) {
369
+ report("replaced the footer by the catalog's.");
370
+ ctx = null;
371
+ afterKnown = false;
372
+ i++;
373
+ continue;
374
+ }
375
+
376
+ // A run of lines up to a blank line or the start of another kind of block.
377
+ let j = i + 1;
378
+ while (j < lines.length && lines[j]!.trim() !== "" && !startsBlock(lines[j]!)) j++;
379
+ const run = lines.slice(i, j);
380
+ // Inside a known section the lines the catalog itself generates are structure; the others are the repository's.
381
+ const rest = run.filter((l) => !bodyCovered([l]));
382
+ if (rest.length > 0) toPreamble(rest, i, j);
383
+ afterKnown = false;
384
+ i = j;
385
+ }
386
+
387
+ // The loader reads the whole preamble, so a fence is judged across blocks (one may hold a blank line).
388
+ const openFence = fenceProblem(result.preamble);
389
+ if (openFence !== undefined) {
390
+ const at = result.preamble.findLastIndex((entry) => /^ {0,3}(`{3,}|~{3,})/m.test(entry));
391
+ const where = result.preamble.slice(Math.max(at, 0)).join("\n").trim();
392
+ throw new UsageError(
393
+ `The old template ${openFence} near "${clip(where, 60)}", which would render the rest of the generated template as code. Close the fence in the old file and run install again; nothing was written.`,
394
+ );
395
+ }
396
+
397
+ const parts = [
398
+ knownCount.rows > 0 ? `${knownCount.rows} status rows` : "",
399
+ knownCount.sections > 0 ? `${knownCount.sections} sections` : "",
400
+ knownCount.details > 0 ? `${knownCount.details} collapsed blocks` : "",
401
+ ].filter((p) => p !== "");
402
+ if (parts.length > 0) report(`replaced by the catalog's version: ${parts.join(", ")}.`);
403
+ return result;
404
+ }
405
+
406
+ /** What `.github/pr-template.json` becomes: the existing definition (or an empty one) plus what the old file contributed. */
407
+ export function mergeAdoption(base: Record<string, unknown> | null, adoption: Adoption): Record<string, unknown> {
408
+ const out: Record<string, unknown> = { base: "default", ...(base ?? {}) };
409
+ const list = (key: string) => ((out[key] as unknown[] | undefined) ?? []) as string[];
410
+
411
+ const add = [...list("add")];
412
+ for (const id of adoption.add) if (!add.includes(id)) add.push(id);
413
+ if (add.length > 0) out.add = add;
414
+
415
+ const custom = [...((out.custom as CustomEntry[] | undefined) ?? [])];
416
+ const nameKey = { "status-row": "label", section: "heading", details: "summary" } as const;
417
+ const serial = (c: CustomEntry) => {
418
+ const { name, kind, ...rest } = c;
419
+ return { id: rest.id, kind, [nameKey[kind]]: name, required: rest.required, ...(rest.guide !== undefined ? { guide: rest.guide } : {}), ...(rest.after !== undefined ? { after: rest.after } : {}) };
420
+ };
421
+ for (const c of adoption.custom) custom.push(serial(c) as unknown as CustomEntry);
422
+ if (custom.length > 0) out.custom = custom;
423
+
424
+ const preamble = [...list("preamble")];
425
+ const fresh = adoption.preamble.filter((p, i, all) => (p === "" ? all[i - 1] !== "" : !preamble.includes(p)));
426
+ while (fresh.length > 0 && fresh[0] === "") fresh.shift();
427
+ while (fresh.length > 0 && fresh[fresh.length - 1] === "") fresh.pop();
428
+ if (fresh.length > 0) {
429
+ if (preamble.length > 0 && preamble[preamble.length - 1] !== "") preamble.push("");
430
+ preamble.push(...fresh);
431
+ }
432
+ if (preamble.length > 0) out.preamble = preamble;
433
+ return out;
434
+ }