From cef5709b84f60366fe4d1c9dd143e9706e566d27 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 12:35:29 -0500 Subject: [PATCH 1/4] fix(ci): native-queue-policy skips the evidence wait on cosmetic label flips Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .github/scripts/native-queue-policy.cjs | 11 ++++++++++- .github/scripts/test-native-queue-workflows.py | 17 +++++++++++++++++ .github/workflows/native-queue-policy.yml | 12 ++++++++++++ ...ve-queue-policy-cosmetic-label-early-exit.md | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index 1f4b7e82d2..06df72e2a8 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -217,6 +217,15 @@ async function evaluate(args) { } } +// A labeled/unlabeled event only matters when the label is one assertEligible +// reads (BLOCKED_LABEL); every other flip cannot change the verdict. +function isCosmeticLabelFlip({ eventName, payload }) { + return eventName === 'pull_request_target' && + ['labeled', 'unlabeled'].includes(payload.action) && + typeof payload.label?.name === 'string' && + !BLOCKED_LABEL.test(payload.label.name); +} + async function evaluateOnce({ github, context }) { const repo = context.repo; if (context.eventName === 'pull_request_target') { @@ -270,7 +279,7 @@ async function evaluateOnce({ github, context }) { module.exports = { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, - isTransientAPIError, + isTransientAPIError, isCosmeticLabelFlip, BLOCKED_LABEL, ProviderUnavailable, DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK, DCO_PROVIDER_UNAVAILABLE_TEXT, DCO_PROVIDER_RUNBOOK, diff --git a/.github/scripts/test-native-queue-workflows.py b/.github/scripts/test-native-queue-workflows.py index 429a771340..cdb009dcdf 100644 --- a/.github/scripts/test-native-queue-workflows.py +++ b/.github/scripts/test-native-queue-workflows.py @@ -74,6 +74,23 @@ def test_owned_gate_retriggers_never_cancel_the_evidence_wait(self): self.assertIn("github.event.pull_request.number", concurrency["group"]) self.assertIs(concurrency["cancel-in-progress"], False) + def test_cosmetic_label_flips_skip_the_evidence_wait(self): + # Only labels matching the policy's BLOCKED_LABEL are read; any other + # labeled/unlabeled flip must skip the 22.5 min wait (#16945) while + # the job still concludes successfully. + steps = workflow("native-queue-policy.yml")["jobs"]["policy"]["steps"] + gate = next(s for s in steps + if s.get("name") == "Skip evidence wait for cosmetic label flips") + self.assertEqual(gate["id"], "label-flip") + self.assertIn("isCosmeticLabelFlip", gate["with"]["script"]) + self.assertNotIn("setFailed", gate["with"]["script"]) + helper = (ROOT / ".github/scripts/native-queue-policy.cjs").read_text() + self.assertIn("['labeled', 'unlabeled'].includes(payload.action)", helper) + self.assertIn("!BLOCKED_LABEL.test(payload.label.name)", helper) + wait = next(s for s in steps if s.get("name", "").startswith("Validate exact-head evidence")) + self.assertIn("steps.label-flip.outputs.cosmetic != 'true'", wait["if"]) + self.assertLess(steps.index(gate), steps.index(wait)) + def test_dependabot_runs_the_same_trusted_pr_verifier(self): verifier = workflow("pr-verifier.yml") events = verifier.get("on", verifier.get(True)) diff --git a/.github/workflows/native-queue-policy.yml b/.github/workflows/native-queue-policy.yml index 14e47553dc..aff1f8de39 100644 --- a/.github/workflows/native-queue-policy.yml +++ b/.github/workflows/native-queue-policy.yml @@ -39,7 +39,19 @@ jobs: persist-credentials: false - name: Test policy behavior run: node --test .github/scripts/native-queue-policy.test.cjs .github/scripts/diagnose-dco-provider.test.cjs + - name: Skip evidence wait for cosmetic label flips + id: label-flip + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const { isCosmeticLabelFlip } = require('./.github/scripts/native-queue-policy.cjs'); + const cosmetic = isCosmeticLabelFlip(context); + core.setOutput('cosmetic', String(cosmetic)); + if (cosmetic) { + core.info('Label "' + context.payload.label.name + '" is not read by the policy; skipping the evidence wait.'); + } - name: Validate exact-head evidence (bounded wait for PR providers) + if: steps.label-flip.outputs.cosmetic != 'true' uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | diff --git a/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md b/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md new file mode 100644 index 0000000000..ae5b38d6b9 --- /dev/null +++ b/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md @@ -0,0 +1 @@ +- `native-queue-policy` no longer spends a ~22-minute evidence wait on label flips it never reads — `size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels ([#16945](https://github.com/hivecommons/hive/issues/16945)). Flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` still run the full exact-head check, and draft/closed/head state is still read from the PR itself. From f373ec9366142936260853e3b2f3c32cf8f1f1bf Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:43:04 -0500 Subject: [PATCH 2/4] fix(ci): native-queue-policy replays the last verdict on cosmetic label flips Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .github/scripts/native-queue-policy.cjs | 26 ++++++++- .github/scripts/native-queue-policy.test.cjs | 56 ++++++++++++++++++- .../scripts/test-native-queue-workflows.py | 7 +-- .github/workflows/native-queue-policy.yml | 11 +++- ...-queue-policy-cosmetic-label-early-exit.md | 1 - changelog.d/fixed-16952.md | 1 + src/docs/native-merge-queue.md | 7 ++- 7 files changed, 97 insertions(+), 12 deletions(-) delete mode 100644 changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md create mode 100644 changelog.d/fixed-16952.md diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index 06df72e2a8..3345eafbf9 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -21,6 +21,7 @@ const BUILD_CHECKS = { const SHA = /^[a-f0-9]{40}$/; const BLOCKED_LABEL = /^(hold|needs-human|release-in-progress|do-not-merge(?:\/.*)?)$/i; const MAX_QUEUE_PAGES = 10; +const POLICY_CHECK_NAME = 'native-queue-policy'; const closedNoop = (pr) => `PR #${pr.number} is closed; nothing to evaluate`; @@ -226,6 +227,29 @@ function isCosmeticLabelFlip({ eventName, payload }) { !BLOCKED_LABEL.test(payload.label.name); } +// A cosmetic flip skips the evidence wait but must not mint a fresh passing +// required check: replay the latest completed run of this check on the head +// (fail closed when none or not success) and still read live PR eligibility. +async function replayLastVerdict({ github, context, currentCheckRunId }) { + const repo = context.repo; + const event = context.payload.pull_request; + const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: event.number }); + if (pr.state === 'closed') return closedNoop(pr); + assertEligible(pr, event.head.sha, event.base.ref); + const runs = await github.paginate(github.rest.checks.listForRef, { + ...repo, ref: pr.head.sha, check_name: POLICY_CHECK_NAME, per_page: 100, filter: 'all', + }); + const prior = runs + .filter(r => r.name === POLICY_CHECK_NAME && r.status === 'completed' && + r.head_sha === pr.head.sha && r.app?.id === ACTIONS_APP_ID && r.id !== currentCheckRunId) + .sort((a, b) => b.id - a.id)[0]; + if (!prior) throw new Error(`No completed ${POLICY_CHECK_NAME} run on ${pr.head.sha} to replay`); + if (prior.conclusion !== 'success') { + throw new Error(`Replaying ${POLICY_CHECK_NAME} run ${prior.id} on ${pr.head.sha}: ${prior.conclusion}`); + } + return `PR #${pr.number}: replayed ${POLICY_CHECK_NAME} run ${prior.id} (success) on ${pr.head.sha}`; +} + async function evaluateOnce({ github, context }) { const repo = context.repo; if (context.eventName === 'pull_request_target') { @@ -279,7 +303,7 @@ async function evaluateOnce({ github, context }) { module.exports = { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, - isTransientAPIError, isCosmeticLabelFlip, BLOCKED_LABEL, + isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, BLOCKED_LABEL, ProviderUnavailable, DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK, DCO_PROVIDER_UNAVAILABLE_TEXT, DCO_PROVIDER_RUNBOOK, diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index 07e5eeb76e..785d96826b 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -4,7 +4,7 @@ const assert = require('node:assert/strict'); const { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, - isTransientAPIError, + isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, ProviderUnavailable, DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK, @@ -387,3 +387,57 @@ test('API failure is never interpreted as an empty successful member list', asyn f.github.graphql = async () => { throw new Error('403 queue read forbidden'); }; await assert.rejects(evaluate(f), /403/); }); + +test('isCosmeticLabelFlip is true only for unread labels on label events', () => { + const ctx = (eventName, action, label) => ({ eventName, payload: { action, ...(label && { label: { name: label } }) } }); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'size/L')), true); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'unlabeled', 'size/L')), true); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'hold')), false); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled', 'do-not-merge/foo')), false); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'synchronize', 'size/L')), false); + assert.equal(isCosmeticLabelFlip(ctx('merge_group', 'checks_requested')), false); + assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled')), false); +}); + +function replayFixture(priorRuns, labels = []) { + const f = fixture('pull_request_target'); + f.state.prs[0].labels = labels; + const head = sha(11); + f.state.listed = []; + f.github.paginate = async (method, args) => { + assert.equal(method, 'checks'); + f.state.listed.push(args); + return clone(priorRuns).map(r => ({ + name: 'native-queue-policy', head_sha: head, app: { id: ACTIONS_APP_ID }, ...r, + })); + }; + return f; +} + +test('cosmetic flip replays the latest completed success on the same head', async () => { + const f = replayFixture([ + { id: 5, status: 'completed', conclusion: 'failure' }, + { id: 7, status: 'completed', conclusion: 'success' }, + { id: 9, status: 'in_progress', conclusion: null }, + ]); + assert.match(await replayLastVerdict(f), /run 7 \(success\)/); + assert.equal(f.state.listed[0].ref, sha(11)); +}); + +test('cosmetic flip replays a prior failure and fails closed without a completed run', async () => { + await assert.rejects(replayLastVerdict(replayFixture([ + { id: 5, status: 'completed', conclusion: 'success' }, + { id: 7, status: 'completed', conclusion: 'failure' }, + ])), /run 7.*failure/); + await assert.rejects(replayLastVerdict(replayFixture([ + { id: 9, status: 'in_progress', conclusion: null }, + ])), /No completed/); + await assert.rejects(replayLastVerdict(replayFixture([])), /No completed/); + const own = replayFixture([{ id: 8, status: 'completed', conclusion: 'success' }]); + await assert.rejects(replayLastVerdict({ ...own, currentCheckRunId: 8 }), /No completed/); +}); + +test('cosmetic flip still honours a live hold even after a prior success', async () => { + const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }], [{ name: 'hold' }]); + await assert.rejects(replayLastVerdict(f), /held/); +}); diff --git a/.github/scripts/test-native-queue-workflows.py b/.github/scripts/test-native-queue-workflows.py index cdb009dcdf..c639fe4a2d 100644 --- a/.github/scripts/test-native-queue-workflows.py +++ b/.github/scripts/test-native-queue-workflows.py @@ -80,13 +80,10 @@ def test_cosmetic_label_flips_skip_the_evidence_wait(self): # the job still concludes successfully. steps = workflow("native-queue-policy.yml")["jobs"]["policy"]["steps"] gate = next(s for s in steps - if s.get("name") == "Skip evidence wait for cosmetic label flips") + if s.get("name") == "Replay last verdict for cosmetic label flips") self.assertEqual(gate["id"], "label-flip") self.assertIn("isCosmeticLabelFlip", gate["with"]["script"]) - self.assertNotIn("setFailed", gate["with"]["script"]) - helper = (ROOT / ".github/scripts/native-queue-policy.cjs").read_text() - self.assertIn("['labeled', 'unlabeled'].includes(payload.action)", helper) - self.assertIn("!BLOCKED_LABEL.test(payload.label.name)", helper) + self.assertIn("replayLastVerdict", gate["with"]["script"]) wait = next(s for s in steps if s.get("name", "").startswith("Validate exact-head evidence")) self.assertIn("steps.label-flip.outputs.cosmetic != 'true'", wait["if"]) self.assertLess(steps.index(gate), steps.index(wait)) diff --git a/.github/workflows/native-queue-policy.yml b/.github/workflows/native-queue-policy.yml index aff1f8de39..65afbcb16e 100644 --- a/.github/workflows/native-queue-policy.yml +++ b/.github/workflows/native-queue-policy.yml @@ -39,16 +39,21 @@ jobs: persist-credentials: false - name: Test policy behavior run: node --test .github/scripts/native-queue-policy.test.cjs .github/scripts/diagnose-dco-provider.test.cjs - - name: Skip evidence wait for cosmetic label flips + - name: Replay last verdict for cosmetic label flips id: label-flip uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | - const { isCosmeticLabelFlip } = require('./.github/scripts/native-queue-policy.cjs'); + const { isCosmeticLabelFlip, replayLastVerdict } = require('./.github/scripts/native-queue-policy.cjs'); const cosmetic = isCosmeticLabelFlip(context); core.setOutput('cosmetic', String(cosmetic)); if (cosmetic) { - core.info('Label "' + context.payload.label.name + '" is not read by the policy; skipping the evidence wait.'); + core.info('Label "' + context.payload.label.name + '" is not read by the policy; replaying the last completed verdict instead of waiting for evidence.'); + try { + core.info(await replayLastVerdict({github, context})); + } catch (error) { + core.setFailed(error.message); + } } - name: Validate exact-head evidence (bounded wait for PR providers) if: steps.label-flip.outputs.cosmetic != 'true' diff --git a/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md b/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md deleted file mode 100644 index ae5b38d6b9..0000000000 --- a/changelog.d/changed-native-queue-policy-cosmetic-label-early-exit.md +++ /dev/null @@ -1 +0,0 @@ -- `native-queue-policy` no longer spends a ~22-minute evidence wait on label flips it never reads — `size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels ([#16945](https://github.com/hivecommons/hive/issues/16945)). Flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` still run the full exact-head check, and draft/closed/head state is still read from the PR itself. diff --git a/changelog.d/fixed-16952.md b/changelog.d/fixed-16952.md new file mode 100644 index 0000000000..75bcdc691b --- /dev/null +++ b/changelog.d/fixed-16952.md @@ -0,0 +1 @@ +- `native-queue-policy` no longer spends its ~22-minute evidence wait on label flips it never reads (`size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels); instead it replays the conclusion of the latest completed `native-queue-policy` run on the same head (failing closed when that run was not `success` or none exists) rather than fabricating a pass, and still applies the live hold/draft/head-move check, while flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` still run the full exact-head evidence wait ([#16945](https://github.com/hivecommons/hive/issues/16945)). diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index b21f83ad93..0b232643d4 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -218,7 +218,12 @@ metadata, not a fabricated last-member PR event. Label, edit, or unlabel events re-trigger this workflow and serialize behind any in-flight run (the concurrency group is per-PR and never cancels in-progress); the queued run re-reads mutable PR state after the prior run - finishes, preserving fail-closed semantics. + finishes, preserving fail-closed semantics. Labels the policy never reads + (`size/*`, `dco-signoff`, stage labels, ...) skip the wait: the job replays + the latest completed `native-queue-policy` conclusion on the same head + (failing when it was not `success` or none exists) and still checks the + live PR for holds, draft and head moves. `hold`, `needs-human`, + `release-in-progress` and `do-not-merge*` flips get the full wait. - On a group it executes the event's trusted `base_sha` validator. It reads the live queue prefix ending at the event's exact synthetic head, verifies contiguous membership and the base/head commit chain, and checks From 370db0233aabf0c18040636c63e4d3a6204718e5 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 05:38:09 -0500 Subject: [PATCH 3/4] fix(ci): bind replayed native-queue-policy verdicts to workflow provenance replayLastVerdict accepted any completed check run named native-queue-policy from the Actions App. All Actions workflows share that App id, so a fork PR could add a workflow with a job of that name that exits 0 and have the forged success replayed on the next cosmetic label flip. Extract the provenance block of assertEvidence into assertActionsProvenance and apply it to the replayed run, requiring native-queue-policy.yml on pull_request_target with matching run, check suite and job identity. Add negative tests for name, head_sha, app id and workflow mismatches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .github/scripts/native-queue-policy.cjs | 72 +++++++++++--------- .github/scripts/native-queue-policy.test.cjs | 52 ++++++++++++-- 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index 3345eafbf9..0e18d4779b 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -22,6 +22,7 @@ const SHA = /^[a-f0-9]{40}$/; const BLOCKED_LABEL = /^(hold|needs-human|release-in-progress|do-not-merge(?:\/.*)?)$/i; const MAX_QUEUE_PAGES = 10; const POLICY_CHECK_NAME = 'native-queue-policy'; +const POLICY_CHECK_SOURCE = ['native-queue-policy.yml', ['pull_request_target']]; const closedNoop = (pr) => `PR #${pr.number} is closed; nothing to evaluate`; @@ -112,6 +113,41 @@ function evidenceForHead(runs, head, checks) { }); } +// Every Actions workflow shares one App id, so a same-named check run proves +// nothing: bind it to the expected workflow file, event, head, suite and job. +async function assertActionsProvenance(github, repo, number, head, run, source, workflows) { + const url = new URL(run.details_url); + const prefix = `/${repo.owner}/${repo.repo}/actions/runs/`; + const id = url.pathname.startsWith(prefix) && url.pathname.slice(prefix.length).split('/')[0]; + if (url.hostname !== 'github.com' || !/^[0-9]+$/.test(id)) { + throw new Error(`Invalid Actions provenance for ${run.name}`); + } + const runID = Number(id); + if (!workflows.has(runID)) { + const { data: workflow } = await github.rest.actions.getWorkflowRun({ ...repo, run_id: runID }); + const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, { + ...repo, run_id: runID, per_page: 100, filter: 'latest', + }); + workflows.set(runID, { workflow, jobs }); + } + const { workflow, jobs } = workflows.get(runID); + const [file, events] = source; + if (workflow.path !== `.github/workflows/${file}` || !events.includes(workflow.event) || + workflow.head_sha !== head || workflow.id !== runID) { + throw new Error(`Unexpected workflow/event/head for ${run.name} on #${number}`); + } + // details_url is only a locator. The API-owned suite and job identities + // must independently bind this exact check run to the expected workflow. + const checkURL = `https://api.github.com/repos/${repo.owner}/${repo.repo}/check-runs/${run.id}`; + if (!Number.isSafeInteger(run.check_suite?.id) || + run.check_suite.id !== workflow.check_suite_id || + !jobs.some(job => job.run_id === runID && job.name === run.name && + job.head_sha === head && job.check_run_url === checkURL && + job.status === 'completed' && job.conclusion === run.conclusion)) { + throw new Error(`Check suite/job identity mismatch for ${run.name} on #${number}`); + } +} + async function assertEvidence(github, repo, number, head, checks) { const runs = await github.paginate(github.rest.checks.listForRef, { ...repo, ref: head, per_page: 100, filter: 'latest', @@ -119,36 +155,7 @@ async function assertEvidence(github, repo, number, head, checks) { const workflows = new Map(); for (const { run, source } of evidenceForHead(runs, head, checks)) { if (!source) continue; // Probot DCO is deliberately not impersonated. - const url = new URL(run.details_url); - const prefix = `/${repo.owner}/${repo.repo}/actions/runs/`; - const id = url.pathname.startsWith(prefix) && url.pathname.slice(prefix.length).split('/')[0]; - if (url.hostname !== 'github.com' || !/^[0-9]+$/.test(id)) { - throw new Error(`Invalid Actions provenance for ${run.name}`); - } - const runID = Number(id); - if (!workflows.has(runID)) { - const { data: workflow } = await github.rest.actions.getWorkflowRun({ ...repo, run_id: runID }); - const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, { - ...repo, run_id: runID, per_page: 100, filter: 'latest', - }); - workflows.set(runID, { workflow, jobs }); - } - const { workflow, jobs } = workflows.get(runID); - const [file, events] = source; - if (workflow.path !== `.github/workflows/${file}` || !events.includes(workflow.event) || - workflow.head_sha !== head || workflow.id !== runID) { - throw new Error(`Unexpected workflow/event/head for ${run.name} on #${number}`); - } - // details_url is only a locator. The API-owned suite and job identities - // must independently bind this exact check run to the expected workflow. - const checkURL = `https://api.github.com/repos/${repo.owner}/${repo.repo}/check-runs/${run.id}`; - if (!Number.isSafeInteger(run.check_suite?.id) || - run.check_suite.id !== workflow.check_suite_id || - !jobs.some(job => job.run_id === runID && job.name === run.name && - job.head_sha === head && job.check_run_url === checkURL && - job.status === 'completed' && job.conclusion === run.conclusion)) { - throw new Error(`Check suite/job identity mismatch for ${run.name} on #${number}`); - } + await assertActionsProvenance(github, repo, number, head, run, source, workflows); } } @@ -229,7 +236,7 @@ function isCosmeticLabelFlip({ eventName, payload }) { // A cosmetic flip skips the evidence wait but must not mint a fresh passing // required check: replay the latest completed run of this check on the head -// (fail closed when none or not success) and still read live PR eligibility. +// (fail closed when none, not success, or not from this workflow) and still read live PR eligibility. async function replayLastVerdict({ github, context, currentCheckRunId }) { const repo = context.repo; const event = context.payload.pull_request; @@ -244,6 +251,9 @@ async function replayLastVerdict({ github, context, currentCheckRunId }) { r.head_sha === pr.head.sha && r.app?.id === ACTIONS_APP_ID && r.id !== currentCheckRunId) .sort((a, b) => b.id - a.id)[0]; if (!prior) throw new Error(`No completed ${POLICY_CHECK_NAME} run on ${pr.head.sha} to replay`); + // A same-named job from any other workflow shares the Actions App id; only + // a run of this workflow file on this event may be replayed. + await assertActionsProvenance(github, repo, pr.number, pr.head.sha, prior, POLICY_CHECK_SOURCE, new Map()); if (prior.conclusion !== 'success') { throw new Error(`Replaying ${POLICY_CHECK_NAME} run ${prior.id} on ${pr.head.sha}: ${prior.conclusion}`); } diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index 785d96826b..3cdaca7bf5 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -399,17 +399,33 @@ test('isCosmeticLabelFlip is true only for unread labels on label events', () => assert.equal(isCosmeticLabelFlip(ctx('pull_request_target', 'labeled')), false); }); -function replayFixture(priorRuns, labels = []) { +function replayFixture(priorRuns, labels = [], workflowOverride = {}) { const f = fixture('pull_request_target'); f.state.prs[0].labels = labels; const head = sha(11); f.state.listed = []; + const runs = clone(priorRuns).map(r => ({ + name: 'native-queue-policy', head_sha: head, app: { id: ACTIONS_APP_ID }, + check_suite: { id: 5000 + r.id }, + details_url: `https://github.com/acme/widget/actions/runs/${3000 + r.id}/job/1`, ...r, + })); + f.github.rest.actions.getWorkflowRun = async ({ run_id: id }) => ({ data: { + id, check_suite_id: id + 2000, path: '.github/workflows/native-queue-policy.yml', + event: 'pull_request_target', head_sha: head, ...workflowOverride, + } }); f.github.paginate = async (method, args) => { + if (method === 'jobs') { + const run = runs.find(r => 3000 + r.id === args.run_id); + if (!run) return []; + return [{ + id: 200000 + run.id, run_id: args.run_id, name: run.name, head_sha: head, + check_run_url: `https://api.github.com/repos/acme/widget/check-runs/${run.id}`, + status: 'completed', conclusion: run.conclusion, + }]; + } assert.equal(method, 'checks'); f.state.listed.push(args); - return clone(priorRuns).map(r => ({ - name: 'native-queue-policy', head_sha: head, app: { id: ACTIONS_APP_ID }, ...r, - })); + return clone(runs); }; return f; } @@ -441,3 +457,31 @@ test('cosmetic flip still honours a live hold even after a prior success', async const f = replayFixture([{ id: 7, status: 'completed', conclusion: 'success' }], [{ name: 'hold' }]); await assert.rejects(replayLastVerdict(f), /held/); }); + +test('replay ignores runs with a different name, head_sha or app id', async () => { + for (const change of [ + { name: 'other-check' }, { head_sha: sha(99) }, { app: { id: DCO_APP_ID } }, { app: undefined }, + ]) { + await assert.rejects( + replayLastVerdict(replayFixture([{ id: 7, status: 'completed', conclusion: 'success', ...change }])), + /No completed/); + } +}); + +test('replay rejects a same-named success run from another workflow', async () => { + const run = { id: 7, status: 'completed', conclusion: 'success' }; + for (const override of [ + { path: '.github/workflows/forged.yml' }, + { event: 'pull_request' }, + { head_sha: sha(99) }, + { check_suite_id: 1 }, + ]) { + await assert.rejects(replayLastVerdict(replayFixture([run], [], override)), /Unexpected workflow|identity mismatch/); + } + await assert.rejects(replayLastVerdict(replayFixture([ + { ...run, details_url: 'https://example.com/acme/widget/actions/runs/3007/job/1' }, + ])), /Invalid Actions provenance/); + await assert.rejects(replayLastVerdict(replayFixture([ + { ...run, details_url: 'https://github.com/acme/widget/actions/runs/3008/job/1' }, + ])), /Unexpected workflow|identity mismatch/); +}); From 9687acdee30c9a3fe74abf5268808b3ecf8f80d0 Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sat, 7 Nov 2026 07:09:23 -0500 Subject: [PATCH 4/4] fix(ci): native-queue-policy falls through to the full wait when there is no success to replay A cosmetic label flip used to replay whatever the latest completed native-queue-policy run concluded, so one failed or timed-out (cancelled) wait pinned the required check red on every later size/*, dco-signoff or stage-label flip until a push, edit or hold flip re-entered the full wait. replayLastVerdict now returns null when there is no prior success on the head, and the workflow step then runs the authoritative evidence wait (attempts/15s retry unchanged) instead of failing; a 5xx during the replay reads takes the same route, matching the transient-API handling the main path already has (#16258). Provenance mismatches still fail closed. Refs #16945 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .github/scripts/native-queue-policy.cjs | 14 +++++----- .github/scripts/native-queue-policy.test.cjs | 26 ++++++++++++------- .../scripts/test-native-queue-workflows.py | 5 ++++ .github/workflows/native-queue-policy.yml | 24 +++++++++++++---- changelog.d/fixed-16952.md | 2 +- src/docs/native-merge-queue.md | 11 +++++--- 6 files changed, 56 insertions(+), 26 deletions(-) diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index 0e18d4779b..5baeb710e0 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -235,8 +235,12 @@ function isCosmeticLabelFlip({ eventName, payload }) { } // A cosmetic flip skips the evidence wait but must not mint a fresh passing -// required check: replay the latest completed run of this check on the head -// (fail closed when none, not success, or not from this workflow) and still read live PR eligibility. +// required check: replay the latest completed `success` run of this check on +// the head (rejecting one not from this workflow) and still read live PR +// eligibility. Returns null when there is nothing to replay — no completed +// run, or the latest one did not succeed (a failed, cancelled or timed-out +// wait) — so the caller falls through to the full evidence wait instead of +// pinning that earlier verdict onto every later stage-label flip. async function replayLastVerdict({ github, context, currentCheckRunId }) { const repo = context.repo; const event = context.payload.pull_request; @@ -250,13 +254,11 @@ async function replayLastVerdict({ github, context, currentCheckRunId }) { .filter(r => r.name === POLICY_CHECK_NAME && r.status === 'completed' && r.head_sha === pr.head.sha && r.app?.id === ACTIONS_APP_ID && r.id !== currentCheckRunId) .sort((a, b) => b.id - a.id)[0]; - if (!prior) throw new Error(`No completed ${POLICY_CHECK_NAME} run on ${pr.head.sha} to replay`); + if (!prior) return null; // A same-named job from any other workflow shares the Actions App id; only // a run of this workflow file on this event may be replayed. await assertActionsProvenance(github, repo, pr.number, pr.head.sha, prior, POLICY_CHECK_SOURCE, new Map()); - if (prior.conclusion !== 'success') { - throw new Error(`Replaying ${POLICY_CHECK_NAME} run ${prior.id} on ${pr.head.sha}: ${prior.conclusion}`); - } + if (prior.conclusion !== 'success') return null; return `PR #${pr.number}: replayed ${POLICY_CHECK_NAME} run ${prior.id} (success) on ${pr.head.sha}`; } diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index 3cdaca7bf5..7a08dfec2f 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -440,17 +440,23 @@ test('cosmetic flip replays the latest completed success on the same head', asyn assert.equal(f.state.listed[0].ref, sha(11)); }); -test('cosmetic flip replays a prior failure and fails closed without a completed run', async () => { - await assert.rejects(replayLastVerdict(replayFixture([ +test('cosmetic flip has nothing to replay without a prior success and falls through', async () => { + // A failed, cancelled (timed-out) or absent prior run must not be pinned + // onto every later stage-label flip: null sends the caller to the full wait. + assert.equal(await replayLastVerdict(replayFixture([ { id: 5, status: 'completed', conclusion: 'success' }, { id: 7, status: 'completed', conclusion: 'failure' }, - ])), /run 7.*failure/); - await assert.rejects(replayLastVerdict(replayFixture([ + ])), null); + assert.equal(await replayLastVerdict(replayFixture([ + { id: 5, status: 'completed', conclusion: 'success' }, + { id: 7, status: 'completed', conclusion: 'cancelled' }, + ])), null); + assert.equal(await replayLastVerdict(replayFixture([ { id: 9, status: 'in_progress', conclusion: null }, - ])), /No completed/); - await assert.rejects(replayLastVerdict(replayFixture([])), /No completed/); + ])), null); + assert.equal(await replayLastVerdict(replayFixture([])), null); const own = replayFixture([{ id: 8, status: 'completed', conclusion: 'success' }]); - await assert.rejects(replayLastVerdict({ ...own, currentCheckRunId: 8 }), /No completed/); + assert.equal(await replayLastVerdict({ ...own, currentCheckRunId: 8 }), null); }); test('cosmetic flip still honours a live hold even after a prior success', async () => { @@ -462,9 +468,9 @@ test('replay ignores runs with a different name, head_sha or app id', async () = for (const change of [ { name: 'other-check' }, { head_sha: sha(99) }, { app: { id: DCO_APP_ID } }, { app: undefined }, ]) { - await assert.rejects( - replayLastVerdict(replayFixture([{ id: 7, status: 'completed', conclusion: 'success', ...change }])), - /No completed/); + assert.equal( + await replayLastVerdict(replayFixture([{ id: 7, status: 'completed', conclusion: 'success', ...change }])), + null); } }); diff --git a/.github/scripts/test-native-queue-workflows.py b/.github/scripts/test-native-queue-workflows.py index c639fe4a2d..e598728df9 100644 --- a/.github/scripts/test-native-queue-workflows.py +++ b/.github/scripts/test-native-queue-workflows.py @@ -84,6 +84,11 @@ def test_cosmetic_label_flips_skip_the_evidence_wait(self): self.assertEqual(gate["id"], "label-flip") self.assertIn("isCosmeticLabelFlip", gate["with"]["script"]) self.assertIn("replayLastVerdict", gate["with"]["script"]) + # Nothing to replay (no prior success, or a transient API error) must + # fall through to the full wait rather than fail or fabricate a pass. + self.assertIn("replayed === null", gate["with"]["script"]) + self.assertIn("isTransientAPIError(error)", gate["with"]["script"]) + self.assertIn("cosmetic = false", gate["with"]["script"]) wait = next(s for s in steps if s.get("name", "").startswith("Validate exact-head evidence")) self.assertIn("steps.label-flip.outputs.cosmetic != 'true'", wait["if"]) self.assertLess(steps.index(gate), steps.index(wait)) diff --git a/.github/workflows/native-queue-policy.yml b/.github/workflows/native-queue-policy.yml index 65afbcb16e..9469ed0203 100644 --- a/.github/workflows/native-queue-policy.yml +++ b/.github/workflows/native-queue-policy.yml @@ -44,17 +44,31 @@ jobs: uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: script: | - const { isCosmeticLabelFlip, replayLastVerdict } = require('./.github/scripts/native-queue-policy.cjs'); - const cosmetic = isCosmeticLabelFlip(context); - core.setOutput('cosmetic', String(cosmetic)); + const { isCosmeticLabelFlip, replayLastVerdict, isTransientAPIError } = require('./.github/scripts/native-queue-policy.cjs'); + let cosmetic = isCosmeticLabelFlip(context); if (cosmetic) { core.info('Label "' + context.payload.label.name + '" is not read by the policy; replaying the last completed verdict instead of waiting for evidence.'); try { - core.info(await replayLastVerdict({github, context})); + const replayed = await replayLastVerdict({github, context}); + if (replayed === null) { + // No prior success on this head (never evaluated, or the last + // wait failed/timed out): run the authoritative wait instead of + // pinning that verdict onto every later cosmetic flip. + core.info('No successful native-queue-policy run on this head to replay; running the full evidence wait.'); + cosmetic = false; + } else { + core.info(replayed); + } } catch (error) { - core.setFailed(error.message); + if (isTransientAPIError(error)) { + core.info('GitHub API ' + error.status + ' while replaying; running the full evidence wait (which retries) instead.'); + cosmetic = false; + } else { + core.setFailed(error.message); + } } } + core.setOutput('cosmetic', String(cosmetic)); - name: Validate exact-head evidence (bounded wait for PR providers) if: steps.label-flip.outputs.cosmetic != 'true' uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 diff --git a/changelog.d/fixed-16952.md b/changelog.d/fixed-16952.md index 75bcdc691b..a6a30ce219 100644 --- a/changelog.d/fixed-16952.md +++ b/changelog.d/fixed-16952.md @@ -1 +1 @@ -- `native-queue-policy` no longer spends its ~22-minute evidence wait on label flips it never reads (`size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels); instead it replays the conclusion of the latest completed `native-queue-policy` run on the same head (failing closed when that run was not `success` or none exists) rather than fabricating a pass, and still applies the live hold/draft/head-move check, while flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` still run the full exact-head evidence wait ([#16945](https://github.com/hivecommons/hive/issues/16945)). +- `native-queue-policy` no longer spends its ~22-minute evidence wait on label flips it never reads (`size/*`, `dco-signoff`, `hive/claimed-by-*`, `copilot`, stage labels); instead it replays the latest successful `native-queue-policy` run on the same head rather than fabricating a pass, still applies the live hold/draft/head-move check, and falls through to the full exact-head evidence wait when there is no prior success to replay (the head was never evaluated, or the last wait failed or timed out) or GitHub returns a 5xx, so a stale red verdict is never pinned onto later cosmetic flips. Flips of `hold`, `needs-human`, `release-in-progress` and `do-not-merge` always run the full wait ([#16945](https://github.com/hivecommons/hive/issues/16945)). diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index 0b232643d4..ebd97271be 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -220,10 +220,13 @@ metadata, not a fabricated last-member PR event. in-progress); the queued run re-reads mutable PR state after the prior run finishes, preserving fail-closed semantics. Labels the policy never reads (`size/*`, `dco-signoff`, stage labels, ...) skip the wait: the job replays - the latest completed `native-queue-policy` conclusion on the same head - (failing when it was not `success` or none exists) and still checks the - live PR for holds, draft and head moves. `hold`, `needs-human`, - `release-in-progress` and `do-not-merge*` flips get the full wait. + the latest completed `native-queue-policy` `success` on the same head and + still checks the live PR for holds, draft and head moves. When there is no + prior success to replay (never evaluated, or the last wait failed or timed + out) or GitHub returns a 5xx, the job falls through to the full wait so a + stale red verdict is never pinned onto later cosmetic flips. `hold`, + `needs-human`, `release-in-progress` and `do-not-merge*` flips always get + the full wait. - On a group it executes the event's trusted `base_sha` validator. It reads the live queue prefix ending at the event's exact synthetic head, verifies contiguous membership and the base/head commit chain, and checks