@williacj/rigger 0.0.0-stage → 0.1.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 (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +370 -2
  3. package/package.json +56 -4
  4. package/scripts/doc-reference-check.mjs +59 -0
  5. package/scripts/gate-check.mjs +249 -0
  6. package/scripts/lib/pack-result.mjs +9 -0
  7. package/scripts/record-commit.mjs +67 -0
  8. package/src/cli/doctor.mjs +1021 -0
  9. package/src/cli/init.mjs +384 -0
  10. package/src/cli/once.mjs +489 -0
  11. package/src/cli/pause.mjs +56 -0
  12. package/src/cli/plan.mjs +106 -0
  13. package/src/cli/recording.mjs +52 -0
  14. package/src/cli/report.mjs +242 -0
  15. package/src/cli/resume.mjs +96 -0
  16. package/src/cli/rigger.mjs +12 -0
  17. package/src/cli/run.mjs +10 -0
  18. package/src/cli/setup-board.mjs +108 -0
  19. package/src/cli/verbs.mjs +82 -0
  20. package/src/cli/verdict.mjs +180 -0
  21. package/src/config/validate.mjs +960 -0
  22. package/src/execution/groups.mjs +185 -0
  23. package/src/execution/kept-output.mjs +21 -0
  24. package/src/execution/role.mjs +171 -0
  25. package/src/execution/run.mjs +490 -0
  26. package/src/execution/step.mjs +26 -0
  27. package/src/execution/workspace.mjs +866 -0
  28. package/src/observation/push.mjs +103 -0
  29. package/src/observation/report.mjs +415 -0
  30. package/src/observation/sink.mjs +271 -0
  31. package/src/package-commit.json +1 -0
  32. package/src/scheduling/admission.mjs +390 -0
  33. package/src/scheduling/clock.mjs +98 -0
  34. package/src/scheduling/lane.mjs +160 -0
  35. package/src/scheduling/loop.mjs +1738 -0
  36. package/src/scheduling/pull-order.mjs +46 -0
  37. package/src/substrate/data-ref.mjs +147 -0
  38. package/src/substrate/forge/assets.mjs +5 -0
  39. package/src/substrate/forge/graphql.mjs +258 -0
  40. package/src/substrate/forge/item-write.mjs +74 -0
  41. package/src/substrate/forge/read.mjs +998 -0
  42. package/src/substrate/forge/repository-write.mjs +152 -0
  43. package/src/substrate/forge/runners.mjs +510 -0
  44. package/src/substrate/forge/schema-write.mjs +70 -0
  45. package/src/substrate/forge/write-made.mjs +40 -0
  46. package/src/substrate/git-environment.mjs +54 -0
  47. package/src/substrate/host.mjs +23 -0
  48. package/src/substrate/launchd.mjs +23 -0
  49. package/src/substrate/process-table.sh +48 -0
  50. package/src/substrate/process.mjs +2474 -0
  51. package/src/substrate/providers/adapters.mjs +8 -0
  52. package/src/substrate/providers/claude.mjs +427 -0
  53. package/src/substrate/providers/codex.mjs +349 -0
  54. package/src/substrate/standard-error.mjs +134 -0
  55. package/src/substrate/worktrees.mjs +517 -0
  56. package/src/workflow/clock-mark.mjs +40 -0
  57. package/src/workflow/escalation.mjs +110 -0
  58. package/src/workflow/facts.mjs +92 -0
  59. package/src/workflow/first-above.mjs +17 -0
  60. package/src/workflow/form-check.mjs +88 -0
  61. package/src/workflow/gate.mjs +62 -0
  62. package/src/workflow/judges.mjs +695 -0
  63. package/src/workflow/marker.mjs +265 -0
  64. package/src/workflow/next-action.mjs +456 -0
  65. package/src/workflow/status-issue.mjs +268 -0
  66. package/src/workflow/transitions.mjs +588 -0
  67. package/templates/claude/agents/architect.md +116 -0
  68. package/templates/claude/agents/engineer.md +57 -0
  69. package/templates/claude/agents/pm.md +114 -0
  70. package/templates/claude/agents/reviewer.md +37 -0
  71. package/templates/claude/agents/spike-engineer.md +80 -0
  72. package/templates/claude/hooks/refuse-reserved-git-commands.mjs +909 -0
  73. package/templates/claude/settings.json +50 -0
  74. package/templates/claude/skills/acceptance/SKILL.md +275 -0
  75. package/templates/claude/skills/agent-style/SKILL.md +78 -0
  76. package/templates/claude/skills/code-review/SKILL.md +242 -0
  77. package/templates/claude/skills/proposal/SKILL.md +175 -0
  78. package/templates/claude/skills/spec-style/SKILL.md +112 -0
  79. package/templates/claude/skills/tdd/SKILL.md +230 -0
  80. package/templates/codex/config.toml +19 -0
  81. package/templates/doc-reference-check.md +25 -0
  82. package/templates/githooks/pre-push +24 -0
  83. package/templates/github/workflows/rigger-gate.yml +209 -0
  84. package/templates/launchd/com.williacj.rigger.plist +31 -0
  85. package/templates/rigger.config.mjs +78 -0
@@ -0,0 +1,960 @@
1
+ // ABOUTME: The config core: the declarations a consumer may make, which of them Rigger requires,
2
+ // the values only the consumer can answer, and the refusal of anything else, each
3
+ // refusal naming what it refused.
4
+
5
+ import { posix } from 'node:path';
6
+ import { inspect } from 'node:util';
7
+
8
+ import { ADAPTERS } from '../substrate/providers/adapters.mjs';
9
+ import { EXIT_BOUND } from '../substrate/process.mjs';
10
+ import { kindOf } from '../workflow/next-action.mjs';
11
+ import { checkAcceptanceForm } from '../workflow/form-check.mjs';
12
+
13
+ /**
14
+ * The value the starter config carries where only the consumer can answer, by the key it sits
15
+ * under. A placeholder is a name where a value belongs, so a config still holding one is refused.
16
+ *
17
+ * `repo` is here because a repository not yet pushed anywhere has no `origin` for `init` to read.
18
+ * `project` is here for the stronger reason: a board number is GitHub's fact and nothing `init`
19
+ * can read names it. Asking GitHub for the boards linked to the repository answers three for
20
+ * `nodejs/node`, none for `cli/cli`, and a permission error for a repository behind an
21
+ * organization's SAML enforcement, each measured with `gh api graphql` on 2026-09-23 — so which
22
+ * board Rigger works is the consumer's to say, and a plausible number shipped as a default would
23
+ * have Rigger work whatever board it happens to name.
24
+ */
25
+ export const PLACEHOLDER = {
26
+ repo: 'OWNER/REPOSITORY',
27
+ project: 'PROJECT_NUMBER',
28
+ };
29
+
30
+ /**
31
+ * Every declaration a consumer may make, by the shape it sits in.
32
+ *
33
+ * `ARCHITECTURE.md`'s extension points are the whole of what a consumer may name, and the config
34
+ * shape published under that table is where each one is spelled. This table is that spelling and
35
+ * nothing besides: `test/config.test.mjs` runs the published shape and holds the two to each
36
+ * other, so a declaration the architecture does not spell is not offered here.
37
+ *
38
+ * A rule carries four things. `required` says Rigger can do nothing without the key and no
39
+ * default is fixed anywhere in the binding documents — which is why `repo` is required and
40
+ * `concurrency` is not, the engine-settings row defaulting N to three. `keys` names the shape the
41
+ * value's own keys are read against. `entries` names the shape each value under a consumer-named
42
+ * key is read against, which is how `roles`, `kinds` and `provisioning` hold names Rigger never
43
+ * fixes. `placeholder` names the value the starter config arrives holding, which is a name rather
44
+ * than an answer and is refused as one.
45
+ */
46
+ export const SHAPES = {
47
+ config: {
48
+ repo: { required: true, placeholder: PLACEHOLDER.repo },
49
+ board: { required: true, keys: 'board' },
50
+ concurrency: {},
51
+ worktrees: { keys: 'worktrees' },
52
+ roles: { required: true, entries: 'role' },
53
+ kinds: { required: true, entries: 'kind' },
54
+ // Optional: where it is absent no label marks an epic, and every card is selected by the
55
+ // kinds' own labels alone (`ARCHITECTURE.md`, below the extension-point table).
56
+ epicLabel: {},
57
+ provisioning: { entries: 'step' },
58
+ // Optional: a destination per escalation category, and a category given none goes to the
59
+ // owner (`ARCHITECTURE.md`, the Escalation destinations row).
60
+ escalate: { type: 'object' },
61
+ telemetry: { keys: 'telemetry' },
62
+ clock: { keys: 'clock' },
63
+ },
64
+ clock: {
65
+ poll: { required: true },
66
+ triggers: { entries: 'trigger' },
67
+ },
68
+ trigger: {
69
+ interval: { required: true },
70
+ anchor: { required: true },
71
+ catchUp: { required: true },
72
+ card: { keys: 'card' },
73
+ job: {},
74
+ },
75
+ card: {
76
+ title: { required: true },
77
+ labels: { required: true },
78
+ acceptance: { required: true },
79
+ author: {},
80
+ },
81
+ board: {
82
+ // Optional: where it is absent the board's owner is the repository's owner, which only the
83
+ // forge adapter supplies (`ARCHITECTURE.md`, below the extension-point table).
84
+ owner: {},
85
+ project: { required: true, placeholder: PLACEHOLDER.project },
86
+ columns: { required: true, keys: 'columns' },
87
+ // Optional: a board that declares no priority ranks every card alike, oldest first, which is
88
+ // the Engine settings row's shared bottom rank with nothing above it.
89
+ priority: { keys: 'priority' },
90
+ },
91
+ // The board field holding a card's priority, and that field's option display names, highest
92
+ // rank first. The declared order ranks cards, whatever order the board's own field holds.
93
+ priority: {
94
+ field: { required: true },
95
+ options: { required: true, type: 'array' },
96
+ },
97
+ // Every column the config declares, which M1 reads off the board. A display name has no
98
+ // default: a board whose columns are named differently drives the same loop only because the
99
+ // consumer says what they are called.
100
+ columns: {
101
+ ready: { required: true },
102
+ coding: { required: true },
103
+ review: { required: true },
104
+ owner: { required: true },
105
+ done: { required: true },
106
+ },
107
+ role: {
108
+ agent: { required: true },
109
+ provider: { required: true },
110
+ tier: { required: true },
111
+ // Optional: a card label mapped to the tier it selects for this role, and the milliseconds the
112
+ // role's dispatch may run. A role declaring neither runs at its `tier` for the default time
113
+ // (`ARCHITECTURE.md`, below the extension-point table).
114
+ labels: {},
115
+ timeout: {},
116
+ // Optional: a card label mapped to the provider it selects for this role. A role declaring
117
+ // none runs on its `provider` (`ARCHITECTURE.md`, below the extension-point table).
118
+ providerLabels: {},
119
+ },
120
+ // A judge entry that is not a plain role name: the role, and its provider rule, a provider name
121
+ // or a map from the maker's provider to the judge's (`D24` rule 1). The owner is never one.
122
+ judge: {
123
+ role: { required: true },
124
+ provider: { required: true },
125
+ },
126
+ kind: {
127
+ select: { required: true, keys: 'select' },
128
+ maker: { required: true },
129
+ judges: { required: true },
130
+ // `rounds` omitted is the default of three (`R-LOOP-9`), and a kind needing no provisioning
131
+ // names none: an absent block is a tested state rather than a degraded one (`R-OPTION-1`).
132
+ rounds: {},
133
+ provisioning: {},
134
+ },
135
+ select: {
136
+ labels: { required: true },
137
+ },
138
+ step: {
139
+ run: { required: true },
140
+ // A step that declares nothing is optional (`R-PROV-1`), so `required` is the declaration and
141
+ // never the state, and a step that selects no labels is selected by the kinds that name it.
142
+ // The declaration is a boolean because it answers whether: anything else is read by `required
143
+ // === true` as a step that declared nothing, which is not what its author wrote.
144
+ required: { type: 'boolean' },
145
+ select: { keys: 'select' },
146
+ cwd: {},
147
+ timeout: {},
148
+ },
149
+ worktrees: {
150
+ root: {},
151
+ topic: {},
152
+ },
153
+ telemetry: {
154
+ push: {},
155
+ },
156
+ };
157
+
158
+ /**
159
+ * The owner, who judges a kind of work last if at all and is never dispatched (`R-LOOP-11`). It
160
+ * is the one judge that is not a role, which is why a kind may name it and `roles` may not.
161
+ */
162
+ export const OWNER = 'owner';
163
+
164
+ /** The role a kind's judge entry names: the entry itself where it is a role name, or its `role`. */
165
+ export const judgeRole = (entry) => (typeof entry === 'string' ? entry : entry?.role);
166
+
167
+ /**
168
+ * The escalation categories, which are Rigger's and not the consumer's (`R-ESCALATE-19`). A
169
+ * consumer names where each of them goes and adds none (`R-ESCALATE-13`), so a config naming
170
+ * anything else names a category Rigger does not offer.
171
+ */
172
+ export const CATEGORIES = ['recorded-decision', 'critical', 'ambiguous'];
173
+
174
+ /** Labels Rigger fixes, shared by provisioning, selector validation and L2 (`D27`). */
175
+ export const FIXED_LABELS = { escalated: 'rigger:escalated', status: 'rigger:status' };
176
+
177
+ /**
178
+ * The tiers a role may run at, which are Rigger's and not the consumer's (`O4` on #467): each
179
+ * provider adapter maps every one of them to a model of its own, so a config naming another names
180
+ * a tier no adapter can run (`ARCHITECTURE.md`, below the extension-point table).
181
+ */
182
+ export const TIERS = ['standard', 'high'];
183
+
184
+ /** The tiers as a refusal names them. */
185
+ const TIER_NAMES = TIERS.map((tier) => `\`${tier}\``).join(' or ');
186
+
187
+ /** Where a key sits, written as a reader of a refusal would look for it in the file. */
188
+ const at = (path, key) => (path ? `${path}.${key}` : key);
189
+
190
+ /** What a refusal calls each type a rule can ask for, and whether a value is one. */
191
+ const SAYS = { boolean: 'true or false', array: 'a list', object: 'an object' };
192
+ const holds = (value, type) => {
193
+ if (type === 'array') return Array.isArray(value);
194
+ return type === 'object' ? declares(value) : typeof value === type;
195
+ };
196
+
197
+ /** Whether a value is a set of declarations, which is what a shape and a container both hold. */
198
+ const declares = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
199
+
200
+ /**
201
+ * A value as a refusal names it: as a template writes it, or as `inspect` does where a template
202
+ * cannot. A Symbol, a null-prototype object and an object whose `toString` throws have no string
203
+ * form, and a refusal that throws while writing one names nothing (`R-SCHED-10`).
204
+ */
205
+ function written(value) {
206
+ try { return `${value}`; }
207
+ catch { return inspect(value); }
208
+ }
209
+
210
+ /**
211
+ * Whether `roles` declares `name` as its own. A name with no string form converts to no key, so it
212
+ * names no role rather than throwing where it is read.
213
+ */
214
+ function declaresRole(roles, name) {
215
+ try { return Object.hasOwn(roles, name); }
216
+ catch { return false; }
217
+ }
218
+
219
+ /**
220
+ * What `roles` holds under `name`, read as a property is, through its prototype. A name with no
221
+ * string form converts to no key, so it reads as nothing rather than throwing.
222
+ */
223
+ function roleUnder(roles, name) {
224
+ try { return roles[name]; }
225
+ catch { return undefined; }
226
+ }
227
+
228
+ /**
229
+ * Whether a value is a name: a string holding something other than whitespace. A label and a
230
+ * priority option are each read by name, and a string of only whitespace names nothing either
231
+ * could match.
232
+ */
233
+ const names = (value) => typeof value === 'string' && value.trim() !== '';
234
+
235
+ /** A label name with every ASCII letter given one case: the spelling two names share when GitHub holds them as one label. */
236
+ const folded = (name) => name.replace(/[A-Z]/g, (letter) => letter.toLowerCase());
237
+
238
+ /**
239
+ * Whether two label names are one label, as GitHub holds them: equal once every ASCII letter in
240
+ * both is given one case (#301's measurement, which covers ASCII letters only). Anything that is
241
+ * no string is one label with nothing.
242
+ */
243
+ export const sameLabel = (one, other) => typeof one === 'string' && typeof other === 'string' && folded(one) === folded(other);
244
+
245
+ /** A fixed label cannot select a kind of work or mark a card as an epic (`R-SCHED-19`). */
246
+ function readSelectorLabel(label, where, refusals) {
247
+ if (Object.values(FIXED_LABELS).some((fixed) => sameLabel(label, fixed))) {
248
+ refusals.push(`\`${where}\` names \`${label}\`, a label Rigger fixes and reserves from kind and epic selectors`);
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Whether a list holds anything that is no name. Read through `Array.from`, which visits every
254
+ * index, because `some` skips an empty slot.
255
+ */
256
+ const holdsUnnamed = (list) => Array.from(list).some((entry) => !names(entry));
257
+
258
+ /**
259
+ * The refusal a value earns where Rigger reads declarations and finds something else.
260
+ *
261
+ * Refused where it sits rather than read as an empty set, which would report every key under it
262
+ * as missing and bury the one fault that caused them. A container earns this as a shape does: a
263
+ * string read as a set of names yields one refusal per character, each naming an index the
264
+ * author never wrote.
265
+ */
266
+ function holdsNothing(path, value) {
267
+ let shown;
268
+ try { shown = JSON.stringify(value) ?? String(value); }
269
+ catch { shown = inspect(value); }
270
+ return `\`${path || 'the config'}\` holds no declarations, and Rigger reads ${shown} as none`;
271
+ }
272
+
273
+ /** Reads one value's keys against its shape, and every shape nested under it. */
274
+ function readShape(value, shape, path, refusals) {
275
+ const rules = SHAPES[shape];
276
+ if (!declares(value)) {
277
+ refusals.push(holdsNothing(path, value));
278
+ return;
279
+ }
280
+ // Both questions below ask about an object's own keys, as `readKinds` does. A key an object
281
+ // inherits was written by nobody: read through the prototype chain, a required key is answered
282
+ // by `Object.prototype` rather than by the consumer, and every name that object carries —
283
+ // `toString`, `constructor`, `valueOf`, and a `__proto__` a JSON parse made own — reads as a
284
+ // declaration Rigger offers.
285
+ for (const [key, rule] of Object.entries(rules)) {
286
+ if (!Object.hasOwn(value, key)) {
287
+ if (rule.required) {
288
+ refusals.push(`\`${at(path, key)}\` is required, and the config does not name it`);
289
+ }
290
+ continue;
291
+ }
292
+ if (rule.type && !holds(value[key], rule.type)) {
293
+ refusals.push(`\`${at(path, key)}\` must be ${SAYS[rule.type]}`);
294
+ }
295
+ // `undefined` on both sides is a rule that names no placeholder against a key declared with
296
+ // no value, which is a refusal the shape earns elsewhere and not this one.
297
+ if (rule.placeholder !== undefined && value[key] === rule.placeholder) {
298
+ refusals.push(
299
+ `\`${at(path, key)}\` still holds \`${rule.placeholder}\`, which is the name the starter `
300
+ + 'config carries where only you can answer it',
301
+ );
302
+ }
303
+ if (rule.keys) readShape(value[key], rule.keys, at(path, key), refusals);
304
+ if (rule.entries) {
305
+ if (!declares(value[key])) refusals.push(holdsNothing(at(path, key), value[key]));
306
+ else {
307
+ for (const [name, entry] of Object.entries(value[key])) {
308
+ readShape(entry, rule.entries, at(at(path, key), name), refusals);
309
+ }
310
+ }
311
+ }
312
+ }
313
+ for (const key of Object.keys(value)) {
314
+ if (!Object.hasOwn(rules, key)) refusals.push(`\`${at(path, key)}\` is not a declaration Rigger offers`);
315
+ }
316
+ }
317
+
318
+ /**
319
+ * What a `select.labels` may be, for a kind or a provisioning step alike: the labels a card
320
+ * carries to be selected by it, as a list the engine reads when it selects cards.
321
+ *
322
+ * A select that is no set of declarations, or that does not name `labels`, earned its refusal
323
+ * where the shape was read.
324
+ */
325
+ function readLabels(select, where, refusals) {
326
+ if (!declares(select) || !Object.hasOwn(select, 'labels')) return;
327
+ const { labels } = select;
328
+ // The engine reads the value as a list, so anything else reaches it as a throw.
329
+ if (!Array.isArray(labels)) {
330
+ refusals.push(`\`${where}\` must be a list of label names`);
331
+ return;
332
+ }
333
+ // A selector takes a card carrying any one of its labels, so one naming none selects nothing:
334
+ // a kind's work would never run, and a step would never provision a card.
335
+ if (labels.length === 0) {
336
+ refusals.push(`\`${where}\` names no label, so it selects no card`);
337
+ }
338
+ // A card carries labels by name, so an entry that is no name selects nothing.
339
+ if (holdsUnnamed(labels)) {
340
+ refusals.push(`\`${where}\` must list label names, and holds something else`);
341
+ }
342
+ }
343
+
344
+ /**
345
+ * What each kind of work may name: one maker role, and the judges that review it.
346
+ *
347
+ * The names a kind uses are the ones the config declares under `roles`, so a kind naming anything
348
+ * else names something Rigger cannot dispatch (`R-SCHED-10`).
349
+ */
350
+ function readKinds(config, refusals) {
351
+ // Neither rule below can be read where the declarations they read are missing: a maker cannot
352
+ // be held to the roles when there are none, and every kind would be refused for a fault that
353
+ // is one refusal already, naming the container that holds nothing.
354
+ if (!declares(config.roles) || !declares(config.kinds)) return;
355
+ const roles = config.roles;
356
+ if (Object.hasOwn(roles, OWNER)) {
357
+ refusals.push(`\`roles.${OWNER}\` is reserved: \`${OWNER}\` names the owner, who is never dispatched`);
358
+ }
359
+ for (const [name, kind] of Object.entries(config.kinds)) {
360
+ const where = `kinds.${name}`;
361
+ let coercedMaker = false;
362
+ if (kind?.maker !== undefined) {
363
+ const maker = kind.maker;
364
+ // The property key's type distinguishes string-role coercion from Symbol lookup.
365
+ let makerKey;
366
+ try {
367
+ const keyed = { [maker]: null };
368
+ makerKey = Object.keys(keyed)[0] ?? Object.getOwnPropertySymbols(keyed)[0];
369
+ }
370
+ catch {}
371
+ const declaredMaker = makerKey !== undefined && declaresRole(roles, makerKey);
372
+ coercedMaker = declaredMaker && typeof maker !== 'string' && typeof makerKey === 'string';
373
+ if (!declaredMaker || coercedMaker) {
374
+ refusals.push(`\`${where}.maker\` names \`${coercedMaker ? inspect(kind.maker) : written(kind.maker)}\`, which is no role the config declares`);
375
+ }
376
+ }
377
+ readLabels(kind?.select, `${where}.select.labels`, refusals);
378
+ if (Array.isArray(kind?.select?.labels)) {
379
+ for (const label of kind.select.labels) readSelectorLabel(label, `${where}.select.labels`, refusals);
380
+ }
381
+ if (kind?.judges === undefined) continue;
382
+ if (!Array.isArray(kind.judges) || kind.judges.length === 0) {
383
+ // One maker and at least one judge, in the order they judge in (`README.md`, "The
384
+ // guarantee"). A kind naming none would deliver work no judge ruled on.
385
+ refusals.push(`\`${where}.judges\` must be an ordered list naming at least one judge`);
386
+ continue;
387
+ }
388
+ // Every check below reads an entry's role through `judgeRole`, whichever form names it (`D24`
389
+ // rule 1). An entry that is neither form earns its one refusal here, and names no role to check.
390
+ const formed = kind.judges.map((entry, position) => readJudgeEntry(entry, `${where}.judges[${position}]`, coercedMaker ? [] : makerProviders(roleUnder(roles, kind.maker)), refusals));
391
+ const named = kind.judges.map((entry, position) => (formed[position] ? judgeRole(entry) : undefined));
392
+ // No judge is the maker, and no configuration removes that separation (`R-LOOP-3`). An engine
393
+ // that could approve its own work would be a faster way to merge mistakes.
394
+ if (!coercedMaker && named.includes(kind.maker)) {
395
+ refusals.push(`\`${where}\` names \`${written(kind.maker)}\` as both its maker and one of its judges, and no judge is the maker`);
396
+ }
397
+ // Each judge of a card runs in a directory named for its role, so a judge named twice would
398
+ // be two dispatches sharing one directory (ruling 1 Q3 on #467; `R-WORK-12`). The owner is
399
+ // never dispatched, and named twice is named before last, which the owner's own rule below
400
+ // refuses.
401
+ const repeated = named.filter((judge, position) => formed[position] && judge !== undefined && judge !== OWNER && named.indexOf(judge) !== position);
402
+ for (const judge of new Set(repeated)) {
403
+ refusals.push(`\`${where}.judges\` names \`${written(judge)}\` more than once, and each judge of a card has a directory of its own`);
404
+ }
405
+ named.forEach((judge, position) => {
406
+ if (!formed[position]) return;
407
+ if (judge !== OWNER && !(typeof judge === 'string' && Object.hasOwn(roles, judge))) {
408
+ refusals.push(`\`${where}.judges\` names \`${written(judge)}\`, which is no role the config declares`);
409
+ }
410
+ // The owner judges last if at all (`R-LOOP-11`), so the owner is asked once every agent
411
+ // judge is satisfied rather than before one of them.
412
+ if (judge === OWNER && position !== kind.judges.length - 1) {
413
+ refusals.push(`\`${where}.judges\` names \`${OWNER}\` at position ${position + 1} of ${kind.judges.length}, and the owner judges last`);
414
+ }
415
+ });
416
+ }
417
+ }
418
+
419
+ /**
420
+ * The providers the maker declared as `maker` can run on: its `provider`, and each provider its
421
+ * `providerLabels` selects, where each is a name. A maker that declares neither runs on none the
422
+ * validator can read, and earned its refusal where its role was read.
423
+ */
424
+ function makerProviders(maker) {
425
+ if (!declares(maker)) return [];
426
+ const labelled = mapsNamesToStrings(maker.providerLabels) ? Object.values(maker.providerLabels) : [];
427
+ return [...new Set([maker.provider, ...labelled].filter(names))];
428
+ }
429
+
430
+ /** Whether `provider` names a provider Rigger has an adapter for, read from L0's adapter map. */
431
+ const adapted = (provider) => typeof provider === 'string' && Object.hasOwn(ADAPTERS, provider);
432
+
433
+ /**
434
+ * Whether a value is a plain object whose every enumerable own key is a name: what `mapsNamesToStrings`
435
+ * asks of an object besides the type of its values.
436
+ */
437
+ function plainWithNamedKeys(value) {
438
+ if (!declares(value) || Object.getPrototypeOf(value) !== Object.prototype) return false;
439
+ if (Object.getOwnPropertySymbols(value).some((symbol) => Object.getOwnPropertyDescriptor(value, symbol).enumerable)) return false;
440
+ return Object.keys(value).every(names);
441
+ }
442
+
443
+ /**
444
+ * What one entry of a kind's `judges` may be, sitting at `where`: a role name, or an object holding
445
+ * exactly `role` and `provider`, whose `provider` is a provider Rigger has an adapter for, or a map
446
+ * from each provider in `providers`, those the kind's maker can run on, to one (`D24` rule 1). The
447
+ * entry object and a map are read as a role's `labels` is (`mapsNamesToStrings`). The owner has no
448
+ * provider, so it is written only as a plain name. It answers whether the entry is either form, so
449
+ * the kind's checks read a role from it through `judgeRole`.
450
+ */
451
+ function readJudgeEntry(entry, where, providers, refusals) {
452
+ if (typeof entry === 'string') return true;
453
+ if (!declares(entry)) {
454
+ refusals.push(`\`${where}\` is neither a role name nor \`{ role, provider }\`, and the config gives ${inspect(entry)}`);
455
+ return false;
456
+ }
457
+ readShape(entry, 'judge', where, refusals);
458
+ if (judgeRole(entry) === OWNER) {
459
+ refusals.push(`\`${where}\` names \`${OWNER}\` as \`{ role, provider }\`, and the owner, who has no provider, is written only as a plain name`);
460
+ }
461
+ const { provider } = entry;
462
+ if (typeof provider === 'string') {
463
+ if (!mapsNamesToStrings(entry)) refusals.push(`\`${where}\` must be a plain object of names to strings, and the config gives ${inspect(entry)}`);
464
+ else if (!adapted(provider)) refusals.push(`\`${where}.provider\` names ${inspect(provider)}, which is no provider Rigger has an adapter for`);
465
+ } else if (Object.hasOwn(entry, 'provider')) {
466
+ if (!plainWithNamedKeys(entry)) refusals.push(`\`${where}\` must be a plain object of names, and the config gives ${inspect(entry)}`);
467
+ if (!mapsNamesToStrings(provider)) {
468
+ refusals.push(`\`${where}.provider\` must be a provider name or map provider names to provider names, and the config gives ${inspect(provider)}`);
469
+ } else {
470
+ for (const [from, to] of Object.entries(provider)) {
471
+ for (const named of [from, to]) {
472
+ if (!adapted(named)) refusals.push(`\`${where}.provider\` maps ${inspect(named)}, which is no provider Rigger has an adapter for`);
473
+ }
474
+ }
475
+ for (const held of providers) {
476
+ if (!Object.hasOwn(provider, held)) refusals.push(`\`${where}.provider\` holds no key for \`${held}\`, a provider the kind's maker can run on`);
477
+ }
478
+ }
479
+ }
480
+ return true;
481
+ }
482
+
483
+ /**
484
+ * What a declared `timeout` may be, on a step or a role alike: the milliseconds L1 lets it run,
485
+ * which is a count. One that declares none runs for its default, so only a declared one has
486
+ * anything to read.
487
+ */
488
+ function readTimeout(holder, where, refusals) {
489
+ if (Object.hasOwn(holder, 'timeout') && !(Number.isInteger(holder.timeout) && holder.timeout >= 1)) {
490
+ refusals.push(`\`${where}.timeout\` must be a positive whole number of milliseconds, and the config gives ${inspect(holder.timeout)}`);
491
+ }
492
+ }
493
+
494
+ /**
495
+ * Whether a value is a plain object whose every key is a name holding a string: what an object
496
+ * literal mapping names to strings is. A Map, a class instance, an inherited or computed entry,
497
+ * and an enumerable symbol key each hold something `Object.entries` would never show, so the
498
+ * validator would pass what the engine later reads differently.
499
+ *
500
+ * A key of `labels` is an enumerable own key, as for every declaration the validator reads. A key
501
+ * the config defines as non-enumerable is no declaration: the validator neither reads nor refuses
502
+ * it, as for every other declaration in the config (ruling 16 on #467).
503
+ */
504
+ function mapsNamesToStrings(value) {
505
+ if (!declares(value) || Object.getPrototypeOf(value) !== Object.prototype) return false;
506
+ if (Object.getOwnPropertySymbols(value).some((symbol) => Object.getOwnPropertyDescriptor(value, symbol).enumerable)) return false;
507
+ return Object.keys(value).every((key) => names(key) && typeof Object.getOwnPropertyDescriptor(value, key).value === 'string');
508
+ }
509
+
510
+ /**
511
+ * What a role's `labels` may be: card labels, each mapped to the tier it selects for the role.
512
+ * A card carries labels by name, and the tier a label selects is one of the tiers Rigger fixes.
513
+ */
514
+ function readTierLabels(labels, where, refusals) {
515
+ if (!mapsNamesToStrings(labels)) {
516
+ refusals.push(`\`${where}\` must map label names to tiers, and the config gives ${inspect(labels)}`);
517
+ return;
518
+ }
519
+ for (const [label, tier] of Object.entries(labels)) {
520
+ if (!TIERS.includes(tier)) {
521
+ refusals.push(`\`${where}\` maps \`${label}\` to ${inspect(tier)}, and a label selects ${TIER_NAMES}`);
522
+ }
523
+ }
524
+ }
525
+
526
+ /**
527
+ * What a role's `providerLabels` may be: card labels, each mapped to the provider it selects for
528
+ * the role, read as `labels` is, and each a provider Rigger has an adapter for.
529
+ */
530
+ function readProviderLabels(labels, where, refusals) {
531
+ if (!mapsNamesToStrings(labels)) {
532
+ refusals.push(`\`${where}\` must map label names to providers, and the config gives ${inspect(labels)}`);
533
+ return;
534
+ }
535
+ for (const [label, provider] of Object.entries(labels)) {
536
+ if (!adapted(provider)) refusals.push(`\`${where}\` maps \`${label}\` to ${inspect(provider)}, which is no provider Rigger has an adapter for`);
537
+ }
538
+ }
539
+
540
+ /**
541
+ * What each role may be: a name holding no `/`, and declarations naming a provider Rigger has an
542
+ * adapter for, the tier it runs at, the labels that select another, and the time it may run.
543
+ */
544
+ function readRoles(roles, refusals) {
545
+ // Roles that are no set of declarations earned their refusal where the shape was read.
546
+ if (!declares(roles)) return;
547
+ for (const [name, role] of Object.entries(roles)) {
548
+ const where = `roles.${name}`;
549
+ // A judge's directory is named for its role, directly under the card's topic, so a `/` would
550
+ // place it somewhere else (ruling 1 Q3 on #467).
551
+ if (name.includes('/')) {
552
+ refusals.push(`\`${where}\` holds a \`/\` in its name, and a role's name names a directory`);
553
+ }
554
+ // Each of these resolves to the directory above the role's own, or to that directory itself,
555
+ // so making it fresh would remove what every other card holds there (#558; ruling 19 on #555).
556
+ if (['..', '.', ''].includes(name)) {
557
+ refusals.push(`\`${where}\` names no directory of its own beside the others, and a role's name names a directory`);
558
+ }
559
+ // A role that is no set of declarations, or names no tier, earned its refusal where the shape
560
+ // was read.
561
+ if (!declares(role)) continue;
562
+ if (Object.hasOwn(role, 'tier') && !TIERS.includes(role.tier)) {
563
+ refusals.push(`\`${where}.tier\` must be ${TIER_NAMES}, and the config gives ${inspect(role.tier)}`);
564
+ }
565
+ // L0's adapter map holds every provider a role can name, so the names are its and never
566
+ // typed here (ruling 3 P11 on #467; `D8`).
567
+ if (Object.hasOwn(role, 'provider') && !adapted(role.provider)) {
568
+ refusals.push(`\`${where}.provider\` names ${inspect(role.provider)}, which is no provider Rigger has an adapter for`);
569
+ }
570
+ if (Object.hasOwn(role, 'labels')) readTierLabels(role.labels, `${where}.labels`, refusals);
571
+ if (Object.hasOwn(role, 'providerLabels')) readProviderLabels(role.providerLabels, `${where}.providerLabels`, refusals);
572
+ readTimeout(role, where, refusals);
573
+ }
574
+ }
575
+
576
+ /**
577
+ * What the epic label may be: one label name, which a card carries to mark it an epic. A config
578
+ * that declares none marks no card, so only a declared one has anything to read.
579
+ *
580
+ * No kind may select it. Every card that kind selected would be an epic, which Rigger never pulls
581
+ * (`R-SCHED-11`), so the kind's work would never run and nobody would be told why.
582
+ */
583
+ function readEpicLabel(config, refusals) {
584
+ if (!Object.hasOwn(config, 'epicLabel')) return;
585
+ const { epicLabel } = config;
586
+ if (!names(epicLabel)) {
587
+ refusals.push('`epicLabel` must be one label name');
588
+ return;
589
+ }
590
+ readSelectorLabel(epicLabel, 'epicLabel', refusals);
591
+ // Kinds that are no set of declarations, and labels that are no list, earned their refusal
592
+ // where they were read.
593
+ if (!declares(config.kinds)) return;
594
+ for (const [name, kind] of Object.entries(config.kinds)) {
595
+ const labels = kind?.select?.labels;
596
+ if (Array.isArray(labels) && labels.some((label) => sameLabel(label, epicLabel))) {
597
+ refusals.push(`\`epicLabel\` names \`${epicLabel}\`, which \`kinds.${name}\` selects, so no card that kind selects would ever be pulled`);
598
+ }
599
+ }
600
+ }
601
+
602
+ /**
603
+ * What a declared board owner may be: the login of one GitHub user or organisation, which is a
604
+ * name. A config that declares none works the repository's owner's board, so only a declared one
605
+ * has anything to read.
606
+ */
607
+ function readBoardOwner(board, refusals) {
608
+ // A board that is no set of declarations earned its refusal where the shape was read.
609
+ if (!declares(board) || !Object.hasOwn(board, 'owner')) return;
610
+ if (!names(board.owner)) refusals.push('`board.owner` must be one login: a string holding something other than whitespace');
611
+ }
612
+
613
+ /**
614
+ * What a declared concurrency may be: N, the number of cards L3 works at once, which is a
615
+ * positive whole number. A config that declares none runs at the default, so only a declared one
616
+ * has anything to read.
617
+ */
618
+ function readConcurrency(config, refusals) {
619
+ if (!Object.hasOwn(config, 'concurrency')) return;
620
+ const { concurrency } = config;
621
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
622
+ // Named as a module would write it: JSON spells NaN and Infinity as null, and throws on a BigInt.
623
+ refusals.push(`\`concurrency\` must be a positive whole number, and the config gives ${inspect(concurrency)}`);
624
+ }
625
+ }
626
+
627
+ /**
628
+ * What a step's working directory may be: a relative path naming a directory under the card's
629
+ * workspace, which L1 runs the step in.
630
+ */
631
+ function readStepDirectory(cwd, where, refusals) {
632
+ if (typeof cwd !== 'string') {
633
+ refusals.push(`\`${where}.cwd\` must be a path under the card's workspace, and the config gives ${inspect(cwd)}`);
634
+ return;
635
+ }
636
+ // An absolute path names its directory wherever the workspace is.
637
+ if (posix.isAbsolute(cwd)) {
638
+ refusals.push(`\`${where}\` runs in \`${cwd}\`, which is absolute, and a step runs under the card's workspace`);
639
+ return;
640
+ }
641
+ // Normalised, a relative path that climbs out of the workspace begins with a `..` segment.
642
+ const normal = posix.normalize(cwd);
643
+ if (normal === '..' || normal.startsWith('../')) {
644
+ refusals.push(`\`${where}\` runs in \`${cwd}\`, which resolves outside the card's workspace`);
645
+ }
646
+ }
647
+
648
+ /**
649
+ * What each provisioning step may declare: a command line, and a selector. A step that selects
650
+ * nothing is selected by the kinds that name it, so only a step that declares `select` has labels
651
+ * to read.
652
+ */
653
+ function readProvisioning(provisioning, refusals) {
654
+ // Provisioning that is no set of declarations earned its refusal where the shape was read.
655
+ if (!declares(provisioning)) return;
656
+ for (const [name, step] of Object.entries(provisioning)) {
657
+ const where = `provisioning.${name}`;
658
+ readLabels(step?.select, `${where}.select.labels`, refusals);
659
+ // A step that is no set of declarations, or names no `run`, earned its refusal where the shape
660
+ // was read. L1 runs the command line under `/bin/sh`, so anything else is nothing it can run.
661
+ if (!declares(step)) continue;
662
+ if (Object.hasOwn(step, 'run') && !names(step.run)) {
663
+ refusals.push(`\`${where}.run\` must be a command line: a string holding something other than whitespace`);
664
+ }
665
+ if (Object.hasOwn(step, 'cwd')) readStepDirectory(step.cwd, where, refusals);
666
+ readTimeout(step, where, refusals);
667
+ }
668
+ }
669
+
670
+ /**
671
+ * What a kind's provisioning may be: the steps it runs, as a list of step names. A kind that
672
+ * declares none runs no step, so only a declared list has anything to read.
673
+ */
674
+ function readKindSteps(config, refusals) {
675
+ // Kinds or provisioning that are no set of declarations earned their refusal where the shape
676
+ // was read. A config that declares no provisioning declares no step.
677
+ const declared = Object.hasOwn(config, 'provisioning') ? config.provisioning : {};
678
+ if (!declares(config.kinds) || !declares(declared)) return;
679
+ for (const [name, kind] of Object.entries(config.kinds)) {
680
+ if (!declares(kind) || !Object.hasOwn(kind, 'provisioning')) continue;
681
+ const steps = kind.provisioning;
682
+ // L2 reads the steps as a list of names, so a string would be read a character at a time.
683
+ if (!Array.isArray(steps) || holdsUnnamed(steps)) {
684
+ refusals.push(`\`kinds.${name}.provisioning\` must be a list of step names`);
685
+ continue;
686
+ }
687
+ // A step the config does not declare is one Rigger has no command for (`R-SCHED-10`).
688
+ for (const step of steps) {
689
+ if (!Object.hasOwn(declared, step)) {
690
+ refusals.push(`\`kinds.${name}\` names the step \`${step}\`, which \`provisioning\` does not declare`);
691
+ }
692
+ }
693
+ }
694
+ }
695
+
696
+ /**
697
+ * What a declared worktree root and topic may be: a path naming a directory, and the rule naming
698
+ * each card's workspace directly under it. A key the config does not declare takes the default the
699
+ * Engine settings row states, so only a declared one has anything to read.
700
+ */
701
+ function readWorktrees(worktrees, refusals) {
702
+ // A declaration that is no set of declarations earned its refusal where the shape was read.
703
+ if (!declares(worktrees)) return;
704
+ if (Object.hasOwn(worktrees, 'root') && !names(worktrees.root)) {
705
+ refusals.push('`worktrees.root` must be a path: a string holding something other than whitespace');
706
+ }
707
+ if (!Object.hasOwn(worktrees, 'topic')) return;
708
+ const { topic } = worktrees;
709
+ if (typeof topic !== 'string') {
710
+ refusals.push(`\`worktrees.topic\` must be a string, and the config gives ${inspect(topic)}`);
711
+ return;
712
+ }
713
+ // The issue number is what tells one card's workspace and branch from another's, so a topic
714
+ // that never places it names one of each for every card (`R-WORK-9`).
715
+ if (!topic.includes('{number}')) {
716
+ refusals.push(`\`worktrees.topic\` is \`${topic}\`, which holds no \`{number}\`, so it names one workspace for every card`);
717
+ }
718
+ // A workspace sits directly under the root. A `/` would make directories there that Rigger never
719
+ // removes, and a branch git refuses wherever one holds the part before it. An absolute topic and
720
+ // a `..` segment are each such a topic (ruling 3, P2).
721
+ if (topic.includes('/')) {
722
+ refusals.push(`\`worktrees.topic\` is \`${topic}\`, which holds a \`/\`, and a workspace sits directly under the root`);
723
+ }
724
+ }
725
+
726
+ /**
727
+ * What a priority declaration's options may be: the ranking itself, highest first, so it names at
728
+ * least one option.
729
+ */
730
+ function readPriority(priority, refusals) {
731
+ const options = priority?.options;
732
+ // A declaration that is no set of declarations, or options that are no list, earned their
733
+ // refusal where the shape was read.
734
+ if (!Array.isArray(options)) return;
735
+ const where = 'board.priority.options';
736
+ if (options.length === 0) refusals.push(`\`${where}\` must list at least one option, highest rank first`);
737
+ // An option is named by its display name on the board, so anything else names no option.
738
+ if (holdsUnnamed(options)) {
739
+ refusals.push(`\`${where}\` must list option display names, and holds something else`);
740
+ }
741
+ // An option listed twice holds two ranks, and a card holding it has no one rank to take.
742
+ const seen = new Set();
743
+ const repeated = new Set();
744
+ for (const option of options) (seen.has(option) ? repeated : seen).add(option);
745
+ for (const option of repeated) {
746
+ refusals.push(`\`${where}\` names \`${option}\` more than once, so it holds no one rank`);
747
+ }
748
+ }
749
+
750
+ /**
751
+ * What `escalate` may hold: a destination for any of the fixed categories, and nothing else
752
+ * (`R-ESCALATE-13`). A config that declares none sends every category to the owner, so only a
753
+ * declared one has anything to read, and one that is no object earned its refusal where the shape
754
+ * was read.
755
+ */
756
+ function readEscalate(config, refusals) {
757
+ if (!Object.hasOwn(config, 'escalate') || !declares(config.escalate)) return;
758
+ for (const [key, destination] of Object.entries(config.escalate)) {
759
+ const where = `escalate.${key}`;
760
+ if (!CATEGORIES.includes(key)) refusals.push(`\`${where}\` is no escalation category Rigger offers`);
761
+ else if (typeof destination !== 'string') {
762
+ refusals.push(`\`${where}\` must name a destination, \`${OWNER}\` or a role, and the config gives ${inspect(destination)}`);
763
+ } else if (destination !== OWNER && declares(config.roles) && !Object.hasOwn(config.roles, destination)) {
764
+ refusals.push(`\`${where}\` names \`${destination}\`, which is neither \`${OWNER}\` nor a role the config declares`);
765
+ } else if (destination !== OWNER && key !== 'ambiguous') {
766
+ // Only an ambiguity may go to a role. Critical and a recorded-decision change are the
767
+ // owner's alone (`R-ESCALATE-14`).
768
+ refusals.push(`\`${where}\` names the role \`${destination}\`, and ${key} goes only to the owner`);
769
+ }
770
+ }
771
+ }
772
+
773
+ /** A duration in milliseconds is a positive whole number, as readTimeout requires. */
774
+ const milliseconds = (value) => Number.isInteger(value) && value >= 1;
775
+
776
+ /** The shared refusal text for an invalid millisecond duration, preserving readTimeout's rendering. */
777
+ const durationReason = (value) => `must be a positive whole number of milliseconds, and the config gives ${inspect(value)}`;
778
+
779
+ /** The anchor's epoch milliseconds, or NaN for a spelling outside the accepted anchor grammar. */
780
+ function anchorInstant(value) {
781
+ if (typeof value !== 'string') return NaN;
782
+ const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?(Z|([+-])(\d{2}):(\d{2}))$/.exec(value);
783
+ if (!match || match[0] !== value || match[8] === '-00:00') return NaN;
784
+ const [, year, month, day, hour, minute, second = '0', fraction = '0', , sign, offsetHour = '0', offsetMinute = '0'] = match;
785
+ if (+hour > 23 || +minute > 59 || +second > 59 || +offsetHour > 23 || +offsetMinute > 59) return NaN;
786
+ const local = new Date(0);
787
+ // setUTCFullYear preserves years 0000 to 0099, which Date.UTC maps to 1900 to 1999.
788
+ local.setUTCFullYear(+year, +month - 1, +day);
789
+ local.setUTCHours(+hour, +minute, +second, +fraction.padEnd(3, '0'));
790
+ if (local.getUTCFullYear() !== +year || local.getUTCMonth() !== +month - 1 || local.getUTCDate() !== +day) return NaN;
791
+ const offset = (+offsetHour * 60 + +offsetMinute) * 60000 * (sign === '-' ? -1 : 1);
792
+ return local.getTime() - offset;
793
+ }
794
+
795
+ /**
796
+ * Parse a trigger's schedule and answer its adjacent occurrences at instant (epoch milliseconds).
797
+ * Invalid schedules answer refusals as { key, reason }. Valid ones also answer newest and next,
798
+ * each in epoch milliseconds or null where no occurrence exists in ECMA-262's time range.
799
+ */
800
+ export function schedule(trigger, instant) {
801
+ const refusals = [];
802
+ if (!milliseconds(trigger.interval)) refusals.push({ key: 'interval', reason: durationReason(trigger.interval) });
803
+ const anchor = anchorInstant(trigger.anchor);
804
+ if (!Number.isFinite(anchor)) refusals.push({ key: 'anchor', reason: `must name an existing date and time carrying its offset, and the config gives ${inspect(trigger.anchor)}` });
805
+ if (refusals.length) return { refusals };
806
+ if (instant < anchor) return { refusals, newest: null, next: anchor };
807
+ const newest = anchor + Math.floor((instant - anchor) / trigger.interval) * trigger.interval;
808
+ const following = newest + trigger.interval;
809
+ return { refusals, newest, next: following <= 8640000000000000 ? following : null };
810
+ }
811
+
812
+ /** A plain label list, read once by length and index as carried to the forge, without consumer traversal methods. */
813
+ const plainLabels = (labels) => Array.isArray(labels) ? Array.from({ length: labels.length }, (_, index) => labels[index]) : labels;
814
+
815
+ /** Card-template values; readShape already refused a malformed card and its missing keys. */
816
+ function readClockCard(config, card, where, refusals, canSelect) {
817
+ if (!declares(card)) return;
818
+ const labels = Object.hasOwn(card, 'labels') ? plainLabels(card.labels) : undefined;
819
+ if (Object.hasOwn(card, 'title') && !names(card.title)) {
820
+ refusals.push(`\`${where}.title\` must be a string holding something other than whitespace`);
821
+ }
822
+ if (Object.hasOwn(card, 'labels') && (!Array.isArray(labels) || holdsUnnamed(labels))) refusals.push(`\`${where}.labels\` must be a list of label names`);
823
+ if (Object.hasOwn(card, 'acceptance') && typeof card.acceptance !== 'string') {
824
+ refusals.push(`\`${where}.acceptance\` must be one string`);
825
+ }
826
+ if (names(card.title) && typeof card.acceptance === 'string') {
827
+ const form = checkAcceptanceForm({ title: card.title, body: `## Acceptance\n\n${card.acceptance}` });
828
+ if (!form.admitted) refusals.push(`\`${where}.acceptance\` ${form.reason}`);
829
+ }
830
+ if (Object.hasOwn(card, 'author') && (typeof card.author !== 'string'
831
+ || (card.author !== OWNER && declares(config.roles) && !Object.hasOwn(config.roles, card.author)))) {
832
+ refusals.push(`\`${where}.author\` must name \`owner\` or a declared role, and the config gives ${inspect(card.author)}`);
833
+ }
834
+ if (Array.isArray(labels)) {
835
+ if (canSelect) {
836
+ const { answer } = kindOf({ labels }, config.kinds, config.epicLabel);
837
+ if (answer) refusals.push(`\`${where}.labels\` ${inspect(labels)}: ${answer.reason ?? 'selects no kind'}`);
838
+ }
839
+ if (labels.some((label) => sameLabel(label, FIXED_LABELS.status))) {
840
+ refusals.push(`\`${where}.labels\` ${inspect(labels)} holds the status label`);
841
+ }
842
+ }
843
+ }
844
+
845
+ /** The values of a clock's declarations; readShape already refused malformed containers. */
846
+ function readClock(config, refusals) {
847
+ if (!Object.hasOwn(config, 'clock') || !declares(config.clock)) return;
848
+ // kindOf assumes valid selectors. An already-refused kind or epic label earns no second
849
+ // refusal from its template's selection (card 1077, C33 and C34).
850
+ const canSelect = !refusals.some((reason) => ['`kinds`', '`kinds.', '`epicLabel`'].some((path) => reason.startsWith(path)));
851
+ if (Object.hasOwn(config.clock, 'poll') && !milliseconds(config.clock.poll)) {
852
+ refusals.push(`\`clock.poll\` ${durationReason(config.clock.poll)}`);
853
+ }
854
+ if (!Object.hasOwn(config.clock, 'triggers') || !declares(config.clock.triggers)) return;
855
+ for (const [name, trigger] of Object.entries(config.clock.triggers)) {
856
+ if (!declares(trigger)) continue;
857
+ const where = `clock.triggers.${name}`;
858
+ for (const { key, reason } of schedule(trigger, 0).refusals) {
859
+ if (Object.hasOwn(trigger, key)) refusals.push(`\`${where}.${key}\` ${reason}`);
860
+ }
861
+ if (Object.hasOwn(trigger, 'card') === Object.hasOwn(trigger, 'job')) {
862
+ refusals.push(`\`${where}\` must declare exactly one of \`card\` and \`job\``);
863
+ }
864
+ if (Object.hasOwn(trigger, 'catchUp') && !['fire', 'skip'].includes(trigger.catchUp)) {
865
+ refusals.push(`\`${where}.catchUp\` must name \`fire\` or \`skip\`, and the config gives ${inspect(trigger.catchUp)}`);
866
+ }
867
+ if (Object.hasOwn(trigger, 'job') && trigger.job !== 'report') {
868
+ refusals.push(`\`${where}.job\` must name \`report\`, and the config gives ${inspect(trigger.job)}`);
869
+ }
870
+ if (Object.hasOwn(trigger, 'card')) readClockCard(config, trigger.card, `${where}.card`, refusals, canSelect);
871
+ }
872
+ }
873
+
874
+ /** Every refusal this config earns. An accepted config earns none, so the list is empty. */
875
+ export function validate(config) {
876
+ const refusals = [];
877
+ readShape(config, 'config', '', refusals);
878
+ // A config file with no `export default` hands this `undefined`, and the rules below read
879
+ // declarations there are none of. The refusal for that is already the one above.
880
+ if (!declares(config)) return refusals;
881
+ readRoles(config.roles, refusals);
882
+ readKinds(config, refusals);
883
+ readKindSteps(config, refusals);
884
+ readEpicLabel(config, refusals);
885
+ readConcurrency(config, refusals);
886
+ readWorktrees(config.worktrees, refusals);
887
+ readProvisioning(config.provisioning, refusals);
888
+ readBoardOwner(config.board, refusals);
889
+ readPriority(config.board?.priority, refusals);
890
+ readEscalate(config, refusals);
891
+ readClock(config, refusals);
892
+ return refusals;
893
+ }
894
+
895
+ /**
896
+ * Whether the work requires this provisioning step.
897
+ *
898
+ * `R-PROV-1`: a step declares whether the work requires it, and a step that does not declare it
899
+ * is optional. D12 gives the reason the silence falls that way — the commoner step is a warmup
900
+ * that can fail and cost nothing, where a setup that fails dispatches a maker into a worktree
901
+ * that cannot run the work.
902
+ */
903
+ export const workRequires = (step) => step?.required === true;
904
+
905
+ /**
906
+ * The milliseconds a provisioning step may run: its own `timeout`, or 1,800,000, which is 30
907
+ * minutes, where it declares none (`ARCHITECTURE.md`, "Extension points").
908
+ */
909
+ export const stepTimeout = (step) => step.timeout ?? 1_800_000;
910
+
911
+ /**
912
+ * The milliseconds a role's dispatch may run: its own `timeout`, or 14,400,000, which is four
913
+ * hours, where it declares none (`O7` on #467; `ARCHITECTURE.md`, below the extension-point
914
+ * table). This is the one place the number is held (ruling 3 P10 on #467).
915
+ */
916
+ export const roleTimeout = (role) => role.timeout ?? 14_400_000;
917
+
918
+ /** N when the config declares none (the Engine settings extension point). */
919
+ export const DEFAULT_CONCURRENCY = 3;
920
+
921
+ /** The milliseconds launchd must allow for L0's exit cleanup, which has one fixed budget whatever the config. */
922
+ export const launchdExitTimeout = () => EXIT_BOUND;
923
+
924
+ /**
925
+ * The rule that derives a card's workspace and line of work from its number: the config's
926
+ * `worktrees.topic`, or `rigger-{number}` where it declares none (`ARCHITECTURE.md`, the Engine
927
+ * settings row). This is the one place the default is held, for L1's workspace and L2's line of work.
928
+ */
929
+ export const worktreeTopic = (config = {}) => config.worktrees?.topic ?? 'rigger-{number}';
930
+
931
+ /**
932
+ * Every label an accepted config's kinds and provisioning steps select, each once, kinds first and
933
+ * each in the order declared: the labels a card carries to be selected. The epic label is not
934
+ * among them, because no selector names it.
935
+ */
936
+ export function selectedLabels(config) {
937
+ const selectors = [...Object.values(config.kinds), ...Object.values(config.provisioning ?? {})];
938
+ return [...new Set(selectors.flatMap((selector) => selector.select?.labels ?? []))];
939
+ }
940
+
941
+ /**
942
+ * Every label an accepted config declares, each once: those its kinds and steps select, then its
943
+ * epic label where it declares one, then those its roles' `labels` map to a tier, then those
944
+ * its roles' `providerLabels` map to a provider, roles in the order declared, then the clock
945
+ * triggers' card-template labels, triggers in the order declared, then Rigger's fixed labels.
946
+ * These are the labels `setup-board` gives the repository. Two names GitHub holds
947
+ * as one label are one label here, spelled as the first of them declared.
948
+ */
949
+ export function declaredLabels(config) {
950
+ const declared = [
951
+ ...selectedLabels(config),
952
+ ...(Object.hasOwn(config, 'epicLabel') ? [config.epicLabel] : []),
953
+ ...Object.values(config.roles).flatMap((role) => Object.keys(role.labels ?? {})),
954
+ ...Object.values(config.roles).flatMap((role) => Object.keys(role.providerLabels ?? {})),
955
+ ...(Object.hasOwn(config, 'clock') && Object.hasOwn(config.clock, 'triggers')
956
+ ? Object.values(config.clock.triggers).flatMap((trigger) => Object.hasOwn(trigger, 'card') ? plainLabels(trigger.card.labels) : []) : []),
957
+ ...Object.values(FIXED_LABELS),
958
+ ];
959
+ return declared.filter((name, at) => !declared.slice(0, at).some((earlier) => sameLabel(earlier, name)));
960
+ }