@intentius/chant 0.124.89 → 0.124.91

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 (185) hide show
  1. package/dist/actor.d.ts +5 -3
  2. package/dist/actor.d.ts.map +1 -1
  3. package/dist/actor.js +7 -5
  4. package/dist/audit/core.d.ts.map +1 -1
  5. package/dist/audit/core.js +11 -1
  6. package/dist/ci-comment-commands.d.ts +18 -2
  7. package/dist/ci-comment-commands.d.ts.map +1 -1
  8. package/dist/ci-comment-commands.js +30 -2
  9. package/dist/ci-job-guard.d.ts +42 -45
  10. package/dist/ci-job-guard.d.ts.map +1 -1
  11. package/dist/ci-job-guard.js +206 -3
  12. package/dist/ci-note-commands.d.ts +41 -1
  13. package/dist/ci-note-commands.d.ts.map +1 -1
  14. package/dist/ci-note-commands.js +64 -5
  15. package/dist/ci-product.d.ts +50 -1
  16. package/dist/ci-product.d.ts.map +1 -1
  17. package/dist/ci-product.js +52 -6
  18. package/dist/cli/handlers/pr-comment.d.ts +13 -1
  19. package/dist/cli/handlers/pr-comment.d.ts.map +1 -1
  20. package/dist/cli/handlers/pr-comment.js +90 -1
  21. package/dist/cli/handlers/pr-notes.d.ts.map +1 -1
  22. package/dist/cli/handlers/pr-notes.js +23 -1
  23. package/dist/cli/handlers/pr.d.ts +2 -2
  24. package/dist/cli/handlers/pr.d.ts.map +1 -1
  25. package/dist/cli/handlers/pr.js +41 -20
  26. package/dist/cli/handlers/run-wave.d.ts.map +1 -1
  27. package/dist/cli/handlers/run-wave.js +154 -4
  28. package/dist/cli/main.d.ts.map +1 -1
  29. package/dist/cli/main.js +17 -0
  30. package/dist/cli/parse-args.d.ts.map +1 -1
  31. package/dist/cli/parse-args.js +28 -0
  32. package/dist/cli/registry.d.ts +12 -1
  33. package/dist/cli/registry.d.ts.map +1 -1
  34. package/dist/components/pr-checks.d.ts +39 -1
  35. package/dist/components/pr-checks.d.ts.map +1 -1
  36. package/dist/components/pr-checks.js +45 -2
  37. package/dist/components/pr-pipeline.d.ts +11 -3
  38. package/dist/components/pr-pipeline.d.ts.map +1 -1
  39. package/dist/components/pr-pipeline.js +20 -7
  40. package/dist/components/product-pipeline.d.ts +54 -2
  41. package/dist/components/product-pipeline.d.ts.map +1 -1
  42. package/dist/components/product-pipeline.js +63 -6
  43. package/dist/components/product-plan.d.ts +92 -2
  44. package/dist/components/product-plan.d.ts.map +1 -1
  45. package/dist/components/product-plan.js +172 -22
  46. package/dist/floor-table.data.d.ts.map +1 -1
  47. package/dist/floor-table.data.js +37 -23
  48. package/dist/lexicon-module.d.ts +2 -0
  49. package/dist/lexicon-module.d.ts.map +1 -1
  50. package/dist/lexicon-module.js +6 -2
  51. package/dist/lexicon.d.ts +82 -4
  52. package/dist/lexicon.d.ts.map +1 -1
  53. package/dist/lifecycle/gate-applied.d.ts +9 -0
  54. package/dist/lifecycle/gate-applied.d.ts.map +1 -1
  55. package/dist/op/apply-status.d.ts +84 -0
  56. package/dist/op/apply-status.d.ts.map +1 -0
  57. package/dist/op/apply-status.js +106 -0
  58. package/dist/op/before-merge-decide.d.ts +102 -0
  59. package/dist/op/before-merge-decide.d.ts.map +1 -0
  60. package/dist/op/before-merge-decide.js +142 -0
  61. package/dist/op/before-merge-done.d.ts +56 -0
  62. package/dist/op/before-merge-done.d.ts.map +1 -0
  63. package/dist/op/before-merge-done.js +69 -0
  64. package/dist/op/before-merge-store.d.ts +20 -0
  65. package/dist/op/before-merge-store.d.ts.map +1 -0
  66. package/dist/op/before-merge-store.js +48 -0
  67. package/dist/op/before-merge.d.ts +150 -0
  68. package/dist/op/before-merge.d.ts.map +1 -0
  69. package/dist/op/before-merge.js +207 -0
  70. package/dist/op/follow-on-run.d.ts +117 -0
  71. package/dist/op/follow-on-run.d.ts.map +1 -0
  72. package/dist/op/follow-on-run.js +154 -0
  73. package/dist/op/follow-on-store.d.ts +17 -0
  74. package/dist/op/follow-on-store.d.ts.map +1 -0
  75. package/dist/op/follow-on-store.js +54 -0
  76. package/dist/op/gate.d.ts +6 -0
  77. package/dist/op/gate.d.ts.map +1 -1
  78. package/dist/op/gate.js +10 -1
  79. package/dist/op/index.d.ts +3 -1
  80. package/dist/op/index.d.ts.map +1 -1
  81. package/dist/op/index.js +1 -0
  82. package/dist/op/op-waves-after-plan.d.ts +13 -0
  83. package/dist/op/op-waves-after-plan.d.ts.map +1 -1
  84. package/dist/op/op-waves-evidence-jobs.d.ts +4 -1
  85. package/dist/op/op-waves-evidence-jobs.d.ts.map +1 -1
  86. package/dist/op/op-waves-evidence-jobs.js +10 -2
  87. package/dist/op/op-waves-follow-on.d.ts +162 -0
  88. package/dist/op/op-waves-follow-on.d.ts.map +1 -0
  89. package/dist/op/op-waves-follow-on.js +355 -0
  90. package/dist/op/op-waves-run.d.ts +146 -6
  91. package/dist/op/op-waves-run.d.ts.map +1 -1
  92. package/dist/op/op-waves-run.js +871 -99
  93. package/dist/op/op-waves-split.d.ts +122 -0
  94. package/dist/op/op-waves-split.d.ts.map +1 -0
  95. package/dist/op/op-waves-split.js +166 -0
  96. package/dist/op/op-waves.d.ts +20 -0
  97. package/dist/op/op-waves.d.ts.map +1 -1
  98. package/dist/op/op-waves.js +15 -1
  99. package/dist/op/schedule-overdue.d.ts +15 -1
  100. package/dist/op/schedule-overdue.d.ts.map +1 -1
  101. package/dist/op/schedule-overdue.js +18 -2
  102. package/dist/op/stage-hooks.d.ts +36 -1
  103. package/dist/op/stage-hooks.d.ts.map +1 -1
  104. package/dist/op/stage-hooks.js +17 -3
  105. package/dist/op/wave-stop.d.ts +4 -1
  106. package/dist/op/wave-stop.d.ts.map +1 -1
  107. package/dist/op/wave-stop.js +4 -2
  108. package/dist/planned-files.d.ts +16 -8
  109. package/dist/planned-files.d.ts.map +1 -1
  110. package/dist/planned-files.js +12 -8
  111. package/dist/pr-post.d.ts +6 -0
  112. package/dist/pr-post.d.ts.map +1 -1
  113. package/dist/pr-post.js +7 -5
  114. package/dist/release-floors.d.ts +9 -1
  115. package/dist/release-floors.d.ts.map +1 -1
  116. package/dist/release-floors.js +27 -19
  117. package/dist/wave-blocked.d.ts +7 -1
  118. package/dist/wave-blocked.d.ts.map +1 -1
  119. package/dist/wave-blocked.js +7 -2
  120. package/dist/wave-runtime.mjs +364 -351
  121. package/package.json +1 -1
  122. package/src/actor.test.ts +14 -1
  123. package/src/actor.ts +8 -6
  124. package/src/audit/core.ts +13 -1
  125. package/src/audit/first-load.test.ts +101 -0
  126. package/src/ci-comment-commands.test.ts +63 -0
  127. package/src/ci-comment-commands.ts +43 -4
  128. package/src/ci-image.test.ts +1 -2
  129. package/src/ci-job-guard.before-merge.test.ts +148 -0
  130. package/src/ci-job-guard.follow-on-record.test.ts +137 -0
  131. package/src/ci-job-guard.follow-on.test.ts +144 -0
  132. package/src/ci-job-guard.ts +221 -3
  133. package/src/ci-note-commands.test.ts +76 -1
  134. package/src/ci-note-commands.ts +96 -7
  135. package/src/ci-product.ts +65 -6
  136. package/src/cli/handlers/pr-comment.ts +85 -2
  137. package/src/cli/handlers/pr-notes.ts +30 -6
  138. package/src/cli/handlers/pr.ts +34 -17
  139. package/src/cli/handlers/run-wave.ts +144 -4
  140. package/src/cli/main.ts +17 -0
  141. package/src/cli/parse-args.ts +20 -0
  142. package/src/cli/registry.ts +12 -1
  143. package/src/components/pr-checks.ts +66 -2
  144. package/src/components/pr-pipeline.ts +21 -9
  145. package/src/components/product-pipeline.apply-status.test.ts +50 -0
  146. package/src/components/product-pipeline.ts +117 -9
  147. package/src/components/product-plan-names.test.ts +135 -0
  148. package/src/components/product-plan.test.ts +23 -5
  149. package/src/components/product-plan.ts +248 -25
  150. package/src/floor-table.data.ts +37 -23
  151. package/src/lexicon-module.ts +6 -2
  152. package/src/lexicon.ts +78 -1
  153. package/src/lifecycle/gate-applied.ts +7 -0
  154. package/src/op/apply-status.test.ts +83 -0
  155. package/src/op/apply-status.ts +132 -0
  156. package/src/op/before-merge-decide.ts +190 -0
  157. package/src/op/before-merge-done.ts +99 -0
  158. package/src/op/before-merge-store.ts +44 -0
  159. package/src/op/before-merge.test.ts +201 -0
  160. package/src/op/before-merge.ts +266 -0
  161. package/src/op/follow-on-run.ts +206 -0
  162. package/src/op/follow-on-store.ts +54 -0
  163. package/src/op/gate.ts +16 -1
  164. package/src/op/index.ts +5 -0
  165. package/src/op/op-waves-after-plan.ts +13 -0
  166. package/src/op/op-waves-evidence-jobs.ts +11 -3
  167. package/src/op/op-waves-follow-on-record.test.ts +152 -0
  168. package/src/op/op-waves-follow-on.test.ts +247 -0
  169. package/src/op/op-waves-follow-on.ts +385 -0
  170. package/src/op/op-waves-render-spec.test.ts +1 -2
  171. package/src/op/op-waves-run.ts +971 -93
  172. package/src/op/op-waves-split-product.test.ts +288 -0
  173. package/src/op/op-waves-split-step.test.ts +298 -0
  174. package/src/op/op-waves-split.test.ts +196 -0
  175. package/src/op/op-waves-split.ts +189 -0
  176. package/src/op/op-waves.ts +28 -3
  177. package/src/op/schedule-overdue.test.ts +29 -1
  178. package/src/op/schedule-overdue.ts +27 -3
  179. package/src/op/stage-hooks.ts +52 -4
  180. package/src/op/wave-stop.ts +5 -3
  181. package/src/planned-files.test.ts +33 -0
  182. package/src/planned-files.ts +26 -10
  183. package/src/pr-post.ts +13 -5
  184. package/src/release-floors.ts +31 -19
  185. package/src/wave-blocked.ts +9 -4
@@ -0,0 +1,385 @@
1
+ /**
2
+ * `rest` waves in a split pipeline (#4992): one wave per run, each run
3
+ * starting the next.
4
+ *
5
+ * Maintainer decision (2026-11-10): a follow-on run. Before merge (and on a
6
+ * push of the same pipeline) each wave is four jobs (./op-waves-split.ts),
7
+ * and the jobs of a wave the plan finds at run time cannot be rendered up
8
+ * front. So the rest wave's four jobs (the last rendered wave, N) run wave
9
+ * N in a run, and their record job, once every wave of the run applied and
10
+ * the plan found more waves, starts the waves workflow again for wave N+1:
11
+ *
12
+ * - GitHub and Forgejo dispatch the workflow on its branch with the inputs
13
+ * `wave` and, before merge, `pr`, `head` and `digest`, or, for a push,
14
+ * `commit` (the pushed commit, which every job of the follow-on checks
15
+ * out and checks is on the branch). GitHub dispatches with the job's
16
+ * token (`actions: write` on that job alone); Forgejo with the
17
+ * `CHANT_FORGE_TOKEN` secret, as its resume does.
18
+ * - GitLab starts a pipeline of the branch with the same values as
19
+ * variables (`CHANT_WAVES_WAVE`, the decision's three or
20
+ * `CHANT_WAVES_COMMIT`), with `CHANT_FORGE_TOKEN`, since the job token
21
+ * cannot start one.
22
+ *
23
+ * In the follow-on the jobs of waves 1 to N-1 do not run, and the rest
24
+ * wave's jobs run wave N+1 (`--wave "${CHANT_WAVES_WAVE:-N}"`, and their
25
+ * files at that wave's paths, {@link opWaveRestPath}). The guard admits the
26
+ * run as the same decided head (./follow-on-run.ts). It goes on until the
27
+ * wave the plan found last.
28
+ *
29
+ * The record job leaves a note in the outcome directory
30
+ * ({@link OP_WAVE_REST_NOTE}): the wave it ran, how many the plan found, and
31
+ * the wave it started, if it started one. `before-merge-done`, the control
32
+ * dispatch and the apply status read it, so a run that started a follow-on
33
+ * is not the last: nothing merges, the control repo hears from the last
34
+ * run, and the status stays pending with `wave k of M applied`.
35
+ *
36
+ * #4998: before it starts a follow-on, the record job records it on
37
+ * `chant/lifecycle` (./follow-on-run.ts, ./follow-on-store.ts): the wave it
38
+ * starts, its own run, the decision or the pushed commit, and the outcomes
39
+ * of every run of the chain so far, its own added. The follow-on's guard
40
+ * admits that wave alone. A follow-on's record job reads the record that
41
+ * started it and puts the earlier runs' outcomes in its note as `chain`, so
42
+ * the last run's control dispatch and apply status add up the whole chain
43
+ * ({@link withChainOutcomes}): a chain whose last wave changed nothing
44
+ * reports `applied` when an earlier run applied.
45
+ */
46
+
47
+ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
48
+ import { isAbsolute, join } from "node:path";
49
+ import { CHANT_EXIT } from "../exit-codes";
50
+ import { CONTROL_OUTCOME_VAR } from "../control/dispatch";
51
+ import { beforeMergeRunFromEnv, isBeforeMergeRun } from "./before-merge";
52
+ import { OP_WAVES_COMMIT_ENV, OP_WAVES_WAVE_ENV, type FollowOnChainPort, type FollowOnOutcome, type FollowOnRecord } from "./follow-on-run";
53
+ import type { WaveMemberOutcome } from "../wave-blocked";
54
+ import { opWaveOutcomeDir, opWaveRecordPath, type OpWavesSpec } from "./op-waves";
55
+
56
+ export { FOLLOW_ON_INPUTS, OP_WAVES_COMMIT_ENV, OP_WAVES_WAVE_ENV } from "./follow-on-run";
57
+
58
+ /** The variable the rest wave's record job is told how to start a follow-on in: `{ "forge": ..., "workflow": ... }` as JSON. */
59
+ export const OP_WAVES_FOLLOW_ON_ENV = "CHANT_WAVES_FOLLOW_ON";
60
+
61
+ /**
62
+ * The variable the rest wave's record step hands the token that starts the
63
+ * follow-on in (GitHub: the job's token; Forgejo: the `CHANT_FORGE_TOKEN`
64
+ * secret), so no other line of the job reads it as the forge's token.
65
+ * Unset (GitLab), the record job reads {@link FORGE_TOKEN_VAR}.
66
+ */
67
+ export const FOLLOW_ON_TOKEN_ENV = "CHANT_WAVES_FOLLOW_ON_TOKEN";
68
+
69
+ /** The secret (Forgejo) or CI/CD variable (GitLab) a token that may start a run is kept in. */
70
+ export const FORGE_TOKEN_VAR = "CHANT_FORGE_TOKEN";
71
+
72
+ /** How the rest wave's record job starts a follow-on. */
73
+ export interface OpWavesFollowOnTarget {
74
+ forge: "github" | "forgejo" | "gitlab";
75
+ /** GitHub and Forgejo: the waves workflow's file name, which the record job dispatches. */
76
+ workflow?: string;
77
+ }
78
+
79
+ /** The rest wave's number when the spec's last wave has `rest`, else undefined. */
80
+ export function opWavesRestWave(spec: Pick<OpWavesSpec, "waves">): number | undefined {
81
+ return spec.waves[spec.waves.length - 1]?.rest === true ? spec.waves.length : undefined;
82
+ }
83
+
84
+ /**
85
+ * A path of the rest wave's (`wave-N.json`, `wave-N/`, `outcome-wave-N-apply.json`,
86
+ * `run-record-wave-N.json`) with its wave number put as `token`, the form
87
+ * the forge reads `CHANT_WAVES_WAVE` in at that place.
88
+ */
89
+ export function opWaveRestPath(path: string, wave: number, token: string): string {
90
+ return path.replace(new RegExp(`(^|[/-])wave-${wave}(?=[./-]|$)`, "g"), (_m, lead: string) => `${lead}wave-${token}`);
91
+ }
92
+
93
+ /** The value of {@link OP_WAVES_FOLLOW_ON_ENV} for a forge. */
94
+ export function opWavesFollowOnEnv(target: OpWavesFollowOnTarget): string {
95
+ return JSON.stringify(target);
96
+ }
97
+
98
+ /** Read {@link OP_WAVES_FOLLOW_ON_ENV}; undefined when unset, and a value that is no target throws. */
99
+ export function parseFollowOnTarget(raw: string | undefined): OpWavesFollowOnTarget | undefined {
100
+ if (raw === undefined || raw.trim() === "") return undefined;
101
+ let v: unknown;
102
+ try {
103
+ v = JSON.parse(raw);
104
+ } catch {
105
+ throw new Error(`${OP_WAVES_FOLLOW_ON_ENV} is not JSON`);
106
+ }
107
+ const o = v as Partial<OpWavesFollowOnTarget> | null;
108
+ if (!o || (o.forge !== "github" && o.forge !== "forgejo" && o.forge !== "gitlab")) throw new Error(`${OP_WAVES_FOLLOW_ON_ENV}.forge is github, forgejo or gitlab`);
109
+ if (o.forge !== "gitlab" && (typeof o.workflow !== "string" || !/^[A-Za-z0-9._-]+\.ya?ml$/.test(o.workflow))) throw new Error(`${OP_WAVES_FOLLOW_ON_ENV}.workflow names the waves workflow's file`);
110
+ return { forge: o.forge, ...(o.workflow ? { workflow: o.workflow } : {}) };
111
+ }
112
+
113
+ /**
114
+ * The lines every job of the rest wave runs after its checkout (#4992): in
115
+ * a push's follow-on, stop (exit 9, `misplaced`) unless the commit it goes
116
+ * on with is on the branch the run checked out, then check that commit
117
+ * out. Nothing in any other run. The guard checked it is a commit sha.
118
+ */
119
+ export function followOnCommitScript(product: string): string {
120
+ return [
121
+ `if [ -n "\${${OP_WAVES_COMMIT_ENV}:-}" ]; then`,
122
+ ` git merge-base --is-ancestor "$${OP_WAVES_COMMIT_ENV}" HEAD || { echo "${product}: the follow-on's commit $(printf %.12s "$${OP_WAVES_COMMIT_ENV}") is not on the branch this run checked out, so nothing is applied" >&2; exit ${CHANT_EXIT.misplaced}; }`,
123
+ ` git checkout -q --detach "$${OP_WAVES_COMMIT_ENV}" || exit 1`,
124
+ "fi",
125
+ ].join("\n");
126
+ }
127
+
128
+ /**
129
+ * The first line of the control dispatch's script in a pipeline with a rest
130
+ * wave (#4992): `chant run wave --waves-outcome` adds up nothing for a run
131
+ * that started a follow-on, and the dispatch sends nothing then; the last
132
+ * run of the waves sends the outcome.
133
+ */
134
+ export function followOnControlSkip(product: string): string {
135
+ return `if [ -z "\${${CONTROL_OUTCOME_VAR}:-}" ]; then echo "${product}: this run started a follow-on run for the next wave, which tells the control repo how the waves ended"; exit 0; fi`;
136
+ }
137
+
138
+ /** The note the rest wave's record job leaves in the outcome directory. */
139
+ export const OP_WAVE_REST_NOTE = "rest-wave.json";
140
+
141
+ /** What the note holds. */
142
+ export interface OpWaveRestNote {
143
+ version: 1;
144
+ op: string;
145
+ /** The wave the run's rest wave jobs ran. */
146
+ wave: number;
147
+ /** How many waves the plan found. */
148
+ found: number;
149
+ /** The wave of the follow-on the record job started. Absent: this run is the last. */
150
+ next?: number;
151
+ /** A follow-on's: the outcomes of the chain's earlier runs, by member (#4998), from the record that started it. */
152
+ chain?: Record<string, FollowOnOutcome>;
153
+ }
154
+
155
+ /** A member's outcome as the jobs after the waves add it up: `earlierWaveOutcomes`' entries. */
156
+ export type ChainedOutcome = WaveMemberOutcome & { wave: number; at: string };
157
+
158
+ /** The outcomes of a chain's runs: a valid entry of each, the rest dropped. */
159
+ function chainOf(v: unknown): Record<string, FollowOnOutcome> | undefined {
160
+ if (!v || typeof v !== "object") return undefined;
161
+ const out: Record<string, FollowOnOutcome> = {};
162
+ for (const [member, o] of Object.entries(v as Record<string, Partial<FollowOnOutcome> | null>)) {
163
+ if (!o || typeof o.status !== "string" || !Number.isInteger(o.wave) || typeof o.at !== "string" || (o.by !== undefined && typeof o.by !== "string")) continue;
164
+ out[member] = { status: o.status, ...(o.by !== undefined ? { by: o.by } : {}), wave: o.wave!, at: o.at };
165
+ }
166
+ return Object.keys(out).length > 0 ? out : undefined;
167
+ }
168
+
169
+ /**
170
+ * The outcomes the jobs after the waves add up (#4998): this run's, from
171
+ * its outcome files (`members`, `earlierWaveOutcomes` below wave `before`),
172
+ * with the chain's earlier runs' from the rest wave's note. A member's
173
+ * newer outcome wins. With no note, or one with no chain, `members`.
174
+ */
175
+ export function withChainOutcomes(members: ReadonlyMap<string, ChainedOutcome>, note: OpWaveRestNote | undefined, before: number = Number.MAX_SAFE_INTEGER): Map<string, ChainedOutcome> {
176
+ const out = new Map<string, ChainedOutcome>();
177
+ for (const [member, o] of Object.entries(note?.chain ?? {})) if (o.wave < before) out.set(member, o as ChainedOutcome);
178
+ for (const [member, o] of members) {
179
+ const had = out.get(member);
180
+ if (!had || had.at <= o.at) out.set(member, o);
181
+ }
182
+ return out;
183
+ }
184
+
185
+ /** Where the record job writes the note. */
186
+ export function opWaveRestNotePath(spec: Pick<OpWavesSpec, "name">): string {
187
+ return `${opWaveOutcomeDir(spec)}/${OP_WAVE_REST_NOTE}`;
188
+ }
189
+
190
+ /** The note under `dirs` (downloaded artifacts keep it in a directory of their own, four deep at most), the newest wave's when there are more. */
191
+ export function readOpWaveRestNote(dirs: readonly string[]): OpWaveRestNote | undefined {
192
+ let found: OpWaveRestNote | undefined;
193
+ const walk = (dir: string, depth: number): void => {
194
+ let names: string[];
195
+ try {
196
+ names = readdirSync(dir);
197
+ } catch {
198
+ return;
199
+ }
200
+ for (const name of names) {
201
+ const abs = join(dir, name);
202
+ let isDir = false;
203
+ try {
204
+ isDir = statSync(abs).isDirectory();
205
+ } catch {
206
+ continue;
207
+ }
208
+ if (isDir) {
209
+ if (depth < 4) walk(abs, depth + 1);
210
+ continue;
211
+ }
212
+ if (name !== OP_WAVE_REST_NOTE) continue;
213
+ let v: Partial<OpWaveRestNote> | null;
214
+ try {
215
+ v = JSON.parse(readFileSync(abs, "utf-8")) as Partial<OpWaveRestNote> | null;
216
+ } catch {
217
+ continue;
218
+ }
219
+ if (!v || v.version !== 1 || typeof v.op !== "string" || !Number.isInteger(v.wave) || !Number.isInteger(v.found)) continue;
220
+ if (v.next !== undefined && (!Number.isInteger(v.next) || v.next !== v.wave! + 1)) continue;
221
+ const chain = chainOf(v.chain);
222
+ if (!found || v.wave! > found.wave) found = { version: 1, op: v.op, wave: v.wave!, found: v.found!, ...(v.next !== undefined ? { next: v.next } : {}), ...(chain ? { chain } : {}) };
223
+ }
224
+ };
225
+ for (const d of dirs) if (existsSync(d)) walk(d, 0);
226
+ return found;
227
+ }
228
+
229
+ /** A started run, as the forge links it. */
230
+ export interface StartedFollowOn {
231
+ url?: string;
232
+ }
233
+
234
+ /** What {@link followOnAfterRecord} takes. */
235
+ export interface FollowOnAfterRecordInput {
236
+ spec: OpWavesSpec;
237
+ /** The wave the record job wrote. */
238
+ wave: number;
239
+ /** The record job's exit. */
240
+ exitCode: number;
241
+ cwd?: string;
242
+ env?: Record<string, string | undefined>;
243
+ /** Start the follow-on; default the forge's API ({@link startFollowOn}). */
244
+ start?: (target: OpWavesFollowOnTarget, wave: number, env: Record<string, string | undefined>) => Promise<StartedFollowOn>;
245
+ /** Where the chain's record is read and written (#4998); default `chant/lifecycle` through the checkout's git. */
246
+ chain?: FollowOnChainPort;
247
+ now?: () => Date;
248
+ log?: (line: string) => void;
249
+ }
250
+
251
+ /** What a chain is keyed by and what its record names (#4998): before merge the decision, for a push the commit the run applied. */
252
+ export function followOnChainOf(forge: OpWavesFollowOnTarget["forge"], env: Record<string, string | undefined>): Pick<FollowOnRecord, "key" | "pr" | "digest" | "commit"> | undefined {
253
+ const run = beforeMergeRunFromEnv(env);
254
+ if (isBeforeMergeRun(run)) return run.head ? { key: run.head, ...(run.pr && /^[1-9][0-9]*$/.test(run.pr) ? { pr: Number(run.pr) } : {}), ...(run.digest ? { digest: run.digest } : {}) } : undefined;
255
+ const commit = env[OP_WAVES_COMMIT_ENV]?.trim() || (forge === "gitlab" ? env.CI_COMMIT_SHA : env.GITHUB_SHA)?.trim();
256
+ return commit ? { key: commit, commit } : undefined;
257
+ }
258
+
259
+ /** The run a record job runs in, as the record names it: GitHub and Forgejo `GITHUB_RUN_ID`, GitLab `CI_PIPELINE_ID`, and its page when the forge says. */
260
+ function runOf(forge: OpWavesFollowOnTarget["forge"], env: Record<string, string | undefined>): Pick<FollowOnRecord, "run" | "url"> {
261
+ if (forge === "gitlab") {
262
+ const url = env.CI_PIPELINE_URL?.trim();
263
+ return { run: env.CI_PIPELINE_ID?.trim() || "unknown", ...(url && /^https?:\/\//.test(url) ? { url } : {}) };
264
+ }
265
+ const id = env.GITHUB_RUN_ID?.trim();
266
+ const server = env.GITHUB_SERVER_URL?.trim().replace(/\/+$/, "");
267
+ const repo = env.GITHUB_REPOSITORY?.trim();
268
+ return { run: id || "unknown", ...(forge === "github" && id && server && repo && /^https?:\/\//.test(server) ? { url: `${server}/${repo}/actions/runs/${id}` } : {}) };
269
+ }
270
+
271
+ /**
272
+ * After the rest wave's record job wrote its run records (#4992): leave the
273
+ * note, and when the run applied every wave it ran and the plan found more,
274
+ * start the follow-on for the next one. Resolves to the job's exit: the
275
+ * record job's own, or failed when the follow-on could not be started (the
276
+ * note still says it was to be, so nothing after the run takes it for the
277
+ * last). A job that is not the rest wave's, or whose pipeline sets no
278
+ * {@link OP_WAVES_FOLLOW_ON_ENV}, is left as it is.
279
+ */
280
+ export async function followOnAfterRecord(i: FollowOnAfterRecordInput): Promise<number> {
281
+ const env = i.env ?? process.env;
282
+ const log = i.log ?? ((line: string) => console.error(line));
283
+ const rest = opWavesRestWave(i.spec);
284
+ if (rest === undefined || i.wave < rest) return i.exitCode;
285
+ const target = parseFollowOnTarget(env[OP_WAVES_FOLLOW_ON_ENV]);
286
+ if (!target) return i.exitCode;
287
+ const cwd = i.cwd ?? process.cwd();
288
+ let found: number | undefined;
289
+ try {
290
+ const decision = JSON.parse(readFileSync(join(cwd, opWaveRecordPath(i.spec, i.wave)), "utf-8")) as { discovered?: { waves?: unknown } };
291
+ const w = decision?.discovered?.waves;
292
+ if (Number.isInteger(w)) found = w as number;
293
+ } catch {
294
+ found = undefined;
295
+ }
296
+ const outcomeDir = opWaveOutcomeDir(i.spec);
297
+ const dir = isAbsolute(outcomeDir) ? outcomeDir : join(cwd, outcomeDir);
298
+ const { earlierWaveOutcomes } = await import("../wave-blocked");
299
+ const { applyOutcomeOf } = await import("../control/applied");
300
+ const own = earlierWaveOutcomes([dir], Number.MAX_SAFE_INTEGER).members;
301
+ const summary = applyOutcomeOf(own);
302
+ const through = summary.outcome === "applied" || summary.outcome === "unchanged";
303
+ const next = i.exitCode === CHANT_EXIT.done && through && found !== undefined && found > i.wave ? i.wave + 1 : undefined;
304
+ // #4998: a follow-on's record job reads the record that started it, for the outcomes of the chain's earlier runs.
305
+ const chainKey = followOnChainOf(target.forge, env);
306
+ let port = i.chain;
307
+ const chainPort = async (): Promise<FollowOnChainPort> => (port ??= (await import("./follow-on-store")).gitFollowOnChainPort({ cwd }));
308
+ let chain: Record<string, FollowOnOutcome> = {};
309
+ if (i.wave > rest && chainKey) {
310
+ try {
311
+ const started = await (await chainPort()).read(i.spec.name, chainKey.key);
312
+ if (started && started.wave === i.wave) chain = started.outcomes;
313
+ else log(`wave ${i.wave}: no record on chant/lifecycle started this wave for ${chainKey.key.slice(0, 12)}, so the outcomes of the chain's earlier runs are not added up`);
314
+ } catch (err) {
315
+ log(`wave ${i.wave}: the record that started this run could not be read (${(err instanceof Error ? err.message : String(err)).split("\n")[0]}), so the outcomes of the chain's earlier runs are not added up`);
316
+ }
317
+ }
318
+ mkdirSync(dir, { recursive: true });
319
+ const note: OpWaveRestNote = { version: 1, op: i.spec.name, wave: i.wave, found: found ?? i.wave, ...(next !== undefined ? { next } : {}), ...(Object.keys(chain).length > 0 ? { chain } : {}) };
320
+ writeFileSync(join(dir, OP_WAVE_REST_NOTE), JSON.stringify(note, null, 2) + "\n");
321
+ if (next === undefined) {
322
+ if (found !== undefined && found > i.wave) log(`wave ${i.wave}: the plan found ${found} waves, and this run ended ${summary.outcome} (exit ${i.exitCode}), so no run starts wave ${i.wave + 1}`);
323
+ return i.exitCode;
324
+ }
325
+ // #4998: record the follow-on before it starts: its guard admits the wave this record names, and no other.
326
+ try {
327
+ if (!chainKey) throw new Error("the run names no decided head and no commit to go on with");
328
+ const outcomes: Record<string, FollowOnOutcome> = {};
329
+ for (const [member, o] of withChainOutcomes(own, note)) outcomes[member] = { status: o.status, ...(o.by !== undefined ? { by: o.by } : {}), wave: o.wave, at: o.at };
330
+ const record: FollowOnRecord = { ...chainKey, wave: next, from: i.wave, ...runOf(target.forge, env), at: (i.now ?? (() => new Date()))().toISOString(), outcomes };
331
+ await (await chainPort()).write(i.spec.name, record);
332
+ } catch (err) {
333
+ const message = err instanceof Error ? err.message : String(err);
334
+ log(`wave ${i.wave} applied, and the follow-on for wave ${next} could not be recorded on chant/lifecycle (${message.split("\n")[0]}), so none is started: its guard would refuse it`);
335
+ return CHANT_EXIT.failed;
336
+ }
337
+ try {
338
+ const started = await (i.start ?? startFollowOn)(target, next, env);
339
+ log(`wave ${i.wave} applied; the plan found ${found} waves, so a follow-on run${started.url ? ` (${started.url})` : ""} goes on with wave ${next}`);
340
+ return i.exitCode;
341
+ } catch (err) {
342
+ const message = err instanceof Error ? err.message : String(err);
343
+ log(`wave ${i.wave} applied, and the follow-on run for wave ${next} could not be started (${message.split("\n")[0]}); start the waves workflow by hand with ${OP_WAVES_WAVE_ENV}=${next} and this run's other inputs`);
344
+ return CHANT_EXIT.failed;
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Start the follow-on run for wave `wave` through the forge's API: the
350
+ * values the run carries come from the record job's own variables (the
351
+ * decision, or the commit the run applied).
352
+ */
353
+ export async function startFollowOn(target: OpWavesFollowOnTarget, wave: number, env: Record<string, string | undefined>, fetchFn: typeof fetch = fetch): Promise<StartedFollowOn> {
354
+ const run = beforeMergeRunFromEnv(env);
355
+ const bm = isBeforeMergeRun(run);
356
+ const commit = bm ? undefined : followOnChainOf(target.forge, env)?.commit;
357
+ if (!bm && !commit) throw new Error("the run names no commit to go on with");
358
+ const values: Record<string, string> = bm ? { pr: run.pr!, head: run.head!, digest: run.digest!, wave: String(wave) } : { wave: String(wave), commit: commit! };
359
+ const { forgeTargetFromEnv } = await import("../pr-forge");
360
+ const tokenEnv = [FOLLOW_ON_TOKEN_ENV, FORGE_TOKEN_VAR].find((n) => env[n]?.trim());
361
+ const t = forgeTargetFromEnv(env, { kind: target.forge, ...(tokenEnv ? { tokenEnv } : {}) });
362
+ const post = async (url: string, headers: Record<string, string>, body: unknown): Promise<unknown> => {
363
+ const res = await fetchFn(url, { method: "POST", headers: { ...headers, Accept: "application/json", "Content-Type": "application/json" }, body: JSON.stringify(body) });
364
+ const text = await res.text();
365
+ if (!res.ok) throw new Error(`POST ${url} answered ${res.status}${text ? `: ${text.slice(0, 200)}` : ""}`);
366
+ try {
367
+ return text ? JSON.parse(text) : null;
368
+ } catch {
369
+ return null;
370
+ }
371
+ };
372
+ if (target.forge === "gitlab") {
373
+ const ref = env.CI_COMMIT_REF_NAME;
374
+ if (!ref) throw new Error("the job names no branch (no CI_COMMIT_REF_NAME)");
375
+ const names: Record<string, string> = { pr: "CHANT_BEFORE_MERGE_PR", head: "CHANT_BEFORE_MERGE_HEAD", digest: "CHANT_BEFORE_MERGE_DIGEST", wave: OP_WAVES_WAVE_ENV, commit: OP_WAVES_COMMIT_ENV };
376
+ const variables = Object.entries(values).map(([k, value]) => ({ key: names[k]!, value, variable_type: "env_var" }));
377
+ const project = encodeURIComponent(decodeURIComponent(t.repo));
378
+ const v = (await post(`${t.apiBase}/projects/${project}/pipeline`, { [t.tokenHeader ?? "PRIVATE-TOKEN"]: t.token }, { ref, variables })) as { web_url?: unknown } | null;
379
+ return typeof v?.web_url === "string" && /^https?:\/\//.test(v.web_url) ? { url: v.web_url } : {};
380
+ }
381
+ const ref = env.GITHUB_REF_NAME?.trim() || env.GITHUB_REF?.replace(/^refs\/heads\//, "");
382
+ if (!ref) throw new Error("the job names no branch (no GITHUB_REF_NAME)");
383
+ await post(`${t.apiBase}/repos/${t.repo}/actions/workflows/${encodeURIComponent(target.workflow!)}/dispatches`, { Authorization: `token ${t.token}` }, { ref, inputs: values });
384
+ return {};
385
+ }
@@ -23,7 +23,6 @@ import {
23
23
  renderOpWavesSpecFile,
24
24
  RUNTIME_WAVES_SPEC_FLOOR,
25
25
  } from "./op-waves-render-spec";
26
- import { NEXT_RELEASE } from "../release-floors";
27
26
  import type { OpWavesSpec } from "./op-waves";
28
27
  import { parseArgs } from "../cli/parse-args";
29
28
  import { runOpWaveCommand } from "../cli/handlers/run-wave";
@@ -138,7 +137,7 @@ describe("a waves spec the job renders at run time (#4982)", () => {
138
137
  expect(opWavesRenderSpecEnv({ command: ["terragucci", "waves-spec"] })).toEqual({ CHANT_WAVES_SPEC_COMMAND: '["terragucci","waves-spec"]' });
139
138
  expect(opWavesRenderSpecEnv(undefined)).toEqual({});
140
139
  expect(() => opWavesRenderSpecEnv({ command: [] })).toThrow(/renderSpec.command must be/);
141
- expect(RUNTIME_WAVES_SPEC_FLOOR).toBe(NEXT_RELEASE);
140
+ expect(RUNTIME_WAVES_SPEC_FLOOR).toBe("0.124.89");
142
141
  });
143
142
 
144
143
  test("chant run wave --render-spec writes the rendered spec, and a share it does not have exits 0", async () => {