@patronage/factory-ci 0.1.3 → 0.2.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.
@@ -0,0 +1,669 @@
1
+ /**
2
+ * The GitHub-side **proof-reuse gate** (ADR 0022, #309, #317).
3
+ *
4
+ * It answers one question, before checkout and before install: may this job
5
+ * reuse the local verification the factory already published for *this exact
6
+ * head*? Refusing runs hosted CI; it never fails the candidate. Three
7
+ * repositories had independently grown their own answer to that question and
8
+ * had already drifted apart, so the answer lives here once, with the App
9
+ * identity, check name, step identity, output names, guard, and every trust
10
+ * predicate fixed rather than consumer-configurable.
11
+ *
12
+ * What a consumer chooses is only *what its guarded surface requires*: the
13
+ * plain profile command objects that surface selects. Everything else below is
14
+ * the same for every consumer and every surface.
15
+ */
16
+
17
+ /**
18
+ * Escaped so the emitted text carries a GitHub Actions expression rather than
19
+ * this file carrying a JavaScript template hole.
20
+ */
21
+ const githubExpression = (expression: string): string =>
22
+ `\${{ ${expression} }}`;
23
+
24
+ /** A braced shell expansion that has to survive TypeScript interpolation. */
25
+ const shellExpansion = (expression: string): string => `\${${expression}}`;
26
+
27
+ /**
28
+ * Single-quote a value for the emitted script. Every non-literal value that
29
+ * reaches the script goes through this.
30
+ *
31
+ * `JSON.stringify` is *not* a shell quoter — it escapes `"` and `\` and leaves
32
+ * `'` untouched — so interpolating a JSON document straight into a
33
+ * single-quoted assignment lets one apostrophe in a profile command name close
34
+ * the assignment and run the remainder as shell. Names are charset-validated
35
+ * before they get here; this is the second layer, so the interpolation stays
36
+ * safe if that validation is ever loosened.
37
+ */
38
+ const shellSingleQuote = (value: string): string =>
39
+ `'${value.replaceAll("'", String.raw`'\''`)}'`;
40
+
41
+ /**
42
+ * The check run the gate reads. Fixed: `patronage-factory/pr-verify` is also
43
+ * postable as a *commit status* by any token with write access, so only the
44
+ * Checks API is ever consulted and only under this exact name.
45
+ */
46
+ export const FACTORY_PROOF_GATE_CHECK_NAME = "patronage-factory/pr-verify";
47
+
48
+ /**
49
+ * The Patronage Factory GitHub App, pinned (ADR 0022). A runner has no
50
+ * operator user config to read the producing App identity from, and the
51
+ * identity is a trust predicate, so it is not a repository variable a consumer
52
+ * can point somewhere else. Neither the id nor the slug is a secret: GitHub
53
+ * returns `app.id` on every check run it serves.
54
+ */
55
+ export const FACTORY_PROOF_GATE_APP_ID = "4314840";
56
+
57
+ /** Step id the guard condition refers to. */
58
+ export const FACTORY_PROOF_GATE_STEP_ID = "factory-proof";
59
+
60
+ /**
61
+ * The shell the gate runs under, and it is a correctness requirement rather
62
+ * than a preference.
63
+ *
64
+ * GitHub's default for a `run:` step is `bash -e {0}`: **errexit comes from
65
+ * the invocation, not from the script**, and no `set +e` in the script body
66
+ * can be relied on to express the intent because the abort happens at the
67
+ * failing command. The whole fail-open design below — every path reaching one
68
+ * final `GITHUB_OUTPUT` write, no `set -e` — is void under that default. It
69
+ * was: the gate died at a `read` returning 1 at EOF, two seconds in, with no
70
+ * stdout, no verdict and no `reason`, so reuse collapsed to never *and* the
71
+ * signal built to reveal that could not fire (#319).
72
+ *
73
+ * A `shell:` value containing `{0}` is used verbatim by the runner, so this
74
+ * form supplies no `-e`. The shorthand `shell: bash` must never be used here:
75
+ * it expands to `bash --noprofile --norc -eo pipefail {0}`. `pipefail` is set
76
+ * by the script itself.
77
+ *
78
+ * The script is additionally written to survive errexit anyway. Both layers
79
+ * are deliberate: this one states the contract, that one means a runner which
80
+ * ignores it still gets a decision.
81
+ */
82
+ export const FACTORY_PROOF_GATE_SHELL = "bash --noprofile --norc {0}";
83
+
84
+ /** Output the verdict is written to. `true` — and only `true` — skips. */
85
+ export const FACTORY_PROOF_GATE_OUTPUT = "reuse-proof";
86
+
87
+ /**
88
+ * Machine-readable refusal category. Fleet-status reads this: it is what makes
89
+ * a reuse-collapses-to-never regression visible instead of hidden behind
90
+ * fail-open, which by construction looks exactly like healthy full CI.
91
+ */
92
+ export const FACTORY_PROOF_GATE_REASON_OUTPUT = "reason";
93
+
94
+ /**
95
+ * Recorded verification mode, when the gate could read one. Diagnostic
96
+ * metadata only — never an authorization condition (ADR 0022). A reduced-mode
97
+ * proof that executed every command the surface requires is reusable.
98
+ */
99
+ export const FACTORY_PROOF_GATE_MODE_OUTPUT = "mode";
100
+
101
+ /**
102
+ * Guard for every step the gate protects. Deliberately `!= 'true'` and not
103
+ * `== 'false'`: an unset, empty, or garbled output must run the suite.
104
+ */
105
+ export const FACTORY_PROOF_GATE_GUARD = `steps.${FACTORY_PROOF_GATE_STEP_ID}.outputs.${FACTORY_PROOF_GATE_OUTPUT} != 'true'`;
106
+
107
+ /**
108
+ * Pull requests only. A push to a protected branch must never skip: deploy
109
+ * gates wait for a green required check at the *merged* SHA, and no local
110
+ * proof was ever written for that tree.
111
+ */
112
+ export const FACTORY_PROOF_GATE_IF = "github.event_name == 'pull_request'";
113
+
114
+ /**
115
+ * The complete refusal vocabulary. Deliberately few, because these are the
116
+ * only distinctions the gate can honestly make from one Checks API read.
117
+ *
118
+ * - `proven` a covering, passing, unambiguous proof for this exact head
119
+ * - `none` no App-verified factory check run at this commit
120
+ * - `pending` the newest generation had not completed when this job read it
121
+ * - `failed` the newest generation records no pass
122
+ * - `unreadable` it passed but carries no binding for this repository and head
123
+ * - `incomplete` it passed but did not execute every required command
124
+ * - `ambiguous` two newest generations share the greatest start time
125
+ * - `error` the gate could not reach a decision (fail open)
126
+ *
127
+ * There is no `reduced`: mode stopped being an authorization condition in
128
+ * ADR 0022, so a narrower run that still executed every required command is
129
+ * indistinguishable from a full one *for this surface*, which is the point.
130
+ */
131
+ export const FACTORY_PROOF_GATE_REASONS = [
132
+ "proven",
133
+ "none",
134
+ "pending",
135
+ "failed",
136
+ "unreadable",
137
+ "incomplete",
138
+ "ambiguous",
139
+ "error",
140
+ ] as const;
141
+
142
+ export type FactoryProofGateReason =
143
+ (typeof FACTORY_PROOF_GATE_REASONS)[number];
144
+
145
+ /**
146
+ * One entry of a consumer's `verification.commands` (ADR 0021). Only the two
147
+ * load-bearing fields are declared: `name` is the command *identity* the proof
148
+ * records in `executedCommands`, `command` is the string the static coverage
149
+ * assertion matches against the work a reused proof would skip.
150
+ */
151
+ export interface ProofReuseCommand {
152
+ readonly command: string;
153
+ readonly name: string;
154
+ }
155
+
156
+ /**
157
+ * Command identities are baked into the emitted script, so their charset is a
158
+ * safety property, not a style rule. `surface` has always gone through
159
+ * `safeLabel` for this reason; `name` needs the same treatment.
160
+ *
161
+ * Deliberately a strict allow-list rather than a quote-escaper: a name is an
162
+ * identifier, and every identity across the fleet's profiles today is
163
+ * `[a-z0-9:-]`. Anything outside this — hostile or merely a stray apostrophe —
164
+ * makes the whole selection unusable, which degrades to the refusing script.
165
+ * A stray apostrophe reaching interpolation would otherwise kill the step with
166
+ * a bash syntax error *before* the single `GITHUB_OUTPUT` write, so reuse
167
+ * would collapse to never with no `reason` written at all — the silent
168
+ * regression the vocabulary exists to make visible.
169
+ */
170
+ const COMMAND_IDENTITY = /^\w[\w.:@/-]*$/u;
171
+ const COMMAND_IDENTITY_MAX_LENGTH = 120;
172
+
173
+ const isProofReuseCommand = (value: unknown): value is ProofReuseCommand => {
174
+ if (!(value && typeof value === "object")) {
175
+ return false;
176
+ }
177
+ const entry = value as Record<string, unknown>;
178
+ return (
179
+ typeof entry.command === "string" &&
180
+ entry.command.length > 0 &&
181
+ typeof entry.name === "string" &&
182
+ entry.name.length <= COMMAND_IDENTITY_MAX_LENGTH &&
183
+ COMMAND_IDENTITY.test(entry.name)
184
+ );
185
+ };
186
+
187
+ /**
188
+ * The command identities a guarded surface requires, derived from the profile
189
+ * command objects that surface selected. This is the single derivation: the
190
+ * gate script and the static coverage assertion both go through it, so a
191
+ * consumer can never have one notion of "required" at generation time and
192
+ * another at assertion time.
193
+ *
194
+ * `undefined` means the selection is unusable — not an array, empty, or
195
+ * carrying an entry whose `command` is blank or whose `name` is not a plain
196
+ * command identity. An empty required set would make *every* passing proof
197
+ * trivially covering, so it is never silently treated as "requires nothing";
198
+ * callers must refuse instead.
199
+ */
200
+ export const proofReuseRequiredCommands = (
201
+ commands: readonly ProofReuseCommand[]
202
+ ): readonly string[] | undefined => {
203
+ if (!(Array.isArray(commands) && commands.length > 0)) {
204
+ return;
205
+ }
206
+ if (!commands.every(isProofReuseCommand)) {
207
+ return;
208
+ }
209
+ return [...new Set(commands.map(({ name }) => name))].toSorted();
210
+ };
211
+
212
+ /**
213
+ * jq program: every page of the Checks API result in, three sanitized lines
214
+ * (`reason`, `mode`, missing commands) out.
215
+ *
216
+ * The input is what `gh api --paginate` actually writes: the pages
217
+ * *concatenated* as a stream of top-level response objects, not merged into
218
+ * one document and not wrapped in an array. That is why the script invokes
219
+ * `jq -s` and why this program opens with `.[] | (.check_runs // [])[]`. A
220
+ * test double that emits `[{ "check_runs": [...] }]` instead is a different
221
+ * shape: `jq` fails with `Cannot index array with string ("check_runs")`, the
222
+ * script lands on `reason=error`, and — because the gate fails open — the
223
+ * result looks exactly like healthy full CI. Stub `gh` with a bare response
224
+ * object per page.
225
+ *
226
+ * `binding` recovers the machine-readable payload from `output.text`
227
+ * (preferred) or `output.summary`, which the factory writes as a fenced JSON
228
+ * document. Anything unparseable degrades to `null`, and therefore to a
229
+ * refusal.
230
+ *
231
+ * This is the *second* parser of that payload: `parsePrVerifyCheckPayload`
232
+ * (`software-factory/src/pr-verify-check-payload.ts`) regex-extracts the fenced
233
+ * block, while this one strips fence lines and `fromjson`s what remains. The
234
+ * one input that separates them is prose around the fence, which parses there
235
+ * and refuses here. Both halves of that are asserted in the test suite rather
236
+ * than asserted here in prose — the exact shape the renderer emits is proven,
237
+ * and the divergent shape is proven to fail safe. Two parsers of one payload
238
+ * can drift; what must not drift silently is which direction they drift in.
239
+ *
240
+ * Ordering is by `started_at` alone, normalized to whole seconds. Two
241
+ * generations that share the greatest start time are `ambiguous` rather than
242
+ * arbitrarily resolved: the gate cannot tell which one describes the tree, and
243
+ * silently picking either is exactly the kind of coin-flip authorization this
244
+ * whole mechanism exists to remove. Sub-second precision is dropped so
245
+ * `…:00Z` and `…:00.000Z` compare equal; two genuine verification generations
246
+ * starting within the same second at the same head is not a real state, and
247
+ * collapsing them errs toward refusing.
248
+ *
249
+ * The comparison is lexical, which is correct only for fixed-width UTC. A
250
+ * `+HH:MM` offset would sort by its literal text rather than by its instant,
251
+ * and an absent `started_at` would sort to the bottom. The Checks API returns
252
+ * neither today, so this is guarded rather than merely noted: every candidate
253
+ * must carry a canonical UTC timestamp, and one that does not makes the newest
254
+ * generation unestablishable, which is `ambiguous`. Refusing costs a hosted
255
+ * run; ranking an unorderable set could authorize the wrong one.
256
+ */
257
+ const GATE_JQ = String.raw`
258
+ def binding:
259
+ [ .output.text, .output.summary ]
260
+ | map(select(type == "string"))
261
+ | map(split("\n") | map(select(startswith("${"```"}") | not)) | join("\n"))
262
+ | map(. as $body | try ($body | fromjson) catch null)
263
+ | map(select(type == "object"))
264
+ | .[0];
265
+
266
+ def startedAt: (.started_at // "") | tostring;
267
+
268
+ def rankable:
269
+ test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?Z$");
270
+
271
+ [ .[]
272
+ | (.check_runs // [])[]
273
+ | select(.name == $name)
274
+ | select(((.app.id // "") | tostring) == $app)
275
+ | { started: startedAt,
276
+ status: (.status // ""),
277
+ conclusion: (.conclusion // ""),
278
+ binding: binding }
279
+ ] as $runs
280
+ | ($runs | map(.started | rankable) | all) as $orderable
281
+ | ($runs | map(.started | sub("\\.[0-9]+Z$"; "Z")) | max) as $newest
282
+ | [ $runs[] | select((.started | sub("\\.[0-9]+Z$"; "Z")) == $newest) ] as $generation
283
+ | (if ($runs | length) == 0 then ["none", "", ""]
284
+ elif ($orderable | not) then ["ambiguous", "", ""]
285
+ elif ($generation | length) != 1 then ["ambiguous", "", ""]
286
+ else
287
+ $generation[0] as $run
288
+ | (if ($run.binding | type) == "object" then $run.binding else {} end) as $proof
289
+ | (($proof.mode // "") | tostring) as $mode
290
+ | (($proof.executedCommands // []) | map(select(type == "string"))) as $executed
291
+ | ($required - $executed) as $missing
292
+ | (if $run.status != "completed" then "pending"
293
+ elif $run.conclusion != "success" then "failed"
294
+ elif (($run.binding | type) != "object")
295
+ or ($proof.kind != "pr-verify-proof-binding")
296
+ or ($proof.schemaVersion != 1)
297
+ or ($proof.headSha != $sha)
298
+ or ($proof.repository != $repository) then "unreadable"
299
+ elif $proof.outcome != "passed" then "failed"
300
+ elif ($required | length) == 0 then "incomplete"
301
+ elif ($missing | length) != 0 then "incomplete"
302
+ else "proven"
303
+ end) as $reason
304
+ | [$reason, $mode, ($missing | join(", "))]
305
+ end)
306
+ | map(gsub("[\\r\\n\\t]"; " "))
307
+ | join("\n")
308
+ `.trim();
309
+
310
+ /**
311
+ * Every refusal ends on the same line. `pr:verify` is the command that
312
+ * produces the proof this job looks for, so it is the corrective action
313
+ * whatever the refusal was.
314
+ *
315
+ * It is phrased as an option, not a reprimand. A pull request opened by hand
316
+ * has no factory proof and never will; the full suite running for it is the
317
+ * designed behavior, not a mistake to report.
318
+ */
319
+ const CORRECTIVE_LINE =
320
+ "Run `psf pr:verify` before publishing to reuse local proof and avoid a duplicate server run.";
321
+
322
+ /**
323
+ * Emitted when a consumer's command selection is unusable. Mirrors the
324
+ * retired hosted docs-only detector: a malformed profile slice degrades to the
325
+ * conservative answer at *runtime* rather than throwing at generation time,
326
+ * because the input is JSON that may be absent or reshaped. The loud signal is
327
+ * the static coverage assertion, plus `reason=error` on the step output.
328
+ */
329
+ const UNUSABLE_SELECTION_SCRIPT = String.raw`{
330
+ printf '${FACTORY_PROOF_GATE_OUTPUT}=false\n'
331
+ printf '${FACTORY_PROOF_GATE_REASON_OUTPUT}=error\n'
332
+ } >> "${shellExpansion("GITHUB_OUTPUT:-/dev/null")}"`;
333
+
334
+ /** Surface labels reach markdown, so only a plain, bounded label survives. */
335
+ const safeLabel = (surface: string): string => {
336
+ const cleaned = (typeof surface === "string" ? surface : "")
337
+ .replaceAll(/[^\w -]/gu, "")
338
+ .trim()
339
+ .slice(0, 60);
340
+ return cleaned.length > 0 ? cleaned : "verification";
341
+ };
342
+
343
+ const gateScript = (required: readonly string[], surface: string): string =>
344
+ String.raw`
345
+ set -uo pipefail
346
+
347
+ CHECK_NAME=${shellSingleQuote(FACTORY_PROOF_GATE_CHECK_NAME)}
348
+ FACTORY_APP_ID=${shellSingleQuote(FACTORY_PROOF_GATE_APP_ID)}
349
+ REQUIRED_COMMANDS=${shellSingleQuote(JSON.stringify(required))}
350
+ SURFACE=${shellSingleQuote(surface)}
351
+
352
+ reason=error
353
+ detail=''
354
+ mode=''
355
+ missing=''
356
+
357
+ # filter=all with full pagination is load-bearing (ADR 0022). GitHub's
358
+ # default "latest" filter is ordered by completion, so a newer generation
359
+ # that is still running can be hidden behind an older completed one — the
360
+ # gate would then read a stale pass as current.
361
+ if [ -z "${shellExpansion("HEAD_SHA:-")}" ]; then
362
+ detail='no pull request head SHA'
363
+ elif [ -z "${shellExpansion("GITHUB_REPOSITORY:-")}" ]; then
364
+ detail='no repository name'
365
+ elif ! response=$(gh api --method GET --paginate \
366
+ "repos/$GITHUB_REPOSITORY/commits/$HEAD_SHA/check-runs" \
367
+ -f "check_name=$CHECK_NAME" \
368
+ -f filter=all \
369
+ -f per_page=100 2>&1); then
370
+ detail="checks API unreadable: $response"
371
+ elif ! finding=$(printf '%s' "$response" | jq -r -s \
372
+ --arg name "$CHECK_NAME" \
373
+ --arg app "$FACTORY_APP_ID" \
374
+ --arg sha "$HEAD_SHA" \
375
+ --arg repository "$GITHUB_REPOSITORY" \
376
+ --argjson required "$REQUIRED_COMMANDS" \
377
+ '${GATE_JQ}' 2>&1); then
378
+ detail="unreadable proof binding: $finding"
379
+ else
380
+ # Three separate reads, not one IFS split: tab is IFS whitespace, so a
381
+ # split would collapse the empty fields the vocabulary relies on.
382
+ #
383
+ # The trailing "|| :" on each read is load-bearing. "missing" is empty for
384
+ # every verdict except "incomplete", so jq's third field is the empty
385
+ # string, command substitution strips the trailing newline, and the third
386
+ # read reaches EOF -- having correctly assigned "" -- and returns 1. Under
387
+ # an errexit shell that return value kills the step before the single
388
+ # GITHUB_OUTPUT write below, which is exactly how this gate failed in
389
+ # hosted CI on every decidable verdict while every test passed (#319).
390
+ {
391
+ IFS= read -r reason || :
392
+ IFS= read -r mode || :
393
+ IFS= read -r missing || :
394
+ } <<< "$finding"
395
+ case "$reason" in
396
+ proven | none | pending | failed | unreadable | incomplete | ambiguous) ;;
397
+ *)
398
+ detail="unexpected gate result: $reason"
399
+ reason=error
400
+ ;;
401
+ esac
402
+ fi
403
+
404
+ # The binding is App-verified, but it still reaches markdown. Only a plain
405
+ # lowercase token is quoted back; anything else is reported as unknown.
406
+ case "$mode" in
407
+ '') ;;
408
+ *[!a-z-]*) mode='unknown' ;;
409
+ esac
410
+
411
+ verdict=false
412
+ if [ "$reason" = 'proven' ]; then
413
+ verdict=true
414
+ fi
415
+
416
+ # detail can carry arbitrary gh or jq error text. Collapse it to one bounded
417
+ # line so it cannot restructure the summary it is written into.
418
+ detail=$(printf '%s' "$detail" | tr '\n\r\t' ' ' | cut -c1-240)
419
+ missing=$(printf '%s' "$missing" | tr '\n\r\t' ' ' | cut -c1-240)
420
+
421
+ SUMMARY="${shellExpansion("GITHUB_STEP_SUMMARY:-/dev/null")}"
422
+ say() { printf '%s\n' "$1" >> "$SUMMARY"; }
423
+
424
+ say "## Proof reuse: $SURFACE"
425
+ say ''
426
+
427
+ case "$reason" in
428
+ proven)
429
+ echo "Factory proof: reusing local verification of $HEAD_SHA; skipping the $SURFACE suite."
430
+ say "Skipped. The factory already verified this exact commit, so this job did not run the $SURFACE suite a second time."
431
+ say ''
432
+ say "- Reused proof: the \`$CHECK_NAME\` check run published by the pinned factory GitHub App."
433
+ say "- Covers head: \`$HEAD_SHA\`"
434
+ say "- Recorded mode: \`$mode\` (diagnostic only), outcome \`passed\`."
435
+ ;;
436
+ none)
437
+ say 'Ran the full suite. No trusted factory proof covers this commit.'
438
+ say ''
439
+ say "The factory GitHub App has published no \`$CHECK_NAME\` check run at head \`$HEAD_SHA\`. Proof is keyed to the commit, so proof written for any other commit is not visible here. A pull request opened by hand carries no factory proof at all, which is normal."
440
+ say ''
441
+ say '${CORRECTIVE_LINE}'
442
+ ;;
443
+ pending)
444
+ say 'Ran the full suite. The factory proof for this commit had not finished when this job read it.'
445
+ say ''
446
+ say "The newest \`$CHECK_NAME\` check run at head \`$HEAD_SHA\` was still in progress. The gate reads one snapshot at job start and does not wait."
447
+ say ''
448
+ say "Let \`psf pr:verify\` finish before publishing, so its proof is on the commit before this job reads it."
449
+ ;;
450
+ failed)
451
+ say 'Ran the full suite. The factory proof for this commit records no pass.'
452
+ say ''
453
+ say "The newest \`$CHECK_NAME\` check run at head \`$HEAD_SHA\` finished without a passing conclusion, so it establishes nothing about this tree."
454
+ say ''
455
+ say '${CORRECTIVE_LINE}'
456
+ ;;
457
+ unreadable)
458
+ say 'Ran the full suite. The factory check run for this commit carries no proof this job can read.'
459
+ say ''
460
+ say "The newest \`$CHECK_NAME\` check run at head \`$HEAD_SHA\` passed, but it carries no machine-readable binding naming this repository and this head, or one written to a schema this job does not recognize."
461
+ say ''
462
+ say '${CORRECTIVE_LINE}'
463
+ ;;
464
+ incomplete)
465
+ say "Ran the full suite. The factory proof for this commit does not cover every command the $SURFACE surface requires."
466
+ say ''
467
+ say "The newest \`$CHECK_NAME\` check run at head \`$HEAD_SHA\` passed, but its \`executedCommands\` is missing: \`$missing\`"
468
+ say ''
469
+ say '${CORRECTIVE_LINE}'
470
+ ;;
471
+ ambiguous)
472
+ say 'Ran the full suite. The newest factory proof for this commit is ambiguous.'
473
+ say ''
474
+ say "Two \`$CHECK_NAME\` check runs at head \`$HEAD_SHA\` share the greatest start time, so there is no single newest generation to trust. The gate refuses rather than picking one."
475
+ say ''
476
+ say '${CORRECTIVE_LINE}'
477
+ ;;
478
+ *)
479
+ say 'Ran the full suite. The proof gate could not reach a decision.'
480
+ say ''
481
+ say 'The gate fails open by design: when it cannot tell whether proof exists, every command runs. Nothing about this pull request is implied.'
482
+ say ''
483
+ say " $detail"
484
+ ;;
485
+ esac
486
+ say ''
487
+
488
+ if [ "$verdict" != 'true' ]; then
489
+ echo "No reusable factory proof for ${shellExpansion("HEAD_SHA:-<unknown>")}; running the $SURFACE suite ($reason). $detail"
490
+ fi
491
+
492
+ # The only write. A crash before this line leaves the output unset, the guard
493
+ # reads empty, and every guarded step runs — the fail-open path.
494
+ {
495
+ printf '${FACTORY_PROOF_GATE_OUTPUT}=%s\n' "$verdict"
496
+ printf '${FACTORY_PROOF_GATE_REASON_OUTPUT}=%s\n' "$reason"
497
+ printf '${FACTORY_PROOF_GATE_MODE_OUTPUT}=%s\n' "$mode"
498
+ } >> "${shellExpansion("GITHUB_OUTPUT:-/dev/null")}"
499
+ `.trim();
500
+
501
+ export interface FactoryProofGateOptions {
502
+ /**
503
+ * The profile command objects this guarded surface selected. A consumer with
504
+ * distinct core and docs jobs selects distinct sets, and each job's gate
505
+ * therefore requires a different coverage.
506
+ */
507
+ readonly commands: readonly ProofReuseCommand[];
508
+ /** Names the suite in the job summary. Changes no trust decision. */
509
+ readonly surface: string;
510
+ }
511
+
512
+ /**
513
+ * The gate script. Exported so it can be executed directly under test against
514
+ * a stubbed `gh`, rather than only asserted against as workflow text — the
515
+ * script is the security boundary, and asserting on generated YAML would leave
516
+ * its actual decisions untested.
517
+ *
518
+ * Note the deliberate absence of `set -e`: every failure path has to reach the
519
+ * final write, and that write must emit `false`. Omitting it is necessary but
520
+ * not sufficient — the runner supplies `-e` unless the step declares
521
+ * `FACTORY_PROOF_GATE_SHELL` — so the script is also written to reach that
522
+ * write under errexit, and the tests execute it both ways.
523
+ */
524
+ export const factoryProofGateScript = ({
525
+ commands,
526
+ surface,
527
+ }: FactoryProofGateOptions): string => {
528
+ const required = proofReuseRequiredCommands(commands);
529
+ return required
530
+ ? gateScript(required, safeLabel(surface))
531
+ : UNUSABLE_SELECTION_SCRIPT;
532
+ };
533
+
534
+ export interface FactoryProofGateStep {
535
+ readonly continueOnError: true;
536
+ readonly env: Readonly<{
537
+ GH_TOKEN: string;
538
+ HEAD_SHA: string;
539
+ }>;
540
+ readonly id: typeof FACTORY_PROOF_GATE_STEP_ID;
541
+ readonly if: typeof FACTORY_PROOF_GATE_IF;
542
+ readonly name: string;
543
+ readonly run: string;
544
+ readonly shell: typeof FACTORY_PROOF_GATE_SHELL;
545
+ }
546
+
547
+ /**
548
+ * The step itself, structurally accepted by gagen's `step()` without adding a
549
+ * gagen runtime dependency. It belongs first in the job it guards: one Checks
550
+ * API read with the default `GITHUB_TOKEN` (`checks: read`), no checkout, no
551
+ * install, so a proven head costs a runner nothing beyond job startup.
552
+ *
553
+ * A **step, not a job**, and that is not a style preference. A separate gate
554
+ * job that errored would leave the guarded job `skipped`, and a summary job
555
+ * that only fails on `failure`/`cancelled` would still report green — a Checks
556
+ * API outage would silently skip verification. As a step it fails open by
557
+ * construction: the step errors, the output is never written, the guard reads
558
+ * empty, and every command runs.
559
+ */
560
+ export const factoryProofGateStep = (
561
+ options: FactoryProofGateOptions
562
+ ): FactoryProofGateStep =>
563
+ Object.freeze({
564
+ continueOnError: true as const,
565
+ env: Object.freeze({
566
+ GH_TOKEN: githubExpression("secrets.GITHUB_TOKEN"),
567
+ HEAD_SHA: githubExpression("github.event.pull_request.head.sha"),
568
+ }),
569
+ id: FACTORY_PROOF_GATE_STEP_ID,
570
+ if: FACTORY_PROOF_GATE_IF,
571
+ name: "Check for factory proof of this head",
572
+ run: factoryProofGateScript(options),
573
+ // Never omit: the runner's default `run:` shell supplies `-e`, which
574
+ // voids the fail-open design. See FACTORY_PROOF_GATE_SHELL.
575
+ shell: FACTORY_PROOF_GATE_SHELL,
576
+ });
577
+
578
+ export interface ProofReuseCoverageInput {
579
+ /** The same selection handed to the gate for this surface. */
580
+ readonly commands: readonly ProofReuseCommand[];
581
+ /**
582
+ * Skipped work covered under a different command string, keyed by the
583
+ * skipped string with the reason as its value. Deliberately explicit: a
584
+ * prose rationale for why some narrower command is "equivalent enough" is
585
+ * exactly what this assertion exists to force into the open.
586
+ */
587
+ readonly equivalents?: Readonly<Record<string, string>>;
588
+ /**
589
+ * The command strings the consumer's workflow would skip when the gate says
590
+ * `true`, already resolved to the leaves that actually run. Extracting them
591
+ * is the consumer's job: this package never parses workflow source, because
592
+ * doing so is what killed #288.
593
+ */
594
+ readonly skipped: readonly string[];
595
+ /** Names the surface in the failure message. */
596
+ readonly surface?: string;
597
+ }
598
+
599
+ export interface ProofReuseCoverageReport {
600
+ /** True only when the selection is usable *and* nothing is uncovered. */
601
+ readonly covered: boolean;
602
+ /** Command identities derived from the selection; empty when unusable. */
603
+ readonly requiredCommands: readonly string[];
604
+ /** Skipped command strings no selected profile command accounts for. */
605
+ readonly uncovered: readonly string[];
606
+ }
607
+
608
+ /**
609
+ * The static coverage assertion, as a report.
610
+ *
611
+ * Runtime command coverage is authoritative — the gate's `incomplete` refusal
612
+ * is the backstop that cannot be bypassed. This is the compile-time guard in
613
+ * front of it: it fails a consumer's build the moment a workflow skips work
614
+ * that no selected profile command runs, rather than letting the drift show up
615
+ * as CI quietly verifying nothing. The drift is real history: #227 wired a
616
+ * workspace member into CI while the profile never learned about it.
617
+ */
618
+ export const proofReuseCoverage = ({
619
+ commands,
620
+ equivalents = {},
621
+ skipped,
622
+ }: ProofReuseCoverageInput): ProofReuseCoverageReport => {
623
+ const requiredCommands = proofReuseRequiredCommands(commands) ?? [];
624
+ const proven = new Set(
625
+ requiredCommands.length > 0
626
+ ? commands.map(({ command }) => command.trim())
627
+ : []
628
+ );
629
+ const uncovered = [
630
+ ...new Set(
631
+ (Array.isArray(skipped) ? skipped : [])
632
+ .map((command) => String(command).trim())
633
+ .filter(
634
+ (command) =>
635
+ command.length > 0 &&
636
+ // `Object.hasOwn`, never `in`: `in` walks the prototype chain, so
637
+ // a skipped command named `toString` or `constructor` would report
638
+ // itself as a declared equivalent of nothing. This assertion's only
639
+ // job is to fail loudly.
640
+ !(proven.has(command) || Object.hasOwn(equivalents, command))
641
+ )
642
+ ),
643
+ ].toSorted();
644
+
645
+ return Object.freeze({
646
+ covered: requiredCommands.length > 0 && uncovered.length === 0,
647
+ requiredCommands,
648
+ uncovered,
649
+ });
650
+ };
651
+
652
+ /** `proofReuseCoverage`, as a build failure. */
653
+ export const assertProofReuseCoverage = (
654
+ input: ProofReuseCoverageInput
655
+ ): ProofReuseCoverageReport => {
656
+ const report = proofReuseCoverage(input);
657
+ if (report.covered) {
658
+ return report;
659
+ }
660
+
661
+ const surface = safeLabel(input.surface ?? "");
662
+ const problem =
663
+ report.requiredCommands.length === 0
664
+ ? "selects no usable profile commands, so any passing proof would trivially cover it"
665
+ : `skips work no selected profile command runs: ${report.uncovered.join(", ")}`;
666
+ throw new Error(
667
+ `Proof-reuse coverage failed: the ${surface} surface ${problem}. Add the command to software-factory.profile.json (and to this surface's selection), or stop skipping it.`
668
+ );
669
+ };