Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| A run that is still going offers Cancel run, and not the re-runs, on every page of it. Which buttons a run's page shows is worked out in one place (runControls, lib/run-controls.ts): the run is going while its latest attempt is, as the attempt list's last entry says, whichever attempt the page shows, and on the latest attempt a job that has not finished (waiting for the jobs it needs, held for an environment's approval, queued for a runner, running) keeps it going whatever the run's own status says. So an earlier attempt's page, and its ?job= links, offer Cancel run while a later attempt runs, where they offered nothing, and keep refreshing until it ends; the re-runs show only on the latest attempt of a finished run. A page left open on a finished run asked for nothing and heard nothing when the run was re-run from somewhere else (the API, the MCP tool, another tab): it kept showing the attempt that had ended, with its Re-run buttons and no Cancel run, until a job of the new attempt started or finished. A re-run now tells pages open on the run, as a job starting or finishing does. The run view's status for its current attempt is read by one function (shown_status), with tests that a job queued for a self-hosted runner leaves its run in progress and a job held at an environment makes it wait. The Actions guide says when Cancel run shows. | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import type { Job, RunAttempt, WorkflowRun } from "@g1t/contracts"; | |
| 5 | import { runControls } from "./run-controls.ts"; | |
| 6 | ||
| 7 | type Run = Pick<WorkflowRun, "attempt" | "status" | "conclusion" | "error">; | |
| 8 | type Attempt = Pick<RunAttempt, "attempt" | "status">; | |
| 9 | ||
| 10 | const run = (more: Partial<Run> = {}): Run => ({ attempt: 1, status: "completed", conclusion: "success", error: null, ...more }); | |
| 11 | const jobs = (...statuses: Job["status"][]) => statuses.map((status) => ({ status })); | |
| 12 | /** Attempts 1 to `latest`, the earlier ones finished, the latest as `status`. */ | |
| 13 | function attempts(latest: number, status: Attempt["status"]): Attempt[] { | |
| 14 | return Array.from({ length: latest }, (_, i) => ({ attempt: i + 1, status: i + 1 === latest ? status : "completed" })); | |
| 15 | } | |
| 16 | ||
| 17 | test("a re-run of failed jobs whose job waits for a self-hosted runner can be cancelled, not re-run", () => { | |
| 18 | // Attempt 6 of 6: its first job succeeded, the one that needs it is queued. | |
| 19 | const controls = runControls({ | |
| 20 | run: run({ attempt: 6, status: "in_progress", conclusion: null }), | |
| 21 | jobs: jobs("completed", "queued"), | |
| 22 | attempts: attempts(6, "in_progress"), | |
| 23 | member: true, | |
| 24 | }); | |
| 25 | assert.deepEqual(controls, { latest: true, going: true, cancelling: false, canCancel: true, canRerun: false }); | |
| 26 | }); | |
| 27 | ||
| 28 | test("an earlier attempt's page offers Cancel run while the latest attempt runs", () => { | |
| 29 | // `?attempt=5`: attempt 5 as it ended, every job finished. | |
| 30 | const controls = runControls({ | |
| 31 | run: run({ attempt: 5, status: "completed", conclusion: "failure" }), | |
| 32 | jobs: jobs("completed", "completed"), | |
| 33 | attempts: attempts(6, "in_progress"), | |
| 34 | member: true, | |
| 35 | }); | |
| 36 | assert.equal(controls.latest, false); | |
| 37 | assert.equal(controls.going, true); | |
| 38 | assert.equal(controls.canCancel, true); | |
| 39 | assert.equal(controls.canRerun, false); | |
| 40 | // How attempt 5 ended says nothing of attempt 6 being cancelled. | |
| 41 | assert.equal(runControls({ run: run({ attempt: 5, conclusion: "cancelled" }), jobs: [], attempts: attempts(6, "queued"), member: true }).cancelling, false); | |
| 42 | }); | |
| 43 | ||
| 44 | test("a job not finished keeps the latest attempt going, whatever the run's own status says", () => { | |
| 45 | for (const status of ["waiting", "pending", "queued", "in_progress", "calling"] as const) { | |
| 46 | const controls = runControls({ run: run({ attempt: 2, status: "completed" }), jobs: jobs("completed", status), attempts: attempts(2, "completed"), member: true }); | |
| 47 | assert.equal(controls.canCancel, true, status); | |
| 48 | assert.equal(controls.canRerun, false, status); | |
| 49 | } | |
| 50 | }); | |
| 51 | ||
| 52 | test("every status a run waits or runs in can be cancelled", () => { | |
| 53 | for (const status of ["pending", "action_required", "queued", "in_progress", "waiting"] as const) { | |
| 54 | const controls = runControls({ run: run({ status, conclusion: null }), jobs: [], attempts: attempts(1, status), member: true }); | |
| 55 | assert.equal(controls.canCancel, true, status); | |
| 56 | assert.equal(controls.canRerun, false, status); | |
| 57 | } | |
| 58 | }); | |
| 59 | ||
| 60 | test("a finished run is re-run from its latest attempt only", () => { | |
| 61 | const latest = runControls({ run: run({ attempt: 2, conclusion: "failure" }), jobs: jobs("completed"), attempts: attempts(2, "completed"), member: true }); | |
| 62 | assert.deepEqual(latest, { latest: true, going: false, cancelling: false, canCancel: false, canRerun: true }); | |
| 63 | const earlier = runControls({ run: run({ attempt: 1, conclusion: "failure" }), jobs: jobs("completed"), attempts: attempts(2, "completed"), member: true }); | |
| 64 | assert.equal(earlier.canRerun, false); | |
| 65 | assert.equal(earlier.canCancel, false); | |
| 66 | // A run of one attempt has no list to look through. | |
| 67 | assert.equal(runControls({ run: run(), jobs: jobs("completed"), attempts: [], member: true }).canRerun, true); | |
| 68 | }); | |
| 69 | ||
| 70 | test("a run being cancelled shows Force cancel", () => { | |
| 71 | const controls = runControls({ run: run({ status: "in_progress", conclusion: "cancelled" }), jobs: jobs("in_progress"), attempts: attempts(1, "in_progress"), member: true }); | |
| 72 | assert.equal(controls.cancelling, true); | |
| 73 | assert.equal(controls.canCancel, true); | |
| 74 | }); | |
| 75 | ||
| 76 | test("neither without Write, nor for a run whose workflow file could not be used", () => { | |
| 77 | const going = { run: run({ status: "in_progress", conclusion: null }), jobs: jobs("queued"), attempts: attempts(1, "in_progress") }; | |
| 78 | assert.equal(runControls({ ...going, member: false }).canCancel, false); | |
| 79 | const broken = runControls({ run: run({ conclusion: "failure", error: "Its file is not YAML." }), jobs: [], attempts: [], member: true }); | |
| 80 | assert.equal(broken.canCancel, false); | |
| 81 | assert.equal(broken.canRerun, false); | |
| 82 | }); |