diff --git a/.github/CONTEXT.md b/.github/CONTEXT.md new file mode 100644 index 0000000000..9d9b099a09 --- /dev/null +++ b/.github/CONTEXT.md @@ -0,0 +1,87 @@ +# CI + +This context covers the checks that verify a change, and the machines that run +them. It covers the GitHub Actions workflows of `cipherstash/stack`. It also +covers the CI spend of the other repositories that share its budget. + +## Language + +**CI budget**: +The most that the repositories sharing it may spend on CI in one calendar +month. Vendor invoices measure it, including fixed costs such as licences and +cloud infrastructure. When an invoice also covers other repositories, only the +part for these repositories counts. List-price estimates track it between +invoices, but do not replace it. +_Avoid_: build minutes, runner minutes + +**PR push**: +A new head commit on a pull request's branch. Each one starts the push checks +again. +_Avoid_: PR run, CI run + +**Push check**: +A check that runs on every PR push, so the author learns of a failure while +still working on the change. +_Avoid_: fast check, PR check + +**Gate check**: +A check that runs when a pull request is in a merge queue, instead of on every +PR push. `main` and `next` each have a merge queue. An author can ask for a +gate check to run earlier on the pull request. A failed gate check blocks the +merge. +_Avoid_: heavy check, full CI, merge check + +**Early run**: +A run of the gate checks on a pull request before it joins a merge queue. An +author asks for one by adding the `ci:full` label. Each time the label is +added gives one early run. +_Avoid_: full run, ci:full run + +**Queue commit**: +The commit GitHub builds for a pull request in a merge queue. It contains +the entry ahead of it, plus the pull request. Checks in the queue test this commit. +_Avoid_: merge group commit, temporary branch + +**Trigger paths**: +The paths whose change makes a check run, on PR pushes and in the merge queue. +Every check has them, or a written reason for running on every change. +_Avoid_: path filter, relevance filter, affected checks + +**Selector job**: +The first job of a workflow. It selects the checks that run, by comparing the +files a change touches with each check's trigger paths. When it cannot decide, +it fails. The change then cannot merge until someone runs it again. +_Avoid_: path-filter job, decider, router + +**Summary job**: +The last job of a workflow. It passes when every check that the selector job +selected has passed. It is the only job of the workflow that branch rules +require. +_Avoid_: required job, status job + +**Failsafe**: +The test that fails when the selector job does not decide whether a job runs. +A job with a written exemption passes. The failsafe reads workflow files. It +does not act while a workflow runs. +_Avoid_: lint, selector guard, path-filter lint + +**Light job**: +A job that runs acceptably on a GitHub-hosted standard runner. Whether a job is +light says where it runs, not when: a light job can be a gate check. +_Avoid_: small job, cheap job + +**Heavy job**: +A job that needs more machine than a GitHub-hosted standard runner gives it, +and runs on a self-provisioned cloud runner instead. Whether a job is heavy +says where it runs, not when: a heavy job can be a push check. +_Avoid_: big job, 16-core job + +## Relationships + +- Three separate choices apply to each check. Its trigger paths decide whether + it runs at all. Being a push check or a gate check decides when it runs. + Being a light job or a heavy job decides where it runs. +- The fuzz regression replay, for example, is a light job and a gate check. +- Mutation testing is expensive but important. So it runs only once the + design and implementation of a change are settled. Its failures must be + fixed before the change merges. That makes it a heavy job and a gate check. diff --git a/.github/workflows/crap-crates.yml b/.github/workflows/crap-crates.yml index 8a325ab1ad..f281c94bd1 100644 --- a/.github/workflows/crap-crates.yml +++ b/.github/workflows/crap-crates.yml @@ -12,24 +12,14 @@ name: CRAP gate (crates) # which is `cargo crap`; `--format github` turns each offending function into # an inline `::warning` annotation before the job fails. # -# Pull requests only, so `push` has no filter to mirror; recorded in -# scripts/__tests__/workflow-paths-filter-parity.test.mjs. +# The `selector` job decides which crate's gate a pull request runs, and +# `summary` reports the result (ADR 0003). on: - pull_request: - paths: - - packages/stack-auth/** - - packages/stack-encrypt/** - - packages/stack-encrypt-derive/** - - Cargo.toml - - Cargo.lock - - mise.toml - - mise.test.toml - - .cargo-crap.toml - - .github/workflows/crap-crates.yml - # Keep these excludes last so docs-only changes are skipped. - - "!**.md" - - "!**.example" + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. The trigger paths are in the `selector` job instead. + pull_request: {} workflow_dispatch: {} # Read-only: failures surface as job status and inline annotations. @@ -57,8 +47,118 @@ env: NEXTEST_PROFILE: ci jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + stack_auth: ${{ steps.decide.outputs.stack_auth }} + stack_encrypt: ${{ steps.decide.outputs.stack_encrypt }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + # + # `every` makes a file count only when it matches every rule. That is + # what makes the `!` rules exclude documentation: under dorny's default, + # `some`, a `!` rule matches every other file in the repository. So each + # filter's trigger paths are one brace pattern, written across lines: + # YAML drops a line break that ends in `\`, with the next line's indent. + # + # Each gate scores one crate, and its tests compile that crate's path + # dependencies in the workspace: stack-auth needs stack-profile, and + # stack-encrypt needs stack-encrypt-derive, stack-kms and both of theirs. + # The mise tasks run nextest with the `ci` profile in .config/nextest.toml. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + predicate-quantifier: every + filters: | + stack_auth: + - "{\ + packages/stack-auth/**,\ + packages/stack-profile/**,\ + Cargo.toml,\ + Cargo.lock,\ + mise.toml,\ + mise.test.toml,\ + .cargo-crap.toml,\ + .config/nextest.toml,\ + .github/workflows/crap-crates.yml\ + }" + - "!**/*.md" + - "!**/*.example" + stack_encrypt: + - "{\ + packages/stack-encrypt/**,\ + packages/stack-encrypt-derive/**,\ + packages/stack-kms/**,\ + packages/stack-auth/**,\ + packages/stack-profile/**,\ + Cargo.toml,\ + Cargo.lock,\ + mise.toml,\ + mise.test.toml,\ + .cargo-crap.toml,\ + .config/nextest.toml,\ + .github/workflows/crap-crates.yml\ + }" + - "!**/*.md" + - "!**/*.example" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + STACK_AUTH: ${{ steps.filter.outputs.stack_auth }} + STACK_ENCRYPT: ${{ steps.filter.outputs.stack_encrypt }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + STACK_AUTH=true + STACK_ENCRYPT=true + fi + answers=( + "stack_auth=$STACK_AUTH" + "stack_encrypt=$STACK_ENCRYPT" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + crap-stack-auth: name: CRAP (stack-auth) + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.stack_auth == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 timeout-minutes: 45 steps: @@ -83,6 +183,12 @@ jobs: crap-stack-encrypt: name: CRAP (stack-encrypt) + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.stack_encrypt == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 timeout-minutes: 45 steps: @@ -103,3 +209,31 @@ jobs: - name: Run the CRAP gate run: mise run crap:stack-encrypt -- --format github + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: CRAP gate summary + needs: [selector, crap-stack-auth, crap-stack-encrypt] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index baa8e8cd8f..3e84989a36 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -14,24 +14,15 @@ name: Fuzz (crates) # cache, so coverage compounds; it is minimised with `cargo fuzz cmin`, and # any crash reproducer is uploaded as an artifact. # -# cargo-fuzz needs nightly; the tasks run `cargo +nightly fuzz`. Pull requests -# are the only filtered event, so `push` has no filter to mirror; recorded in -# scripts/__tests__/workflow-paths-filter-parity.test.mjs. +# cargo-fuzz needs nightly; the tasks run `cargo +nightly fuzz`. The `selector` +# job decides whether a pull request runs the regression replay, and `summary` +# reports the result (ADR 0003). on: - pull_request: - paths: - - packages/stack-auth/** - - packages/stack-kms/** - - packages/stack-encrypt/** - - Cargo.toml - - Cargo.lock - - mise.toml - - mise.test.toml - - .github/workflows/fuzz.yml - # Keep these excludes last so docs-only changes are skipped. - - "!**.md" - - "!**.example" + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. The trigger paths are in the `selector` job instead. + pull_request: {} schedule: # Nightly at 04:47 UTC. Scheduled runs fire from the default branch only. - cron: "47 4 * * *" @@ -60,11 +51,99 @@ env: CARGO_NET_GIT_FETCH_WITH_CLI: true jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + fuzz: ${{ steps.decide.outputs.fuzz }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + # + # `every` makes a file count only when it matches every rule. That is + # what makes the `!` rules exclude documentation: under dorny's default, + # `some`, a `!` rule matches every other file in the repository. So each + # filter's trigger paths are one brace pattern, written across lines: + # YAML drops a line break that ends in `\`, with the next line's indent. + # + # Each fuzz crate builds its parent crate by path, and that crate's path + # dependencies with it: stack-profile and stack-encrypt-derive. Their + # `workspace = true` keys are read from the root Cargo.toml. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + predicate-quantifier: every + filters: | + fuzz: + - "{\ + packages/stack-auth/**,\ + packages/stack-profile/**,\ + packages/stack-kms/**,\ + packages/stack-encrypt/**,\ + packages/stack-encrypt-derive/**,\ + Cargo.toml,\ + Cargo.lock,\ + mise.toml,\ + mise.test.toml,\ + .github/workflows/fuzz.yml\ + }" + - "!**/*.md" + - "!**/*.example" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + FUZZ: ${{ steps.filter.outputs.fuzz }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + FUZZ=true + fi + answers=( + "fuzz=$FUZZ" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + fuzz-regression: name: fuzz regression (${{ matrix.slug }}) + needs: [selector] # Not on the nightly schedule, which is the campaign's. A manual dispatch - # runs both jobs. - if: github.event_name != 'schedule' + # runs both jobs. On a pull request, the selector decides. Written as + # exclusions, not a list of events. + if: >- + github.event_name != 'schedule' + && (github.event_name != 'pull_request' + || needs.selector.outputs.fuzz == 'true') runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 strategy: @@ -189,3 +268,31 @@ jobs: name: fuzz-artifacts-${{ matrix.slug }} path: ${{ matrix.dir }}/fuzz/artifacts/** if-no-files-found: ignore + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Fuzz summary + needs: [selector, fuzz-regression, fuzz-campaign] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/miri.yml b/.github/workflows/miri.yml index 75711f5fda..0a8a14b56e 100644 --- a/.github/workflows/miri.yml +++ b/.github/workflows/miri.yml @@ -9,20 +9,14 @@ name: Miri (guest ABI) # deterministic, so a red run is a real defect or a harness that stopped # compiling, never flake. # -# Pull requests only, so `push` has no filter to mirror; recorded in -# scripts/__tests__/workflow-paths-filter-parity.test.mjs. +# The `selector` job decides whether a pull request runs Miri, and `summary` +# reports the result (ADR 0003). on: - pull_request: - paths: - - packages/stack-guest-abi/** - - Cargo.toml - - Cargo.lock - - mise.toml - - .github/workflows/miri.yml - # Keep these excludes last so docs-only changes are skipped. - - "!**.md" - - "!**.example" + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. The trigger paths are in the `selector` job instead. + pull_request: {} workflow_dispatch: {} defaults: @@ -38,8 +32,88 @@ env: CARGO_NET_GIT_FETCH_WITH_CLI: true jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + miri: ${{ steps.decide.outputs.miri }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + # + # `every` makes a file count only when it matches every rule. That is + # what makes the `!` rules exclude documentation: under dorny's default, + # `some`, a `!` rule matches every other file in the repository. So each + # filter's trigger paths are one brace pattern, written across lines: + # YAML drops a line break that ends in `\`, with the next line's indent. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + predicate-quantifier: every + filters: | + miri: + - "{\ + packages/stack-guest-abi/**,\ + Cargo.toml,\ + Cargo.lock,\ + mise.toml,\ + .github/workflows/miri.yml\ + }" + - "!**/*.md" + - "!**/*.example" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + MIRI: ${{ steps.filter.outputs.miri }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + MIRI=true + fi + answers=( + "miri=$MIRI" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + miri-stack-guest-abi: name: Miri (stack-guest-abi) + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.miri == 'true' runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 30 steps: @@ -66,3 +140,31 @@ jobs: - name: Miri run: mise run miri:stack-guest-abi + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Miri summary + needs: [selector, miri-stack-guest-abi] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/test-eql.yml b/.github/workflows/test-eql.yml index b3d0c2d601..cd330d1c2a 100644 --- a/.github/workflows/test-eql.yml +++ b/.github/workflows/test-eql.yml @@ -31,8 +31,8 @@ name: "Test EQL" # in every shard instead of saying what was missing. # # NB: NO path filter on `pull_request`. A workflow skipped by a path filter -# leaves its required checks stuck Pending and blocks merge. Relevance is -# computed by the `changes` job and applied per-job via `if:` instead. `push` +# leaves its required checks stuck Pending and blocks merge. The `selector` job +# decides which jobs a pull request needs, and each job's `if:` reads it. `push` # carries one because nothing waits on a push run, so a skipped one costs # nothing — and the two lists are kept identical by # `scripts/__tests__/eql-workflow-filters.test.mjs`. @@ -70,7 +70,7 @@ on: pull_request: {} push: branches: [main] - # Identical to the `relevant:` filter in the `changes` job below. GitHub + # Identical to the union of the `selector` job's filters below. GitHub # Actions has no YAML anchors, so the list is written twice; the guard named # above fails a one-sided edit. # @@ -148,14 +148,23 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - # Runs on EVERY event and MUST always succeed (never skipped, never failed) — - # downstream heavy jobs `needs: [changes]`, and a skipped/failed `changes` - # would either skip the merge-queue matrix or deadlock `ci-required`. - changes: - name: "Detect relevant changes" - runs-on: blacksmith-16vcpu-ubuntu-2204 + # The selector job: it decides which gated jobs a pull request needs. It runs + # on EVERY event and is never skipped, because every gated job `needs:` it. + # When it cannot decide it FAILS, which fails `ci-required`: a selector that + # answered nothing would skip every gated job, and `ci-required` counts + # skipped as passed (ADR 0003). + # + # On a GitHub-hosted runner because every gated job waits for it, and a + # self-provisioned machine takes longer to start. + selector: + name: "Selector" + runs-on: ubuntu-latest + # Each output is named after the filter that decides it; + # `scripts/__tests__/eql-selector.test.mjs` follows a job's `if:` to its + # filter by that name. outputs: - relevant: ${{ steps.r.outputs.relevant }} + eql: ${{ steps.r.outputs.eql }} + rust_crates: ${{ steps.r.outputs.rust_crates }} steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 with: @@ -177,8 +186,9 @@ jobs: # matrix on changes that cannot affect EQL, and (worse) NOT firing on # EQL's own `src/`, since nothing at the repo root is called that. # - # Identical to the `push` `paths:` list at the top of this file — the - # same question asked of two mechanisms GitHub gives no way to share. + # The union of the two filters is identical to the `push` `paths:` + # list at the top of this file — the same question asked of two + # mechanisms GitHub gives no way to share. # `scripts/__tests__/eql-workflow-filters.test.mjs` compares them, and # also fails any entry here that matches no tracked file (a dead # `packages/eql/sql/**` sat in this list from the port: there is no @@ -191,8 +201,14 @@ jobs: # copies are held EQUAL — see the note above the `push` list, and the # reasoning on `bench-eql.yml`'s own filter for why equal beats # minimal. + # + # `rust_crates` adds the stack-* crates to `eql`. Only `rust-crates` + # compiles them, through eql-bindings' `stack-encrypt` feature; + # `tests/sqlx` and the codegen crates never enable it. With the crates + # on one shared list, a change to stack-encrypt alone ran the whole + # Postgres suite. dorny flattens the `*eql` alias into the list. filters: | - relevant: + eql: &eql - ".github/workflows/test-eql.yml" - ".github/actions/require-cs-secrets/**" - "packages/eql/src/**" @@ -207,6 +223,8 @@ jobs: - "packages/eql/docker/**" - "packages/eql/README.md" - "packages/eql/SUPABASE.md" + rust_crates: + - *eql - "packages/stack-encrypt/**" - "packages/stack-encrypt-derive/**" - "packages/stack-kms/**" @@ -217,25 +235,38 @@ jobs: # Explicit default (not `|| 'true'`, which trips GitHub's inconsistent # treatment of the string 'false'). push/schedule/merge_group/dispatch # never run the filter above — it needs a base ref that only a pull - # request has — so they take the default, and `push` is already narrowed + # request has — so they select every job, and `push` is already narrowed # by its own `on:`-level `paths:`. # - # The event arrives through `env:`, not through a `${{ }}` interpolated - # into the body. That is what lets + # On a pull request, an answer other than `true` or `false` means the + # filter did not decide, so the step fails rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # interpolated into the body. That is what lets + # `scripts/__tests__/eql-selector.test.mjs` and # `scripts/__tests__/eql-matrix-triggers.test.mjs` EXECUTE this script - # once per event and read back what it wrote, rather than reimplementing - # the branch and agreeing with a rewritten one by luck. + # and read back what it wrote, rather than reimplementing the branch and + # agreeing with a rewritten one by luck. - id: r env: EVENT_NAME: ${{ github.event_name }} - FILTER_RELEVANT: ${{ steps.f.outputs.relevant }} + EQL: ${{ steps.f.outputs.eql }} + RUST_CRATES: ${{ steps.f.outputs.rust_crates }} run: | set -euo pipefail - if [ "$EVENT_NAME" = "pull_request" ]; then - echo "relevant=$FILTER_RELEVANT" >> "$GITHUB_OUTPUT" - else - echo "relevant=true" >> "$GITHUB_OUTPUT" + if [ "$EVENT_NAME" != "pull_request" ]; then + EQL=true + RUST_CRATES=true fi + for answer in "eql=$EQL" "rust_crates=$RUST_CRATES"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done # Pure bash; no checkout/toolchain. Derives the PG-version + shard fan-out # from the event: PR and manual dispatch -> PG17 x 4 shards (latency lane); @@ -292,7 +323,7 @@ jobs: # when relevant files changed (docs-only PRs never pay the ~4-min compile). build-archive: name: "Build test archive" - needs: [changes] + needs: [selector] # This repo is PUBLIC and accepts fork PRs (the approval policy only gates # first-time contributors). build-archive is the sole holder of the CS_* # credentials below, so it must never run on a fork PR. The trailing clause @@ -310,7 +341,7 @@ jobs: # `scripts/__tests__/workflow-dispatch-job-conditions.test.mjs`. if: >- (github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true') + || needs.selector.outputs.eql == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) runs-on: blacksmith-16vcpu-ubuntu-2204 @@ -392,11 +423,11 @@ jobs: # shards start right after build-archive. test: name: "Shard PG${{ matrix.postgres-version }} ${{ matrix.shard }}/${{ needs.setup.outputs.shard-total }}" - needs: [changes, setup, build-archive] + needs: [selector, setup, build-archive] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 strategy: fail-fast: false @@ -454,11 +485,11 @@ jobs: # of the old per-version test job so they run ONCE per version, not per shard. validate: name: "Validate (Postgres ${{ matrix.postgres-version }})" - needs: [changes, setup] + needs: [selector, setup] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 strategy: fail-fast: false @@ -513,11 +544,11 @@ jobs: schema: name: "JSON Schema validation" - needs: [changes] + needs: [selector] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 @@ -542,11 +573,13 @@ jobs: rust-crates: name: "Rust workspace crates" - needs: [changes] - # Exclusion, not an allowlist — see the note on `build-archive`. + needs: [selector] + # Exclusion, not an allowlist — see the note on `build-archive`. Reads + # `rust_crates`, not `eql`: this is the one job that compiles the stack-* + # crates. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.rust_crates == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 services: encryption-postgres: @@ -670,11 +703,11 @@ jobs: codegen: name: "Encrypted-domain codegen" - needs: [changes] + needs: [selector] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 @@ -702,11 +735,11 @@ jobs: self-contained-v3: name: "eql_v3 self-containment" - needs: [changes] + needs: [selector] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 @@ -738,11 +771,11 @@ jobs: matrix-coverage: name: "Matrix coverage inventory" - needs: [changes] + needs: [selector] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 steps: - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 @@ -780,11 +813,11 @@ jobs: splinter: name: "Supabase splinter" - needs: [changes] + needs: [selector] # Exclusion, not an allowlist — see the note on `build-archive`. if: >- github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true' + || needs.selector.outputs.eql == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 env: POSTGRES_VERSION: "17" @@ -947,12 +980,12 @@ jobs: # fork-PR guard to keep the secrets off fork runs. e2e: name: "e2e property suite (fresh encryption)" - needs: [changes, setup] + needs: [selector, setup] # Exclusion, not an allowlist — see the note on `build-archive`, which # carries the same two clauses for the same two reasons. if: >- (github.event_name != 'pull_request' - || needs.changes.outputs.relevant == 'true') + || needs.selector.outputs.eql == 'true') && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) runs-on: blacksmith-16vcpu-ubuntu-2204 @@ -1002,7 +1035,7 @@ jobs: # source job is itself in `needs` and reports failure. ci-required: name: "ci-required" - needs: [changes, setup, build-archive, test, validate, schema, rust-crates, + needs: [selector, setup, build-archive, test, validate, schema, rust-crates, codegen, self-contained-v3, matrix-coverage, splinter, docs-static, known-failures, doc-anchors, e2e] if: always() diff --git a/.github/workflows/tests-auth-refresh.yml b/.github/workflows/tests-auth-refresh.yml index 505979d30a..d4e4eb97ed 100644 --- a/.github/workflows/tests-auth-refresh.yml +++ b/.github/workflows/tests-auth-refresh.yml @@ -2,6 +2,8 @@ name: Tests (auth refresh cancellation) # The short run covers every PR, including Version Packages. The longer run # also covers release PRs and changes to the engine, refreshers or toolchain. +# The `selector` job decides whether a pull request runs the longer one, and +# `summary` reports the result (ADR 0003). on: pull_request: {} workflow_dispatch: {} @@ -14,37 +16,116 @@ concurrency: cancel-in-progress: true jobs: - short: - uses: ./.github/workflows/_test-auth-refresh-soak.yml - - changes: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector runs-on: ubuntu-latest - timeout-minutes: 5 - permissions: - contents: read - pull-requests: read + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. outputs: - refresh: ${{ steps.paths.outputs.refresh }} + refresh: ${{ steps.decide.outputs.refresh }} steps: - - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 - with: - persist-credentials: false - - uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 - id: paths + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + # + # The soak tests are in stack-auth, which builds stack-profile by path. + # release-plz.yml runs the long soak before it publishes stack-auth. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 with: filters: | refresh: - - 'packages/stack-auth/**' - - 'Cargo.toml' - - 'Cargo.lock' - - 'mise*.toml' - - '.config/nextest.toml' - - '.github/workflows/*auth-refresh*.yml' - - '.github/workflows/release-plz.yml' + - "packages/stack-auth/**" + - "packages/stack-profile/**" + - "Cargo.toml" + - "Cargo.lock" + - "mise*.toml" + - ".config/nextest.toml" + - ".github/workflows/*auth-refresh*.yml" + - ".github/workflows/release-plz.yml" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + REFRESH: ${{ steps.filter.outputs.refresh }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + REFRESH=true + fi + answers=( + "refresh=$REFRESH" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + + # Runs on every change, so the selector does not decide it. + short: + uses: ./.github/workflows/_test-auth-refresh-soak.yml long: - needs: changes - if: github.event_name == 'workflow_dispatch' || github.head_ref == 'changeset-release/main' || needs.changes.outputs.refresh == 'true' + needs: [selector] + # Written as an exclusion, not a list of events: every event other than a + # pull request runs the long soak. On a pull request, Version Packages + # runs it too. + if: >- + github.event_name != 'pull_request' + || github.head_ref == 'changeset-release/main' + || needs.selector.outputs.refresh == 'true' uses: ./.github/workflows/_test-auth-refresh-soak.yml with: long: true + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Tests (auth refresh cancellation) summary + needs: [selector, short, long] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/tests-crates.yml b/.github/workflows/tests-crates.yml index 40cc572cce..381a9bcd14 100644 --- a/.github/workflows/tests-crates.yml +++ b/.github/workflows/tests-crates.yml @@ -10,35 +10,22 @@ name: Tests (crates) # `scripts/__tests__/crates-ci.test.mjs` pins that, and that every mise task in # the five crate `tasks.toml` files is reached from a root workflow. # -# The filter keeps Markdown in: `stack-auth`'s crate docs are its README +# The filters keep Markdown in: `stack-auth`'s crate docs are its README # (`#![doc = include_str!("../README.md")]`), so a README edit changes the # doctests. The suite's `!**.md` exclude would have skipped that change. +# +# The `selector` job decides which jobs a pull request runs, and `summary` +# reports the result (ADR 0003). on: - pull_request: - paths: - - packages/stack-auth/** - - packages/stack-profile/** - - packages/stack-kms/** - - packages/stack-encrypt/** - - packages/stack-encrypt-derive/** - - packages/stack-guest-abi/** - - languages/typescript/packages/auth/** - - languages/typescript/packages/profile/** - - languages/typescript/packages/stack-auth-wasm/** - # Root manifest, lockfile, toolchain pin and cargo config: each can - # change what compiles without touching a crate. The toolchain pin in - # particular changes the trybuild UI snapshots. - - Cargo.toml - - Cargo.lock - - mise.toml - - mise.test.toml - - .cargo/config.toml - - .config/nextest.toml - - pnpm-lock.yaml - - .github/workflows/tests-crates.yml + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. The trigger paths are in the `selector` job instead. + pull_request: {} push: branches: [main] + # The union of the `selector` job's filters. + # scripts/__tests__/workflow-paths-filter-parity.test.mjs compares them. paths: - packages/stack-auth/** - packages/stack-profile/** @@ -55,7 +42,9 @@ on: - mise.test.toml - .cargo/config.toml - .config/nextest.toml + - package.json - pnpm-lock.yaml + - pnpm-workspace.yaml - .github/workflows/tests-crates.yml workflow_dispatch: {} @@ -77,8 +66,113 @@ env: NEXTEST_PROFILE: ci jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + rust: ${{ steps.decide.outputs.rust }} + node_bindings: ${{ steps.decide.outputs.node_bindings }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + filters: | + # Cargo over the whole root workspace. Root manifest, lockfile, + # toolchain pin and cargo config each change what compiles without + # touching a crate. The toolchain pin in particular changes the + # trybuild UI snapshots. + rust: + - "packages/stack-auth/**" + - "packages/stack-profile/**" + - "packages/stack-kms/**" + - "packages/stack-encrypt/**" + - "packages/stack-encrypt-derive/**" + - "packages/stack-guest-abi/**" + - "languages/typescript/packages/auth/**" + - "languages/typescript/packages/profile/**" + - "languages/typescript/packages/stack-auth-wasm/**" + - "Cargo.toml" + - "Cargo.lock" + - "mise.toml" + - "mise.test.toml" + - ".cargo/config.toml" + - ".config/nextest.toml" + - ".github/workflows/tests-crates.yml" + # The node bindings and stack-auth-wasm, the two crates they wrap, and + # the pnpm install that runs their vitest suites. .cargo/config.toml + # carries the wasm32 rustflags the stack-auth-wasm build needs. + node_bindings: + - "packages/stack-auth/**" + - "packages/stack-profile/**" + - "languages/typescript/packages/auth/**" + - "languages/typescript/packages/profile/**" + - "languages/typescript/packages/stack-auth-wasm/**" + - "Cargo.toml" + - "Cargo.lock" + - "mise.toml" + - ".cargo/config.toml" + - "package.json" + - "pnpm-lock.yaml" + - "pnpm-workspace.yaml" + - ".github/workflows/tests-crates.yml" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + RUST: ${{ steps.filter.outputs.rust }} + NODE_BINDINGS: ${{ steps.filter.outputs.node_bindings }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + RUST=true + NODE_BINDINGS=true + fi + answers=( + "rust=$RUST" + "node_bindings=$NODE_BINDINGS" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + rust: name: fmt, clippy, nextest, doctests, rustdoc + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.rust == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 timeout-minutes: 60 env: @@ -158,6 +252,12 @@ jobs: node-bindings: name: node bindings, napi typings, stack-auth-wasm + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.node_bindings == 'true' runs-on: blacksmith-16vcpu-ubuntu-2204 timeout-minutes: 45 steps: @@ -234,3 +334,31 @@ jobs: - name: wasm-pack test stack-auth-wasm run: mise x -- wasm-pack test --node languages/typescript/packages/stack-auth-wasm + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Tests (crates) summary + needs: [selector, rust, node-bindings] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/tests-rust.yml b/.github/workflows/tests-rust.yml index 1a1bdc3885..7f7ca0774c 100644 --- a/.github/workflows/tests-rust.yml +++ b/.github/workflows/tests-rust.yml @@ -10,36 +10,17 @@ name: Tests (Rust) # `languages/typescript/packages/protect-ffi/src/lintWiring.test.ts` asserts the split from the # manifest side; this is the other half, and that test now reads this file. # -# The filter is broader than `crates/**`: dependency, feature and toolchain -# changes all alter what cargo builds without touching a .rs file. +# The trigger paths are broader than `crates/**`: dependency, feature and +# toolchain changes all alter what cargo builds without touching a .rs file. +# The `selector` job holds them, and `summary` reports the result (ADR 0003). on: - pull_request: - paths: - - 'languages/typescript/packages/protect-ffi/crates/**' - - 'languages/typescript/packages/protect-ffi/Cargo.toml' - - 'languages/typescript/packages/protect-ffi/Cargo.lock' - - 'languages/typescript/packages/protect-ffi/mise.toml' - - 'languages/typescript/packages/protect-ffi/package.json' - # Out of that package, and compiled by every check below all the same: - # `crates/protect-ffi/Cargo.toml` carries - # `eql-bindings = { path = "../../../../../../packages/eql/crates/eql-bindings" }`, so - # `cargo test`, `cargo fmt --check` and both clippy passes build that - # tree. A change there can break this job in a PR touching no file under - # languages/typescript/packages/protect-ffi at all. The workspace root manifest comes with it: - # cargo reads it for the crate's workspace context, so a - # `.workspace = true` added to eql-bindings becomes a compile input - # with no other trace. Same two entries as the native cache key in - # `.github/actions/build-ffi-binding`, which is where the list is derived - # from — pinned by - # scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs. - # - # This is the only copy: `push:` below is `branches: [main]` with no - # `paths:`, so main runs a superset of what PRs run (recorded as a - # deliberate asymmetry in workflow-paths-filter-parity.test.mjs). - - 'packages/eql/crates/**' - - 'packages/eql/Cargo.toml' - - '.github/workflows/tests-rust.yml' + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. + pull_request: {} + # No path filter here either, so main runs a superset of what pull requests + # run. push: branches: [main] workflow_dispatch: {} @@ -56,8 +37,93 @@ concurrency: cancel-in-progress: true jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + rust: ${{ steps.decide.outputs.rust }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + filters: | + # Out of the package, and compiled by every check all the same: + # `crates/protect-ffi/Cargo.toml` carries + # `eql-bindings = { path = "../../../../../../packages/eql/crates/eql-bindings" }`, + # so `cargo test`, `cargo fmt --check` and both clippy passes build that + # tree. The EQL workspace root manifest comes with it: cargo reads it for + # the crate's workspace context. Same two entries as the native cache key + # in `.github/actions/build-ffi-binding`, which is where the list is + # derived from, pinned by + # scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs. + # + # The root mise.toml pins the rust this job builds with: mise reads the + # parent directory's config, and protect-ffi's own does not pin rust. + rust: + - "languages/typescript/packages/protect-ffi/crates/**" + - "languages/typescript/packages/protect-ffi/Cargo.toml" + - "languages/typescript/packages/protect-ffi/Cargo.lock" + - "languages/typescript/packages/protect-ffi/mise.toml" + - "languages/typescript/packages/protect-ffi/package.json" + - "packages/eql/crates/**" + - "packages/eql/Cargo.toml" + - "mise.toml" + - ".github/workflows/tests-rust.yml" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + RUST: ${{ steps.filter.outputs.rust }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + RUST=true + fi + answers=( + "rust=$RUST" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + rust: name: cargo test + clippy + rustfmt + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.rust == 'true' runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 45 steps: @@ -132,3 +198,31 @@ jobs: - name: clippy (host + wasm32) working-directory: languages/typescript/packages/protect-ffi run: mise run lint:rust + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Tests (Rust) summary + needs: [selector, rust] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/.github/workflows/udeps.yml b/.github/workflows/udeps.yml index db3d33b98c..a4480dbb7c 100644 --- a/.github/workflows/udeps.yml +++ b/.github/workflows/udeps.yml @@ -7,28 +7,19 @@ name: Unused dependencies (crates) # PRs. Bump it deliberately, after checking a newer nightly builds the # workspace. (The suite pinned 2026-07-10 because the 2026-07-14 nightly ICEs # on the test-harness entrypoint attribute.) +# +# The `selector` job decides whether a pull request runs the check, and +# `summary` reports the result (ADR 0003). on: - pull_request: - paths: - - packages/stack-auth/** - - packages/stack-profile/** - - packages/stack-kms/** - - packages/stack-encrypt/** - - packages/stack-encrypt-derive/** - - packages/stack-guest-abi/** - - languages/typescript/packages/auth/** - - languages/typescript/packages/profile/** - - languages/typescript/packages/stack-auth-wasm/** - - Cargo.toml - - Cargo.lock - - mise.test.toml - - .github/workflows/udeps.yml - # Keep these excludes last so docs-only changes are skipped. - - "!**.md" - - "!**.example" + # No path filter: a workflow that a path filter skips leaves its required + # checks pending, and GitHub's documentation on required status checks says + # that blocks a merge. The trigger paths are in the `selector` job instead. + pull_request: {} push: branches: [main] + # The same files as the `selector` job's filter. + # scripts/__tests__/workflow-paths-filter-parity.test.mjs compares them. paths: - packages/stack-auth/** - packages/stack-profile/** @@ -41,6 +32,7 @@ on: - languages/typescript/packages/stack-auth-wasm/** - Cargo.toml - Cargo.lock + - mise.toml - mise.test.toml - .github/workflows/udeps.yml - "!**.md" @@ -66,8 +58,100 @@ env: NIGHTLY_TOOLCHAIN: nightly-2026-07-10 jobs: + # The selector job: it decides which jobs a pull request runs. It runs on + # every event and is never skipped, because the gated jobs need it. When it + # cannot decide it fails, and that fails the summary job: a selector that + # answered nothing would skip every gated job, and the summary job counts a + # skipped job as passed (ADR 0003). + # + # On a GitHub-hosted runner because the gated jobs wait for it. No checkout: + # on a pull request, dorny/paths-filter lists the changed files through the + # GitHub API. + selector: + name: Selector + runs-on: ubuntu-latest + timeout-minutes: 10 + # Each output is named after the filter that decides it. + # `scripts/__tests__/workflow-selectors.test.mjs` follows a job's `if:` to + # its filter by that name. + outputs: + udeps: ${{ steps.decide.outputs.udeps }} + steps: + # Only on a pull request, which has a base to compare with. Other events + # select every job in the step below. + # + # `every` makes a file count only when it matches every rule. That is + # what makes the `!` rules exclude documentation: under dorny's default, + # `some`, a `!` rule matches every other file in the repository. So each + # filter's trigger paths are one brace pattern, written across lines: + # YAML drops a line break that ends in `\`, with the next line's indent. + # + # Every crate of the root workspace, and the files that change what it + # resolves. mise.toml pins the rust that mise-action installs. + - id: filter + if: github.event_name == 'pull_request' + uses: dorny/paths-filter@0e4a8c6effa4802afeda77dc8d303f8176d7dfad # v3 + with: + predicate-quantifier: every + filters: | + udeps: + - "{\ + packages/stack-auth/**,\ + packages/stack-profile/**,\ + packages/stack-kms/**,\ + packages/stack-encrypt/**,\ + packages/stack-encrypt-derive/**,\ + packages/stack-guest-abi/**,\ + languages/typescript/packages/auth/**,\ + languages/typescript/packages/profile/**,\ + languages/typescript/packages/stack-auth-wasm/**,\ + Cargo.toml,\ + Cargo.lock,\ + mise.toml,\ + mise.test.toml,\ + .github/workflows/udeps.yml\ + }" + - "!**/*.md" + - "!**/*.example" + + # Explicit default, not `|| 'true'`, which trips GitHub's inconsistent + # treatment of the string 'false'. On a pull request, an answer other than + # `true` or `false` means the filter did not decide, so the step fails + # rather than write it. + # + # The event and the answers arrive through `env:`, not through a `${{ }}` + # in the script. That lets `scripts/__tests__/workflow-selectors.test.mjs` + # run this script and read back what it wrote. + - id: decide + env: + EVENT_NAME: ${{ github.event_name }} + UDEPS: ${{ steps.filter.outputs.udeps }} + run: | + set -euo pipefail + if [ "$EVENT_NAME" != "pull_request" ]; then + UDEPS=true + fi + answers=( + "udeps=$UDEPS" + ) + for answer in "${answers[@]}"; do + case "${answer#*=}" in + true|false) echo "$answer" >> "$GITHUB_OUTPUT" ;; + *) + echo "::error::The path filter answered '${answer#*=}' for ${answer%%=*}. The selector cannot decide which jobs to run." + exit 1 + ;; + esac + done + udeps: name: Check unused Rust dependencies + needs: [selector] + # Written as an exclusion, not a list of events: every event other + # than a pull request runs this job. + if: >- + github.event_name != 'pull_request' + || needs.selector.outputs.udeps == 'true' runs-on: blacksmith-4vcpu-ubuntu-2404 timeout-minutes: 45 steps: @@ -92,3 +176,31 @@ jobs: - name: Check for unused dependencies run: cargo "+$NIGHTLY_TOOLCHAIN" udeps --workspace --all-features --all-targets + + # The summary job: the one job of this workflow that branch rules require. + # It has the same name on every event. It passes when every needed job + # passed or was skipped. Skipped counts as passed because the selector skips + # a job whose trigger paths a change does not touch. A failed job still + # fails this one, because it is in `needs` and reports its failure. + summary: + name: Unused dependencies summary + needs: [selector, udeps] + if: always() + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Check that every needed job passed or was skipped + env: + RESULTS: ${{ join(needs.*.result, ' ') }} + run: | + set -euo pipefail + echo "Needed results: $RESULTS" + for result in $RESULTS; do + case "$result" in + success|skipped) ;; + *) + echo "::error::A needed job reported '$result'." + exit 1 + ;; + esac + done diff --git a/CONTEXT-MAP.md b/CONTEXT-MAP.md index af93fdfbdf..ec9b360eea 100644 --- a/CONTEXT-MAP.md +++ b/CONTEXT-MAP.md @@ -7,6 +7,8 @@ - [Stack Encrypt](./packages/stack-encrypt/CONTEXT.md) — encrypts values under per-value ZeroKMS data keys and derives searchable terms from them, for Rust and, through a WASI guest, Go. +- [CI](./.github/CONTEXT.md) — runs the checks that verify a change, and pays + for the machines that run them, within a shared monthly budget. ## Relationships diff --git a/docs/adr/0002-light-ci-jobs-on-github-hosted-runners-heavy-jobs-on-runs-on.md b/docs/adr/0002-light-ci-jobs-on-github-hosted-runners-heavy-jobs-on-runs-on.md new file mode 100644 index 0000000000..65cc84528f --- /dev/null +++ b/docs/adr/0002-light-ci-jobs-on-github-hosted-runners-heavy-jobs-on-runs-on.md @@ -0,0 +1,55 @@ +--- +status: accepted +--- + +# Move CI from Blacksmith to GitHub-hosted runners and RunsOn + +We decided in October 2026 to stop running CI on Blacksmith. Light jobs run on +GitHub-hosted standard runners. Those runners cost nothing for this public +repository. + +Heavy jobs run on RunsOn. RunsOn starts a machine in our own AWS account for +each job. Heavy jobs use c8a machines with 8 vCPUs. Mutants uses a +c8a machine with 16 vCPUs. The other repositories that share the CI budget +move their Blacksmith jobs to RunsOn too. + +We made the change because of cost. CI for this repository cost about $3,865 a +month on Blacksmith at October 2026's pace. That was 20 times September's +cost. The CI budget for this repository and the others that share it is +$1,000 a month. + +```mermaid +flowchart LR + job[A CI job] --> size{Does a GitHub-hosted standard runner give it enough machine?} + size -- "Yes: light job" --> gh[GitHub-hosted runner. Free for this public repository. At most 60 jobs at a time for the organisation.] + size -- "No: heavy job" --> ro[RunsOn machine in our AWS account. c8a with 8 vCPUs. Mutants uses 16 vCPUs.] +``` + +## Considered options + +- **Stay on Blacksmith and run fewer jobs.** We modelled October's jobs with + gate checks in place. This repository alone still came to about $2,000 a + month at Blacksmith's prices. +- **Run every job on RunsOn.** Light jobs would then cost money. GitHub-hosted + runners run them for nothing. +- **Run every job on GitHub-hosted runners.** In the runner trial in pull + request #1297, jobs took 2.46 times as long overall. The integration suites + took up to 4.8 times as long. + +## Consequences + +- GitHub runs at most 60 jobs at a time for the organisation on the Team plan. + Light jobs wait for a runner when more than 60 jobs are queued. +- We simulated October's light jobs against that limit, with gate checks in + place. One job in a hundred waited between 7 and 11 minutes. +- A RunsOn job starts 40 to 50 seconds later than a Blacksmith job did. +- RunsOn uses discounted spot machines. When AWS has no spare c8a machines, a + job runs at the full price. The full price is more than twice the spot + price. The `family=c8a+m8a` label lets RunsOn try a second machine type + first. +- Every job sets `timeout-minutes`. In the trial, one job without a limit ran + for over 2 hours. +- We maintain the RunsOn resources in AWS. They cost $40 to $60 a month. +- RunsOn's pricing page limits its free licence to non-commercial use. The + trial licence ends in November 2026. Buying a licence is a separate + decision. diff --git a/docs/adr/0003-expensive-ci-checks-run-in-merge-queues-on-main-and-next.md b/docs/adr/0003-expensive-ci-checks-run-in-merge-queues-on-main-and-next.md new file mode 100644 index 0000000000..deab67402f --- /dev/null +++ b/docs/adr/0003-expensive-ci-checks-run-in-merge-queues-on-main-and-next.md @@ -0,0 +1,158 @@ +--- +status: accepted +--- + +# Run the expensive CI checks in merge queues on `main` and `next` + +We decided in October 2026 to split this repository's CI checks into push +checks and gate checks. Push checks run on every push to a pull request. Gate +checks run once, when the pull request is in a merge queue. `main` and `next` +each get a merge queue. + +We made the change because of cost. From 1 to 20 October 2026, 89% of this +repository's CI spending came from pull request runs. Each push ran the +expensive checks again. ADR 0002 moves jobs to cheaper machines. This decision +runs the expensive jobs less often. + +`.github/CONTEXT.md` defines the terms used here. + +```mermaid +flowchart TD + push["An author pushes to a pull request"] --> sel1{"Selector job: does the pull request have the ci:full label?"} + sel1 -- "No" --> pc["The push checks whose trigger paths changed run"] + sel1 -- "Yes" --> early["Push checks and gate checks run once. A job removes the label."] + pc --> ready["The pull request is approved. Its summary jobs pass."] + early --> ready + ready --> queue["The pull request joins the merge queue of main or next"] + queue --> sel2["Selector job compares the queue commit with the entry ahead of it"] + sel2 --> all["Push checks and gate checks run. Mutants starts at once."] + all --> sum{"Does every summary job pass?"} + sum -- "Yes" --> merge["GitHub merges the tested commit"] + sum -- "No" --> out["The pull request leaves the queue"] +``` + +## What runs where + +These are the gate checks: + +- the Test EQL suites that need a database: the PostgreSQL shards, the e2e + property suite, and the test archive build +- Mutants +- CRAP +- the fuzz regression replay +- Go on Windows and macOS, and the Go live tests +- the Drizzle, prisma-next, Supabase, and protect-ffi integration suites +- the two Prisma E2E suites +- udeps and Miri + +Every other check is a push check. + +- The merge queue runs the push checks and the gate checks against the queue + commit. A failure removes the pull request from the queue. +- Pushes to `main` no longer run the tests. The queue has already tested the + commit that GitHub merges. +- `next` gets the same branch rules as `main`, including the merge queue. + Until now, `next` had no protection. Pushes to it ran no workflows. +- Each queue builds at most 3 entries at once. The weekly cost report will + show whether to change this. +- The queues merge with merge commits, as `main` does today. The comparison + with the entry ahead needs merge commits or squash merges. + +## How a workflow decides what runs + +- A selector job starts every workflow, on a GitHub-hosted runner. It selects + the checks whose trigger paths the change touches. +- In a merge queue, the selector job compares the queue commit with its + parent. The parent is the entry ahead, so the difference is this pull + request alone. The entries ahead run their own checks. If one fails, GitHub + rebuilds the entries behind it. +- When the selector job cannot decide, it fails. The change cannot merge until + someone runs it again. So a fault in the selector job is seen at once. +- A summary job ends every workflow. It is the only job of the workflow that + branch rules require. +- The failsafe is a test in `scripts/__tests__/`, run by + `pnpm run test:scripts`. It fails when the selector job does not decide + whether a job runs. A job is exempt only with a written reason. An exemption + that matches no job also fails. + +## Early runs + +An author can add the `ci:full` label to run the gate checks early on the pull +request. A job removes the label when the early run starts. So each early run +is a deliberate request. The queue still runs the gate checks again. + +## Mutants + +- Mutants is a required gate check. It tests only the lines that the pull + request changes. In a merge queue, it finds them the same way as the + selector job. +- It runs in every queue entry except the merge of `next` into `main`. Each + change on `next` has already passed Mutants in the `next` queue. +- It runs as 4 parallel shards on c8a machines with 16 vCPUs. +- It starts at the same time as the push checks, in the queue and on early + runs. A watcher job cancels it when a push check fails. +- On an early run, a cancelled Mutants run counts as a failure. The pull + request cannot join the queue until Mutants runs again. +- The other heavy gate checks also start at once. + +## Considered options + +- **Run gate checks when a draft pull request becomes ready for review.** + Agents push to pull requests that are already ready. So most pushes would + still run everything. +- **Run gate checks when a pull request is approved.** On `main`, an approval + stays valid after later pushes. So code pushed after an approval would merge + without gate checks. +- **Run gate checks only when an author adds a label, with no merge queue.** + That depends on people remembering to add it. +- **Filter workflows by path in `on:`, not in a selector job.** GitHub cannot + filter `merge_group` events by path. A workflow that a path filter skips + leaves its required checks pending. GitHub's documentation on required + status checks says that blocks the merge. +- **Compare each queue commit with the base branch.** With 3 entries + building, the third entry's difference includes the two ahead of it. + Mutants would test their lines again. A failure could point to another + author's lines. +- **Run every check when the selector job cannot decide.** No needed check + would be skipped. But nobody would notice a fault in the selector job. Each + fault would also cost a full run. +- **Run Mutants on the merge of `next` into `main` as well.** That run tests + every line `next` added, a second time. In October 2026, it reached its + 120-minute timeout at least twice on pull request #1229. +- **Run Mutants only in the `main` queue.** All of `next` would be tested at + once, when it merges into `main`. A failure would then point to anyone who + changed `next`. +- **Start Mutants after every push check passes.** That spends nothing on + failing commits. But it adds up to 14 minutes to each queue entry and early + run. In the queue, push checks rarely fail. A pull request joins only after + its required checks pass. +- **Keep the `ci:full` label until the author removes it.** A forgotten label + would run every gate check on every later push. + +## Consequences + +- Authors learn of a gate check failure in the queue, unless they ask for an + early run. +- A merge waits for the slowest gate check. Each queue's status check timeout + must be longer than that check takes. +- Every workflow with a required summary job must also run on `merge_group`. + GitHub's merge queue documentation says the queue starts no workflow + without it. The queue would then wait for a check that never reports. +- The queue setting "Only merge non-failing pull requests" stays on. Without + it, a pull request that failed could merge with the one behind it. +- The smallest group the queue merges is 1 pull request. So a lone pull + request never waits for others. +- Workflows must start when any label is added, because GitHub cannot filter + that event by label name. That run reports under the same check names as + the push run. A test branch must show that it cannot replace an earlier + failure. +- Commits made by the merge queue must pass the rule that requires signed + commits. A test branch must show this before the queues start. +- A test branch must also confirm that a queue commit's parent is the entry + ahead of it. GitHub's webhook schema calls `base_sha` the merge group's + parent commit. It does not say which commit that is. +- After a flaky push check on an early run, the author must re-run Mutants as + well. +- While it waits, the watcher holds one of the organisation's 60 + GitHub-hosted job slots. It waits up to about 14 minutes. +- This decision covers this repository only. `proxy` may follow later. diff --git a/scripts/__tests__/auth-binding-step-order.test.mjs b/scripts/__tests__/auth-binding-step-order.test.mjs index aa1b604ab1..82dd4317ef 100644 --- a/scripts/__tests__/auth-binding-step-order.test.mjs +++ b/scripts/__tests__/auth-binding-step-order.test.mjs @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' import { filterCovers } from './lib/paths-filter.mjs' +import { jobTriggerLists } from './lib/selector.mjs' import { readWorkflow, workflowFiles } from './lib/workflows.mjs' /** @@ -84,13 +85,20 @@ describe('the @cipherstash/auth binding is built wherever the SDK runs', () => { const same = (entry, input) => entry === input -/** The `paths:` lists of a workflow's filtered events. */ -const filters = (relPath) => { - const wf = readWorkflow(relPath) - const on = wf?.on ?? wf?.[true] - return ['push', 'pull_request'] - .map((event) => on?.[event]?.paths) - .filter(Array.isArray) +/** + * The path lists that decide whether the given jobs run, once each: the + * workflow's `push` and `pull_request` `paths:` lists, and the selector filter + * each job reads. Every job of the workflow when none are given. + */ +const filters = (relPath, jobNames) => { + const lists = new Map() + for (const jobName of jobNames ?? + Object.keys(readWorkflow(relPath)?.jobs ?? {})) { + for (const { source, paths } of jobTriggerLists(relPath, jobName)) { + lists.set(source, paths) + } + } + return [...lists.values()] } /** @@ -105,14 +113,19 @@ const AUTH_SOURCE_WORKFLOWS = [ ] describe('a change to the auth binding starts the jobs that load it', () => { - const users = [ - ...new Set( - JOBS.filter((job) => job.auth).map((job) => job.id.split(' / ')[0]), - ), - ] + /** Each workflow with a job that builds the binding, and those jobs. */ + const usersByWorkflow = new Map() + for (const job of JOBS.filter((entry) => entry.auth)) { + const [relPath, jobName] = job.id.split(' / ') + usersByWorkflow.set(relPath, [ + ...(usersByWorkflow.get(relPath) ?? []), + jobName, + ]) + } + const users = [...usersByWorkflow.keys()] it.each(users)('%s filters on the action', (relPath) => { - for (const paths of filters(relPath)) { + for (const paths of filters(relPath, usersByWorkflow.get(relPath))) { expect( filterCovers(paths, '.github/actions/build-auth-binding/**', same), ).toBe(true) @@ -133,7 +146,7 @@ describe('a change to the auth binding starts the jobs that load it', () => { '%s filters on the auth sources', (relPath) => { expect(shipped.length).toBeGreaterThanOrEqual(4) - const lists = filters(relPath) + const lists = filters(relPath, usersByWorkflow.get(relPath)) expect(lists.length).toBe(2) for (const paths of lists) { expect( diff --git a/scripts/__tests__/auth-refresh-soak-ci.test.mjs b/scripts/__tests__/auth-refresh-soak-ci.test.mjs index d35763e350..5f25839702 100644 --- a/scripts/__tests__/auth-refresh-soak-ci.test.mjs +++ b/scripts/__tests__/auth-refresh-soak-ci.test.mjs @@ -29,10 +29,10 @@ describe('auth refresh cancellation soak gates', () => { "github.head_ref == 'changeset-release/main'", ) expect(prs.jobs.long.if).toContain( - "needs.changes.outputs.refresh == 'true'", + "needs.selector.outputs.refresh == 'true'", ) - const paths = prs.jobs.changes.steps.find((step) => step.id === 'paths') - expect(paths.with.filters).toContain("'packages/stack-auth/**'") + const paths = prs.jobs.selector.steps.find((step) => step.id === 'filter') + expect(paths.with.filters).toContain('"packages/stack-auth/**"') }) it('requires a successful long soak before publishing stack-auth', () => { diff --git a/scripts/__tests__/crates-selectors.test.mjs b/scripts/__tests__/crates-selectors.test.mjs new file mode 100644 index 0000000000..8740487972 --- /dev/null +++ b/scripts/__tests__/crates-selectors.test.mjs @@ -0,0 +1,190 @@ +import { describe, expect, it } from 'vitest' +import { readSelectorJob, selectedJobs } from './lib/selector.mjs' + +/** + * What the selector jobs of the Rust crate workflows decide for a pull + * request. Each check's trigger paths list the crates it builds, including + * their path dependencies in the workspace, and the configuration its tools + * read. The gates that excluded documentation before still do. + */ + +const WORKFLOWS = { + crap: '.github/workflows/crap-crates.yml', + fuzz: '.github/workflows/fuzz.yml', + miri: '.github/workflows/miri.yml', + udeps: '.github/workflows/udeps.yml', + crates: '.github/workflows/tests-crates.yml', + rust: '.github/workflows/tests-rust.yml', + refresh: '.github/workflows/tests-auth-refresh.yml', +} + +/** The jobs a pull request runs, without the two that always run. */ +const checks = (relPath, files, options) => + selectedJobs(relPath, files, options).filter( + (id) => id !== 'selector' && id !== 'summary', + ) + +describe('the crate workflows', () => { + it.each(Object.values(WORKFLOWS))('%s has a selector job', (relPath) => { + expect(readSelectorJob(relPath)).not.toBeNull() + }) +}) + +describe('the CRAP gate selector', () => { + const run = (files) => checks(WORKFLOWS.crap, files) + + it('runs the gate of the crate that changed', () => { + expect(run(['packages/stack-auth/src/lib.rs'])).toEqual([ + 'crap-stack-auth', + 'crap-stack-encrypt', + ]) + expect(run(['packages/stack-encrypt/src/lib.rs'])).toEqual([ + 'crap-stack-encrypt', + ]) + }) + + it("runs a gate for a change to that crate's path dependencies", () => { + expect(run(['packages/stack-kms/src/lib.rs'])).toEqual([ + 'crap-stack-encrypt', + ]) + expect(run(['packages/stack-profile/src/lib.rs'])).toEqual([ + 'crap-stack-auth', + 'crap-stack-encrypt', + ]) + }) + + it('skips both gates for documentation and other crates', () => { + expect(run(['packages/stack-auth/README.md'])).toEqual([]) + expect(run(['packages/stack-guest-abi/src/lib.rs'])).toEqual([]) + }) +}) + +describe('the fuzz selector', () => { + const run = (files) => checks(WORKFLOWS.fuzz, files) + + it('replays the corpus for a change to a fuzzed crate or its dependencies', () => { + expect(run(['packages/stack-kms/fuzz/fuzz_targets/a.rs'])).toEqual([ + 'fuzz-regression', + ]) + expect(run(['packages/stack-profile/src/lib.rs'])).toEqual([ + 'fuzz-regression', + ]) + expect(run(['packages/stack-encrypt-derive/src/lib.rs'])).toEqual([ + 'fuzz-regression', + ]) + }) + + it('skips the replay for documentation and other crates', () => { + expect(run(['packages/stack-encrypt/docs/adr/0001-example.md'])).toEqual([]) + expect(run(['packages/stack-guest-abi/src/lib.rs'])).toEqual([]) + }) + + it('never runs the campaign on a pull request', () => { + expect(run(['packages/stack-auth/src/lib.rs'])).not.toContain( + 'fuzz-campaign', + ) + }) +}) + +describe('the Miri selector', () => { + const run = (files) => checks(WORKFLOWS.miri, files) + + it('runs Miri for the guest ABI crate only', () => { + expect(run(['packages/stack-guest-abi/src/lib.rs'])).toEqual([ + 'miri-stack-guest-abi', + ]) + expect(run(['packages/stack-auth/src/lib.rs'])).toEqual([]) + expect(run(['packages/stack-guest-abi/README.md'])).toEqual([]) + }) +}) + +describe('the unused dependencies selector', () => { + const run = (files) => checks(WORKFLOWS.udeps, files) + + it('runs for any crate of the workspace and the rust pin', () => { + expect(run(['languages/typescript/packages/auth/src/lib.rs'])).toEqual([ + 'udeps', + ]) + expect(run(['mise.toml'])).toEqual(['udeps']) + }) + + it('skips documentation', () => { + expect(run(['packages/stack-kms/README.md'])).toEqual([]) + }) +}) + +describe('the Tests (crates) selector', () => { + const run = (files) => checks(WORKFLOWS.crates, files) + + it('runs only the cargo job for a crate the bindings do not wrap', () => { + expect(run(['packages/stack-encrypt/src/lib.rs'])).toEqual(['rust']) + expect(run(['packages/stack-guest-abi/src/lib.rs'])).toEqual(['rust']) + expect(run(['.config/nextest.toml'])).toEqual(['rust']) + }) + + it('runs both jobs for a crate the bindings wrap', () => { + expect(run(['packages/stack-auth/src/lib.rs'])).toEqual([ + 'node-bindings', + 'rust', + ]) + // The README is stack-auth's crate docs, so its doctests read it. + expect(run(['packages/stack-auth/README.md'])).toEqual([ + 'node-bindings', + 'rust', + ]) + }) + + it('runs only the bindings job for a change to the pnpm install', () => { + expect(run(['pnpm-lock.yaml'])).toEqual(['node-bindings']) + expect(run(['pnpm-workspace.yaml'])).toEqual(['node-bindings']) + }) + + it('runs neither for a change outside the crates', () => { + expect(run(['languages/typescript/packages/stack/src/index.ts'])).toEqual( + [], + ) + }) +}) + +describe('the Tests (Rust) selector', () => { + const run = (files) => checks(WORKFLOWS.rust, files) + + it('runs for protect-ffi Rust, the EQL crates it builds, and the rust pin', () => { + for (const file of [ + 'languages/typescript/packages/protect-ffi/crates/protect-ffi/src/lib.rs', + 'packages/eql/crates/eql-bindings/src/lib.rs', + 'mise.toml', + ]) { + expect(run([file]), file).toEqual(['rust']) + } + }) + + it('skips protect-ffi TypeScript and the EQL SQL', () => { + expect( + run(['languages/typescript/packages/protect-ffi/src/index.ts']), + ).toEqual([]) + expect(run(['packages/eql/src/v3/schema.sql'])).toEqual([]) + }) +}) + +describe('the auth refresh selector', () => { + const run = (files, options) => checks(WORKFLOWS.refresh, files, options) + + it('runs the short soak on every pull request', () => { + expect(run(['README.md'])).toEqual(['short']) + }) + + it('runs the long soak for stack-auth and the crate it builds', () => { + expect(run(['packages/stack-auth/src/lib.rs'])).toEqual(['long', 'short']) + expect(run(['packages/stack-profile/src/lib.rs'])).toEqual([ + 'long', + 'short', + ]) + }) + + it('runs the long soak on Version Packages', () => { + expect( + run(['README.md'], { github: { head_ref: 'changeset-release/main' } }), + ).toEqual(['long', 'short']) + }) +}) diff --git a/scripts/__tests__/eql-selector.test.mjs b/scripts/__tests__/eql-selector.test.mjs new file mode 100644 index 0000000000..99afe4fab2 --- /dev/null +++ b/scripts/__tests__/eql-selector.test.mjs @@ -0,0 +1,212 @@ +import { spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import yaml from 'js-yaml' +import { afterAll, describe, expect, it } from 'vitest' +import { runsWhen } from './lib/expressions.mjs' +import { pathsGlobToRegExp } from './lib/paths-filter.mjs' +import { readWorkflow } from './lib/workflows.mjs' + +/** + * Test EQL's selector job decides which checks a pull request runs, by + * matching the changed files against each check's trigger paths. + * + * Only `rust-crates` compiles the stack-* crates, through eql-bindings' + * `stack-encrypt` feature. Every other job builds `tests/sqlx` or the + * codegen crates, which do not enable it. While the crates sat on the one list + * that gated every job, a pull request touching only stack-encrypt ran the + * whole Postgres suite. + * + * The selector is exercised the way GitHub runs it: the filter results go + * into the real decision script, and each job's real `if:` reads its outputs. + */ + +const WORKFLOW = '.github/workflows/test-eql.yml' +const SELECTOR = 'selector' +const REPO = 'cipherstash/stack' + +const wf = readWorkflow(WORKFLOW) +const selector = wf?.jobs?.[SELECTOR] + +const stepOutput = (expression) => + /steps\.([A-Za-z0-9_-]+)\.outputs\.([A-Za-z0-9_-]+)/.exec( + String(expression ?? ''), + ) + +const filterStep = (selector?.steps ?? []).find((step) => + String(step?.uses ?? '').startsWith('dorny/paths-filter@'), +) + +// dorny flattens a list that includes an anchored list; so does this. +const FILTERS = Object.fromEntries( + Object.entries(yaml.load(filterStep?.with?.filters ?? '') ?? {}).map( + ([name, globs]) => [name, [globs].flat(Infinity)], + ), +) + +const decideStepId = stepOutput(Object.values(selector?.outputs ?? {})[0])?.[1] +const decideStep = (selector?.steps ?? []).find( + (step) => step?.id === decideStepId, +) + +const GATED = Object.entries(wf?.jobs ?? {}) + .map(([id, job]) => ({ + id, + job, + output: new RegExp(`needs\\.${SELECTOR}\\.outputs\\.([A-Za-z0-9_-]+)`).exec( + String(job?.if ?? ''), + )?.[1], + })) + .filter((entry) => entry.output) + +const SCRATCH = mkdtempSync(join(tmpdir(), 'eql-selector-')) +afterAll(() => rmSync(SCRATCH, { recursive: true, force: true })) +let runCounter = 0 + +/** + * Runs the decision script with the filter results GitHub would substitute. + * An env entry that names no filter result gets the empty string, as GitHub + * substitutes for an expression that resolves to nothing. + */ +function decide(eventName, filterResults) { + runCounter += 1 + const outputFile = join(SCRATCH, `outputs-${runCounter}.txt`) + writeFileSync(outputFile, '') + const env = { PATH: process.env.PATH, GITHUB_OUTPUT: outputFile } + for (const [key, value] of Object.entries(decideStep?.env ?? {})) { + const text = String(value) + if (/github\.event_name/.test(text)) env[key] = eventName + else env[key] = filterResults[stepOutput(text)?.[2]] ?? '' + } + const run = spawnSync('bash', ['-c', String(decideStep?.run)], { + cwd: SCRATCH, + env, + encoding: 'utf8', + }) + const written = Object.fromEntries( + readFileSync(outputFile, 'utf8') + .split('\n') + .filter(Boolean) + .map((line) => [ + line.slice(0, line.indexOf('=')), + line.slice(line.indexOf('=') + 1), + ]), + ) + const outputs = Object.fromEntries( + Object.entries(selector?.outputs ?? {}).map(([name, expression]) => [ + name, + written[stepOutput(expression)?.[2]], + ]), + ) + return { status: run.status, stderr: `${run.stdout}${run.stderr}`, outputs } +} + +/** The gated jobs a same-repository pull request runs, given its files. */ +function jobsRunFor(changedFiles) { + const filterResults = Object.fromEntries( + Object.entries(FILTERS).map(([name, globs]) => [ + name, + String( + changedFiles.some((file) => + globs.some((glob) => pathsGlobToRegExp(glob).test(file)), + ), + ), + ]), + ) + const { status, stderr, outputs } = decide('pull_request', filterResults) + expect(status, stderr).toBe(0) + const context = { + github: { + event_name: 'pull_request', + repository: REPO, + event: { pull_request: { head: { repo: { full_name: REPO } } } }, + }, + needs: { [SELECTOR]: { result: 'success', outputs } }, + vars: {}, + } + return GATED.filter(({ job }) => runsWhen(job.if, context)) + .map(({ id }) => id) + .sort() +} + +const ALL_GATED = GATED.map(({ id }) => id).sort() + +describe('the Test EQL selector job', () => { + it('finds the selector, its filters, and the jobs it gates', () => { + expect(selector, `${WORKFLOW} has no \`${SELECTOR}\` job.`).toBeTruthy() + expect( + Object.keys(FILTERS), + 'The selector has no path filters.', + ).not.toEqual([]) + expect( + decideStep, + 'No step produces the selector job outputs.', + ).toBeTruthy() + expect(ALL_GATED).toContain('rust-crates') + expect(ALL_GATED.length).toBeGreaterThan(1) + }) + + it('runs on a GitHub-hosted runner', () => { + // ADR 0003: a RunsOn machine takes 40 to 50 seconds to start, and every + // other job in the run waits for this one. + expect(String(selector?.['runs-on'])).toMatch(/^ubuntu-/) + }) + + it('names each output after the path filter that decides it', () => { + // eql-suite-ci.test.mjs follows a job's `if:` to its filter by this name. + for (const { id, output } of GATED) { + expect( + Object.keys(FILTERS), + `"${id}" reads \`needs.${SELECTOR}.outputs.${output}\`, and the selector has no path filter called "${output}".`, + ).toContain(output) + } + }) + + it('runs only rust-crates for a change to a stack crate', () => { + expect(jobsRunFor(['packages/stack-encrypt/src/lib.rs'])).toEqual([ + 'rust-crates', + ]) + expect(jobsRunFor(['Cargo.toml'])).toEqual(['rust-crates']) + }) + + it('runs every gated job for a change to EQL', () => { + expect(jobsRunFor(['packages/eql/src/v3/schema.sql'])).toEqual(ALL_GATED) + expect(jobsRunFor([WORKFLOW])).toEqual(ALL_GATED) + }) + + it('runs no gated job for a change outside EQL and the crates', () => { + expect( + jobsRunFor(['languages/typescript/packages/stack/src/index.ts']), + ).toEqual([]) + }) + + it('opens every output on events other than pull_request', () => { + // The filter runs only on pull_request. Every other event takes the + // default, and a wrong default skips every gated job behind a green run. + for (const event of [ + 'push', + 'schedule', + 'merge_group', + 'workflow_dispatch', + ]) { + const { status, outputs } = decide(event, {}) + expect(status).toBe(0) + for (const name of Object.keys(selector?.outputs ?? {})) + expect(outputs[name], `${name} on ${event}`).toBe('true') + } + }) + + it('fails when a pull request filter gives no answer', () => { + // A missing answer would skip every gated job, and `ci-required` counts + // skipped as passed: a green run that ran nothing. + expect(decideStep?.run).toBeTruthy() + for (const bad of [ + {}, + Object.fromEntries(Object.keys(FILTERS).map((name) => [name, 'maybe'])), + ]) { + const { status } = decide('pull_request', bad) + expect(status).not.toBe(0) + } + }) +}) diff --git a/scripts/__tests__/eql-suite-ci.test.mjs b/scripts/__tests__/eql-suite-ci.test.mjs index ee00032a80..2f37dcc384 100644 --- a/scripts/__tests__/eql-suite-ci.test.mjs +++ b/scripts/__tests__/eql-suite-ci.test.mjs @@ -209,13 +209,15 @@ describe('the relevance filter still selects the imported tree', () => { * parsed rather than pattern-matched — a regex over the raw block would also * match the surrounding commentary. */ - const filterPaths = Object.values(wf?.jobs ?? {}) - .flatMap((job) => job?.steps ?? []) - .filter((step) => (step?.uses ?? '').startsWith('dorny/paths-filter@')) - .flatMap((step) => { - const filters = yaml.load(step?.with?.filters ?? '') - return Object.values(filters ?? {}).flat() - }) + const filters = Object.assign( + {}, + ...Object.values(wf?.jobs ?? {}) + .flatMap((job) => job?.steps ?? []) + .filter((step) => (step?.uses ?? '').startsWith('dorny/paths-filter@')) + .map((step) => yaml.load(step?.with?.filters ?? '') ?? {}), + ) + // A filter that includes an anchored list parses to a nested list. + const filterPaths = [...new Set(Object.values(filters).flat(Infinity))] it('finds the paths-filter step', () => { // Without this the check below iterates an empty list and passes having @@ -395,17 +397,29 @@ describe('the relevance filter still selects the imported tree', () => { // The other direction: a path dependency outside the subtree that the // filter does NOT select is a job that compiles a crate and never runs on // a change to it — the gap commit 7219ea1ad repaired by hand, now derived. + // + // Checked against the filter the `rust-crates` job reads, not the union: + // that job is the one that compiles them, and the other jobs' filter + // leaves them out on purpose (eql-selector.test.mjs). + const filterName = /needs\.selector\.outputs\.([A-Za-z0-9_-]+)/.exec( + String(wf?.jobs?.['rust-crates']?.if ?? ''), + )?.[1] + const rustCratesPaths = [filters[filterName] ?? []].flat(Infinity) + expect( + rustCratesPaths, + `The \`rust-crates\` job in ${EQL_WORKFLOW} reads no selector filter, so this check has nothing to compare.`, + ).not.toEqual([]) const missing = externalPathDependencies.dirs .map((dir) => `${dir}/**`) - .filter((glob) => !filterPaths.includes(glob)) + .filter((glob) => !rustCratesPaths.includes(glob)) if ( externalPathDependencies.inherits && - !filterPaths.includes('Cargo.toml') + !rustCratesPaths.includes('Cargo.toml') ) missing.push('Cargo.toml') expect( missing, - `The EQL workspace compiles these by path and the relevance filter in ${EQL_WORKFLOW} does not select them, so the \`rust-crates\` job skips on a change that can break it. Add each to the filter — and, because the three copies are held equal, to the \`push: paths:\` list and to bench-eql.yml in the same edit.\n${missing.map((p) => ` ${p}`).join('\n')}`, + `The EQL workspace compiles these by path and the \`${filterName}\` filter in ${EQL_WORKFLOW} does not select them, so the \`rust-crates\` job skips on a change that can break it. Add each to that filter — and, because the copies are held equal, to the \`push: paths:\` list and to bench-eql.yml in the same edit.\n${missing.map((p) => ` ${p}`).join('\n')}`, ).toEqual([]) }) }) diff --git a/scripts/__tests__/eql-workflow-filters.test.mjs b/scripts/__tests__/eql-workflow-filters.test.mjs index 5f230772d7..a6fb919600 100644 --- a/scripts/__tests__/eql-workflow-filters.test.mjs +++ b/scripts/__tests__/eql-workflow-filters.test.mjs @@ -3,6 +3,7 @@ import { readFileSync } from 'node:fs' import { join } from 'node:path' import yaml from 'js-yaml' import { describe, expect, it } from 'vitest' +import { pathsGlobToRegExp } from './lib/paths-filter.mjs' import { REPO_ROOT } from './lib/repo-root.mjs' import { readWorkflow } from './lib/workflows.mjs' @@ -12,9 +13,10 @@ import { readWorkflow } from './lib/workflows.mjs' * * `test-eql.yml` writes its list TWICE and in two different mechanisms: an * `on: push: paths:` filter (which decides whether the workflow starts at all - * after a merge) and a `dorny/paths-filter` `relevant:` filter (which decides - * whether the heavy jobs run inside a pull-request run). GitHub gives no way to - * share them. `bench-eql.yml` writes a third copy, for the same tree, on its own + * after a merge) and the selector job's `dorny/paths-filter` filters (which + * decide which heavy jobs run inside a pull-request run). GitHub gives no way + * to share them. The selector splits its list by job, so the copy compared + * here is the union of its filters. `bench-eql.yml` writes a third copy, for the same tree, on its own * `push` trigger. * * Three properties, and each of them was violated by the imported files: @@ -73,21 +75,26 @@ function pushPaths(relPath) { /** * Every path listed by a `dorny/paths-filter` step, across every filter it - * declares. `filters:` is a YAML document embedded in a YAML string, so it is - * parsed rather than pattern-matched — a regex over the raw block would also - * match the commentary around it. + * declares, once each. `filters:` is a YAML document embedded in a YAML string, + * so it is parsed rather than pattern-matched — a regex over the raw block + * would also match the commentary around it. */ function dornyFilterPaths(relPath) { const wf = readWorkflow(relPath) - return Object.values(wf?.jobs ?? {}) - .flatMap((job) => job?.steps ?? []) - .filter((step) => - String(step?.uses ?? '').startsWith('dorny/paths-filter@'), - ) - .flatMap((step) => - Object.values(yaml.load(step?.with?.filters ?? '') ?? {}), - ) - .flat() + return [ + ...new Set( + Object.values(wf?.jobs ?? {}) + .flatMap((job) => job?.steps ?? []) + .filter((step) => + String(step?.uses ?? '').startsWith('dorny/paths-filter@'), + ) + .flatMap((step) => + Object.values(yaml.load(step?.with?.filters ?? '') ?? {}), + ) + // An entry that is an anchored list parses to a nested list. + .flat(Infinity), + ), + ] } /** The local composite actions a workflow reaches through `uses: ./…`. */ @@ -114,37 +121,7 @@ function trackedUnder(pathspec) { .filter(Boolean) } -/** - * Approximates picomatch, which is what `dorny/paths-filter` and GitHub's own - * `paths:` matcher are built on: `*` stops at a separator, `**` does not. - * - * No implicit-descendants tail here, unlike the `@actions/glob` approximation - * in `ffi-binding-action.test.mjs` — a bare directory name is NOT a prefix - * match in a `paths:` filter, which is the difference that makes a - * `.../require-cs-secrets` entry without its `/**` cover nothing. - */ -function globToRegExp(pattern) { - let source = '^' - for (let i = 0; i < pattern.length; i += 1) { - const ch = pattern[i] - if (ch === '*') { - if (pattern[i + 1] === '*') { - source += '.*' - i += 1 - if (pattern[i + 1] === '/') i += 1 - } else { - source += '[^/]*' - } - } else if (ch === '?') { - source += '[^/]' - } else { - source += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&') - } - } - return new RegExp(`${source}$`) -} - -const matches = (pattern, path) => globToRegExp(pattern).test(path) +const matches = (pattern, path) => pathsGlobToRegExp(pattern).test(path) /** Everything in the index a filter entry could plausibly select. */ const TRACKED = trackedUnder('.') @@ -152,7 +129,7 @@ const TRACKED = trackedUnder('.') /** The list each mechanism carries, keyed for the messages below. */ const LISTS = [ { id: `${TEST_EQL} (on.push.paths)`, entries: pushPaths(TEST_EQL) }, - { id: `${TEST_EQL} (dorny relevant:)`, entries: dornyFilterPaths(TEST_EQL) }, + { id: `${TEST_EQL} (selector filters)`, entries: dornyFilterPaths(TEST_EQL) }, { id: `${BENCH_EQL} (on.push.paths)`, entries: pushPaths(BENCH_EQL) }, ] diff --git a/scripts/__tests__/integration-workflow-paths.test.mjs b/scripts/__tests__/integration-workflow-paths.test.mjs index c3b1e812af..a44f65539b 100644 --- a/scripts/__tests__/integration-workflow-paths.test.mjs +++ b/scripts/__tests__/integration-workflow-paths.test.mjs @@ -2,6 +2,7 @@ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs' import { join } from 'node:path' import { describe, expect, it } from 'vitest' import { REPO_ROOT } from './lib/repo-root.mjs' +import { jobTriggerLists } from './lib/selector.mjs' import { readWorkflow, workflowFiles } from './lib/workflows.mjs' /** @@ -40,9 +41,6 @@ const STACK_SRC = 'languages/typescript/packages/stack/src' const STACK_MANIFEST = 'languages/typescript/packages/stack/package.json' const CATALOG_MANIFEST = 'pnpm-workspace.yaml' -/** Both trigger events, in the order GitHub evaluates them. */ -const TRIGGER_EVENTS = ['push', 'pull_request'] - /** * The integration workflows, discovered rather than listed: any workflow whose * `CS_IT_SUITE` globs select suites out of `languages/typescript/packages/stack/` is in scope. A @@ -56,14 +54,21 @@ function discoverWorkflows() { } /** - * `on:` parses as the boolean `true` under YAML 1.1 (the "Norway problem"), - * which is why this reads both keys rather than `wf.on`. + * The path lists that decide whether the jobs running a suite run, once each: + * the workflow's `push` and `pull_request` `paths:` lists, and the selector + * filter each such job reads. */ -function triggerBlocks(wf) { - const on = wf.on ?? wf[true] - return TRIGGER_EVENTS.map((event) => on?.[event]).filter( - (block) => block && Array.isArray(block.paths), - ) +function triggerBlocks(relPath) { + const lists = new Map() + for (const [jobId, job] of Object.entries( + readWorkflow(relPath)?.jobs ?? {}, + )) { + if (!job?.env?.CS_IT_SUITE) continue + for (const { source, paths } of jobTriggerLists(relPath, jobId)) { + lists.set(source, { source, paths }) + } + } + return [...lists.values()] } /** Every `CS_IT_SUITE` glob set declared anywhere in the workflow. */ @@ -190,8 +195,9 @@ describe('integration workflow paths filters', () => { for (const relPath of WORKFLOWS) { it(`${relPath} triggers on the manifests pinning its suites' dependencies`, () => { const wf = readWorkflow(relPath) - const blocks = triggerBlocks(wf) - expect(blocks.length).toBe(TRIGGER_EVENTS.length) + const blocks = triggerBlocks(relPath) + // A `push` list, and a `pull_request` list or a selector filter. + expect(blocks.length).toBe(2) const deps = stackDependencies() const files = suiteFiles(suiteGlobs(wf)) @@ -215,7 +221,7 @@ describe('integration workflow paths filters', () => { it(`${relPath} triggers on every source path its suites import`, () => { const wf = readWorkflow(relPath) - const blocks = triggerBlocks(wf) + const blocks = triggerBlocks(relPath) expect(blocks.length).toBeGreaterThan(0) const files = suiteFiles(suiteGlobs(wf)) diff --git a/scripts/__tests__/lib/paths-filter.mjs b/scripts/__tests__/lib/paths-filter.mjs index 29710f536c..57e77c10ce 100644 --- a/scripts/__tests__/lib/paths-filter.mjs +++ b/scripts/__tests__/lib/paths-filter.mjs @@ -1,3 +1,28 @@ +// Approximates picomatch, the matcher behind dorny/paths-filter and GitHub's +// `paths:`: `*` stops at a separator and `**` does not. A bare directory name +// is not a prefix match, so `.../require-cs-secrets` without `/**` covers +// nothing. +export function pathsGlobToRegExp(pattern) { + let source = '^' + for (let i = 0; i < pattern.length; i += 1) { + const ch = pattern[i] + if (ch === '*') { + if (pattern[i + 1] === '*') { + source += '.*' + i += 1 + if (pattern[i + 1] === '/') i += 1 + } else { + source += '[^/]*' + } + } else if (ch === '?') { + source += '[^/]' + } else { + source += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&') + } + } + return new RegExp(`${source}$`) +} + const globPrefix = (pattern) => { const segments = pattern.split('/') const at = segments.findIndex((segment) => /[*?[]/.test(segment)) diff --git a/scripts/__tests__/lib/selector.mjs b/scripts/__tests__/lib/selector.mjs new file mode 100644 index 0000000000..ec2f910bb2 --- /dev/null +++ b/scripts/__tests__/lib/selector.mjs @@ -0,0 +1,361 @@ +import { spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import yaml from 'js-yaml' +import { runsWhen } from './expressions.mjs' +import { pathsGlobToRegExp } from './paths-filter.mjs' +import { readWorkflow } from './workflows.mjs' + +/** + * Reading and running a workflow's selector job, for the guards that pin what + * it decides. + * + * The selector job is the first job of a workflow. It compares the files a + * pull request changes with each check's trigger paths, using + * dorny/paths-filter, and writes one output per filter. Each gated job's `if:` + * reads the output named after its filter. ADR 0003 and `.github/CONTEXT.md` + * describe the design. + * + * Everything here runs the workflow's own text: the filter rules as written, + * the decision script as written, and each job's real `if:`. A guard that + * reimplemented the decision would agree with a rewritten one by luck. + */ + +export const SELECTOR_JOB = 'selector' + +/** The events the selector decides nothing for: each one selects every job. */ +export const NON_PULL_REQUEST_EVENTS = [ + 'push', + 'schedule', + 'merge_group', + 'workflow_dispatch', +] + +const DORNY = 'dorny/paths-filter@' + +const stepOutputRef = (expression) => + /steps\.([A-Za-z0-9_-]+)\.outputs\.([A-Za-z0-9_-]+)/.exec( + String(expression ?? ''), + ) + +const selectorOutputRefs = (expression) => + [ + ...String(expression ?? '').matchAll( + new RegExp(`needs\\.${SELECTOR_JOB}\\.outputs\\.([A-Za-z0-9_-]+)`, 'g'), + ), + ].map((match) => match[1]) + +/** + * `{a,b}` expanded into its alternatives, as picomatch reads a brace pattern. + * One level only: a nested brace throws, rather than being read wrongly. + */ +export function expandBraces(pattern) { + const open = pattern.indexOf('{') + if (open === -1) return [pattern] + const close = pattern.indexOf('}', open) + const inner = pattern.slice(open + 1, close) + if (close === -1 || inner.includes('{')) { + throw new Error(`Cannot expand the brace pattern ${pattern}`) + } + const head = pattern.slice(0, open) + return inner + .split(',') + .flatMap((alternative) => + expandBraces(`${head}${alternative}${pattern.slice(close + 1)}`), + ) +} + +/** One dorny/paths-filter rule against one file. A `!` rule negates. */ +export function dornyRuleMatches(rule, file) { + if (rule.startsWith('!')) return !dornyRuleMatches(rule.slice(1), file) + return expandBraces(rule).some((glob) => pathsGlobToRegExp(glob).test(file)) +} + +/** + * Whether a file counts for a filter. Under dorny's default quantifier, `some`, + * one matching rule is enough; under `every`, all of them must match. + */ +export function dornyFilterMatches(rules, quantifier, file) { + return quantifier === 'every' + ? rules.every((rule) => dornyRuleMatches(rule, file)) + : rules.some((rule) => dornyRuleMatches(rule, file)) +} + +/** + * A workflow's selector job, parsed, or `null` when it has none. + * + * `filters` maps each filter name to its rules. A filter that includes an + * anchored list (`- *name`) parses to a nested list, and dorny flattens it; + * so does this. `outputs` maps each selector output to the decision step + * output it reads. + */ +export function readSelectorJob(relPath) { + const workflow = readWorkflow(relPath) + const job = workflow?.jobs?.[SELECTOR_JOB] + if (!job) return null + const steps = Array.isArray(job.steps) ? job.steps : [] + const filterSteps = steps.filter((step) => + String(step?.uses ?? '').startsWith(DORNY), + ) + const filterStep = filterSteps.length === 1 ? filterSteps[0] : null + const filters = Object.fromEntries( + Object.entries(yaml.load(filterStep?.with?.filters ?? '') ?? {}).map( + ([name, rules]) => [name, [rules].flat(Infinity).map(String)], + ), + ) + const outputs = Object.fromEntries( + Object.entries(job.outputs ?? {}).map(([name, expression]) => [ + name, + stepOutputRef(expression), + ]), + ) + const decideIds = [ + ...new Set(Object.values(outputs).map((ref) => ref?.[1])), + ].filter(Boolean) + const decideStep = + decideIds.length === 1 + ? steps.find((step) => step?.id === decideIds[0]) + : null + return { + workflow, + job, + filterSteps, + filterStep, + quantifier: filterStep?.with?.['predicate-quantifier'] ?? 'some', + filters, + outputs: Object.fromEntries( + Object.entries(outputs).map(([name, ref]) => [name, ref?.[2]]), + ), + decideStep, + } +} + +/** The selector outputs a job's `if:` reads. */ +export function selectorOutputsRead(job) { + return [...new Set(selectorOutputRefs(job?.if))] +} + +/** + * Runs the selector's decision script the way GitHub would: `answers` stands + * in for the filter step's outputs, keyed by filter name. An env entry that + * names no answer gets the empty string, as GitHub substitutes for an + * expression that resolves to nothing. + * + * Returns the exit status, the log, and the selector job's outputs. + */ +export function runSelectorDecision(relPath, eventName, answers) { + const selector = readSelectorJob(relPath) + const scratch = mkdtempSync(join(tmpdir(), 'selector-')) + try { + const outputFile = join(scratch, 'outputs.txt') + writeFileSync(outputFile, '') + const env = { PATH: process.env.PATH, GITHUB_OUTPUT: outputFile } + for (const [key, value] of Object.entries( + selector?.decideStep?.env ?? {}, + )) { + const text = String(value) + if (/github\.event_name/.test(text)) env[key] = eventName + else env[key] = answers[stepOutputRef(text)?.[2]] ?? '' + } + const run = spawnSync( + 'bash', + ['-c', String(selector?.decideStep?.run ?? 'exit 99')], + { cwd: scratch, env, encoding: 'utf8' }, + ) + const written = Object.fromEntries( + readFileSync(outputFile, 'utf8') + .split('\n') + .filter(Boolean) + .map((line) => [ + line.slice(0, line.indexOf('=')), + line.slice(line.indexOf('=') + 1), + ]), + ) + return { + status: run.status, + log: `${run.stdout}${run.stderr}`, + outputs: Object.fromEntries( + Object.entries(selector?.outputs ?? {}).map(([name, stepOutput]) => [ + name, + written[stepOutput], + ]), + ), + } + } finally { + rmSync(scratch, { recursive: true, force: true }) + } +} + +/** What each filter answers for a pull request that changes `files`. */ +export function filterAnswers(relPath, files) { + const selector = readSelectorJob(relPath) + return Object.fromEntries( + Object.entries(selector?.filters ?? {}).map(([name, rules]) => [ + name, + String( + files.some((file) => + dornyFilterMatches(rules, selector.quantifier, file), + ), + ), + ]), + ) +} + +const needsOf = (job) => [job?.needs ?? []].flat() + +/** + * The jobs that run for a same-repository pull request changing `files`, + * sorted. The selector job's outputs come from its own script; each job's + * `if:` is evaluated as GitHub does, including the implicit `success()` that + * skips a job when one it needs did not succeed. + * + * `github` adds to the context, for a job whose `if:` reads more than the + * event and the selector (such as `head_ref`). + */ +export function selectedJobs(relPath, files, { github = {} } = {}) { + const selector = readSelectorJob(relPath) + const decision = runSelectorDecision( + relPath, + 'pull_request', + filterAnswers(relPath, files), + ) + if (decision.status !== 0) { + throw new Error(`The selector failed:\n${decision.log}`) + } + const repository = 'cipherstash/stack' + const context = { + github: { + event_name: 'pull_request', + repository, + head_ref: 'feature', + event: { + pull_request: { + draft: false, + head: { repo: { full_name: repository } }, + user: { type: 'User' }, + }, + }, + ...github, + }, + needs: {}, + vars: {}, + } + const jobs = selector.workflow.jobs + const results = { + [SELECTOR_JOB]: { result: 'success', outputs: decision.outputs }, + } + const resolve = (id) => { + if (results[id]) return results[id].result + const job = jobs[id] + const needs = needsOf(job) + const needResults = needs.map(resolve) + const condition = String(job?.if ?? '') + const statusFunction = /\b(always|failure|cancelled|success)\(\)/.test( + condition, + ) + let runs + if (!statusFunction && needResults.some((result) => result !== 'success')) { + runs = false + } else if (!condition) { + runs = true + } else { + runs = runsWhen(condition, { + ...context, + needs: Object.fromEntries(needs.map((need) => [need, results[need]])), + }) + } + results[id] = { result: runs ? 'success' : 'skipped', outputs: {} } + return results[id].result + } + return Object.keys(jobs) + .filter((id) => resolve(id) === 'success') + .sort() +} + +// GitHub reads a `paths:` list in order, and the last matching entry decides. +export function pathsListSelects(paths, file) { + let selected = false + for (const entry of paths) { + if (entry.startsWith('!')) { + if (pathsGlobToRegExp(entry.slice(1)).test(file)) selected = false + } else if (pathsGlobToRegExp(entry).test(file)) { + selected = true + } + } + return selected +} + +/** + * A filter as a GitHub `paths:` list: entries in order, a `!` entry excluding + * what came before it. Lets the guards that check a workflow's `paths:` + * coverage read a selector filter with the same code. + * + * Under `every`, the one positive rule's brace alternatives come first and the + * `!` rules after, which selects exactly the files dorny selects. Under + * `some`, a filter has no `!` rule (`workflow-selectors.test.mjs`), so it is + * already that list. + */ +export function filterAsPathsList(rules, quantifier) { + if (quantifier !== 'every') return rules.flatMap(expandBraces) + const positive = rules.filter((rule) => !rule.startsWith('!')) + const negative = rules.filter((rule) => rule.startsWith('!')) + return [...positive.flatMap(expandBraces), ...negative] +} + +/** + * The path lists that decide whether a job runs on a change, as GitHub + * `paths:` lists: the workflow's `on.push.paths` and `on.pull_request.paths` + * where they exist, and each selector filter the job's `if:` reads. + * + * A job in a selector workflow that reads no filter runs on every pull + * request, so only the `push` list, if any, decides it. + */ +export function jobTriggerLists(relPath, jobId) { + const workflow = readWorkflow(relPath) + const on = workflow?.on ?? workflow?.[true] + const lists = ['push', 'pull_request'] + .filter((event) => Array.isArray(on?.[event]?.paths)) + .map((event) => ({ + source: `on.${event}.paths`, + paths: on[event].paths.map(String), + })) + const selector = readSelectorJob(relPath) + for (const name of selectorOutputsRead(workflow?.jobs?.[jobId])) { + const rules = selector?.filters?.[name] + if (!rules) continue + lists.push({ + source: `selector filter "${name}"`, + paths: filterAsPathsList(rules, selector.quantifier), + }) + } + return lists +} + +/** + * Runs the summary job's check with the given results of the jobs it needs, + * and returns its exit status. The results stand in for + * `join(needs.*.result, ' ')`, wherever the step reads it. + */ +export function runSummaryCheck(relPath, summaryId, results) { + const workflow = readWorkflow(relPath) + const steps = workflow?.jobs?.[summaryId]?.steps ?? [] + const joined = results.join(' ') + const resultsExpression = /\$\{\{\s*join\(needs\.\*\.result,\s*' '\)\s*\}\}/g + let status = 0 + for (const step of steps) { + if (typeof step?.run !== 'string') continue + const env = { PATH: process.env.PATH } + for (const [key, value] of Object.entries(step.env ?? {})) { + env[key] = String(value).replace(resultsExpression, joined) + } + const run = spawnSync( + 'bash', + ['-c', step.run.replace(resultsExpression, joined)], + { env, encoding: 'utf8' }, + ) + status = run.status + if (status !== 0) break + } + return status +} diff --git a/scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs b/scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs index 28d96ad942..8f28725adb 100644 --- a/scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs +++ b/scripts/__tests__/wasm-build-inputs-paths-filter.test.mjs @@ -4,6 +4,7 @@ import yaml from 'js-yaml' import { describe, expect, it } from 'vitest' import { filterCovers } from './lib/paths-filter.mjs' import { REPO_ROOT } from './lib/repo-root.mjs' +import { jobTriggerLists } from './lib/selector.mjs' import { readWorkflow, workflowFiles } from './lib/workflows.mjs' /** @@ -57,9 +58,6 @@ const ACTION = '.github/actions/build-ffi-binding/action.yml' const ACTION_USES = './.github/actions/build-ffi-binding' const CACHE_ACTION = /^actions\/cache(\/(restore|save))?@/ -/** Both trigger events that carry a `paths:` filter, in GitHub's own order. */ -const FILTERED_EVENTS = ['push', 'pull_request'] - /** The package whose Rust both cache keys are about. */ const FFI_PACKAGE = 'languages/typescript/packages/protect-ffi' const FFI_PACKAGE_NAME = '@cipherstash/protect-ffi' @@ -276,21 +274,34 @@ function stepDir(wf, job, step) { /** Every job step of a workflow, paired with the job that owns it. */ function steps(wf) { - return Object.values(wf?.jobs ?? {}).flatMap((job) => - (Array.isArray(job?.steps) ? job.steps : []).map((step) => ({ job, step })), + return Object.entries(wf?.jobs ?? {}).flatMap(([jobId, job]) => + (Array.isArray(job?.steps) ? job.steps : []).map((step) => ({ + jobId, + job, + step, + })), ) } -/** Runs `./.github/actions/build-ffi-binding`, optionally with `wasm: 'true'`. */ -function usesAction(relPath, { wasmOnly = false } = {}) { - return steps(readWorkflow(relPath)).some( - ({ step }) => - typeof step?.uses === 'string' && - step.uses.trim() === ACTION_USES && - (!wasmOnly || String(step?.with?.wasm) === 'true'), +const jobIdsOf = (entries) => [...new Set(entries.map(({ jobId }) => jobId))] + +/** + * The jobs that run `./.github/actions/build-ffi-binding`, optionally with + * `wasm: 'true'`. + */ +function actionJobs(relPath, { wasmOnly = false } = {}) { + return jobIdsOf( + steps(readWorkflow(relPath)).filter( + ({ step }) => + typeof step?.uses === 'string' && + step.uses.trim() === ACTION_USES && + (!wasmOnly || String(step?.with?.wasm) === 'true'), + ), ) } +const usesAction = (relPath, options) => actionJobs(relPath, options).length > 0 + /** * Compiles this package's Rust — through the action, or directly. * @@ -300,10 +311,9 @@ function usesAction(relPath, { wasmOnly = false } = {}) { * EQL workspace has its own, with its own workflows — is not mistaken for this * one. */ -function compilesFfiRust(relPath) { - if (usesAction(relPath)) return true +function ffiRustJobs(relPath) { const wf = readWorkflow(relPath) - return steps(wf).some(({ job, step }) => { + const direct = steps(wf).filter(({ job, step }) => { const run = String(step?.run ?? '') if (!run) return false const targetsPackage = @@ -316,24 +326,28 @@ function compilesFfiRust(relPath) { (inPackage && mentionsAny(run, CARGO_TASKS)) ) }) + return [...new Set([...actionJobs(relPath), ...jobIdsOf(direct)])] } +const compilesFfiRust = (relPath) => ffiRustJobs(relPath).length > 0 + /** - * The `paths:` filters a workflow declares, one entry per filtered event. + * The path lists that decide whether the given jobs run: the workflow's `push` + * and `pull_request` `paths:` lists, and the selector filter each job reads. + * One entry per list, labelled with where it comes from. * - * `on:` parses as the boolean `true` under YAML 1.1 (the "Norway problem"), - * hence `wf.on ?? wf[true]`. An event with no `paths:` is UNFILTERED and is - * skipped rather than failed — it already runs on every diff, so it cannot miss - * one. `tests.yml` and `tests-rust.yml` are both half-cases: `push` is - * `branches: [main]` with no filter, so only their `pull_request` lists are - * here. `tests.yml`'s is `'**'` with one negation, which `filterCovers` reads. + * A job that no list decides is skipped rather than failed: it runs on every + * diff, so it cannot miss one. A selector filter is read as a `paths:` list, + * so a `'**'` with negations is read the same way in either. */ -function filteredEvents(relPath) { - const wf = readWorkflow(relPath) - const on = wf?.on ?? wf?.[true] - return FILTERED_EVENTS.filter((event) => - Array.isArray(on?.[event]?.paths), - ).map((event) => ({ relPath, event, paths: on[event].paths })) +function filteredEvents(relPath, jobIds) { + const lists = new Map() + for (const jobId of jobIds) { + for (const { source, paths } of jobTriggerLists(relPath, jobId)) { + lists.set(source, { relPath, event: source, paths }) + } + } + return [...lists.values()] } const ALL_WORKFLOWS = workflowFiles() @@ -381,6 +395,7 @@ const KEYS = [ what: '`index.node`, the binding every credentialed suite encrypts through', reaches: 'compiles the crate graph behind `index.node`', workflows: ALL_WORKFLOWS.filter(compilesFfiRust), + jobs: ffiRustJobs, expected: [ '.github/workflows/_build-ffi-artifacts.yml', '.github/workflows/integration-drizzle.yml', @@ -401,6 +416,7 @@ const KEYS = [ workflows: ALL_WORKFLOWS.filter((relPath) => usesAction(relPath, { wasmOnly: true }), ), + jobs: (relPath) => actionJobs(relPath, { wasmOnly: true }), expected: [ '.github/workflows/integration-drizzle.yml', '.github/workflows/integration-protect-ffi.yml', @@ -447,7 +463,9 @@ describe('the FFI cache keys and the filters that trigger them agree', () => { for (const key of KEYS) { const inputs = buildInputs(key.step) - const filtered = key.workflows.flatMap(filteredEvents) + const filtered = key.workflows.flatMap((relPath) => + filteredEvents(relPath, key.jobs(relPath)), + ) describe(`the ${key.id} key`, () => { it('hashes build inputs that name real files', () => { @@ -506,7 +524,7 @@ describe('the FFI cache keys and the filters that trigger them agree', () => { ) expect( uncovered, - `These files are hashed into the ${key.id} cache key of \`${ACTION}\`, so an edit to one MISSES the cache and rebuilds ${key.what}. ${relPath} ${key.reaches}, but its \`${event}\` \`paths:\` filter does not list them, so a PR touching only one of them rebuilds the artifact and starts no suite that exercises it. Nothing goes red; nothing ran.\nAdd each to BOTH copies of the filter — GitHub Actions has no YAML anchors, and \`workflow-paths-filter-parity.test.mjs\` fails a one-sided edit.`, + `These files are hashed into the ${key.id} cache key of \`${ACTION}\`, so an edit to one MISSES the cache and rebuilds ${key.what}. ${relPath} ${key.reaches}, but its \`${event}\` \`paths:\` filter does not list them, so a PR touching only one of them rebuilds the artifact and starts no suite that exercises it. Nothing goes red; nothing ran.\nAdd each to every copy of the list — GitHub Actions has no YAML anchors, and \`workflow-paths-filter-parity.test.mjs\` fails a one-sided edit.`, ).toEqual([]) }) } @@ -521,7 +539,7 @@ describe('the FFI cache keys and the filters that trigger them agree', () => { KEYS.flatMap((key) => { const inputs = buildInputs(key.step) return key.workflows - .flatMap(filteredEvents) + .flatMap((relPath) => filteredEvents(relPath, key.jobs(relPath))) .flatMap(({ relPath, paths }) => inputs .filter((pattern) => !filterCovers(paths, pattern, entryCovers)) diff --git a/scripts/__tests__/workflow-dispatch-job-conditions.test.mjs b/scripts/__tests__/workflow-dispatch-job-conditions.test.mjs index 3b02b86e77..d7a6ea95e1 100644 --- a/scripts/__tests__/workflow-dispatch-job-conditions.test.mjs +++ b/scripts/__tests__/workflow-dispatch-job-conditions.test.mjs @@ -174,13 +174,17 @@ function extractForkClause(condition) { * held open — otherwise a job that skips for an unrelated reason reads as a * job the fork guard skipped, and the assertion below would be satisfied by * the wrong mechanism. `test-eql.yml`'s two credentialed jobs are `&&`-ed with - * `needs.changes.outputs.relevant == 'true'`, which is that unrelated reason: + * `needs.selector.outputs.eql == 'true'`, which is that unrelated reason: * with `needs` absent the path resolves to null, `null == 'true'` is false, * and both jobs read as skipped on EVERY event including the ones that must * run. */ const PERMISSIVE_NEEDS = { - changes: { outputs: { relevant: 'true' } }, + // Every selector output, whatever its name: each workflow's selector names + // its outputs after its own filters. + selector: { + outputs: new Proxy({}, { has: () => true, get: () => 'true' }), + }, // The release workflows' gates, none of which is about how the run was // triggered. Held open, or "skips because there is nothing to publish" would // be indistinguishable from "skips on a dispatch". @@ -447,7 +451,7 @@ describe('the fork-PR guard keeps its meaning', () => { expect( verdicts, - ` if: ${condition}\nEvaluated with every OTHER gate held open (needs.changes.outputs.relevant = 'true'), so a false here is the fork guard's doing and nothing else.`, + ` if: ${condition}\nEvaluated with every OTHER gate held open (needs.selector.outputs.* = 'true'), so a false here is the fork guard's doing and nothing else.`, ).toEqual(expected) }) } diff --git a/scripts/__tests__/workflow-paths-filter-parity.test.mjs b/scripts/__tests__/workflow-paths-filter-parity.test.mjs index 5e719237c9..73b76ae25b 100644 --- a/scripts/__tests__/workflow-paths-filter-parity.test.mjs +++ b/scripts/__tests__/workflow-paths-filter-parity.test.mjs @@ -1,4 +1,11 @@ +import { execFileSync } from 'node:child_process' import { describe, expect, it } from 'vitest' +import { REPO_ROOT } from './lib/repo-root.mjs' +import { + dornyFilterMatches, + pathsListSelects, + readSelectorJob, +} from './lib/selector.mjs' import { readWorkflow, workflowFiles } from './lib/workflows.mjs' /** @@ -57,6 +64,14 @@ function pathsFilters(relPath) { ) } +/** Every tracked file: what a `paths:` list or a filter can select. */ +const TRACKED = execFileSync('git', ['ls-files', '-z'], { + cwd: REPO_ROOT, + encoding: 'utf8', +}) + .split('\0') + .filter(Boolean) + const CLASSIFIED = workflowFiles().map((relPath) => { const filters = pathsFilters(relPath) const declared = FILTERED_EVENTS.filter((event) => filters[event] !== null) @@ -66,8 +81,23 @@ const CLASSIFIED = workflowFiles().map((relPath) => { /** Workflows writing the list twice — the ones the parity check applies to. */ const PARITY = CLASSIFIED.filter((entry) => entry.declared.length === 2) +/** + * Workflows whose selector job decides what a pull request runs, and whose + * `push` keeps a `paths:` list. Their `pull_request` has no filter, by design + * (ADR 0003), so the second copy of the list is the selector's filters. The + * check below compares the two by what they select. + */ +const SELECTOR_PUSH = CLASSIFIED.filter( + (entry) => + entry.filters.push !== null && + entry.filters.pull_request === null && + readSelectorJob(entry.relPath) !== null, +) + /** Workflows filtering one event and not the other. */ -const ASYMMETRIC = CLASSIFIED.filter((entry) => entry.declared.length === 1) +const ASYMMETRIC = CLASSIFIED.filter( + (entry) => entry.declared.length === 1 && !SELECTOR_PUSH.includes(entry), +) /** * The guard on the scan, in the shape the rest of this directory uses. Every @@ -113,15 +143,6 @@ const EXPECTED_ASYMMETRIES = new Map([ // own `packages/stack-*/Cargo.toml`, which the filter names. 'push is the only trigger; a pull_request copy would make a crates.io publisher reachable from a fork', ], - [ - '.github/workflows/tests-rust.yml', - // `push:` is `branches: [main]` with no `paths:`, so every push to main runs - // the Rust checks and only pull requests are filtered. There is no second - // list to drift from, and the direction that would hurt — a `push` filter - // narrower than the `pull_request` one, letting a change land on main - // unchecked — is unreachable when `push` is unfiltered. - 'push is deliberately unfiltered (branches: [main]), so main runs a superset of what PRs run', - ], [ '.github/workflows/tests.yml', // `push: branches: [main]` has no `paths:`, so main runs everything and only @@ -145,24 +166,6 @@ const EXPECTED_ASYMMETRIES = new Map([ // check-auth-npm-changeset.test.mjs, which is the second copy here. 'pull_request is the only trigger; the check diffs a pull request against its base, which a push does not have', ], - [ - '.github/workflows/test-eql.yml', - // `pull_request` is deliberately UNFILTERED and `push: branches: [main]` - // carries the list, which is the same shape as tests-rust.yml with the two - // events swapped. Unfiltered on the PR side because this workflow's - // `ci-required` is the check branch protection is meant to reference, and a - // workflow skipped by a path filter leaves a required check stuck Pending; - // relevance is applied per-job via `if:` instead. Filtered on the push side - // because nothing waits on a push run, so skipping one costs nothing. - // - // The direction that would hurt — a `pull_request` filter narrower than the - // `push` one, so the PR introducing a change never runs the suite — is - // unreachable when `pull_request` has no filter at all. The push list is - // not unchecked: `eql-workflow-filters.test.mjs` compares it against the - // `relevant:` filter in the `changes` job, which is the second copy of the - // same list and the one this file cannot see. - 'pull_request is deliberately unfiltered (per-job relevance instead), so PRs run a superset of what push runs; the push list is compared against the `changes` job filter by eql-workflow-filters.test.mjs', - ], [ '.github/workflows/bench-eql.yml', // The mirror image of lint-release.yml: `push: branches: [main]` with a @@ -174,26 +177,14 @@ const EXPECTED_ASYMMETRIES = new Map([ // PR filter) does not exist because PRs never run it. 'push to main and a nightly schedule are the only triggers; a 60-minute bench deliberately stays off the PR path', ], - // The four PR gates ported with the stack-* crates. Each has no `push:` + // The PR gates ported with the stack-* crates. Each has no `push:` // trigger, as in cipherstash-suite: they gate a change before it merges, // and the post-merge coverage of the same crates is tests-crates.yml and // tests-golang.yml, which filter both events and are parity-checked. - [ - '.github/workflows/crap-crates.yml', - 'pull_request is the only filtered trigger; a CRAP gate on a merged change reports too late to block it', - ], [ '.github/workflows/mutants.yml', 'pull_request is the only filtered trigger; `--in-diff` needs a PR base to diff against, and a dispatch sweeps unfiltered', ], - [ - '.github/workflows/fuzz.yml', - 'pull_request (corpus regression) is the only filtered trigger; the campaign runs on a nightly schedule, which takes no paths', - ], - [ - '.github/workflows/miri.yml', - 'pull_request is the only filtered trigger; Miri is deterministic, so a post-merge run would repeat the PR result', - ], ]) describe('paths filters are written twice, identically', () => { @@ -235,6 +226,27 @@ describe('paths filters are written twice, identically', () => { ).toEqual([]) }) + for (const { relPath, filters } of SELECTOR_PUSH) { + it(`${relPath} selects on push what its selector selects on a pull request`, () => { + // A selector workflow writes its list twice as well: as the `push` + // `paths:` list, and split across the selector job's filters. The two + // are written in different syntaxes, so they are compared by what they + // select from the tracked files, not by their text. + const selector = readSelectorJob(relPath) + const differ = TRACKED.filter( + (file) => + pathsListSelects(filters.push, file) !== + Object.values(selector.filters).some((rules) => + dornyFilterMatches(rules, selector.quantifier, file), + ), + ) + expect( + differ, + `In ${relPath}, the \`push\` \`paths:\` list and the union of the selector job's filters disagree about these files. Change both in the same edit.`, + ).toEqual([]) + }) + } + for (const { relPath } of PARITY) { it(`${relPath} filters push and pull_request identically`, () => { const filters = pathsFilters(relPath) diff --git a/scripts/__tests__/workflow-selectors.test.mjs b/scripts/__tests__/workflow-selectors.test.mjs new file mode 100644 index 0000000000..341b95f8d5 --- /dev/null +++ b/scripts/__tests__/workflow-selectors.test.mjs @@ -0,0 +1,220 @@ +import { describe, expect, it } from 'vitest' +import { runsWhen } from './lib/expressions.mjs' +import { + NON_PULL_REQUEST_EVENTS, + readSelectorJob, + runSelectorDecision, + runSummaryCheck, + SELECTOR_JOB, + selectorOutputsRead, +} from './lib/selector.mjs' +import { workflowFiles } from './lib/workflows.mjs' + +/** + * Every workflow with a selector job follows the same design (ADR 0003): the + * selector job decides which checks a pull request runs, each gated job reads + * the selector's answer in its `if:`, and a summary job ends the workflow. + * `.github/CONTEXT.md` defines the terms. + * + * The workflows are discovered, not listed, so a workflow that gains a + * selector job is held to this the day it lands. What each selector decides + * for particular changes is pinned per workflow, in the `*-selector*.test.mjs` + * files. + * + * Whether every job is decided by the selector, or has a written reason to run + * on every change, is the failsafe's question, not this file's. + */ + +const SELECTOR_WORKFLOWS = workflowFiles().filter( + (relPath) => readSelectorJob(relPath) !== null, +) + +/** + * The guard on the scan. A minimum: a workflow that gains a selector job must + * not fail this. Each workflow's own `*-selector*.test.mjs` file checks that + * its selector job exists. + */ +const EXPECTED_SELECTOR_WORKFLOWS = ['.github/workflows/test-eql.yml'] + +const triggersOf = (workflow) => workflow?.on ?? workflow?.[true] + +const needsOf = (job) => [job?.needs ?? []].flat() + +/** The jobs whose `if:` reads a selector output. */ +function gatedJobs(workflow) { + return Object.entries(workflow?.jobs ?? {}) + .map(([id, job]) => ({ id, job, reads: selectorOutputsRead(job) })) + .filter((entry) => entry.reads.length > 0) +} + +describe('the workflows with a selector job', () => { + it('include the converted workflows', () => { + expect(SELECTOR_WORKFLOWS).toEqual( + expect.arrayContaining(EXPECTED_SELECTOR_WORKFLOWS), + ) + }) +}) + +for (const relPath of SELECTOR_WORKFLOWS) { + const selector = readSelectorJob(relPath) + const { workflow } = selector + const jobIds = Object.keys(workflow.jobs) + const summaryId = jobIds.at(-1) + const filterNames = Object.keys(selector.filters) + const outputNames = Object.keys(selector.outputs) + + describe(relPath, () => { + it('has no path filter on pull_request', () => { + // A workflow that a path filter skips leaves its required checks + // pending, and GitHub's documentation on required status checks says + // that blocks a merge. The selector job decides instead. + const pullRequest = triggersOf(workflow)?.pull_request ?? {} + expect(Object.keys(pullRequest)).not.toContain('paths') + expect(Object.keys(pullRequest)).not.toContain('paths-ignore') + }) + + it('starts with the selector job, on a GitHub-hosted runner', () => { + // ADR 0003: every gated job waits for it, and a self-provisioned + // machine takes longer to start. + expect(jobIds[0]).toBe(SELECTOR_JOB) + expect(String(selector.job['runs-on'])).toMatch(/^ubuntu-/) + expect(selector.job.needs).toBeUndefined() + expect(selector.job.if).toBeUndefined() + }) + + it('has one path filter step, run only on a pull request', () => { + expect(selector.filterSteps).toHaveLength(1) + expect(String(selector.filterStep?.if)).toBe( + "github.event_name == 'pull_request'", + ) + expect(filterNames).not.toEqual([]) + }) + + it('writes no `!` rule that dorny would read as matching everything', () => { + // Under dorny's default quantifier, `some`, a file counts when ANY rule + // matches, so `!**/*.md` matches every file that is not Markdown. A `!` + // rule only excludes under `every`, where a file must match every rule, + // and there a second positive rule would select nothing at all. + expect(['some', 'every']).toContain(selector.quantifier) + for (const [name, rules] of Object.entries(selector.filters)) { + const positive = rules.filter((rule) => !rule.startsWith('!')) + const negative = rules.filter((rule) => rule.startsWith('!')) + if (selector.quantifier === 'every') { + expect(positive, `filter "${name}"`).toHaveLength(1) + } else { + expect(negative, `filter "${name}"`).toEqual([]) + } + } + }) + + it('names each output after the filter that decides it', () => { + // The guards follow a job's `if:` to its filter by this name. + expect(outputNames.sort()).toEqual([...filterNames].sort()) + for (const [name, stepOutput] of Object.entries(selector.outputs)) { + expect(stepOutput, `output "${name}"`).toBe(name) + } + expect( + selector.decideStep, + 'No single step writes the selector outputs.', + ).toBeTruthy() + }) + + it('selects every job on events other than pull_request', () => { + for (const event of NON_PULL_REQUEST_EVENTS) { + const { status, log, outputs } = runSelectorDecision(relPath, event, {}) + expect(status, log).toBe(0) + for (const name of outputNames) { + expect(outputs[name], `${name} on ${event}`).toBe('true') + } + } + }) + + it('passes the filter answers through on a pull request', () => { + for (const answer of ['true', 'false']) { + const answers = Object.fromEntries( + filterNames.map((name) => [name, answer]), + ) + const { status, log, outputs } = runSelectorDecision( + relPath, + 'pull_request', + answers, + ) + expect(status, log).toBe(0) + expect(outputs).toEqual(answers) + } + }) + + it('fails when a pull request filter gives no answer', () => { + // A missing answer would skip every gated job, and the summary job + // counts skipped as passed: a green run that ran nothing. + for (const name of filterNames) { + for (const bad of ['', 'maybe']) { + const answers = Object.fromEntries( + filterNames.map((other) => [other, other === name ? bad : 'true']), + ) + const { status } = runSelectorDecision( + relPath, + 'pull_request', + answers, + ) + expect(status, `filter "${name}" answering '${bad}'`).not.toBe(0) + } + } + }) + + it('gates jobs only on outputs the selector writes, and needs it', () => { + const gated = gatedJobs(workflow) + expect(gated).not.toEqual([]) + for (const { id, job, reads } of gated) { + expect(needsOf(job), `job "${id}"`).toContain(SELECTOR_JOB) + for (const name of reads) { + expect(outputNames, `job "${id}" reads "${name}"`).toContain(name) + } + } + }) + + it("writes each gated job's `if:` as an exclusion of pull_request", () => { + // Outside a pull request the selector's answers must not matter: a job + // written as a list of allowed events skips on the event nobody listed. + for (const { id, job } of gatedJobs(workflow)) { + for (const event of NON_PULL_REQUEST_EVENTS) { + const verdict = (answer) => + runsWhen(job.if, { + github: { + event_name: event, + repository: 'cipherstash/stack', + event: {}, + }, + needs: { + [SELECTOR_JOB]: { + result: 'success', + outputs: Object.fromEntries( + outputNames.map((name) => [name, answer]), + ), + }, + }, + vars: {}, + }) + expect(verdict('false'), `job "${id}" on ${event}`).toBe( + verdict('true'), + ) + } + } + }) + + it('ends with a summary job that needs every other job', () => { + const summary = workflow.jobs[summaryId] + expect(String(summary.if)).toBe('always()') + expect([...needsOf(summary)].sort()).toEqual( + jobIds.filter((id) => id !== summaryId).sort(), + ) + }) + + it('passes the summary only when every needed job passed or skipped', () => { + const status = (results) => runSummaryCheck(relPath, summaryId, results) + expect(status(['success', 'skipped', 'success'])).toBe(0) + expect(status(['success', 'failure'])).not.toBe(0) + expect(status(['skipped', 'cancelled'])).not.toBe(0) + }) + }) +}