# yad-managed: yad-checks
# SDLC check gates (Phase 3 build plan §C). The gates below run on every PR and must pass before
# merge. They are CI-agnostic bash in checks/ — this workflow just invokes them with the PR base.
# This workflow file is OWNED by yad-checks (the marker on line 1 identifies it). It runs
# independently of any other workflow in .github/workflows/, so wiring never edits a foreign workflow.
#
# Gate scripts (#426): every job runs the BASE branch's copy of checks/, never the PR's own, so a PR that
# changes a gate script is still judged by the script already merged. A script the base does not have yet
# (the PR that first installs the gates, or a `yad update` that adds one) runs from the PR, with a note.
# The base's script runs with THIS file's call: keep every call one the previous release's script accepts.
# This file itself still comes from the PR: protect checks/ and the CI files with a CODEOWNERS rule and
# a required Code Owner review (docs/branch-protection.md, "Who may change the gates").
name: yad-checks
on:
  pull_request:
    # Never `edited` (#425). A job skipped by `if:` counts as passed, so a run started by an edit of the PR
    # text would show every gate here as passed on the same commit — a failed gate could turn green. The two
    # checks that read the PR text, pr-title and pr-template, run from yad-pr-text.yml, which does start on
    # an edit.
    types: [opened, synchronize, reopened]
    branches: ["**"]

# A newer push to the same pull request cancels this workflow's older run (#428): only the newest commit
# can be merged, and every gate here re-reads the whole PR range, so the older run checks nothing it misses.
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true

# Read-only by default: no gate here writes to the repo. A job that needs more asks for it itself.
permissions:
  contents: read

jobs:
  spec-link:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: bash "$YAD_CHECKS/spec-link.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_SOURCE_BRANCH: "${{ github.head_ref }}", YAD_PROMOTE_FROM: "${{ vars.YAD_PROMOTE_FROM }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }

  contract-check:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      # The PR's own head commit, not refs/pull/<n>/merge (#708). The merge ref is a merge commit GitHub
      # makes, and the quote rule refuses any merge commit in the range, so a rebase could never pass it.
      # The gate builds the merge into the base itself (git merge-tree). Only this job: the others ignore or
      # tolerate merge commits, and build-test-lint tests the merged tree.
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false, ref: "${{ github.event.pull_request.head.sha }}" }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      # E120: check the Product out where .sdlc/product-link.json says, when the YAD_PRODUCT_TOKEN
      # secret is set; without it the gate defers, with a WARNING unless the PR comes from a fork (#345).
      - run: bash "$YAD_CHECKS/product-checkout.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_PRODUCT_TOKEN: "${{ secrets.YAD_PRODUCT_TOKEN }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }
      - run: bash "$YAD_CHECKS/contract-check.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }

  # E65/E66 — the risk map (.sdlc/risk-map: a risk level per directory, no names). Warnings, and the
  # approver count read from the base branch's map. The script always exits 0: the count is reported,
  # not enforced, and a stale map never blocks a merge.
  risk-map:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: bash "$YAD_CHECKS/risk-map-check.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }

  build-test-lint:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    env:
      YAD_TEST_MAX_WORKERS: "2" # cap jest/vitest test workers in CI; ignored by other runners
      YAD_NODE_VERSION: ${{ vars.YAD_NODE_VERSION || '22' }} # repo variable; a current 22 or 24 line (Node 25+ has no Corepack; 18 and 20 are end of life)
      NX_BASE: ${{ github.event.pull_request.base.sha }}
      NX_HEAD: ${{ github.event.pull_request.head.sha }}
      COREPACK_HOME: ${{ github.workspace }}/.corepack-cache # pinned-manager downloads, cached below
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 0
          # No `filter: blob:none` (#426): the next step reads the base's checks/, which a
          # partial clone leaves out when the PR changed them, and with no credentials kept it
          # could not fetch them.
          persist-credentials: false
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
        with:
          node-version: ${{ env.YAD_NODE_VERSION }}
          # Off: setup-node v5+ caches npm by itself when package.json names a packageManager; the next step
          # already caches npm, pnpm and Corepack, so a second cache would only cost time (#429).
          package-manager-cache: false
      # Dependency cache for either manager: npm's ~/.npm, pnpm's content-addressable store, and the
      # Corepack home where `corepack prepare` keeps the pinned manager (otherwise re-downloaded from
      # the registry on every job, even when the dependency cache hits). Keyed on the lockfiles AND
      # package.json, since a packageManager bump changes no lockfile. setup-node's own `cache:` must
      # name the manager up front (and needs pnpm on PATH before it runs), which package.json only
      # decides later — so cache the directories directly.
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.npm
            ~/.local/share/pnpm/store
            ${{ env.COREPACK_HOME }}
          key: yad-deps-${{ runner.os }}-node${{ env.YAD_NODE_VERSION }}-${{ hashFiles('package-lock.json', 'npm-shrinkwrap.json', 'pnpm-lock.yaml', 'package.json') }}
          restore-keys: yad-deps-${{ runner.os }}-node${{ env.YAD_NODE_VERSION }}-
      # The base branch lets a Unity project skip these two, and stops a change from skipping them by deleting package.json (#496).
      - run: bash "$YAD_CHECKS/install-deps.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }
      - run: bash "$YAD_CHECKS/build-test-lint.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }

  # Phase 6 — feature-thread gates. lineage-check: the change links a real threaded epic. epic-open:
  # a sealed epic (all stories shipped) refuses new behaviour, forcing a change-epic. reconcile-debt:
  # a thread with open hotfix debt is frozen for new changes until paid. All build on spec-link's
  # story->epic resolution and degrade to a note when the product repo is not reachable from CI.
  lineage-check:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      # E120: check the Product out where .sdlc/product-link.json says, when the YAD_PRODUCT_TOKEN
      # secret is set; without it the gate defers, with a WARNING unless the PR comes from a fork (#345).
      - run: bash "$YAD_CHECKS/product-checkout.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_PRODUCT_TOKEN: "${{ secrets.YAD_PRODUCT_TOKEN }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }
      - run: bash "$YAD_CHECKS/lineage-check.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_SOURCE_BRANCH: "${{ github.head_ref }}", YAD_PROMOTE_FROM: "${{ vars.YAD_PROMOTE_FROM }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }

  epic-open:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      # E120: check the Product out where .sdlc/product-link.json says, when the YAD_PRODUCT_TOKEN
      # secret is set; without it the gate defers, with a WARNING unless the PR comes from a fork (#345).
      - run: bash "$YAD_CHECKS/product-checkout.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_PRODUCT_TOKEN: "${{ secrets.YAD_PRODUCT_TOKEN }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }
      - run: bash "$YAD_CHECKS/epic-open.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_SOURCE_BRANCH: "${{ github.head_ref }}", YAD_PROMOTE_FROM: "${{ vars.YAD_PROMOTE_FROM }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }

  reconcile-debt:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      # E120: check the Product out where .sdlc/product-link.json says, when the YAD_PRODUCT_TOKEN
      # secret is set; without it the gate defers, with a WARNING unless the PR comes from a fork (#345).
      - run: bash "$YAD_CHECKS/product-checkout.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_PRODUCT_TOKEN: "${{ secrets.YAD_PRODUCT_TOKEN }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }
      - run: bash "$YAD_CHECKS/reconcile-debt-check.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_SOURCE_BRANCH: "${{ github.head_ref }}", YAD_PROMOTE_FROM: "${{ vars.YAD_PROMOTE_FROM }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }

  # Pattern gate: every commit subject follows the convention (profile: code). The PR title and body are
  # checked by yad-pr-text.yml.
  commit-message:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: bash "$YAD_CHECKS/commit-message.sh" --profile code "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}", YAD_SOURCE_BRANCH: "${{ github.head_ref }}", YAD_PROMOTE_FROM: "${{ vars.YAD_PROMOTE_FROM }}", YAD_PR_FORK: "${{ github.event.pull_request.head.repo.full_name != github.repository }}" }

  # No unverified commits: a platform-Verified signature on every commit (write access decides who authors).
  verified-commits:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: read
    env:
      GH_TOKEN: ${{ github.token }} # read-only: gh api commits/<sha> for the Verified badge
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with: { fetch-depth: 0, persist-credentials: false }
      - name: Gate scripts from the base branch (#426)
        env: { YAD_BASE: "${{ github.base_ref }}" }
        run: |
          YAD_CHECKS="$RUNNER_TEMP/yad-base-checks/checks"
          (
            set -euo pipefail
            b="origin/${YAD_BASE}"; d="${YAD_CHECKS%/checks}"
            rm -rf "$d"; mkdir -p "$YAD_CHECKS"
            git rev-parse --verify --quiet "${b}^{commit}" > /dev/null || { echo "FAIL [base-checks]: ${b} is not in this checkout, so the gate scripts cannot be read from the base branch. Check the code out with its full history." >&2; exit 1; }
            t="$(git ls-tree "$b" checks)"
            case "$t" in
              "") on_base=0 ;;
              "040000 tree "*) git archive "$b" checks | tar -x -C "$d"; on_base=1 ;;
              *) echo "FAIL [base-checks]: checks on ${b} is not a folder, so the gate scripts cannot be read from it. Make checks/ a plain folder on the base branch." >&2; exit 1 ;;
            esac
            git ls-tree -z HEAD checks/ > "$d/.pr"
            n=0
            while IFS= read -r -d '' e; do
              m="${e%% *}"; p="${e#*$'\t'}"; o="${e#* * }"; o="${o%%$'\t'*}"; s="${p//[[:cntrl:]]/?}"
              if [ -e "$d/$p" ] || [ -L "$d/$p" ]; then continue; fi
              case "$m" in
                100644 | 100755) git cat-file blob "$o" > "$d/$p"; if [ "$m" = 100755 ]; then chmod +x "$d/$p"; fi ;;
                *) printf 'note: %s in this PR is not a plain file, so it is not run (#426)\n' "$s"; continue ;;
              esac
              n=$((n + 1))
              if [ "$on_base" = 1 ]; then printf 'note: %s is not on %s yet, so the copy from this PR runs (#426)\n' "$s" "$YAD_BASE"; fi
            done < "$d/.pr"
            rm -f "$d/.pr"
            if [ "$n" = 1 ]; then w="1 file"; else w="$n files"; fi
            if [ "$on_base" = 0 ]; then printf 'note: %s has no checks/ folder yet, so the gate scripts from this PR run: %s (#426)\n' "$YAD_BASE" "$w"; fi
          )
          echo "YAD_CHECKS=$YAD_CHECKS" >> "$GITHUB_ENV"
      - run: bash "$YAD_CHECKS/verified-commits.sh" "origin/$YAD_BASE"
        env: { YAD_BASE: "${{ github.base_ref }}" }
