fix(ci): a merge on main stopped having a path to the box (#662)

Four merges reached main on 2026-09-11 and never left the repository.
Every check was green, no job was red, nothing anywhere was marked failed.
Each one needed a Deploy dispatched by hand, and the only way to notice was
to compare the box's build stamp against main's tip.

Two paths ship a commit, and exactly one fires per commit by design: a push
to main chains through workflow_run, and a dispatch runs ci.yml's own ship
job. Under a concurrency group keyed only by branch, auto-merge's re-armed
dispatch cancels the push run for the same commit — which is intended, and
the comment on the ship job says so.

What was never intended is the NEXT merge cancelling the surviving run of
the previous one. That kills both paths at once. For the stranded commit,
gh run list showed a cancelled push run AND a cancelled dispatch run, with
a clean success five minutes later belonging to a different commit.

So main is now keyed per commit. Same-commit cancellation is preserved, so
a commit still ships exactly once; cross-commit cancellation is gone, so it
ships at all. Branch behaviour is unchanged.

The gate that comes with it states the property narrowly — on main, one
commit's CI must never cancel another's — and also pins the five premises
that make it matter, because if the chain is ever rewired the gate must
fail loudly rather than guard a ghost.

Both were proved by mutation on a copy, six mutations, each asserted to
have actually applied. That assertion earned its keep immediately: the
first draft of this gate checked for `workflow_dispatch` with a plain
substring, which matched a comment and an `if:` elsewhere in the file, so
removing the actual trigger left it green. A mutation that silently no-ops
makes a gate look proven when nothing was tested.


Claude-Session: https://claude.ai/code/session_018Z7YRpgsEtNjRqr1usHMQY

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
CatoandClaude Opus 5 authored and GitHub committed 2026-09-11 12:58:34 -07:00
1 parent 6310c3d330
commit d5c83e66b5
2 files changed
+173 -3

No files matched your search

+21 -3
View File
@@ -10,10 +10,28 @@ on:
# main by hand, every automated merge would land and silently never ship.
workflow_dispatch: {}
# Cancel in-progress runs of the same workflow on the same branch — saves
# minutes when commits land in quick succession.
# On a PR branch the newest push is the only one that matters, so cancel the
# rest — that is where the minutes are saved.
#
# On main the group is keyed per COMMIT, and the bug that forced this was a
# commit cancelling a DIFFERENT commit's run.
#
# Two paths ship a commit, and exactly one fires per commit by design: a push to
# main chains through `workflow_run`, and a dispatch runs the `ship` job below.
# Under one group-per-branch, the dispatch cancels the push run for the same
# commit — that is intended, it is how `ship` becomes the single path. What was
# never intended is the NEXT merge cancelling the surviving run of the previous
# one, which kills both paths at once and leaves the commit in the repository.
#
# Seen four times on 2026-09-11. Nothing was red. `gh run list` showed, for one
# commit, a cancelled push run and a cancelled dispatch run, and for the commit
# five minutes later, a clean success. Main green, every check passing, and the
# box still serving the previous build.
#
# Per-commit keeps same-commit cancellation (so it still ships once) and stops
# cross-commit cancellation (so it ships at all).
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.ref == 'refs/heads/main' && github.sha || 'branch' }}
cancel-in-progress: true
jobs:
+152
View File
@@ -0,0 +1,152 @@
/**
* A commit on main must keep a path to the box.
*
* WHY THIS EXISTS
* ---------------
* On 2026-09-11 four merges reached main and never left the repository. Every
* check was green, no job was red, nothing anywhere was marked failed — the
* commits simply sat there while the box served the previous build. Each needed
* a Deploy dispatched by hand.
*
* Two paths ship a commit, and exactly one of them fires per commit by design:
*
* push to main → CI finishes → Deploy chains off `workflow_run`
* dispatch on main → CI finishes → the `ship` job dispatches Deploy
*
* Under a concurrency group keyed only by branch, auto-merge's re-armed dispatch
* cancels the push run for the same commit. That is FINE and intended: `ship`
* then becomes the single path, and the comment in ci.yml says so.
*
* What is not fine is the next merge cancelling the surviving run of the
* previous one. That kills both paths at once. `gh run list` for the stranded
* commit showed a cancelled push run AND a cancelled dispatch run, with a clean
* success five minutes later belonging to a different commit.
*
* So the property is narrow and worth stating exactly: **on main, one commit's
* CI must never cancel another commit's.** Same-commit cancellation stays.
*
* There is no red to notice when this breaks. The symptom is silence, and
* silence is also what a healthy pipeline looks like — which is why this is a
* gate rather than a comment.
*
* Run: npx tsx scripts/test/ci-main-is-shippable.ts
*/
import { readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const REPO = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
const wf = (name: string) => readFileSync(join(REPO, ".github", "workflows", name), "utf8");
const CI = wf("ci.yml");
const DEPLOY = wf("deploy.yml");
const AUTOMERGE = wf("auto-merge.yml");
/** Lines with the comment stripped, so a check can never be satisfied by prose. */
function code(src: string): string[] {
return src
.split("\n")
.map((l) => l.replace(/#.*$/, ""))
.filter((l) => l.trim().length > 0);
}
/**
* The keys of a top-level block, by indentation. Used for `on:` so that the
* word "workflow_dispatch" appearing in a comment or an `if:` cannot stand in
* for the trigger actually being declared — the first draft of this gate made
* exactly that mistake and passed a mutation that removed the trigger.
*/
function blockKeys(src: string, block: string): string[] {
const lines = src.split("\n");
const start = lines.findIndex((l) => new RegExp(`^${block}:`).test(l));
if (start < 0) return [];
const keys: string[] = [];
for (const line of lines.slice(start + 1)) {
if (/^\S/.test(line) && line.trim()) break; // next top-level key
const m = /^\s{2}([A-Za-z_][\w-]*):/.exec(line.replace(/#.*$/, ""));
if (m) keys.push(m[1]);
}
return keys;
}
const problems: string[] = [];
let passed = 0;
function check(label: string, condition: boolean, why: string) {
if (condition) passed++;
else problems.push(`${label}\n ${why}`);
}
// ── the property itself ─────────────────────────────────────────────────────
const group =
code(CI)
.find((l) => /^\s*group:/.test(l))
?.trim() ?? "";
check(
"on main, one commit's CI cannot cancel another commit's",
group.includes("github.sha"),
"The concurrency group does not vary by `github.sha`, so a newer merge cancels\n" +
" an older commit's still-running CI — killing both its ship paths at once.\n" +
" Group was: " +
(group || "(none found)"),
);
check(
"branches still cancel superseded runs",
/cancel-in-progress:\s*true/.test(code(CI).join("\n")),
"`cancel-in-progress` is no longer true. On a PR branch the newest push is the\n" +
" only one that matters; without this every superseded push burns a full run.",
);
// ── the premises. If one moves, this gate is guarding a ghost. ──────────────
check(
"CI is dispatchable, so auto-merge can re-arm it",
blockKeys(CI, "on").includes("workflow_dispatch"),
"ci.yml's `on:` block no longer declares `workflow_dispatch`. A merge made with\n" +
" GITHUB_TOKEN triggers no workflows at all, so the re-arm is the only thing\n" +
" that starts CI for it — and the `ship` job below only runs on a dispatch.",
);
check(
"the dispatch path still ships",
/ship:/.test(CI) && /gh workflow run deploy\.yml/.test(CI),
"ci.yml lost the `ship` job that dispatches Deploy when main is green. That is\n" +
" the path a re-armed dispatch uses; without it, a dispatch-only commit has\n" +
" no route to the box but the sweep's poll.",
);
check(
"the push path still ships",
/workflow_run:/.test(DEPLOY) && /workflows:\s*\[?["']CI["']/.test(DEPLOY),
"deploy.yml no longer chains off CI's `workflow_run`. If the chain changed,\n" +
" re-derive what starves it and rewrite this gate — do not delete it.",
);
check(
"Deploy still refuses a run that did not pass",
/workflow_run\.conclusion\s*==\s*'success'/.test(DEPLOY),
"deploy.yml no longer gates on `conclusion == 'success'`. That gate is why a\n" +
" cancelled run stops a deploy — the whole reason cross-commit cancellation\n" +
" on main is fatal.",
);
check(
"auto-merge still reconciles deployment",
/deploy_workflow:\s*deploy\.yml/.test(AUTOMERGE),
"auto-merge.yml lost `deploy_workflow: deploy.yml` — the backstop that ships a\n" +
" commit whose trigger went missing anyway.",
);
if (problems.length > 0) {
console.error("✗ ci-main-is-shippable:");
for (const p of problems) console.error(` ${p}`);
console.error(
"\n The failure this protects against is SILENT: main green, nothing red,\n" +
" and the commit never reaching the box.",
);
process.exit(1);
}
console.log(
`✓ ci-main-is-shippable: ${passed} properties hold — a merge on main keeps a path to the box`,
);