diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 301969e59c..43e502bfe7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,6 +115,9 @@ jobs: - name: Player-facing name check run: bash scripts/ci/player_facing_names.sh + - name: Workflow policy + run: python3 scripts/ci/test_workflow_policy.py + - name: Rust CI setup if: needs.changes.outputs.docs_only != 'true' uses: ./.github/actions/rust-ci-setup diff --git a/.github/workflows/nightly-failure-issue.yml b/.github/workflows/nightly-failure-issue.yml index f678ee9302..4d7535d7b9 100644 --- a/.github/workflows/nightly-failure-issue.yml +++ b/.github/workflows/nightly-failure-issue.yml @@ -8,7 +8,7 @@ on: workflow_run: workflows: - Comprehensive CI Pipeline - - Determinism Replay + - determinism-replay - Performance Benchmarks - Sanitizers - Code Coverage @@ -22,9 +22,11 @@ on: - Allocation Measurement types: [completed] +# One workflow_run webhook per upstream failure; key by that run id so the 13 +# watched workflows never cancel each other's pending reports (BS-011 / #576). concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + group: ${{ github.workflow }}-${{ github.event.workflow_run.id }} + cancel-in-progress: false permissions: contents: read @@ -37,8 +39,11 @@ jobs: timeout-minutes: 10 if: >- github.event.workflow_run.conclusion == 'failure' && + github.event.workflow_run.head_repository.full_name == github.repository && (github.event.workflow_run.event == 'schedule' || - github.event.workflow_run.head_branch == 'main') + (github.event.workflow_run.head_branch == 'main' && + (github.event.workflow_run.event == 'push' || + github.event.workflow_run.event == 'workflow_dispatch'))) steps: - name: Open or update the issue uses: actions/github-script@v7 @@ -77,25 +82,97 @@ jobs: } } - const listParams = { - owner: context.repo.owner, - repo: context.repo.repo, - state: "open", - per_page: 100, + const listOpenIssues = async () => { + const listParams = { + owner: context.repo.owner, + repo: context.repo.repo, + state: "open", + per_page: 100, + }; + if (labelReady) { + listParams.labels = "ci-nightly"; + } + return github.paginate(github.rest.issues.listForRepo, listParams); }; - if (labelReady) { - listParams.labels = "ci-nightly"; - } - const existing = await github.paginate(github.rest.issues.listForRepo, listParams); - const match = existing.find((issue) => issue.title === title); - if (match) { + + const commentOnIssue = async (issueNumber) => { await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: match.number, + issue_number: issueNumber, body, }); - core.notice(`Updated issue #${match.number}`); + core.notice(`Updated issue #${issueNumber}`); + }; + + const REPORTER_MARKER = + "Opened by `.github/workflows/nightly-failure-issue.yml`"; + + const isReportableIssue = (issue) => + issue.title === title && !issue.pull_request; + + const pickKeeper = (issues) => { + const matches = issues.filter(isReportableIssue); + if (matches.length === 0) { + return undefined; + } + matches.sort((a, b) => a.number - b.number); + return matches[0]; + }; + + const issueBodyMatches = async (issue) => { + if (issue.body && issue.body.includes(REPORTER_MARKER)) { + return true; + } + const full = await github.rest.issues.get({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + }); + return full.data.body.includes(REPORTER_MARKER); + }; + + const closeAsDuplicateOf = async (issueNumber, keeperNumber) => { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, + body: `Duplicate of #${keeperNumber}. Closing in favor of the canonical nightly issue.`, + }); + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, + state: "closed", + state_reason: "not_planned", + }); + }; + + const syncKeeper = async () => { + const open = await listOpenIssues(); + const keeper = pickKeeper(open); + if (!keeper) { + return undefined; + } + await commentOnIssue(keeper.number); + for (const issue of open) { + if (!isReportableIssue(issue) || issue.number === keeper.number) { + continue; + } + if (!(await issueBodyMatches(issue))) { + continue; + } + await closeAsDuplicateOf(issue.number, keeper.number); + } + return keeper; + }; + + let keeper = await syncKeeper(); + if (!keeper) { + keeper = await syncKeeper(); + } + if (keeper) { + core.notice(`Updated issue #${keeper.number}`); } else { const created = await github.rest.issues.create({ owner: context.repo.owner, @@ -104,5 +181,23 @@ jobs: body, labels: labelReady ? ["ci-nightly"] : [], }); - core.notice(`Opened issue #${created.data.number}`); + const open = await listOpenIssues(); + const reportable = open.filter(isReportableIssue); + const older = reportable.find( + (issue) => issue.number < created.data.number, + ); + if (older) { + await closeAsDuplicateOf(created.data.number, older.number); + await commentOnIssue(older.number); + core.notice( + `Closed duplicate #${created.data.number}; kept #${older.number}`, + ); + } else { + keeper = await syncKeeper(); + if (keeper) { + core.notice(`Opened issue #${keeper.number}`); + } else { + core.notice(`Opened issue #${created.data.number}`); + } + } } diff --git a/scripts/ci/test_workflow_policy.py b/scripts/ci/test_workflow_policy.py index 3c4bc6798f..5f696ed4ef 100644 --- a/scripts/ci/test_workflow_policy.py +++ b/scripts/ci/test_workflow_policy.py @@ -3,6 +3,7 @@ from __future__ import annotations +import re import unittest from pathlib import Path @@ -17,6 +18,40 @@ def workflow_texts() -> dict[str, str]: return {path.name: path.read_text(encoding="utf-8") for path in sorted(WORKFLOWS.glob("*.yml"))} +def workflow_display_names() -> set[str]: + names: set[str] = set() + name_re = re.compile(r"^name:\s*(.+?)\s*$") + for text in workflow_texts().values(): + for line in text.splitlines(): + match = name_re.match(line) + if not match: + continue + value = match.group(1).strip() + if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'": + value = value[1:-1] + names.add(value) + break + return names + + +def nightly_failure_watched_workflows(text: str) -> list[str]: + watched: list[str] = [] + in_list = False + for line in text.splitlines(): + if re.match(r"^\s+workflows:\s*$", line): + in_list = True + continue + if not in_list: + continue + item = re.match(r"^\s+-\s+(.+)\s*$", line) + if item: + watched.append(item.group(1).strip()) + continue + if line.strip(): + break + return watched + + def has_pull_request(text: str) -> bool: # Quoted "on" (scorecard) and bare on: both count. in_on = False @@ -303,6 +338,38 @@ def test_detect_checks_out_merge_parents_only(self) -> None: self.assertIn("--head HEAD", changes) self.assertIn("reason=missing-merge-parent", changes) + def test_nightly_failure_issue_serializes_per_upstream_run(self) -> None: + text = (WORKFLOWS / "nightly-failure-issue.yml").read_text(encoding="utf-8") + self.assertIn( + "group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}", + text, + ) + self.assertIn("cancel-in-progress: false", text) + self.assertNotIn("group: ${{ github.workflow }}-${{ github.ref }}", text) + + def test_nightly_failure_issue_watched_names_match_workflows(self) -> None: + text = (WORKFLOWS / "nightly-failure-issue.yml").read_text(encoding="utf-8") + watched = nightly_failure_watched_workflows(text) + known = workflow_display_names() + self.assertEqual(len(watched), 13) + missing = [name for name in watched if name not in known] + self.assertEqual(missing, []) + self.assertIn("determinism-replay", watched) + self.assertNotIn("Determinism Replay", watched) + + def test_nightly_failure_issue_job_skips_fork_and_pull_request_runs(self) -> None: + text = (WORKFLOWS / "nightly-failure-issue.yml").read_text(encoding="utf-8") + self.assertIn("head_repository.full_name == github.repository", text) + self.assertIn("github.event.workflow_run.event == 'schedule'", text) + self.assertIn("github.event.workflow_run.event == 'push'", text) + self.assertIn("github.event.workflow_run.event == 'workflow_dispatch'", text) + self.assertNotIn("github.event.workflow_run.event == 'pull_request'", text) + self.assertNotIn( + "github.event.workflow_run.event == 'schedule' ||\n" + " github.event.workflow_run.head_branch == 'main')", + text, + ) + def test_allocation_measurement_is_a_real_gate_off_prs(self) -> None: text = (WORKFLOWS / "allocation-measurement.yml").read_text(encoding="utf-8") self.assertIn("schedule:", text)