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,429 @@
1
+ /**
2
+ * The PR description template's section catalog (`templates/pr/sections.json`) and a
3
+ * repository's choice on top of it (`.github/pr-template.json`). Both are data: this file reads
4
+ * them, checks them and returns typed entries. Resolving, rendering and checking live elsewhere
5
+ * (ADR 0050).
6
+ *
7
+ * Every error is a `PrTemplateConfigError`, a `UsageError` (exit 2), and there is never a
8
+ * fallback to another version or to the default: a broken definition stops the command.
9
+ */
10
+ import { readFileSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ import { UsageError } from "../config";
13
+
14
+ export class PrTemplateConfigError extends UsageError {}
15
+
16
+ export type SectionKind = "status-row" | "section" | "details" | "block" | "footer";
17
+
18
+ interface EntryBase {
19
+ /** Lower-case words joined by hyphens. Unique across the catalog and a repository's custom entries. */
20
+ id: string;
21
+ /** Locked entries are in every template and a repository can neither drop them nor reuse their name. */
22
+ locked: boolean;
23
+ /** Required entries cannot be dropped; the checker reports one that is missing. */
24
+ required: boolean;
25
+ /** Part of the default template. Catalog entries with `false` are offered for a repository to add. */
26
+ default: boolean;
27
+ /** A rule the checker applies: `risk-floor`, `not-empty`, `commit-is-head` or `max-rows:N`. */
28
+ rule?: string;
29
+ /** The comment that tells the author what goes here. */
30
+ guide?: string;
31
+ /** True for an entry that came from a repository's `custom` list. */
32
+ custom?: boolean;
33
+ /** For a custom entry: the id it was placed after. */
34
+ after?: string;
35
+ }
36
+ export interface StatusRowEntry extends EntryBase { kind: "status-row"; label: string }
37
+ export interface SectionEntry extends EntryBase { kind: "section"; heading: string; body: string[] }
38
+ export interface DetailsEntry extends EntryBase { kind: "details"; summary: string; summarySuffix: string; body: string[] }
39
+ export interface BlockEntry extends EntryBase { kind: "block"; start: string; end: string }
40
+ export interface FooterEntry extends EntryBase { kind: "footer"; text: string }
41
+ export type CatalogEntry = StatusRowEntry | SectionEntry | DetailsEntry | BlockEntry | FooterEntry;
42
+
43
+ export interface Catalog {
44
+ version: number;
45
+ /** The comment at the top of the generated file, under the stamp. */
46
+ intro: string;
47
+ sections: CatalogEntry[];
48
+ }
49
+
50
+ export type CustomKind = "status-row" | "section" | "details";
51
+ export interface CustomEntry {
52
+ id: string;
53
+ kind: CustomKind;
54
+ /** The label of a status row, the heading of a section or the summary of a details block. */
55
+ name: string;
56
+ required: boolean;
57
+ guide?: string;
58
+ after?: string;
59
+ }
60
+
61
+ export interface RepoChoice {
62
+ base: "default";
63
+ add: string[];
64
+ drop: string[];
65
+ custom: CustomEntry[];
66
+ preamble: string[];
67
+ }
68
+
69
+ export const CATALOG_FILE = "templates/pr/sections.json";
70
+ export const CHOICE_FILE = ".github/pr-template.json";
71
+
72
+ const ID = /^[a-z0-9]+(-[a-z0-9]+)*$/;
73
+ const RULE = /^(risk-floor|not-empty|commit-is-head|max-rows:[1-9][0-9]*)$/;
74
+
75
+ /** Where the catalog is read from: the package or clone root. */
76
+ export function catalogPath() {
77
+ return join(import.meta.dir, "..", "..", "templates", "pr", "sections.json");
78
+ }
79
+
80
+ export function loadCatalog(path: string = catalogPath()): Catalog {
81
+ let text: string;
82
+ try {
83
+ text = readFileSync(path, "utf8");
84
+ } catch (err) {
85
+ throw new PrTemplateConfigError(`${CATALOG_FILE} cannot be read: ${(err as Error).message}`);
86
+ }
87
+ return parseCatalog(text);
88
+ }
89
+
90
+ // ------------------------------------------------------------------ shared checks
91
+
92
+ type Obj = Record<string, unknown>;
93
+ const isObj = (v: unknown): v is Obj => typeof v === "object" && v !== null && !Array.isArray(v);
94
+
95
+ function parseJson(text: string, file: string): unknown {
96
+ try {
97
+ return JSON.parse(text);
98
+ } catch (err) {
99
+ throw new PrTemplateConfigError(`${file} is not valid JSON: ${(err as Error).message}`);
100
+ }
101
+ }
102
+
103
+ function onlyKeys(obj: Obj, allowed: readonly string[], where: string) {
104
+ for (const key of Object.keys(obj)) {
105
+ if (!allowed.includes(key)) throw new PrTemplateConfigError(`${where} has an unknown key "${key}" (allowed: ${allowed.join(", ")}).`);
106
+ }
107
+ }
108
+
109
+ function str(obj: Obj, key: string, where: string, { optional = false, nonEmpty = true } = {}): string | undefined {
110
+ const v = obj[key];
111
+ if (v === undefined) {
112
+ if (optional) return undefined;
113
+ throw new PrTemplateConfigError(`${where} needs "${key}" (a string).`);
114
+ }
115
+ if (typeof v !== "string") throw new PrTemplateConfigError(`${where}: "${key}" must be a string.`);
116
+ if (nonEmpty && v.trim() === "") throw new PrTemplateConfigError(`${where}: "${key}" must not be empty.`);
117
+ return v;
118
+ }
119
+
120
+ function bool(obj: Obj, key: string, where: string, fallback: boolean): boolean {
121
+ const v = obj[key];
122
+ if (v === undefined) return fallback;
123
+ if (typeof v !== "boolean") throw new PrTemplateConfigError(`${where}: "${key}" must be true or false.`);
124
+ return v;
125
+ }
126
+
127
+ function strings(obj: Obj, key: string, where: string): string[] {
128
+ const v = obj[key];
129
+ if (v === undefined) return [];
130
+ if (!Array.isArray(v) || v.some((s) => typeof s !== "string")) {
131
+ throw new PrTemplateConfigError(`${where}: "${key}" must be a list of strings.`);
132
+ }
133
+ return v as string[];
134
+ }
135
+
136
+ /** A guide comment sits inside `<!-- -->`, and a status row's sits inside a table cell. */
137
+ function checkGuide(guide: string | undefined, kind: SectionKind, where: string) {
138
+ if (guide === undefined) return;
139
+ if (/<!--|-->/.test(guide)) throw new PrTemplateConfigError(`${where}: the guide comment must not contain "<!--" or "-->".`);
140
+ if (kind === "status-row" && /[|\r\n]/.test(guide)) {
141
+ throw new PrTemplateConfigError(`${where}: the guide comment of a status row must be one line without "|".`);
142
+ }
143
+ }
144
+
145
+ /** The label, heading or summary an entry shows. */
146
+ export function entryName(entry: CatalogEntry): string | undefined {
147
+ if (entry.kind === "status-row") return entry.label;
148
+ if (entry.kind === "section") return entry.heading;
149
+ if (entry.kind === "details") return entry.summary;
150
+ return undefined;
151
+ }
152
+
153
+ const sameName = (a: string, b: string) => a.trim().toLowerCase() === b.trim().toLowerCase();
154
+
155
+ // ------------------------------------------------------------------ the catalog
156
+
157
+ const ENTRY_KEYS: Record<SectionKind, readonly string[]> = {
158
+ "status-row": ["id", "kind", "label", "locked", "required", "default", "rule", "guide"],
159
+ section: ["id", "kind", "heading", "locked", "required", "default", "rule", "guide", "body"],
160
+ details: ["id", "kind", "summary", "summarySuffix", "locked", "required", "default", "rule", "guide", "body"],
161
+ block: ["id", "kind", "start", "end", "locked", "required", "default", "rule", "guide"],
162
+ footer: ["id", "kind", "text", "locked", "required", "default", "rule", "guide"],
163
+ };
164
+
165
+ export function parseCatalog(text: string): Catalog {
166
+ const raw = parseJson(text, CATALOG_FILE);
167
+ const here = CATALOG_FILE;
168
+ if (!isObj(raw)) throw new PrTemplateConfigError(`${here} must be a JSON object.`);
169
+ onlyKeys(raw, ["version", "intro", "sections"], here);
170
+ const version = raw.version;
171
+ if (typeof version !== "number" || !Number.isInteger(version) || version < 1) {
172
+ throw new PrTemplateConfigError(`${here}: "version" must be a positive integer.`);
173
+ }
174
+ const intro = str(raw, "intro", here, { optional: true }) ?? "";
175
+ checkGuide(intro, "section", `${here} intro`);
176
+ if (!Array.isArray(raw.sections) || raw.sections.length === 0) {
177
+ throw new PrTemplateConfigError(`${here}: "sections" must be a non-empty list.`);
178
+ }
179
+
180
+ const entries: CatalogEntry[] = [];
181
+ const ids = new Set<string>();
182
+ raw.sections.forEach((item, i) => {
183
+ let where = `${here} sections[${i}]`;
184
+ if (!isObj(item)) throw new PrTemplateConfigError(`${where} must be an object.`);
185
+ const kind = item.kind;
186
+ if (kind !== "status-row" && kind !== "section" && kind !== "details" && kind !== "block" && kind !== "footer") {
187
+ throw new PrTemplateConfigError(`${where}: "kind" must be status-row, section, details, block or footer.`);
188
+ }
189
+ onlyKeys(item, ENTRY_KEYS[kind], where);
190
+ const id = str(item, "id", where) as string;
191
+ where = `${here} section "${id}"`;
192
+ if (!ID.test(id)) throw new PrTemplateConfigError(`${where}: the id must be lower-case words joined by hyphens.`);
193
+ if (ids.has(id)) throw new PrTemplateConfigError(`${here}: the id "${id}" is used twice.`);
194
+ ids.add(id);
195
+
196
+ const locked = bool(item, "locked", where, false);
197
+ const base = {
198
+ id,
199
+ locked,
200
+ required: bool(item, "required", where, locked),
201
+ default: bool(item, "default", where, true),
202
+ rule: str(item, "rule", where, { optional: true }),
203
+ guide: str(item, "guide", where, { optional: true }),
204
+ };
205
+ if (locked && !base.required) throw new PrTemplateConfigError(`${where} is locked, so it must be required.`);
206
+ if (locked && !base.default) throw new PrTemplateConfigError(`${where} is locked, so it must be in the default template.`);
207
+ if (base.rule !== undefined && !RULE.test(base.rule)) {
208
+ throw new PrTemplateConfigError(`${where}: unknown rule "${base.rule}" (known: risk-floor, not-empty, commit-is-head, max-rows:N).`);
209
+ }
210
+ checkGuide(base.guide, kind, where);
211
+ const clean = <T extends EntryBase>(e: T): T => {
212
+ if (e.rule === undefined) delete e.rule;
213
+ if (e.guide === undefined) delete e.guide;
214
+ return e;
215
+ };
216
+
217
+ if (kind === "status-row") {
218
+ entries.push(clean({ ...base, kind, label: str(item, "label", where) as string }));
219
+ } else if (kind === "section") {
220
+ entries.push(clean({ ...base, kind, heading: str(item, "heading", where) as string, body: strings(item, "body", where) }));
221
+ } else if (kind === "details") {
222
+ entries.push(clean({
223
+ ...base,
224
+ kind,
225
+ summary: str(item, "summary", where) as string,
226
+ summarySuffix: str(item, "summarySuffix", where, { optional: true, nonEmpty: false }) ?? "",
227
+ body: strings(item, "body", where),
228
+ }));
229
+ } else if (kind === "block") {
230
+ const start = str(item, "start", where) as string;
231
+ const end = str(item, "end", where) as string;
232
+ if (!locked) throw new PrTemplateConfigError(`${where}: a marked block must be locked.`);
233
+ entries.push(clean({ ...base, kind, start, end }));
234
+ } else {
235
+ const footer = str(item, "text", where) as string;
236
+ if (!locked) throw new PrTemplateConfigError(`${where}: the footer must be locked.`);
237
+ if (!footer.includes("{sha}")) throw new PrTemplateConfigError(`${where}: the footer text must contain the {sha} placeholder.`);
238
+ if (/<(sha|model)>/i.test(footer)) {
239
+ throw new PrTemplateConfigError(`${where}: write placeholders as {sha}; GitHub hides <sha> as an HTML tag.`);
240
+ }
241
+ entries.push(clean({ ...base, kind, text: footer }));
242
+ }
243
+ });
244
+
245
+ validateLayout(entries);
246
+ validateNames(entries);
247
+ return { version, intro, sections: entries };
248
+ }
249
+
250
+ /** Status rows first, one wachiman block, the footer last. */
251
+ function validateLayout(entries: CatalogEntry[]) {
252
+ const here = CATALOG_FILE;
253
+ const kinds = entries.map((e) => e.kind);
254
+ const firstOther = kinds.findIndex((k) => k !== "status-row");
255
+ if (firstOther === -1 || kinds.slice(firstOther).includes("status-row")) {
256
+ throw new PrTemplateConfigError(`${here}: the status rows must come first, together, and be followed by other entries.`);
257
+ }
258
+ if (kinds.filter((k) => k === "block").length !== 1) throw new PrTemplateConfigError(`${here} must have exactly one marked block.`);
259
+ if (kinds.filter((k) => k === "footer").length !== 1) throw new PrTemplateConfigError(`${here} must have exactly one footer.`);
260
+ if (kinds[kinds.length - 1] !== "footer") throw new PrTemplateConfigError(`${here}: the footer must be the last entry.`);
261
+ if (kinds[kinds.length - 2] !== "block") throw new PrTemplateConfigError(`${here}: the marked block must come right before the footer.`);
262
+ const hasRisk = entries.some((e) => e.kind === "status-row" && e.locked && e.rule === "risk-floor");
263
+ if (!hasRisk) throw new PrTemplateConfigError(`${here} must have a locked status row with the risk-floor rule.`);
264
+ }
265
+
266
+ /** No two entries of one kind show the same name. */
267
+ function validateNames(entries: CatalogEntry[]) {
268
+ const seen = new Map<string, string>();
269
+ for (const e of entries) {
270
+ const name = entryName(e);
271
+ if (name === undefined) continue;
272
+ const key = `${e.kind}:${name.trim().toLowerCase()}`;
273
+ const prev = seen.get(key);
274
+ if (prev !== undefined) throw new PrTemplateConfigError(`${CATALOG_FILE}: "${prev}" and "${e.id}" show the same name "${name}".`);
275
+ seen.set(key, e.id);
276
+ }
277
+ }
278
+
279
+ // ------------------------------------------------------------------ the repository's choice
280
+
281
+ export const CUSTOM_NAME_KEY: Record<CustomKind, string> = { "status-row": "label", section: "heading", details: "summary" };
282
+
283
+ export const MAX_NAME_LENGTH = 80;
284
+ const LINE_BREAKING_OR_CONTROL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
285
+
286
+ /**
287
+ * A custom name is written into the template as a heading, a table label or a bold summary, so it
288
+ * must not be able to add structure: no line break or control character (a second heading), no `|`
289
+ * in a status row (a second cell), no `<` or `>` (an HTML tag) and no `-->` (it would close the
290
+ * comment around a guide). The check applies to the name as written; nothing is repaired.
291
+ */
292
+ export function checkCustomName(name: string, nameKey: string, kind: CustomKind, where: string) {
293
+ if (name.trim() === "") throw new PrTemplateConfigError(`${where}: the ${nameKey} must not be empty.`);
294
+ if (LINE_BREAKING_OR_CONTROL.test(name)) throw new PrTemplateConfigError(`${where}: the ${nameKey} must be one line without control characters.`);
295
+ if (name !== name.trim() || / {2,}/.test(name)) {
296
+ throw new PrTemplateConfigError(`${where}: the ${nameKey} must not start or end with whitespace or contain a run of spaces; the checker reads headings and cells trimmed and collapsed.`);
297
+ }
298
+ if (name.length > MAX_NAME_LENGTH) {
299
+ throw new PrTemplateConfigError(`${where}: the ${nameKey} is ${name.length} characters; the limit is ${MAX_NAME_LENGTH}.`);
300
+ }
301
+ if (kind === "status-row" && name.includes("|")) throw new PrTemplateConfigError(`${where}: the ${nameKey} must not contain "|"; it is a table cell.`);
302
+ if (name.includes("-->")) throw new PrTemplateConfigError(`${where}: the ${nameKey} must not contain "-->".`);
303
+ if (/[<>]/.test(name)) throw new PrTemplateConfigError(`${where}: the ${nameKey} must not contain "<" or ">".`);
304
+ }
305
+
306
+ /**
307
+ * What is wrong with the HTML comments of one preamble entry: it opens a comment it does not close
308
+ * (GitHub would hide everything after it) or closes one it did not open. Undefined when balanced.
309
+ */
310
+ export function commentProblem(text: string): string | undefined {
311
+ let at = 0;
312
+ for (;;) {
313
+ const open = text.indexOf("<!--", at);
314
+ const close = text.indexOf("-->", at);
315
+ if (open === -1) return close === -1 ? undefined : 'contains a stray "-->"';
316
+ if (close !== -1 && close < open) return 'contains a stray "-->"';
317
+ const end = text.indexOf("-->", open + 4);
318
+ if (end === -1) return 'opens a comment ("<!--") it does not close';
319
+ at = end + 3;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Whether the preamble, taken as one block of lines, leaves a fenced code block open (GitHub would
325
+ * render everything below it as code). A fence opens on a line that starts, after up to three
326
+ * spaces, with three or more backticks or tildes, and closes on a later line of the same character,
327
+ * at least as long and with nothing after it.
328
+ */
329
+ export function fenceProblem(lines: readonly string[]): string | undefined {
330
+ let fence: string | null = null;
331
+ for (const line of lines.flatMap((l) => l.split("\n"))) {
332
+ const mark = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
333
+ if (mark === null) continue;
334
+ if (fence === null) fence = mark[1]!;
335
+ else if (mark[1]![0] === fence[0] && mark[1]!.length >= fence.length && mark[2]!.trim() === "") fence = null;
336
+ }
337
+ return fence === null ? undefined : 'opens a fenced code block ("```" or "~~~") it does not close';
338
+ }
339
+
340
+ /**
341
+ * Parse and validate `.github/pr-template.json`. `catalog` is the one the choice is read against
342
+ * (default: the shipped one). The text that is not valid is named in the error; nothing is
343
+ * repaired and nothing falls back.
344
+ */
345
+ export function loadRepoChoice(text: string, catalog: Catalog = loadCatalog()): RepoChoice {
346
+ const here = CHOICE_FILE;
347
+ const raw = parseJson(text, here);
348
+ if (!isObj(raw)) throw new PrTemplateConfigError(`${here} must be a JSON object.`);
349
+ onlyKeys(raw, ["base", "add", "drop", "custom", "preamble"], here);
350
+
351
+ const base = raw.base ?? "default";
352
+ if (base !== "default") {
353
+ throw new PrTemplateConfigError(`${here}: unsupported base ${JSON.stringify(base)}; the only base is "default".`);
354
+ }
355
+ const add = strings(raw, "add", here);
356
+ const drop = strings(raw, "drop", here);
357
+ const preamble = strings(raw, "preamble", here);
358
+ const byId = new Map(catalog.sections.map((e) => [e.id, e]));
359
+
360
+ for (const id of add) {
361
+ if (!byId.has(id)) throw new PrTemplateConfigError(`${here}: "add" names the unknown section "${id}".`);
362
+ }
363
+ for (const id of drop) {
364
+ const entry = byId.get(id);
365
+ if (!entry) throw new PrTemplateConfigError(`${here}: "drop" names the unknown section "${id}".`);
366
+ if (entry.locked) throw new PrTemplateConfigError(`${here}: "${id}" is locked and cannot be dropped.`);
367
+ if (entry.required) throw new PrTemplateConfigError(`${here}: "${id}" is required and cannot be dropped.`);
368
+ if (add.includes(id)) throw new PrTemplateConfigError(`${here}: "${id}" is both added and dropped.`);
369
+ }
370
+
371
+ if (raw.custom !== undefined && !Array.isArray(raw.custom)) throw new PrTemplateConfigError(`${here}: "custom" must be a list.`);
372
+ const customRaw = (raw.custom ?? []) as unknown[];
373
+ const custom: CustomEntry[] = [];
374
+ const customIds = new Set<string>();
375
+ const lockedNames = catalog.sections.filter((e) => e.locked).map((e) => entryName(e)).filter((n): n is string => n !== undefined);
376
+ const included = catalog.sections.filter((e) => e.locked || add.includes(e.id) || (e.default && !drop.includes(e.id)));
377
+
378
+ customRaw.forEach((item, i) => {
379
+ let where = `${here} custom[${i}]`;
380
+ if (!isObj(item)) throw new PrTemplateConfigError(`${where} must be an object.`);
381
+ const kind = item.kind;
382
+ if (kind !== "status-row" && kind !== "section" && kind !== "details") {
383
+ throw new PrTemplateConfigError(`${where}: "kind" must be status-row, section or details.`);
384
+ }
385
+ const nameKey = CUSTOM_NAME_KEY[kind];
386
+ onlyKeys(item, ["id", "kind", nameKey, "required", "guide", "after"], where);
387
+ const id = str(item, "id", where) as string;
388
+ where = `${here} custom entry "${id}"`;
389
+ if (!ID.test(id)) throw new PrTemplateConfigError(`${where}: the id must be lower-case words joined by hyphens.`);
390
+ if (byId.has(id) || customIds.has(id)) throw new PrTemplateConfigError(`${where}: the id is already used by another section.`);
391
+ const name = str(item, nameKey, where) as string;
392
+ checkCustomName(name, nameKey, kind, where);
393
+ const locked = lockedNames.find((n) => sameName(n, name));
394
+ if (locked !== undefined) throw new PrTemplateConfigError(`${where}: "${name}" reuses the locked name "${locked}".`);
395
+ const twin = included.find((e) => e.kind === kind && sameName(entryName(e) ?? "", name));
396
+ if (twin) throw new PrTemplateConfigError(`${where}: "${name}" is already the name of the section "${twin.id}".`);
397
+ const twinCustom = custom.find((c) => c.kind === kind && sameName(c.name, name));
398
+ if (twinCustom) throw new PrTemplateConfigError(`${where}: "${name}" is already the name of the custom entry "${twinCustom.id}".`);
399
+ const guide = str(item, "guide", where, { optional: true });
400
+ checkGuide(guide, kind, where);
401
+ const after = str(item, "after", where, { optional: true });
402
+ if (after !== undefined) {
403
+ const targetKind: SectionKind | undefined = byId.get(after)?.kind ?? custom.find((c) => c.id === after)?.kind;
404
+ if (!targetKind) throw new PrTemplateConfigError(`${where}: "after" names the unknown id "${after}" (it must be a catalog id or a custom entry listed before this one).`);
405
+ if (targetKind === "block" || targetKind === "footer") {
406
+ throw new PrTemplateConfigError(`${where}: nothing can be placed after "${after}"; the marked block and the footer close the template.`);
407
+ }
408
+ if ((kind === "status-row") !== (targetKind === "status-row")) {
409
+ throw new PrTemplateConfigError(`${where}: a ${kind} cannot be placed after "${after}", which is ${kind === "status-row" ? "not a status row" : "a status row"}.`);
410
+ }
411
+ }
412
+ customIds.add(id);
413
+ const entry: CustomEntry = { id, kind, name, required: bool(item, "required", where, false) };
414
+ if (guide !== undefined) entry.guide = guide;
415
+ if (after !== undefined) entry.after = after;
416
+ custom.push(entry);
417
+ });
418
+
419
+ preamble.forEach((line, i) => {
420
+ if (/wachiman:confidence:|wachiman:pr-template|<!--\s*wachiman:/.test(line)) {
421
+ throw new PrTemplateConfigError(`${here}: a preamble line must not contain a Wachiman marker.`);
422
+ }
423
+ const problem = commentProblem(line);
424
+ if (problem !== undefined) throw new PrTemplateConfigError(`${here}: preamble[${i}] ${problem}; the rest of the template would be hidden or broken.`);
425
+ });
426
+ const open = fenceProblem(preamble);
427
+ if (open !== undefined) throw new PrTemplateConfigError(`${here}: the preamble ${open}; the rest of the template would be rendered as code.`);
428
+ return { base: "default", add, drop, custom, preamble };
429
+ }