diff --git a/changelog.d/1282.fixed.md b/changelog.d/1282.fixed.md new file mode 100644 index 00000000..609fa64a --- /dev/null +++ b/changelog.d/1282.fixed.md @@ -0,0 +1 @@ +The workflow comparator no longer reads a missing aggregate job as "nothing to check": a pull request that renames the in-run aggregate away from the accepted ids (`ci-ok-head`, `ci-ok`) is refused, naming the ids it looked for and the ids the head declares, a head that declares both is refused by both names, and the success line names the id it actually checked. The aggregate's `needs:`, base-evaluator and `path: base` checks follow whichever accepted id the head declares, so `#1273`'s rename is checked rather than skipped. diff --git a/docs/autonomous-dev.md b/docs/autonomous-dev.md index 52b6113d..107e6bd0 100644 --- a/docs/autonomous-dev.md +++ b/docs/autonomous-dev.md @@ -443,7 +443,7 @@ the base branch into `base/` and executed from there: | `workflow-lint` | `node base/scripts/workflow-injection.mjs --workflows .github/workflows --allowlist base/.github/agent/workflow-injection-allowlist.json` | | `reserved-contexts` | `node base/scripts/reserved-contexts.mjs --workflows .github/workflows` | | `codeql-required` | `node base/scripts/codeql-required.mjs` | -| `ci-ok` (the in-run aggregate, until the follow-up renames it) | `node base/scripts/ci-ok.mjs` | +| `ci-ok` / `ci-ok-head` (the in-run aggregate; `#1273` renames it and both ids are accepted meanwhile) | `node base/scripts/ci-ok.mjs` | | `ci-ok` (the publisher's verdict) | `node base/scripts/ci-ok-run.mjs`, run by `ci-ok-publish.yml` | | CodeQL gate | `node base/scripts/codeql-gate.mjs --sarif codeql-results --baseline base/.github/agent/codeql-baseline.json` | @@ -461,6 +461,16 @@ there is the file the next step executes — so a branch that rewrites substitution. It is evidence, not a gate: the verdict still comes from the base file. +The workflow comparator finds the aggregate by an accepted id set +(`ci-ok-head`, `ci-ok`) instead of one literal. A head that declares none of them +is refused, naming the ids it looked for and the ids it declares; a head that +declares both is refused by both names. The three checks behind that lookup — +every required job in the aggregate's `needs:`, the base evaluator's command, and +the `path: base` checkout — decide whether the base tree's gate is still the +thing that runs, so a lookup miss is never a skip. The success line names the id +it actually checked, and the set is narrowed in a later card once no branch +carries the old id. + The `changes` job also runs `scripts/gate-integrity.mjs`, which writes every gate file in the diff into the job summary. It fails closed when the diff cannot be read: a summary that says "no gate file" over an unreadable diff would be a diff --git a/scripts/lib/gate-compare.d.mts b/scripts/lib/gate-compare.d.mts index 6d727e61..94176c0d 100644 --- a/scripts/lib/gate-compare.d.mts +++ b/scripts/lib/gate-compare.d.mts @@ -26,6 +26,7 @@ export declare const GATE_COMPARE_FILE: string export declare const GATE_COMPARE_ALLOWED_KEYS: readonly ['timeout-minutes'] export declare const REQUIRED_EVALUATOR: string export declare const AGGREGATE_JOB: string +export declare const AGGREGATE_JOBS: readonly string[] export declare const WORKFLOW_CHANGE_LABEL: string export declare class GateWorkflowError extends Error { diff --git a/scripts/lib/gate-compare.mjs b/scripts/lib/gate-compare.mjs index 7ca41bba..43cf50f8 100644 --- a/scripts/lib/gate-compare.mjs +++ b/scripts/lib/gate-compare.mjs @@ -38,6 +38,14 @@ // key, an unreadable line or an empty `jobs:` mapping is a refusal with a // reason, never a skipped comparison. // +// #1282: the aggregate is found by an accepted id set, never by one literal. +// `#1273` renames `ci-ok` to `ci-ok-head`, so while branches still carry the old +// id the set is `['ci-ok-head', 'ci-ok']`. A head that declares none of them has +// removed the job the three checks below read, and a head that declares two has +// two candidates for one context: both are refusals naming what was looked for +// and what the head declares, because a lookup miss must never read as a pass. +// The success line names the id it actually checked. +// // Nothing from a workflow text is echoed verbatim: a message names the file, a // required job id or key name that matches `[A-Za-z0-9_-]{1,40}`, and nothing a // `run:` body, a `name:` value or an `env:` value holds. That keeps a refusal @@ -54,9 +62,17 @@ export const GATE_COMPARE_ALLOWED_KEYS = Object.freeze(['timeout-minutes']) /** The step the aggregate must run, as the base tree spells it. */ export const REQUIRED_EVALUATOR = 'node base/scripts/ci-ok.mjs' -/** The required aggregate job, whose `needs` and step are pinned separately. */ +/** The aggregate id the base tree declares today; `#1273` moves it to `ci-ok-head`. */ export const AGGREGATE_JOB = 'ci-ok' +/** + * Every aggregate id a head may declare while `#1273` moves the job from + * `ci-ok` to `ci-ok-head`. Explicit data, not a fallback: an id outside this set + * is never looked up, so a rename away from it is a refusal rather than a + * skipped check. Narrow it in a later card, once no branch names the old id. + */ +export const AGGREGATE_JOBS = Object.freeze(['ci-ok-head', 'ci-ok']) + /** The label that records a reviewed workflow change (owner-applied). */ export const WORKFLOW_CHANGE_LABEL = 'ci-workflow-change' @@ -270,12 +286,26 @@ function isLiteralName(value) { return value !== '' && !value.startsWith('|') && !value.startsWith('>') && !value.includes('${{') } +// The job ids a head declares, for the refusal that has to say what it found +// instead of an accepted aggregate id. Only ids that parse as a plain name are +// quoted; any other is counted rather than echoed. +function declaredJobIds(jobs) { + const named = [] + let unquoted = 0 + for (const id of jobs.keys()) { + if (KEY_NAME.test(id)) named.push(`\`${id}\``) + else unquoted += 1 + } + const list = named.length === 0 ? 'no job id the comparator can name' : named.join(', ') + return unquoted === 0 ? list : `${list} (and ${String(unquoted)} more it will not quote)` +} + /** * Compare the head's workflow with the base tree's. `ok` is true only when every * top-level block other than `jobs:` matches, every required job is defined * identically (allowlisted keys aside), every required display name belongs to - * exactly one job, the aggregate still depends on all of them, and its step - * still runs the base evaluator. + * exactly one job, exactly one accepted aggregate id is declared, and that + * aggregate depends on every required job and runs the base evaluator. */ export function compareGate({ base, head, required = REQUIRED_JOBS } = {}) { const problems = [] @@ -340,12 +370,13 @@ export function compareGate({ base, head, required = REQUIRED_JOBS } = {}) { continue } // The aggregate may wait on more than the base did, but never on less: - // `needsIds` has already refused a list it cannot read. - if (id === AGGREGATE_JOB && name === 'needs') { + // `needsIds` has already refused a list it cannot read. Any accepted id + // counts, so `#1273`'s rename does not lose the allowance. + if (AGGREGATE_JOBS.includes(id) && name === 'needs') { const headNeeds = needsIds(headJob) for (const dependency of needsIds(baseJob)) { if (!headNeeds.includes(dependency)) { - refuse(`the pull request's \`${AGGREGATE_JOB}\` does not depend on required job \`${dependency}\``) + refuse(`the pull request's \`${id}\` does not depend on required job \`${dependency}\``) } } continue @@ -404,19 +435,33 @@ export function compareGate({ base, head, required = REQUIRED_JOBS } = {}) { refuse(`the pull request's ${GATE_COMPARE_FILE} gives jobs \`${seen}\` and \`${job.id}\` the same display name${DOOR}`) } - const aggregate = headJobs.get(AGGREGATE_JOB) - if (aggregate !== undefined) { + // The aggregate is found by an accepted id, never by one literal. A head that + // declares none of them has removed the job the three checks below read; a + // head that declares two has two candidates for the same context. Both are + // refusals — a lookup miss is "not checked", which is exactly what a green + // verdict must not mean. + const aggregateIds = AGGREGATE_JOBS.filter((id) => headJobs.has(id)) + let aggregateClause = '' + if (aggregateIds.length === 0) { + const looked = AGGREGATE_JOBS.map((id) => `\`${id}\``).join(' or ') + refuse(`the pull request's ${GATE_COMPARE_FILE} declares no aggregate job the gate accepts (looked for ${looked}; the head declares ${declaredJobIds(headJobs)}), so the aggregate checks are not skipped${DOOR}`) + } else if (aggregateIds.length > 1) { + refuse(`the pull request's ${GATE_COMPARE_FILE} declares more than one aggregate job (\`${aggregateIds.join('` and `')}\`), so the gate cannot tell which one it checks${DOOR}`) + } else { + const aggregateId = aggregateIds[0] + const aggregate = headJobs.get(aggregateId) const headNeeds = needsIds(aggregate) for (const id of requiredIds) { - if (!headNeeds.includes(id)) refuse(`the pull request's \`${AGGREGATE_JOB}\` does not depend on required job \`${id}\``) + if (!headNeeds.includes(id)) refuse(`the pull request's \`${aggregateId}\` does not depend on required job \`${id}\``) } if (!runCommands(aggregate).includes(REQUIRED_EVALUATOR)) { - refuse(`the pull request's \`${AGGREGATE_JOB}\` does not run the base evaluator \`${REQUIRED_EVALUATOR}\``) + refuse(`the pull request's \`${aggregateId}\` does not run the base evaluator \`${REQUIRED_EVALUATOR}\``) } const checkout = /^[ \t]*path:[ \t]*base[ \t]*$/ if (!aggregate.keys.get('steps')?.lines.some((line) => checkout.test(line))) { - refuse(`the pull request's \`${AGGREGATE_JOB}\` does not check the gate out under \`path: base\``) + refuse(`the pull request's \`${aggregateId}\` does not check the gate out under \`path: base\``) } + aggregateClause = `\`${aggregateId}\` runs \`${REQUIRED_EVALUATOR}\`` } if (problems.length > 0) return { ok: false, problems, lines: [] } @@ -424,7 +469,7 @@ export function compareGate({ base, head, required = REQUIRED_JOBS } = {}) { ok: true, problems: [], lines: [ - `gate-compare: the pull request's ${GATE_COMPARE_FILE} matches the base tree outside \`jobs:\`, defines all ${String(requiredIds.length)} required job(s) as the base tree does (allowlisted: ${GATE_COMPARE_ALLOWED_KEYS.join(', ')}), gives each required display name to one job, and \`${AGGREGATE_JOB}\` runs \`${REQUIRED_EVALUATOR}\``, + `gate-compare: the pull request's ${GATE_COMPARE_FILE} matches the base tree outside \`jobs:\`, defines all ${String(requiredIds.length)} required job(s) as the base tree does (allowlisted: ${GATE_COMPARE_ALLOWED_KEYS.join(', ')}), gives each required display name to one job, and ${aggregateClause}`, ], } } diff --git a/test/gateCompare.test.ts b/test/gateCompare.test.ts index c113516c..4716fb89 100644 --- a/test/gateCompare.test.ts +++ b/test/gateCompare.test.ts @@ -23,6 +23,13 @@ import { // checked-in workflow and over synthetic texts, and they pin the refusal // messages: a red that names the job and the key is the evidence a reviewer // needs, and a silent pass is the bug. +// +// #1282: the aggregate the comparator reads the `needs:`, the evaluator command +// and `path: base` from is looked up by an accepted id set, never by one +// literal. A head that renames it away used to skip those three checks and come +// back green with a line claiming they ran, so the lookup miss is now a refusal +// too — with the id it looked for and the ids the head declares — and a head +// declaring two candidates is refused by both names. const read = (path: string): string => readFileSync(fileURLToPath(new URL(`../${path}`, import.meta.url)), 'utf8') const ci = read(GATE_COMPARE_FILE) @@ -59,6 +66,21 @@ function withJob(text: string, id: string, body: string[]): string { const STUB_ROWS = [' runs-on: ubuntu-latest', ' steps:', ' - run: true'] +// A head that renames the aggregate away from `ci-ok`. `ci-ok` is not in +// `REQUIRED_JOBS` — it is the aggregate, looked up on its own — so moving the id +// is the shape that used to lose the `needs:`/evaluator/`path: base` checks. +function aggregateRename(id: string): string { + return editJob(ci, AGGREGATE_JOB, (block) => [ + ` ${id}:`, + ...block.slice(1).map((line) => (line === ` name: ${AGGREGATE_JOB}` ? ` name: ${id}` : line)), + ]) +} + +// The same loss with no rename at all: the aggregate job is deleted. +function aggregateDeleted(): string { + return editJob(ci, AGGREGATE_JOB, () => []) +} + // Acceptance 1: the job keeps its name and its body becomes a no-op. function stubbedRequiredJob(): string { return editJob(ci, 'mock-check', (block) => { @@ -126,6 +148,19 @@ test('the checked-in workflow is its own base, so an untouched pull request is a assert.equal(result.lines.length, 1) assert.match(result.lines[0]!, /gate-compare/) assert.match(result.lines[0]!, new RegExp(String(REQUIRED_JOBS.length))) + assert.match(result.lines[0]!, /`ci-ok` runs `node base\/scripts\/ci-ok\.mjs`/, 'the line names the id it checked') +}) + +test('the accepted aggregate ids are explicit data, and the publisher reads the same two names', async () => { + // Read the set through `import()` so this pin fails as an assertion on the + // unbroken tip instead of as a module-load error that hides the four + // acceptance cases below. + const module: Record = await import('../scripts/lib/gate-compare.mjs') + assert.deepEqual(module.AGGREGATE_JOBS, ['ci-ok-head', 'ci-ok'], 'the transition set is exact: nothing outside it may pass') + assert.equal(AGGREGATE_JOB, 'ci-ok', 'the base tree declares `ci-ok` today; #1273 moves it to `ci-ok-head`') + const publisher = read('scripts/lib/ci-ok-run.mjs') + assert.match(publisher, /export const HEAD_AGGREGATE_JOB = 'ci-ok-head'/, 'the publisher and the comparator agree on the new id') + assert.match(publisher, /export const REQUIRED_CHECK = 'ci-ok'/, 'and on the old one') }) test('acceptance 1: a required job stubbed to `run: true` is refused, naming the job and the body', () => { @@ -272,6 +307,66 @@ test('the `path: base` checkout is pinned: removing it is a refusal', () => { assert.match(result.problems.join('\n'), /does not check the gate out under `path: base`/) }) +test('acceptance 1: an aggregate id outside the accepted set is refused, naming what it looked for and what the head declares', () => { + for (const [label, head] of [ + ['renamed to an unknown id', aggregateRename('ci-ok-v2')], + ['deleted', aggregateDeleted()], + ] as const) { + assert.notEqual(head, ci, `${label}: the fixture changed the aggregate`) + const result = compareGate({ base: ci, head }) + assert.equal(result.ok, false, `${label} must not skip the three aggregate checks`) + const text = result.problems.join('\n') + assert.match(text, /declares no aggregate job the gate accepts/, label) + assert.match(text, /looked for `ci-ok-head` or `ci-ok`/, `${label}: the ids it looked for are named`) + assert.match(text, /the head declares /, `${label}: the ids the head does declare are named`) + assert.equal(result.lines.length, 0, `${label}: a refusal carries no line that could be read as a pass`) + assert.doesNotMatch(text, /`ci-ok` runs /, `${label}: the success line cannot claim a check that did not run`) + } + const renamed = compareGate({ base: ci, head: aggregateRename('ci-ok-v2') }).problems.join('\n') + assert.match(renamed, /the head declares [^)]*`ci-ok-v2`/, 'the id the head declares instead is named in the refusal') +}) + +test('acceptance 2: a head declaring both accepted aggregate ids is refused, naming both', () => { + const head = withJob(ci, 'ci-ok-head', [' name: ci-ok-head', ...STUB_ROWS]) + assert.notEqual(head, ci, 'the fixture added the second candidate') + const result = compareGate({ base: ci, head }) + assert.equal(result.ok, false, 'two candidates for one aggregate is not a pass') + const text = result.problems.join('\n') + assert.match(text, /more than one aggregate job/) + assert.match(text, /`ci-ok-head` and `ci-ok`/, 'both declared ids are named') + assert.equal(result.lines.length, 0) +}) + +test('acceptance 3: a head with exactly one accepted aggregate id passes, and the line names the id it checked', () => { + const head = aggregateRename('ci-ok-head') + const result = compareGate({ base: ci, head }) + assert.equal(result.ok, true, result.problems.join('\n')) + assert.equal(result.lines.length, 1) + assert.match(result.lines[0]!, /`ci-ok-head` runs `node base\/scripts\/ci-ok\.mjs`/, 'the green line names the id it read') + assert.doesNotMatch(result.lines[0]!, /`ci-ok` runs /, 'a green line cannot credit an id that was not read') +}) + +test('acceptance 4: the three aggregate checks follow the accepted id the head declares', () => { + const renamed = aggregateRename('ci-ok-head') + const droppedNeeds = editJob(renamed, 'ci-ok-head', (block) => + block.map((line) => (line.startsWith(' needs: ') ? line.replace('mock-check, ', '') : line)), + ) + const noEvaluator = editJob(renamed, 'ci-ok-head', (block) => + block.map((line) => (line.trim() === `run: ${REQUIRED_EVALUATOR}` ? ' run: exit 0' : line)), + ) + const noCheckout = editJob(renamed, 'ci-ok-head', (block) => block.filter((line) => line.trim() !== 'path: base')) + + for (const [label, head, expected] of [ + ['needs', droppedNeeds, /`ci-ok-head` does not depend on required job `mock-check`/], + ['evaluator', noEvaluator, /`ci-ok-head` does not run the base evaluator/], + ['path: base', noCheckout, /`ci-ok-head` does not check the gate out under `path: base`/], + ] as const) { + const result = compareGate({ base: ci, head }) + assert.equal(result.ok, false, `${label} must still be refused when the aggregate id is renamed`) + assert.match(result.problems.join('\n'), expected, label) + } +}) + test('acceptance 5: a duplicated job, a duplicated key or an unreadable line is refused, not skipped', () => { const duplicateJob = mini((text) => text.replace(' ci-ok:\n', ' build:\n name: build\n ci-ok:\n')) const duplicateKey = mini((text) => text.replace(' runs-on: ubuntu-latest\n', ' runs-on: ubuntu-latest\n runs-on: ubuntu-24.04\n'))