Skip to content

Commit b916f29

Browse files
cliffhallclaude
andcommitted
fix: derive CI parity from the workflows; smoke runs the bin itself
Copilot round 3 on #4920 (four findings in unchanged code): - The gate's CI parity is derived: every npm script and scripts/*.mjs a push or pull-request workflow runs must be reached by local:gate. - The smoke executes dist/index.js through its shebang on POSIX, as the installed command is, so a lost shebang or executable bit fails it. - verify:no-test-retries scans workflows for a test runner's retry flag. - docs/quality-gate.md no longer says the lease never fails the gate: a waiter that gives up does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 0b9c77e commit b916f29

7 files changed

Lines changed: 210 additions & 33 deletions

‎docs/quality-gate.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ The claim "the gate runs every check CI runs" is only worth something if it
102102
cannot go stale quietly, so the script tests hold it in place:
103103

104104
- **A check added to CI is added to `local:gate:stages` in the same change.**
105-
`scripts/lib/workflow-gate.test.mjs` asserts the gate still reaches each of
106-
the stages above; a new CI job gets a line in that list.
105+
`scripts/lib/workflow-gate.test.mjs` derives this from the workflows: every
106+
npm script and every `scripts/*.mjs` that a push or pull-request workflow
107+
runs must be reached by `local:gate`, so a check added to CI alone fails the
108+
script tests. It reads script names, so a check written as a bare command in
109+
a workflow step (not behind an npm script) is outside what it can see; put
110+
new checks behind a script.
107111
- **No workflow invokes a `local:*` script.** The prefix means local-only. CI
108112
runs the same checks as separate jobs, and a workflow that ran the gate
109113
itself would queue on a lease nothing else shares and collapse the parallel
@@ -139,8 +143,9 @@ gate-lease: pid 12345 in /path/to/other-worktree, running for 40s holds the gate
139143
waiter then names the path when it gives up.
140144
- **A waiter gives up after 45 minutes** in total, with a message naming the
141145
holder. That budget is not reset as the queue ahead drains.
142-
- **The lease never fails the gate.** If the lease directory cannot be created
143-
or written, the gate runs without it and says so.
146+
- **A lease that cannot be set up does not fail the gate.** If the lease
147+
directory cannot be created or written, the gate runs without it and says
148+
so. A waiter that gives up (above) is the one way the lease ends a run.
144149
- **`SERVERS_SKIP_GATE_LEASE=1`** runs without the lease. It does not get a
145150
result sooner than waiting would: an overlapped run is slower than a queued
146151
one. `SERVERS_GATE_LEASE_DIR` moves the lease directory, which lives under

‎scripts/lib/workflow-gate.mjs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,52 @@ export function findJobsWithoutTimeout(text, file = "<workflow>") {
316316
return findings;
317317
}
318318

319+
/**
320+
* What one workflow runs, for the CI-parity assertion: the npm scripts and the
321+
* `scripts/*.mjs` files its steps invoke, and whether it runs on a push or a
322+
* pull request at all (a dispatch-only workflow, such as the release, is not a
323+
* check on a change).
324+
*
325+
* Read from the same executable regions the rules above scan, so a step
326+
* `name:` or a comment that mentions a script is not counted. `node --test`
327+
* invocations are left out: they run test files, which the gate reaches
328+
* through `test:scripts`'s glob rather than by name.
329+
*
330+
* @param {string} text
331+
* @param {string} [file]
332+
* @returns {{ onChange: boolean, npmScripts: string[], nodeScripts: string[] }}
333+
*/
334+
export function workflowCommands(text, file = "<workflow>") {
335+
const npmScripts = new Set();
336+
const nodeScripts = new Set();
337+
for (const region of extractExecutableRegions(text, file)) {
338+
if (region.kind !== "run") continue;
339+
const script = region.text
340+
.split("\n")
341+
.filter((l) => !l.trimStart().startsWith("#"))
342+
.join("\n");
343+
for (const m of script.matchAll(
344+
/\bnpm run(?:-script)? ([a-z0-9][\w:.-]*)/gi,
345+
))
346+
npmScripts.add(m[1]);
347+
for (const m of script.matchAll(/\bnode (scripts\/[\w./-]+\.mjs)\b/g))
348+
nodeScripts.add(m[1]);
349+
}
350+
const [doc] = parseAllDocuments(text);
351+
const on = doc?.toJS()?.on;
352+
const triggers =
353+
typeof on === "string"
354+
? [on]
355+
: Array.isArray(on)
356+
? on
357+
: Object.keys(on ?? {});
358+
return {
359+
onChange: triggers.some((t) => t === "push" || t === "pull_request"),
360+
npmScripts: [...npmScripts].sort(),
361+
nodeScripts: [...nodeScripts].sort(),
362+
};
363+
}
364+
319365
/**
320366
* Render findings as a single message, for an assertion failure or a CLI.
321367
*

‎scripts/lib/workflow-gate.test.mjs‎

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ import {
2323
findJobsWithoutTimeout,
2424
findWorkflowViolations,
2525
formatWorkflowViolations,
26+
workflowCommands,
2627
} from "./workflow-gate.mjs";
27-
import { GATE_LEASE_WRAPPER, scriptChainRuns } from "./npm-scripts.mjs";
28+
import {
29+
GATE_LEASE_WRAPPER,
30+
reachableScripts,
31+
scriptChainRuns,
32+
} from "./npm-scripts.mjs";
2833

2934
const repoRoot = join(import.meta.dirname, "..", "..");
3035
const workflowDir = join(repoRoot, ".github", "workflows");
@@ -297,6 +302,46 @@ describe("findJobsWithoutTimeout", () => {
297302
});
298303
});
299304

305+
describe("workflowCommands", () => {
306+
it("collects the scripts a workflow's steps run, and its triggers", () => {
307+
const cmds = workflowCommands(
308+
[
309+
"on:",
310+
" push:",
311+
" pull_request:",
312+
"jobs:",
313+
" build:",
314+
" steps:",
315+
" - name: npm run not-a-command",
316+
" run: |",
317+
" # npm run commented-out",
318+
" npm run validate:guards && npm run smoke",
319+
" - run: node scripts/validate-py.mjs ${{ matrix.package }}",
320+
" - run: node --test scripts/validate-py.test.mjs",
321+
].join("\n"),
322+
);
323+
assert.deepEqual(cmds, {
324+
onChange: true,
325+
npmScripts: ["smoke", "validate:guards"],
326+
nodeScripts: ["scripts/validate-py.mjs"],
327+
});
328+
});
329+
330+
it("a dispatch-only workflow is not a check on a change", () => {
331+
for (const on of ["on: workflow_dispatch", "on: [workflow_dispatch]"])
332+
assert.equal(
333+
workflowCommands(`${on}\njobs:\n a:\n steps:\n - run: x\n`)
334+
.onChange,
335+
false,
336+
);
337+
assert.equal(
338+
workflowCommands("on: push\njobs:\n a:\n steps:\n - run: x\n")
339+
.onChange,
340+
true,
341+
);
342+
});
343+
});
344+
300345
describe("the gate's name", () => {
301346
const { scripts } = JSON.parse(
302347
readFileSync(join(repoRoot, "package.json"), "utf8"),
@@ -319,9 +364,9 @@ describe("the gate's name", () => {
319364
);
320365
});
321366

322-
// Every check CI runs, by the script CI runs it through. A stage dropped
323-
// from the gate would leave "local:gate runs every check CI runs" false
324-
// with nothing red.
367+
// The stages by name. The CI-parity test under `.github/workflows` derives
368+
// the same requirement from what the workflows actually run; this list also
369+
// pins the stage that has no CI counterpart (`verify:install-fresh`).
325370
for (const stage of [
326371
"verify:install-fresh",
327372
"validate",
@@ -363,6 +408,41 @@ describe(".github/workflows", () => {
363408
);
364409
});
365410

411+
it("runs no check on a change that local:gate does not also run", () => {
412+
// "local:gate runs every check CI runs", derived rather than listed: every
413+
// npm script and every `scripts/*.mjs` a push/pull-request workflow invokes
414+
// must be reached by the gate. A check added to CI alone fails here.
415+
const { scripts } = JSON.parse(
416+
readFileSync(join(repoRoot, "package.json"), "utf8"),
417+
);
418+
const reached = reachableScripts(scripts, "local:gate");
419+
const reachedBodies = [...reached].map((n) => scripts[n] ?? "");
420+
const missing = [];
421+
let checked = 0;
422+
for (const file of files) {
423+
const cmds = workflowCommands(read(file), file);
424+
if (!cmds.onChange) continue;
425+
for (const name of cmds.npmScripts) {
426+
checked += 1;
427+
if (!reached.has(name)) missing.push(`${file}: npm run ${name}`);
428+
}
429+
for (const script of cmds.nodeScripts) {
430+
checked += 1;
431+
if (!reachedBodies.some((body) => body.includes(`node ${script}`)))
432+
missing.push(`${file}: node ${script}`);
433+
}
434+
}
435+
assert.ok(
436+
checked > 0,
437+
"found no CI commands, so parity would hold vacuously",
438+
);
439+
assert.deepEqual(
440+
missing,
441+
[],
442+
`CI runs these and \`npm run local:gate\` does not. Add each to local:gate:stages:\n ${missing.join("\n ")}`,
443+
);
444+
});
445+
366446
it("bounds every job with timeout-minutes", () => {
367447
const findings = files.flatMap((file) =>
368448
findJobsWithoutTimeout(read(file), file),

‎scripts/smoke-servers.mjs‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,24 +242,32 @@ export function selectTargets(names, servers = SERVERS) {
242242
* @param {string} transport
243243
* @param {{ dir: string, pageUrl: string }} ctx
244244
* @param {string} [root]
245-
* @returns {{ command: string, args: string[], cwd: string }}
245+
* @param {string} [platform]
246+
* @returns {{ command: string, args: string[], cwd: string, bin?: string }}
246247
*/
247-
export function launchSpec(server, transport, ctx, root = repoRoot) {
248+
export function launchSpec(
249+
server,
250+
transport,
251+
ctx,
252+
root = repoRoot,
253+
platform = process.platform,
254+
) {
248255
const cwd = path.join(root, "src", server.name);
249256
const serverArgs = server.args?.(ctx) ?? [];
250257
if (server.language === "ts") {
251258
// `everything` picks its transport from its first argument; the others
252259
// are stdio-only and take none.
253260
const transportArg = server.transports.length > 1 ? [transport] : [];
254-
return {
255-
command: process.execPath,
256-
args: [
257-
path.join(cwd, "dist", "index.js"),
258-
...transportArg,
259-
...serverArgs,
260-
],
261-
cwd,
262-
};
261+
const bin = path.join(cwd, "dist", "index.js");
262+
const rest = [...transportArg, ...serverArgs];
263+
// The published `bin` is the file itself, run through its shebang, so that
264+
// is what is executed: `node dist/index.js` would still work with the
265+
// shebang or the executable bit lost, and the installed command would not.
266+
// Windows has neither (npm generates a `.cmd` shim that calls node), so
267+
// there the file is handed to node.
268+
return platform === "win32"
269+
? { command: process.execPath, args: [bin, ...rest], cwd, bin }
270+
: { command: bin, args: rest, cwd, bin };
263271
}
264272
return {
265273
command: "uv",
@@ -362,9 +370,9 @@ function envFor(server, ctx, extra = {}) {
362370
*/
363371
async function smokeOne(server, transport, ctx) {
364372
const spec = launchSpec(server, transport, ctx);
365-
if (server.language === "ts" && !existsSync(spec.args[0]))
373+
if (server.language === "ts" && !existsSync(spec.bin))
366374
throw new Error(
367-
`${path.relative(repoRoot, spec.args[0])} does not exist — build first (\`npm run build -w src/${server.name}\`).`,
375+
`${path.relative(repoRoot, spec.bin)} does not exist — build first (\`npm run build -w src/${server.name}\`).`,
368376
);
369377
if (server.language === "py" && !existsSync(path.join(spec.cwd, ".venv")))
370378
throw new Error(

‎scripts/smoke-servers.test.mjs‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,30 @@ test("selectTargets: an unknown name throws rather than selecting nothing", () =
8282
});
8383

8484
test("launchSpec: a TypeScript server runs its built bin, with the transport where it takes one", () => {
85-
const everything = launchSpec(byName("everything"), "sse", ctx, "/repo");
86-
assert.equal(everything.command, process.execPath);
87-
assert.deepEqual(everything.args, [
88-
path.join("/repo", "src", "everything", "dist", "index.js"),
85+
const bin = (name) => path.join("/repo", "src", name, "dist", "index.js");
86+
// POSIX: the bin itself, through its shebang, as the installed command is.
87+
const everything = launchSpec(
88+
byName("everything"),
8989
"sse",
90-
]);
91-
const filesystem = launchSpec(byName("filesystem"), "stdio", ctx, "/repo");
92-
assert.deepEqual(filesystem.args, [
93-
path.join("/repo", "src", "filesystem", "dist", "index.js"),
94-
"/tmp/x",
95-
]);
90+
ctx,
91+
"/repo",
92+
"linux",
93+
);
94+
assert.equal(everything.command, bin("everything"));
95+
assert.deepEqual(everything.args, ["sse"]);
96+
const filesystem = launchSpec(
97+
byName("filesystem"),
98+
"stdio",
99+
ctx,
100+
"/repo",
101+
"darwin",
102+
);
103+
assert.equal(filesystem.command, bin("filesystem"));
104+
assert.deepEqual(filesystem.args, ["/tmp/x"]);
105+
// Windows has no shebang: the file is handed to node.
106+
const win = launchSpec(byName("everything"), "sse", ctx, "/repo", "win32");
107+
assert.equal(win.command, process.execPath);
108+
assert.deepEqual(win.args, [bin("everything"), "sse"]);
96109
});
97110

98111
test("isPortTaken: only a lost port is relaunched", () => {

‎scripts/verify-no-test-retries.mjs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
// while a false negative costs the rule. A finding names the file and line.
3030
//
3131
// Scope: tracked files under `src/`, plus every `package.json` (root included)
32-
// for the CLI flag. Build output is never tracked, so it never appears.
32+
// and every workflow for the CLI flags (`npx vitest --retry=2` in a step would
33+
// otherwise pass both this guard and the workflow guard). Build output is never tracked, so it never appears.
3334

3435
import { spawnSync } from "node:child_process";
3536
import { readFileSync } from "node:fs";
@@ -62,6 +63,14 @@ export const RULES = [
6263
pattern: /--retry\b/,
6364
why: "Vitest's `--retry` flag",
6465
},
66+
{
67+
// Tied to the runner's name on the same line: `--retry` alone is also a
68+
// curl flag, which a workflow may use legitimately.
69+
id: "workflow-retry-flag",
70+
files: /(?:^|\/)\.github\/workflows\/[^/]+\.ya?ml$/,
71+
pattern: /\b(?:vitest|pytest)\b.*--(?:retry|reruns|force-flaky)\b/,
72+
why: "a test runner's retry flag in a workflow",
73+
},
6574
{
6675
id: "pytest-rerun-plugin",
6776
files: /(?:^|\/)(?:pyproject\.toml|uv\.lock)$/,
@@ -112,7 +121,7 @@ export function findRetries(file, text) {
112121
function trackedFiles(root) {
113122
const res = spawnSync(
114123
"git",
115-
["ls-files", "-z", "--", "src", "package.json"],
124+
["ls-files", "-z", "--", "src", "package.json", ".github/workflows"],
116125
{ cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 },
117126
);
118127
if (res.error) throw res.error;

‎scripts/verify-no-test-retries.test.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ const cases = [
4747
["vitest-retry-flag"],
4848
],
4949
["package.json", '"test": "vitest run --retry 2"', ["vitest-retry-flag"]],
50+
// A workflow step
51+
[
52+
".github/workflows/typescript.yml",
53+
" run: npx vitest run --retry=2",
54+
["workflow-retry-flag"],
55+
],
56+
[
57+
".github/workflows/python.yml",
58+
" run: uv run pytest --reruns 3",
59+
["workflow-retry-flag"],
60+
],
61+
[
62+
".github/workflows/release.yml",
63+
" run: curl --retry 3 https://example.com",
64+
[],
65+
],
5066
// pytest
5167
[
5268
"src/b/pyproject.toml",

0 commit comments

Comments
 (0)