@intentius/chant 0.124.68 → 0.124.70

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 (43) hide show
  1. package/bin/chant +96 -39
  2. package/dist/cli/main.d.ts.map +1 -1
  3. package/dist/cli/main.js +6 -0
  4. package/dist/cli/parse-args.d.ts.map +1 -1
  5. package/dist/cli/parse-args.js +15 -0
  6. package/dist/cli/registry.d.ts +4 -0
  7. package/dist/cli/registry.d.ts.map +1 -1
  8. package/dist/components/unchanged-inputs.d.ts.map +1 -1
  9. package/dist/components/unchanged-inputs.js +5 -3
  10. package/dist/release-floors.d.ts +1 -1
  11. package/dist/release-floors.js +1 -1
  12. package/dist/wave-runtime.mjs +290 -290
  13. package/dist/workspace/ci-green-cli.d.ts +7 -3
  14. package/dist/workspace/ci-green-cli.d.ts.map +1 -1
  15. package/dist/workspace/ci-green-cli.js +29 -8
  16. package/dist/workspace/ci-green-forge.d.ts +29 -0
  17. package/dist/workspace/ci-green-forge.d.ts.map +1 -1
  18. package/dist/workspace/ci-green-forge.js +50 -0
  19. package/dist/workspace/ci-green-workflow.d.ts +9 -1
  20. package/dist/workspace/ci-green-workflow.d.ts.map +1 -1
  21. package/dist/workspace/ci-green-workflow.js +15 -3
  22. package/dist/workspace/ci-green.d.ts +35 -1
  23. package/dist/workspace/ci-green.d.ts.map +1 -1
  24. package/dist/workspace/ci-green.js +123 -4
  25. package/dist/workspace/declaration.d.ts +14 -2
  26. package/dist/workspace/declaration.d.ts.map +1 -1
  27. package/dist/workspace/declaration.js +14 -1
  28. package/dist/workspace/declaration.schema.json +17 -2
  29. package/package.json +1 -1
  30. package/src/cli/bin-launcher.test.ts +201 -0
  31. package/src/cli/main.ts +6 -0
  32. package/src/cli/parse-args.ts +11 -0
  33. package/src/cli/registry.ts +4 -0
  34. package/src/components/unchanged-inputs.test.ts +15 -0
  35. package/src/components/unchanged-inputs.ts +5 -3
  36. package/src/release-floors.ts +1 -1
  37. package/src/workspace/ci-green-cli.ts +27 -8
  38. package/src/workspace/ci-green-forge.ts +70 -0
  39. package/src/workspace/ci-green-workflow.ts +15 -3
  40. package/src/workspace/ci-green.ts +131 -5
  41. package/src/workspace/ci-verdict-retries.test.ts +376 -0
  42. package/src/workspace/declaration.schema.json +17 -2
  43. package/src/workspace/declaration.ts +28 -4
@@ -0,0 +1,376 @@
1
+ /**
2
+ * #4729 — `chant ci verdict --wait` re-runs a failed job's workflow run
3
+ * before it fails a merge group, looks past a forge 5xx, and gives up before
4
+ * the job's own timeout. With retries 0 it fails at the first failed job, as
5
+ * #4644 did.
6
+ */
7
+
8
+ import { afterAll, afterEach, describe, expect, test, vi } from "vitest";
9
+ import { ForgeApiError, type ForgeFetch } from "../pr-forge";
10
+ import { parseArgs } from "../cli/parse-args";
11
+ import { parseYAMLDocument } from "../yaml";
12
+ import { cleanScratch, declaration, git, scratchDir, writeFiles } from "./__fixtures__/contract-repo";
13
+ import { CI_VERDICT_BACKOFF_MS, CI_VERDICT_FORGE_ERRORS, CI_VERDICT_INTERVAL_MS, ciVerdict, CiVerdictGaveUpError } from "./ci-green";
14
+ import { runCiVerdict } from "./ci-green-cli";
15
+ import { githubCiForge, isTransientForgeError, type CheckRun, type CiForge, type WorkflowRun } from "./ci-green-forge";
16
+ import { MERGE_QUEUE_VERDICT_JOB, MERGE_QUEUE_VERDICT_TIMEOUT_MINUTES, renderMergeQueueWorkflow } from "./ci-green-workflow";
17
+ import { parseDeclaration } from "./declaration";
18
+
19
+ afterAll(cleanScratch);
20
+ afterEach(() => {
21
+ vi.restoreAllMocks();
22
+ vi.unstubAllEnvs();
23
+ vi.unstubAllGlobals();
24
+ vi.useRealTimers();
25
+ });
26
+
27
+ const SHA = "a".repeat(40);
28
+ const run = (id: number, name: string, conclusion: string | null, status = "completed"): CheckRun => ({ id, name, status, conclusion: status === "completed" ? conclusion : null, completedAt: null, url: null });
29
+ const wfRun = (attempt: number, status = "completed", conclusion: string | null = status === "completed" ? "failure" : null): WorkflowRun => ({ id: 70, name: "check", attempt, status, conclusion, url: "https://github.com/acme/app/actions/runs/70" });
30
+ const http = (status: number) => new ForgeApiError("GET", `https://api.github.com/repos/acme/app/commits/${SHA}/check-runs`, status, "");
31
+
32
+ function workspace(mergeQueue: unknown): string {
33
+ const root = scratchDir("chant-verdict-retries-");
34
+ git(root, "init", "-q");
35
+ writeFiles(root, { "chant.workspace.json": declaration([], { ci: { green: { branch: "main", phases: { lint: ["lint"], test: ["test"] }, require: ["lint", "test"], mergeQueue } } }) });
36
+ return root;
37
+ }
38
+
39
+ /** A forge that answers each read from a script (the last answer repeats), and a clock that only `sleep` moves. */
40
+ function scripted(checks: (CheckRun[] | Error)[], runs: (WorkflowRun | null | Error)[] = []) {
41
+ let t = 0;
42
+ const log = { reads: 0, runReads: 0, reruns: [] as number[], sleeps: [] as number[], notes: [] as string[] };
43
+ const next = <T>(list: T[], i: number): T => list[Math.min(i, list.length - 1)]!;
44
+ const forge: CiForge = {
45
+ kind: "github",
46
+ async checkRuns() {
47
+ const a = next(checks, log.reads++);
48
+ if (a instanceof Error) throw a;
49
+ return a;
50
+ },
51
+ async workflowRun() {
52
+ const a = next(runs, log.runReads++);
53
+ if (a instanceof Error) throw a;
54
+ return a;
55
+ },
56
+ async rerunFailedJobs(id) {
57
+ log.reruns.push(id);
58
+ },
59
+ };
60
+ const clock = {
61
+ now: () => t,
62
+ sleep: async (ms: number) => {
63
+ log.sleeps.push(ms);
64
+ t += ms;
65
+ },
66
+ onNote: (line: string) => void log.notes.push(line),
67
+ };
68
+ return { forge, clock, log };
69
+ }
70
+
71
+ const LINT = run(1, "lint", "success");
72
+
73
+ describe("ci verdict --wait with retries (#4729)", () => {
74
+ test("a flake that passes on its re-run passes, and its run is re-run once", async () => {
75
+ const { forge, clock, log } = scripted(
76
+ [[LINT, run(2, "test", "failure")], [LINT, run(2, "test", "failure")], [LINT, run(2, "test", "failure"), run(3, "test", null, "in_progress")], [LINT, run(2, "test", "failure"), run(3, "test", "success")]],
77
+ [wfRun(1), wfRun(2, "queued")],
78
+ );
79
+ const out = await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock });
80
+ expect(out.verdict.verdict).toBe("pass");
81
+ expect(out.repeated).toBeUndefined();
82
+ expect(log.reruns).toEqual([70]);
83
+ expect(log.notes[0]).toBe("test (phase test) in workflow run check https://github.com/acme/app/actions/runs/70 failed on attempt 1 of 2; re-running its failed jobs");
84
+ });
85
+
86
+ test("a failure that repeats fails, naming the phase, the job and the workflow run", async () => {
87
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", "failure")], [LINT, run(2, "test", "failure"), run(3, "test", "failure")]], [wfRun(1), wfRun(2)]);
88
+ const out = await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock });
89
+ expect(out.verdict.verdict).toBe("fail");
90
+ expect(out.repeated).toMatchObject({ phase: "test", run: { id: 3, name: "test" }, workflowRun: { id: 70, name: "check", attempt: 2 } });
91
+ expect(log.reruns).toEqual([70]);
92
+ });
93
+
94
+ test("retries 2 re-runs twice and fails on the third attempt", async () => {
95
+ const failed = [LINT, run(2, "test", "failure")];
96
+ const { forge, clock, log } = scripted([failed, failed, failed], [wfRun(1), wfRun(2), wfRun(3)]);
97
+ const out = await ciVerdict({ cwd: workspace({ retries: 2 }), sha: SHA, forge, wait: true, ...clock });
98
+ expect(out.repeated?.workflowRun.attempt).toBe(3);
99
+ expect(log.reruns).toEqual([70, 70]);
100
+ });
101
+
102
+ test("a failed job waits for its workflow run to finish before the re-run", async () => {
103
+ const failed = [LINT, run(2, "test", "failure")];
104
+ const { forge, clock, log } = scripted([failed, failed, failed, [LINT, run(3, "test", "success")]], [wfRun(1, "in_progress"), wfRun(1, "in_progress"), wfRun(1)]);
105
+ const out = await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock });
106
+ expect(out.verdict.verdict).toBe("pass");
107
+ expect(log.reruns).toEqual([70]);
108
+ // Said once, however long the run takes.
109
+ expect(log.notes.filter((n) => n.includes("waiting for workflow run check to finish"))).toHaveLength(1);
110
+ });
111
+
112
+ test("a re-run GitHub hasn't started yet isn't asked for twice", async () => {
113
+ const failed = [LINT, run(2, "test", "failure")];
114
+ const { forge, clock, log } = scripted([failed, failed, failed, [LINT, run(3, "test", "success")]], [wfRun(1), wfRun(1), wfRun(2, "queued")]);
115
+ expect((await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock })).verdict.verdict).toBe("pass");
116
+ expect(log.reruns).toEqual([70]);
117
+ });
118
+
119
+ test("--retries overrides the setting", async () => {
120
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", "failure")], [LINT, run(3, "test", "success")]], [wfRun(1)]);
121
+ expect((await ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, retries: 1, ...clock })).verdict.verdict).toBe("pass");
122
+ expect(log.reruns).toEqual([70]);
123
+ });
124
+
125
+ test("retries 0 fails at the first failed job on the first read and asks nothing more, as before", async () => {
126
+ for (const mq of [true, { retries: 0 }]) {
127
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", "failure")]], [wfRun(1)]);
128
+ const out = await ciVerdict({ cwd: workspace(mq), sha: SHA, forge, wait: true, ...clock });
129
+ expect(out.verdict).toMatchObject({ verdict: "fail", failure: { phase: "test", run: { name: "test" } } });
130
+ expect(out.repeated).toBeUndefined();
131
+ expect(log).toMatchObject({ reads: 1, runReads: 0, reruns: [], sleeps: [], notes: [] });
132
+ }
133
+ // And --retries 0 over a setting of 1.
134
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", "failure")]], [wfRun(1)]);
135
+ expect((await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, retries: 0, ...clock })).verdict.verdict).toBe("fail");
136
+ expect(log.reruns).toEqual([]);
137
+ });
138
+
139
+ test("without --wait it reads once and re-runs nothing", async () => {
140
+ const { forge, log } = scripted([[LINT, run(2, "test", "failure")]], [wfRun(1)]);
141
+ expect((await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge })).verdict.verdict).toBe("fail");
142
+ expect(log).toMatchObject({ reads: 1, runReads: 0, reruns: [] });
143
+ });
144
+
145
+ test("a check run that is no Actions job fails at once: there is nothing to re-run", async () => {
146
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", "failure")]], [null]);
147
+ const out = await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock });
148
+ expect(out.verdict.verdict).toBe("fail");
149
+ expect(out.repeated).toBeUndefined();
150
+ expect(log.reruns).toEqual([]);
151
+ });
152
+
153
+ test("a refused re-run says the job needs actions: write", async () => {
154
+ const { forge, clock } = scripted([[LINT, run(2, "test", "failure")]], [wfRun(1)]);
155
+ forge.rerunFailedJobs = async () => {
156
+ throw new ForgeApiError("POST", "https://api.github.com/repos/acme/app/actions/runs/70/rerun-failed-jobs", 403, "Resource not accessible by integration");
157
+ };
158
+ await expect(ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock })).rejects.toThrow(/could not re-run the failed jobs of workflow run check \(70\).*needs actions: write/);
159
+ });
160
+ });
161
+
162
+ describe("ci verdict --wait past forge errors (#4729)", () => {
163
+ test("a 5xx that clears passes, after a backoff that doubles", async () => {
164
+ const { forge, clock, log } = scripted([http(502), http(503), [LINT, run(2, "test", "success")]]);
165
+ const out = await ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, ...clock });
166
+ expect(out.verdict.verdict).toBe("pass");
167
+ expect(log.sleeps).toEqual([CI_VERDICT_BACKOFF_MS, 2 * CI_VERDICT_BACKOFF_MS]);
168
+ expect(log.notes[0]).toMatch(/^the forge failed \(GET .* answered 502\); looking again in 15s \(1 of 7 looked past\)$/);
169
+ });
170
+
171
+ test("a request with no answer is looked past too, and a 5xx while reading a workflow run", async () => {
172
+ const failed = [LINT, run(2, "test", "failure")];
173
+ const { forge, clock, log } = scripted([new TypeError("fetch failed"), failed, failed, [LINT, run(3, "test", "success")]], [http(500), wfRun(1)]);
174
+ expect((await ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, ...clock })).verdict.verdict).toBe("pass");
175
+ expect(log.reruns).toEqual([70]);
176
+ });
177
+
178
+ test("errors that are not in a row each start the count again", async () => {
179
+ const pending = [LINT, run(2, "test", null, "in_progress")];
180
+ const answers: (CheckRun[] | Error)[] = [];
181
+ for (let i = 0; i < CI_VERDICT_FORGE_ERRORS + 2; i++) answers.push(http(502), pending);
182
+ answers.push([LINT, run(2, "test", "success")]);
183
+ const { forge, clock } = scripted(answers);
184
+ expect((await ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, ...clock })).verdict.verdict).toBe("pass");
185
+ });
186
+
187
+ test("a 5xx that doesn't clear fails at the bound, saying so", async () => {
188
+ const { forge, clock, log } = scripted([http(502)]);
189
+ const err = await ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, ...clock }).catch((e: unknown) => e);
190
+ expect(err).toBeInstanceOf(CiVerdictGaveUpError);
191
+ expect((err as Error).message).toMatch(new RegExp(`^the forge failed ${CI_VERDICT_FORGE_ERRORS} times in a row, the last: GET .* answered 502$`));
192
+ expect(log.reads).toBe(CI_VERDICT_FORGE_ERRORS);
193
+ expect(log.sleeps).toEqual([15_000, 30_000, 60_000, 120_000, 120_000, 120_000, 120_000]);
194
+ });
195
+
196
+ test("a 4xx is an answer, not a blip: it fails at once, as before", async () => {
197
+ const { forge, clock, log } = scripted([http(404)]);
198
+ await expect(ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, ...clock })).rejects.toBeInstanceOf(ForgeApiError);
199
+ expect(log.reads).toBe(1);
200
+ });
201
+
202
+ test("without --wait a 5xx fails at once, as before", async () => {
203
+ const { forge, log } = scripted([http(502)]);
204
+ await expect(ciVerdict({ cwd: workspace(true), sha: SHA, forge })).rejects.toBeInstanceOf(ForgeApiError);
205
+ expect(log.reads).toBe(1);
206
+ });
207
+ });
208
+
209
+ describe("ci verdict --wait --timeout-minutes (#4729)", () => {
210
+ test("a run that never finishes gives up before the timeout, saying what it waited on", async () => {
211
+ const { forge, clock, log } = scripted([[LINT, run(2, "test", null, "in_progress")]]);
212
+ const err = await ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, timeoutMs: 10 * 60_000, ...clock }).catch((e: unknown) => e);
213
+ expect(err).toBeInstanceOf(CiVerdictGaveUpError);
214
+ expect((err as Error).message).toBe("no verdict after 10 minutes: still waiting on test");
215
+ expect(clock.now()).toBe(10 * 60_000);
216
+ expect(log.sleeps.every((ms) => ms <= CI_VERDICT_INTERVAL_MS)).toBe(true);
217
+ // It looked once more at the deadline before giving up.
218
+ expect(log.reads).toBe(10 * 60_000 / CI_VERDICT_INTERVAL_MS + 1);
219
+ });
220
+
221
+ test("a re-run that never finishes gives up too, naming the run", async () => {
222
+ const failed = [LINT, run(2, "test", "failure")];
223
+ const { forge, clock } = scripted([failed], [wfRun(1), wfRun(2, "in_progress")]);
224
+ await expect(ciVerdict({ cwd: workspace({ retries: 1 }), sha: SHA, forge, wait: true, timeoutMs: 5 * 60_000, ...clock })).rejects.toThrow("no verdict after 5 minutes: waiting for workflow run check to finish");
225
+ });
226
+
227
+ test("a forge that keeps failing gives up at the deadline when that comes first", async () => {
228
+ const { forge, clock } = scripted([http(502)]);
229
+ await expect(ciVerdict({ cwd: workspace(true), sha: SHA, forge, wait: true, timeoutMs: 60_000, ...clock })).rejects.toThrow(/^no verdict after 1 minute: the forge failing: GET .* answered 502$/);
230
+ expect(clock.now()).toBe(60_000);
231
+ });
232
+ });
233
+
234
+ describe("ci.green.mergeQueue's retries (#4729)", () => {
235
+ const green = (mergeQueue: unknown) => parseDeclaration(JSON.stringify({ name: "w", schema: 1, members: [], ci: { green: { branch: "main", phases: { test: ["test"] }, require: ["test"], mergeQueue } } }), "chant.workspace.json").ci!.green!;
236
+
237
+ test("true reads as no retries; the object as its retries; false as no merge queue", () => {
238
+ expect(green(true).mergeQueue).toEqual({ retries: 0 });
239
+ expect(green({ retries: 0 }).mergeQueue).toEqual({ retries: 0 });
240
+ expect(green({ retries: 2 }).mergeQueue).toEqual({ retries: 2 });
241
+ expect(green(false).mergeQueue).toBeUndefined();
242
+ });
243
+
244
+ test("retries is a whole number from 0 to 5, and nothing else is in the object", () => {
245
+ for (const bad of [{ retries: -1 }, { retries: 6 }, { retries: 1.5 }, { retries: "1" }, {}, { retries: 1, extra: true }]) {
246
+ expect(() => green(bad), JSON.stringify(bad)).toThrow();
247
+ }
248
+ });
249
+
250
+ test("ci workflow without retries writes the verdict workflow as before", () => {
251
+ const yaml = renderMergeQueueWorkflow({ green: green(true), chantVersion: "0.124.70", workspaceRoot: "." });
252
+ expect(yaml).toBe(renderMergeQueueWorkflow({ green: green({ retries: 0 }), chantVersion: "0.124.70", workspaceRoot: "." }));
253
+ const q = parseYAMLDocument(yaml) as { permissions: unknown; jobs: Record<string, { "timeout-minutes": number; steps: { run?: string }[] }> };
254
+ expect(q.permissions).toEqual({ contents: "read", checks: "read" });
255
+ expect(q.jobs[MERGE_QUEUE_VERDICT_JOB]!["timeout-minutes"]).toBe(120);
256
+ expect(q.jobs[MERGE_QUEUE_VERDICT_JOB]!.steps.at(-1)!.run).toBe("npx --yes @intentius/chant@0.124.70 ci verdict --wait");
257
+ expect(yaml).not.toContain("actions:");
258
+ expect(yaml).not.toContain("re-run");
259
+ });
260
+
261
+ test("ci workflow with retries gives the job actions: write and a time budget under its timeout", () => {
262
+ const yaml = renderMergeQueueWorkflow({ green: green({ retries: 1 }), chantVersion: "0.124.70", workspaceRoot: "." });
263
+ const q = parseYAMLDocument(yaml) as { permissions: unknown; jobs: Record<string, { "timeout-minutes": number; steps: { run?: string }[] }> };
264
+ expect(q.permissions).toEqual({ contents: "read", checks: "read", actions: "write" });
265
+ const job = q.jobs[MERGE_QUEUE_VERDICT_JOB]!;
266
+ expect(job.steps.at(-1)!.run).toBe(`npx --yes @intentius/chant@0.124.70 ci verdict --wait --timeout-minutes ${MERGE_QUEUE_VERDICT_TIMEOUT_MINUTES}`);
267
+ expect(MERGE_QUEUE_VERDICT_TIMEOUT_MINUTES).toBeLessThan(job["timeout-minutes"]);
268
+ expect(yaml).toContain("# A failed job's workflow run is re-run up to 1 time first (ci.green.mergeQueue.retries).");
269
+ // --chant's command takes the same flag.
270
+ const own = parseYAMLDocument(renderMergeQueueWorkflow({ green: green({ retries: 2 }), chantVersion: "0.124.70", workspaceRoot: ".", chant: "npx chant" })) as typeof q;
271
+ expect(own.jobs[MERGE_QUEUE_VERDICT_JOB]!.steps.at(-1)!.run).toBe(`npx chant ci verdict --wait --timeout-minutes ${MERGE_QUEUE_VERDICT_TIMEOUT_MINUTES}`);
272
+ });
273
+ });
274
+
275
+ describe("the GitHub client's workflow runs (#4729)", () => {
276
+ function github(routes: Record<string, { status: number; body?: unknown }>) {
277
+ const calls: string[] = [];
278
+ const fetch: ForgeFetch = async (url, init) => {
279
+ calls.push(`${init.method} ${url}`);
280
+ const r = routes[`${init.method} ${url}`] ?? { status: 404, body: { message: "Not Found" } };
281
+ return { ok: r.status >= 200 && r.status < 300, status: r.status, text: async () => (r.body === undefined ? "" : JSON.stringify(r.body)) };
282
+ };
283
+ return { forge: githubCiForge({ apiBase: "https://api.github.com", repo: "acme/app", token: "t", fetch }), calls };
284
+ }
285
+ const API = "https://api.github.com/repos/acme/app";
286
+
287
+ test("a job's workflow run, its job read once and the run each time", async () => {
288
+ const { forge, calls } = github({
289
+ [`GET ${API}/actions/jobs/2`]: { status: 200, body: { id: 2, run_id: 70 } },
290
+ [`GET ${API}/actions/runs/70`]: { status: 200, body: { id: 70, name: "check", run_attempt: 2, status: "completed", conclusion: "failure", html_url: "https://github.com/acme/app/actions/runs/70" } },
291
+ });
292
+ const expected = { id: 70, name: "check", attempt: 2, status: "completed", conclusion: "failure", url: "https://github.com/acme/app/actions/runs/70" };
293
+ expect(await forge.workflowRun!(2)).toEqual(expected);
294
+ expect(await forge.workflowRun!(2)).toEqual(expected);
295
+ expect(calls).toEqual([`GET ${API}/actions/jobs/2`, `GET ${API}/actions/runs/70`, `GET ${API}/actions/runs/70`]);
296
+ });
297
+
298
+ test("another app's check run has no workflow run", async () => {
299
+ expect(await github({}).forge.workflowRun!(5)).toBeNull();
300
+ });
301
+
302
+ test("a 5xx is thrown for the verdict to look past", async () => {
303
+ const { forge } = github({ [`GET ${API}/actions/jobs/2`]: { status: 502 } });
304
+ const err = await forge.workflowRun!(2).catch((e: unknown) => e);
305
+ expect(err).toBeInstanceOf(ForgeApiError);
306
+ expect(isTransientForgeError(err)).toBe(true);
307
+ });
308
+
309
+ test("re-running posts rerun-failed-jobs every time it's asked", async () => {
310
+ const { forge, calls } = github({ [`POST ${API}/actions/runs/70/rerun-failed-jobs`]: { status: 201 } });
311
+ await forge.rerunFailedJobs!(70);
312
+ await forge.rerunFailedJobs!(70);
313
+ expect(calls).toEqual([`POST ${API}/actions/runs/70/rerun-failed-jobs`, `POST ${API}/actions/runs/70/rerun-failed-jobs`]);
314
+ });
315
+
316
+ test("which errors say to look again", () => {
317
+ expect([500, 502, 503, 504, 429].map((s) => isTransientForgeError(http(s)))).toEqual([true, true, true, true, true]);
318
+ expect([400, 401, 403, 404, 422].map((s) => isTransientForgeError(http(s)))).toEqual([false, false, false, false, false]);
319
+ expect(isTransientForgeError(new TypeError("fetch failed"))).toBe(true);
320
+ expect(isTransientForgeError(new Error("something else"))).toBe(false);
321
+ });
322
+ });
323
+
324
+ describe("chant ci verdict's flags (#4729)", () => {
325
+ test("--retries and --timeout-minutes parse, and refuse what isn't a number", () => {
326
+ expect(parseArgs(["ci", "verdict", "--wait", "--retries", "2", "--timeout-minutes", "85"])).toMatchObject({ command: "ci", path: "verdict", ciWait: true, ciRetries: 2, ciTimeoutMinutes: 85 });
327
+ expect(() => parseArgs(["ci", "verdict", "--wait", "--retries", "-1"])).toThrow("--retries needs a whole number");
328
+ expect(() => parseArgs(["ci", "verdict", "--wait", "--retries"])).toThrow("--retries needs a whole number");
329
+ expect(() => parseArgs(["ci", "verdict", "--wait", "--timeout-minutes", "0"])).toThrow("--timeout-minutes needs a number of minutes above 0");
330
+ expect(() => parseArgs(["ci", "verdict", "--wait", "--timeout-minutes", "soon"])).toThrow("--timeout-minutes needs a number of minutes above 0");
331
+ });
332
+
333
+ test("they go with --wait", async () => {
334
+ vi.spyOn(process, "cwd").mockReturnValue(workspace(true));
335
+ const errors: string[] = [];
336
+ vi.spyOn(console, "error").mockImplementation((...m: unknown[]) => void errors.push(m.map(String).join(" ")));
337
+ expect(await runCiVerdict({ args: { command: "ci", path: "verdict", ciRetries: 1 } } as never)).toBe(1);
338
+ expect(errors.join("\n")).toContain("--retries and --timeout-minutes go with --wait");
339
+ });
340
+
341
+ test("end to end against a fake GitHub: a failure that repeats exits 1 naming the phase, the job and the run", async () => {
342
+ vi.useFakeTimers({ toFake: ["setTimeout", "Date"] });
343
+ vi.spyOn(process, "cwd").mockReturnValue(workspace({ retries: 1 }));
344
+ vi.stubEnv("GITHUB_REPOSITORY", "acme/app");
345
+ vi.stubEnv("GITHUB_TOKEN", "t");
346
+ vi.stubEnv("CHANT_FORGE_TOKEN", "t");
347
+ vi.stubEnv("GITHUB_API_URL", "https://api.github.com");
348
+ const API = "https://api.github.com/repos/acme/app";
349
+ // GitHub: test fails on attempt 1; after the re-run, attempt 2's test fails too.
350
+ let attempt = 1;
351
+ const posts: string[] = [];
352
+ const checkRun = (id: number, name: string, conclusion: string) => ({ id, name, status: "completed", conclusion, completed_at: "2026-11-06T10:00:00Z", html_url: `https://github.com/acme/app/runs/${id}` });
353
+ const answer = (status: number, body: unknown) => ({ ok: status < 300, status, text: async () => JSON.stringify(body) });
354
+ vi.stubGlobal("fetch", (async (url: string, init: { method: string }) => {
355
+ if (init.method === "POST") {
356
+ posts.push(url);
357
+ attempt = 2;
358
+ return answer(201, {});
359
+ }
360
+ if (url.includes("/check-runs")) {
361
+ const runs = [checkRun(1, "lint", "success"), checkRun(2, "test", "failure"), ...(attempt === 2 ? [checkRun(3, "test", "failure")] : [])];
362
+ return answer(200, { total_count: runs.length, check_runs: runs });
363
+ }
364
+ if (url.startsWith(`${API}/actions/jobs/`)) return answer(200, { run_id: 70 });
365
+ if (url === `${API}/actions/runs/70`) return answer(200, { id: 70, name: "check", run_attempt: attempt, status: "completed", conclusion: "failure", html_url: "https://github.com/acme/app/actions/runs/70" });
366
+ return answer(404, { message: "Not Found" });
367
+ }) as ForgeFetch);
368
+ const errors: string[] = [];
369
+ vi.spyOn(console, "error").mockImplementation((...m: unknown[]) => void errors.push(m.map(String).join(" ")));
370
+ const exit = runCiVerdict({ args: { command: "ci", path: "verdict", commit: SHA, ciWait: true, ciTimeoutMinutes: 115 } } as never);
371
+ await vi.runAllTimersAsync();
372
+ expect(await exit).toBe(1);
373
+ expect(posts).toEqual([`${API}/actions/runs/70/rerun-failed-jobs`]);
374
+ expect(errors.at(-1)).toBe(`${SHA.slice(0, 12)}: phase test failed in test (failure) again on attempt 2 of workflow run check (https://github.com/acme/app/actions/runs/70)`);
375
+ });
376
+ });
@@ -1902,8 +1902,23 @@
1902
1902
  "description": "Added by #4031, read by chant 0.124.28 and newer. The full sha of the first commit on branch whose ci/green and ci/revoked tags must carry a DSSE statement in their annotation, signed by a key .chant/trust.json at base lists under runners for a principal holding the runner role. A tag on a strict first-parent ancestor of this commit counts unsigned, so existing tags keep working; a tag on any other commit, this one included, needs a statement. Position in history decides, never a date, which whoever makes a commit or a tag sets. A reader refuses a sha that names no commit in its clone with declaration-invalid. Absent, no tag needs a statement. Since chant 0.124.32 (#4156) it covers Op run records the same way, when ci.green.source is runs: an unsigned run counts on a strict first-parent ancestor of this commit and needs a statement on any other, ci serve refuses to start without a runner key trusted at the target's head, and ci run and chant run warn about a run they leave unsigned. Absent, an unsigned run counts and nothing asks for a key."
1903
1903
  },
1904
1904
  "mergeQueue": {
1905
- "type": "boolean",
1906
- "description": "The branch takes changes through GitHub's merge queue (#4644). chant ci workflow adds the merge_group trigger (checks_requested) to each workflow holding a phase that sets build, and writes .github/workflows/chant-merge-queue.yml, whose chant-verdict job runs chant ci verdict --wait on the group's commit: it passes when every required phase passed there, judged as the tick judges, so the queue's ruleset requires that one check. The queue lands the commit its group tested, so the group's runs count for it; a later push run's skipped or cancelled attempt of the same job never replaces the group's decided one. A workflow holding a phase without build gets no trigger: add merge_group to it by hand. Written by chant 0.124.61 and newer, so a declaration that sets it sets minReader to 0.124.61 or newer."
1905
+ "oneOf": [
1906
+ { "type": "boolean" },
1907
+ {
1908
+ "type": "object",
1909
+ "additionalProperties": false,
1910
+ "required": ["retries"],
1911
+ "properties": {
1912
+ "retries": {
1913
+ "type": "integer",
1914
+ "minimum": 0,
1915
+ "maximum": 5,
1916
+ "description": "Added by #4729. How many times the verdict re-runs a failed workflow run's failed jobs before the group fails. On a failed required job, chant ci verdict --wait waits for the job's workflow run to finish, re-runs its failed jobs while the run's attempt is no more than retries, and fails when it fails on attempt retries + 1. Above 0, chant ci workflow gives the verdict job actions: write and passes --timeout-minutes so the verdict gives up, with a message, before Actions kills the job. 0 is true's behaviour: the first failed job fails the group. Read by chant 0.124.70 and newer, so a declaration that sets the object form sets minReader to 0.124.70 or newer."
1917
+ }
1918
+ }
1919
+ }
1920
+ ],
1921
+ "description": "The branch takes changes through GitHub's merge queue (#4644). chant ci workflow adds the merge_group trigger (checks_requested) to each workflow holding a phase that sets build, and writes .github/workflows/chant-merge-queue.yml, whose chant-verdict job runs chant ci verdict --wait on the group's commit: it passes when every required phase passed there, judged as the tick judges, so the queue's ruleset requires that one check. The queue lands the commit its group tested, so the group's runs count for it; a later push run's skipped or cancelled attempt of the same job never replaces the group's decided one. A workflow holding a phase without build gets no trigger: add merge_group to it by hand. true is { \"retries\": 0 }. Written by chant 0.124.61 and newer, so a declaration that sets it sets minReader to 0.124.61 or newer."
1907
1922
  },
1908
1923
  "notify": {
1909
1924
  "type": "object",
@@ -295,9 +295,10 @@ export interface CiGreen {
295
295
  * The branch takes changes through GitHub's merge queue (#4644): `ci
296
296
  * workflow` adds the `merge_group` trigger to the workflows holding a
297
297
  * phase that sets `build`, and writes `chant-merge-queue.yml`, whose
298
- * `chant-verdict` job the queue's ruleset requires. Absent when unset.
298
+ * `chant-verdict` job the queue's ruleset requires. Absent when unset;
299
+ * `true` reads as `{ retries: 0 }`.
299
300
  */
300
- mergeQueue?: true;
301
+ mergeQueue?: CiMergeQueue;
301
302
  /**
302
303
  * Known-flaky tests the phases' runners leave out (#4677), each with the
303
304
  * issue tracking its fix and the day it comes back. Absent when the
@@ -307,6 +308,19 @@ export interface CiGreen {
307
308
  pointer: string;
308
309
  }
309
310
 
311
+ /** `ci.green.mergeQueue` (#4644), read from `true` or `{ "retries": <n> }` (#4729). */
312
+ export interface CiMergeQueue {
313
+ /**
314
+ * How many times `ci verdict --wait` re-runs a failed workflow run's
315
+ * failed jobs before the group fails (#4729). 0, the default, fails at the
316
+ * first failed job.
317
+ */
318
+ retries: number;
319
+ }
320
+
321
+ /** The most re-runs `ci.green.mergeQueue.retries` may ask for (#4729). */
322
+ export const MERGE_QUEUE_MAX_RETRIES = 5;
323
+
310
324
  /** The default `ci.green.quarantine.maxDays` (#4677): how far out an entry's `until` may be. */
311
325
  export const DEFAULT_QUARANTINE_MAX_DAYS = 14;
312
326
 
@@ -1455,7 +1469,7 @@ function cloudIdentityOf(raw: CloudIdentityDeclaration, at: (pointer: string, ke
1455
1469
 
1456
1470
  function ciOf(raw: unknown, at: (pointer: string, key?: boolean) => ErrorLocation): CiDeclaration | null {
1457
1471
  if (raw === undefined) return null;
1458
- const block = raw as { reportUrl?: string; workflow?: CiWorkflow; hooks?: unknown; identity?: { cloud?: CloudIdentityDeclaration }; branches?: Record<string, string[]>; labels?: Record<string, string[]>; locks?: CiLocks; lockGrain?: CiLockGrain; policy?: unknown; prEnvironment?: { template: string; ttl?: string; teardown?: { name?: string; command: string[]; lexicons?: string[] } }; green?: { branch: string | string[]; window?: string; phases: Record<string, string[] | { runs: string[]; skipped?: CiSkipped; build?: CiBuild }>; require: string[]; preMerge?: string[]; signedSince?: string; source?: CiSource; lanes?: Record<string, { require: string[] }>; notify?: CiNotify; mergeQueue?: boolean; quarantine?: { maxDays?: number; phases: Record<string, { test: string; file?: string; issue: string; until: string; why: string }[]> } } };
1472
+ const block = raw as { reportUrl?: string; workflow?: CiWorkflow; hooks?: unknown; identity?: { cloud?: CloudIdentityDeclaration }; branches?: Record<string, string[]>; labels?: Record<string, string[]>; locks?: CiLocks; lockGrain?: CiLockGrain; policy?: unknown; prEnvironment?: { template: string; ttl?: string; teardown?: { name?: string; command: string[]; lexicons?: string[] } }; green?: { branch: string | string[]; window?: string; phases: Record<string, string[] | { runs: string[]; skipped?: CiSkipped; build?: CiBuild }>; require: string[]; preMerge?: string[]; signedSince?: string; source?: CiSource; lanes?: Record<string, { require: string[] }>; notify?: CiNotify; mergeQueue?: boolean | { retries?: unknown }; quarantine?: { maxDays?: number; phases: Record<string, { test: string; file?: string; issue: string; until: string; why: string }[]> } } };
1459
1473
  const hooks = ciHooksOf(block.hooks, "/ci/hooks", at);
1460
1474
  const policy = ciPolicyOf(block.policy, at);
1461
1475
  const workflow = {
@@ -1540,8 +1554,18 @@ function ciOf(raw: unknown, at: (pointer: string, key?: boolean) => ErrorLocatio
1540
1554
  }
1541
1555
  }
1542
1556
  const quarantine = g.quarantine !== undefined ? quarantineOf(g.quarantine, phases, at) : undefined;
1557
+ // #4729: true, or { retries }, which the schema has checked; false is no merge queue.
1558
+ let mergeQueue: CiMergeQueue | undefined;
1559
+ if (g.mergeQueue === true) mergeQueue = { retries: 0 };
1560
+ else if (typeof g.mergeQueue === "object" && g.mergeQueue !== null) {
1561
+ const retries = g.mergeQueue.retries ?? 0;
1562
+ if (!(typeof retries === "number" && Number.isInteger(retries) && retries >= 0 && retries <= MERGE_QUEUE_MAX_RETRIES)) {
1563
+ throw new WorkspaceReadError("declaration-invalid", `ci.green.mergeQueue.retries: a whole number from 0 to ${MERGE_QUEUE_MAX_RETRIES}`, at("/ci/green/mergeQueue/retries"));
1564
+ }
1565
+ mergeQueue = { retries };
1566
+ }
1543
1567
  const judged = Array.isArray(g.branch) ? g.branch : [g.branch];
1544
- return { green: { branch: judged[0]!, ...(judged.length > 1 ? { branches: [...judged] } : {}), window: g.window ?? DEFAULT_CI_WINDOW, phases, require: [...g.require], ...(g.preMerge !== undefined ? { preMerge: [...g.preMerge] } : {}), signedSince: g.signedSince ?? null, source: g.source ?? "checks", ...(lanes.length > 0 ? { lanes } : {}), ...(g.notify !== undefined ? { notify: { ...g.notify, ...(g.notify.events ? { events: [...g.notify.events] } : {}) } } : {}), ...(g.mergeQueue === true ? { mergeQueue: true as const } : {}), ...(quarantine ? { quarantine } : {}), pointer: "/ci/green" }, ...workflow, reportUrl };
1568
+ return { green: { branch: judged[0]!, ...(judged.length > 1 ? { branches: [...judged] } : {}), window: g.window ?? DEFAULT_CI_WINDOW, phases, require: [...g.require], ...(g.preMerge !== undefined ? { preMerge: [...g.preMerge] } : {}), signedSince: g.signedSince ?? null, source: g.source ?? "checks", ...(lanes.length > 0 ? { lanes } : {}), ...(g.notify !== undefined ? { notify: { ...g.notify, ...(g.notify.events ? { events: [...g.notify.events] } : {}) } } : {}), ...(mergeQueue ? { mergeQueue } : {}), ...(quarantine ? { quarantine } : {}), pointer: "/ci/green" }, ...workflow, reportUrl };
1545
1569
  }
1546
1570
 
1547
1571
  /** A YYYY-MM-DD that names a real day, as days since 1970-01-01 UTC, or null. */