Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 69 additions & 16 deletions .github/scripts/vale-upgrade-wait.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,37 @@
* in seconds, occasionally longer. Ask the registry the exact question
* the detect step is about to ask, with the same script and no side
* effects (`--json` alone, which the script refuses to combine with
* `--write`), up to five times a minute apart.
* `--write`), up to five times a minute apart, and hold until the
* published set is AT (OR PAST) THE VALE VERSION THIS COMMIT'S MANIFEST
* RECORDS.
*
* WHY "AT THE MANIFEST'S VERSION" AND NOT "NEWER THAN THE PINS". The pins can
* lag more than one release behind, because the previous upgrade pull request
* may still be open. Measured on 3.29.0 (2026-12-02): the pins were at 3.27.0
* with the 3.28.1 upgrade unmerged, the Release Vale run for the 3.29.0
* manifest succeeded at 07:10:47Z, and the registry still served 3.28.1 as
* latest until 07:12:28Z. "Newer than the pins" was already true of 3.28.1,
* so the first probe returned at once and the run rebuilt the upgrade branch
* at 3.28.1, an older Vale than the release that triggered it. The manifest
* at GITHUB_SHA names the release this run exists for, so the wait holds
* until the base version inside the published stamp (3.29.0 of
* 3.29.0-20261202071030) is at least it. "At least" rather than "equal": if a
* later manifest bump has already published by the time this run probes, the
* registry is past this run's release, and upgrading to it is still never
* older than what triggered the run.
*
* EVERY EXIT IS 0. A publish that concludes anything but success, one that
* never appears inside the bound, a registry that still says "current" at the
* end: each is a `::warning::` and a clean exit, never a failed check. This
* never appears inside the bound, a registry that has not reached the
* manifest's version at the end: each is a `::warning::` and a clean exit, never a failed check. This
* workflow must not go red over a race it did not cause, and a red here would
* say "the upgrade is broken" when the truth is "the publish is". The daily
* schedule in vale-upgrade.yml retries once the packages are actually there,
* which is what a backstop is for.
*
* What the workflow learns is the `ready` output: whether the detect step
* should run at all. It is "true" when the registry serves a newer set, and
* "false" when waiting established there is nothing to upgrade to yet.
* should run at all. It is "true" when the registry serves a newer set at the
* manifest's version, and "false" when waiting established there is nothing
* to upgrade to yet.
*
* TWO CASES ARE "true" WITHOUT A NEWER SET, on purpose:
*
Expand All @@ -65,8 +83,9 @@
* [--workflow <file>] [--timeout-minutes <n>] [--poll-seconds <n>]
* [--attempts <n>] [--interval-seconds <n>] [--call-timeout-seconds <n>]
*
* Reads GITHUB_EVENT_NAME and GITHUB_SHA. `gh` needs GH_TOKEN with
* `actions: read`.
* Reads GITHUB_EVENT_NAME and GITHUB_SHA, and on a push the `valeVersion` of
* vale-manifest.json as checked out, which is the manifest at GITHUB_SHA. `gh`
* needs GH_TOKEN with `actions: read`.
*
* Outputs (appended to $GITHUB_OUTPUT when set):
* ready "true" when the detect step should run
Expand All @@ -75,11 +94,14 @@
*/

const { execFileSync } = require("node:child_process");
const { appendFileSync } = require("node:fs");
const { appendFileSync, readFileSync } = require("node:fs");
const { join } = require("node:path");

const { compareValeVersions } = require("./vale-release.cjs");

const RELEASE_WORKFLOW = "release-vale.yml";
const DETECT_SCRIPT = join(__dirname, "vale-upgrade-detect.cjs");
const MANIFEST_PATH = join(__dirname, "vale-manifest.json");

const MINUTE = 60 * 1000;
const SECOND = 1000;
Expand Down Expand Up @@ -212,6 +234,21 @@ function probeRegistry({
}
}

/**
* The Vale release the checked-out manifest records. On a push the checkout
* is GITHUB_SHA, so this is the release whose publish this run waited for.
*/
function readManifestVersion(path = MANIFEST_PATH) {
const { valeVersion } = JSON.parse(readFileSync(path, "utf8"));
if (typeof valeVersion !== "string" || valeVersion === "") {
throw new TypeError(`${path} records no valeVersion`);
}
return valeVersion;
}

/** `3.29.0-20261202071030` -> `3.29.0`: the upstream release inside a stamp. */
const stampBase = (stamped) => String(stamped).split("-")[0];

const sleepFor = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

/** `gh run list --json status,conclusion` as a list of {status, conclusion}. */
Expand Down Expand Up @@ -311,14 +348,20 @@ async function waitForReleaseRun({
}

/**
* Hold until the registry serves a newer set than the pins.
* Hold until the registry serves a newer set than the pins, and, when
* `expectedVersion` is given, one whose base version is that release or later.
* A newer set at an EARLIER version (an intermediate release published before
* this one, while the pins were further behind) is not what this run is for,
* and counts as "not yet" like an answer of "current". A set already past it
* (a later manifest bump that published first) counts as reached.
*
* Resolves to `{ outcome: "ahead" | "stale" | "probe-errored", ... }`. "stale"
* means at least one probe answered and the answer was "current" every time it
* did; "probe-errored" means no probe answered at all, which is a different
* fact and is reported as one.
* means at least one probe answered and no answer was the set waited for;
* "probe-errored" means no probe answered at all, which is a different fact
* and is reported as one.
*/
async function waitForRegistry({
expectedVersion,
attempts = DEFAULTS.attempts,
intervalMs = DEFAULTS.intervalMs,
probe = probeRegistry,
Expand All @@ -330,14 +373,20 @@ async function waitForRegistry({
try {
const comparison = await probe();
answered += 1;
if (comparison.ahead === true) {
const reached =
expectedVersion === undefined ||
compareValeVersions(stampBase(comparison.upstream), expectedVersion) >=
0;
if (comparison.ahead === true && reached) {
log(
`The registry serves a newer set than the pins: ${JSON.stringify(comparison)}`
`The registry serves a newer set than the pins${expectedVersion === undefined ? "" : `, at Vale ${expectedVersion}`}: ${JSON.stringify(comparison)}`
);
return { outcome: "ahead", comparison, attempt };
}
log(
`The registry still serves the pinned version (attempt ${attempt} of ${attempts}): ${JSON.stringify(comparison)}`
comparison.ahead === true
? `The registry serves a newer set than the pins, but not yet Vale ${expectedVersion} (attempt ${attempt} of ${attempts}): ${JSON.stringify(comparison)}`
: `The registry still serves the pinned version (attempt ${attempt} of ${attempts}): ${JSON.stringify(comparison)}`
);
} catch (error) {
log(
Expand All @@ -364,6 +413,7 @@ async function main({
sleep = sleepFor,
log = (line) => console.log(line),
now = Date.now,
manifestVersion = readManifestVersion,
} = {}) {
const options = parseArgs(argv);
const finish = (ready, outcome) => {
Expand All @@ -384,6 +434,7 @@ async function main({
if (!sha) {
throw new Error("GITHUB_SHA must be set on a push");
}
const expectedVersion = manifestVersion();

// The per-call bound reaches the real runners through a second argument an
// injected fake is free to ignore.
Expand Down Expand Up @@ -413,6 +464,7 @@ async function main({
log(`Release Vale succeeded for ${sha}.`);

const registry = await waitForRegistry({
expectedVersion,
attempts: options.attempts,
intervalMs: options.intervalMs,
probe: () => probe(call),
Expand All @@ -424,7 +476,7 @@ async function main({
}
if (registry.outcome === "stale") {
log(
`::warning::no probe in ${registry.attempts} attempts after the publish succeeded reported a newer set than the pins; each attempt is logged above with its result. The daily schedule will retry.`
`::warning::no probe in ${registry.attempts} attempts after the publish succeeded reported a newer set than the pins at or past Vale ${expectedVersion}, the version this commit's manifest records; each attempt is logged above with its result. The daily schedule will retry.`
);
return finish(false, "registry-stale");
}
Expand Down Expand Up @@ -464,6 +516,7 @@ module.exports = {
parseArgs,
parseRuns,
probeRegistry,
readManifestVersion,
recover,
runGh,
waitForRegistry,
Expand Down
159 changes: 158 additions & 1 deletion .github/scripts/vale-upgrade-wait.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
parseArgs,
parseRuns,
probeRegistry,
readManifestVersion,
recover,
runGh,
waitForRegistry,
Expand Down Expand Up @@ -90,6 +91,26 @@ const failed = { status: "completed", conclusion: "failure" };
const stale = { pinned: "3.21.0-1", upstream: "3.21.0-1", ahead: false };
const ahead = { pinned: "3.21.0-1", upstream: "3.22.0-2", ahead: true };

/** The manifest version `ahead` is at, which main() is told it waits for. */
const MANIFEST_VERSION = "3.22.0";

/*
* The 3.29.0 incident (2026-12-02), in stamps: pins at 3.27.0 because the
* 3.28.1 upgrade was still open, the registry still serving 3.28.1 when the
* 3.29.0 publish run succeeded, and 3.29.0 visible a minute and a half later.
*/
const pinsBehind = "3.27.0-20261101000000";
const intermediate = {
pinned: pinsBehind,
upstream: "3.28.1-20261120000000",
ahead: true,
};
const atManifest = {
pinned: pinsBehind,
upstream: "3.29.0-20261202071030",
ahead: true,
};

test("parseRuns reads gh's JSON and tolerates a null conclusion", () => {
assert.deepEqual(parseRuns("[]"), []);
assert.deepEqual(parseRuns("\n"), []);
Expand Down Expand Up @@ -342,8 +363,99 @@ test("registry: one answer among errors is still 'stale'", async () => {
assert.equal(result.answered, 1);
});

test("registry: an intermediate newer set is not the manifest's version, so it waits", async () => {
const time = clock();
const logged = [];
const { probe, count } = probeScript([
intermediate,
intermediate,
atManifest,
]);
const result = await waitForRegistry({
expectedVersion: "3.29.0",
probe,
sleep: time.sleep,
log: (line) => logged.push(line),
});
assert.equal(result.outcome, "ahead");
assert.equal(result.attempt, 3);
assert.deepEqual(result.comparison, atManifest);
assert.equal(count(), 3);
assert.equal(
logged.filter((line) =>
/newer set than the pins, but not yet Vale 3\.29\.0/.test(line)
).length,
2
);
});

test("registry: a newer set that never reaches the manifest's version is 'stale'", async () => {
const time = clock();
const { probe } = probeScript([intermediate]);
const result = await waitForRegistry({
expectedVersion: "3.29.0",
probe,
sleep: time.sleep,
log: () => {},
});
assert.equal(result.outcome, "stale");
assert.equal(result.answered, DEFAULTS.attempts);
});

test("registry: a set already past the manifest's version counts as reached", async () => {
const time = clock();
const later = {
pinned: pinsBehind,
upstream: "3.30.0-20261203000000",
ahead: true,
};
const { probe } = probeScript([later]);
const result = await waitForRegistry({
expectedVersion: "3.29.0",
probe,
sleep: time.sleep,
log: () => {},
});
assert.equal(result.outcome, "ahead");
assert.equal(result.attempt, 1);
assert.deepEqual(result.comparison, later);
});

test("registry: without an expected version, any newer set is ahead", async () => {
const time = clock();
const { probe } = probeScript([intermediate]);
const result = await waitForRegistry({
probe,
sleep: time.sleep,
log: () => {},
});
assert.equal(result.outcome, "ahead");
assert.equal(result.attempt, 1);
});

test("readManifestVersion reads valeVersion and refuses a manifest without one", () => {
const directory = mkdtempSync(join(tmpdir(), "vale-upgrade-wait-test-"));
try {
const path = join(directory, "vale-manifest.json");
writeFileSync(path, JSON.stringify({ valeVersion: "3.29.0" }));
assert.equal(readManifestVersion(path), "3.29.0");
writeFileSync(path, JSON.stringify({}));
assert.throws(() => readManifestVersion(path), /records no valeVersion/);
} finally {
rmSync(directory, { recursive: true, force: true });
}
// The real manifest parses, so a push run never falls back to wait-errored.
assert.match(readManifestVersion(), /^\d+\.\d+\.\d+$/);
});

/** Drive main() with everything injected and read back $GITHUB_OUTPUT. */
async function run({ event = "push", gh, probe, argv = [] }) {
async function run({
event = "push",
gh,
probe,
argv = [],
manifestVersion = MANIFEST_VERSION,
}) {
const directory = mkdtempSync(join(tmpdir(), "vale-upgrade-wait-test-"));
const outputPath = join(directory, "github-output");
const time = clock();
Expand All @@ -361,6 +473,10 @@ async function run({ event = "push", gh, probe, argv = [] }) {
sleep: time.sleep,
now: time.now,
log: (line) => logged.push(line),
manifestVersion:
typeof manifestVersion === "function"
? manifestVersion
: () => manifestVersion,
});
let outputs = {};
try {
Expand Down Expand Up @@ -467,6 +583,46 @@ test("main: probe never ahead -> warning, ready=false", async () => {
assert.deepEqual(outputs, { ready: "false", outcome: "registry-stale" });
assert.equal(warnings.length, 1);
assert.match(warnings[0], /no probe in 5 attempts .* reported a newer set/);
assert.match(
warnings[0],
/at or past Vale 3\.22\.0, the version this commit's manifest records/
);
});

test("main: pins behind, registry at an intermediate release, then at the manifest's -> waits for the manifest's", async () => {
const probed = probeScript([intermediate, atManifest]);
const { outputs, warnings, slept } = await run({
probe: probed.probe,
manifestVersion: "3.29.0",
});
assert.deepEqual(outputs, { ready: "true", outcome: "ahead" });
assert.deepEqual(warnings, []);
assert.equal(probed.count(), 2);
assert.deepEqual(slept, [DEFAULTS.intervalMs]);
});

test("main: registry stuck at an intermediate release -> registry-stale, detect never runs", async () => {
const { outputs, warnings } = await run({
probe: probeScript([intermediate]).probe,
manifestVersion: "3.29.0",
});
assert.deepEqual(outputs, { ready: "false", outcome: "registry-stale" });
assert.equal(warnings.length, 1);
assert.match(
warnings[0],
/at or past Vale 3\.29\.0, the version this commit's manifest records/
);
assert.match(warnings[0], /daily schedule will retry/);
});

test("main: a non-push event never reads the manifest", async () => {
const { outputs } = await run({
event: "schedule",
manifestVersion: () => {
throw new Error("a non-push run read the manifest");
},
});
assert.deepEqual(outputs, { ready: "true", outcome: "skipped" });
});

test("main: a non-push event skips both waits with ready=true", async () => {
Expand Down Expand Up @@ -659,6 +815,7 @@ test("main: the call timeout reaches gh and the probe as a second argument", asy
},
sleep: async () => {},
log: () => {},
manifestVersion: () => MANIFEST_VERSION,
});
} finally {
rmSync(directory, { recursive: true, force: true });
Expand Down
Loading
Loading