diff --git a/.github/workflows/dev-lead-reusable.yml b/.github/workflows/dev-lead-reusable.yml index b5d660d97..023ceddc6 100644 --- a/.github/workflows/dev-lead-reusable.yml +++ b/.github/workflows/dev-lead-reusable.yml @@ -644,13 +644,18 @@ jobs: # ── ci-relay ────────────────────────────────────────────────────────────────── # Handles check_run completed failure events. No LLM — resolves PR number and # fires repository_dispatch dev-lead-ci-failure back to the calling repo. + # `template-drift` is never relayed (#2220): it checks petry-projects/repo-template, + # is not required, and nothing in a PR can clear it — "fixing" it led passes to + # repin their own dev-lead.yml. Keep in sync with FIX_CI_IGNORED_CHECKS in + # scripts/dev-lead-fix-ci.sh. ci-relay: if: >- vars.AGENTS_PAUSED != 'true' && github.event_name == 'check_run' && github.event.action == 'completed' && github.event.check_run.conclusion == 'failure' && - !startsWith(github.event.check_run.name, 'dev-lead / ') + !startsWith(github.event.check_run.name, 'dev-lead / ') && + github.event.check_run.name != 'template-drift' runs-on: ubuntu-latest timeout-minutes: 5 permissions: diff --git a/AGENTS.md b/AGENTS.md index 62524ff8f..5ab89356f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -521,6 +521,16 @@ PR-triggered check on the existing Lint workflow — **no new cron/scheduled wor commits-list call per DRIFTED file and one emission per commit in the range. That adds network cost to every PR's lint run. The message removes the wrong advice without that cost. +- **A red `template-drift` is never fixed from a PR here (#2220).** The check reads `repo-template`, never + this repo's own stubs, so nothing in a PR here can clear it. It is not a fix-ci trigger: the reusable's + `ci-relay` job does not relay it, and `dev-lead-fix-ci.sh` drops it (`FIX_CI_IGNORED_CHECKS`). Its + annotations still carry a `file=` anchor, so GitHub pins them to the PR's own same-named file (dropping + the anchor is tracked in #2215). That is how two passes (#2157, #1796) "fixed" the drift by repinning + this repo's `dev-lead.yml` from `dev-lead/v139-ring0` to the stale `dev-lead/v1-stable`. As a backstop, `scripts/lib/channel-pin-guard.sh` + refuses any dev-lead pass (fix-ci and every fix-reviews intent) that changes a first-party channel pin + (`uses: petry-projects/…@` or `agent_ref`) on a workflow file, and escalates the PR. Ending on the + base branch's pin (a rebase, or restoring `main`'s pin) is allowed. + - **`seed-repo-template.sh` has one home: this repo (#1927).** `petry-projects/.github` once carried a different, smaller script under the same name, and the two were mistaken for diverged copies. That script only prints one standard file from a local checkout and is now `scripts/emit-standard-file.sh` diff --git a/prompts/dev-lead/fix-ci.md b/prompts/dev-lead/fix-ci.md index 7372ca82d..64ac69163 100644 --- a/prompts/dev-lead/fix-ci.md +++ b/prompts/dev-lead/fix-ci.md @@ -31,6 +31,8 @@ Analyze the CI failure logs and annotations above, then fix the root cause(s). W > **Guardrail — never forward an undeclared input across a channel pin.** A thin caller stub pins a first-party reusable at a **moving channel tag** (e.g. `…@dev-lead/v1-stable`). **Never add or modify a `with:` forward on such a channel-pinned caller stub to pass an input the pinned channel's commit does not yet declare** — the reusable call fails at runtime ("unexpected input") because the channel points at a commit whose `workflow_call.inputs` lacks it (the channel-skew defect, #1052). This can itself surface as the CI failure you are fixing: the fix is **not** to keep the forward, but to remove it or complete the sequencing. Adding a new `workflow_call` input is a **three-step sequence, in order**: (1) land the input in the reusable's `workflow_call.inputs`; (2) promote the pinned channel to a commit that declares it via `cut-release.sh --channel `; (3) **only then** teach the stub to forward it with `with:`. See AGENTS.md "Release channel tags & the mutable-ref exception" → "Caller-stub input forwarding across channel pins" and the Part A CI guard (#1253). +> **Guardrail — never repin a caller stub.** Never change the `uses:` ref or `agent_ref` of a first-party channel-pinned caller stub (e.g. `.github/workflows/dev-lead.yml` → `…@dev-lead/v139-ring0`). Do not move it to another channel, SHA-pin it, re-seed it from a template, or delete the comment explaining its pin. Channels move only by moving the tag centrally. The harness refuses to push a pass that changes such a pin, and escalates the PR (#2220). A red `template-drift` concerns `petry-projects/repo-template`, not this repository's stubs, so re-seeding a stub here cannot clear it. + ### Phase 1 — Diagnose 1. Identify the specific errors or test failures from the logs and annotations diff --git a/scripts/dev-lead-fix-ci.sh b/scripts/dev-lead-fix-ci.sh index 39925f051..16de53426 100755 --- a/scripts/dev-lead-fix-ci.sh +++ b/scripts/dev-lead-fix-ci.sh @@ -7,17 +7,19 @@ set -euo pipefail # Optional: PROMPTS_DIR (defaults to prompts/dev-lead relative to CWD) # Optional: MAX_FAIL_ATTEMPTS — consecutive engine failures before exhaustion (default: 2) -source "$(dirname "$0")/engine.sh" -source "$(dirname "$0")/lib/git-identity.sh" -source "$(dirname "$0")/lib/pr-worktree.sh" -source "$(dirname "$0")/lib/auto-merge.sh" -source "$(dirname "$0")/lib/pr-automation-budget.sh" +source "$(dirname "${BASH_SOURCE[0]}")/engine.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/git-identity.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/pr-worktree.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/auto-merge.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/pr-automation-budget.sh" # Structured PR-body backfill (#1805): heal an existing PR whose body is still # missing 3+ required description sections, once, marker-keyed. -source "$(dirname "$0")/lib/dev-lead-pr-body.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/dev-lead-pr-body.sh" # Sync-stub guard (#2185/#2212): a fix on a standards-sync PR must not change the # org-standard stub the PR exists to sync. -source "$(dirname "$0")/lib/sync-stub-guard.sh" +source "$(dirname "${BASH_SOURCE[0]}")/lib/sync-stub-guard.sh" +# Channel-pin guard (#2220): a pass may never change a caller stub's channel pin. +source "$(dirname "${BASH_SOURCE[0]}")/lib/channel-pin-guard.sh" PR_NUMBER="${PR_NUMBER:-}" HEAD_SHA="${HEAD_SHA:-}" @@ -35,6 +37,22 @@ export PROMPTS_DIR="${PROMPTS_DIR:-prompts/dev-lead}" PROMPTS_DIR="$(resolve_abs "$PROMPTS_DIR")" export PROMPTS_DIR +# Checks that never start a fix pass (#2220). `template-drift` compares +# petry-projects/repo-template with the standards baseline: nothing in a PR here +# can clear it, it is not required, and "fixing" it led a pass to repin this +# repo's own dev-lead.yml to a stale channel. Keep in sync with the ci-relay +# job's `if:` in .github/workflows/dev-lead-reusable.yml (asserted by +# tests/dev-lead/unit/test_channel_pin_guard.bats). +FIX_CI_IGNORED_CHECKS=("template-drift") + +# fix_ci_trigger_checks: CHECKS_JSON minus the ignored checks, as a JSON array. +fix_ci_trigger_checks() { + local ignored + ignored=$(printf '%s\n' "${FIX_CI_IGNORED_CHECKS[@]}" | jq -R . | jq -sc .) + printf '%s' "${CHECKS_JSON:-[]}" | jq -c --argjson ignored "$ignored" \ + '(if type == "array" then . else [] end) | map(select((.name // "") as $n | ($ignored | index($n)) | not))' +} + # check_idempotency: returns 0 (skip) if this exact SHA was already TERMINALLY # handled, OR if a PR-level exhaustion marker exists (blocks all future SHAs). # status=rate-limited is NOT terminal — those runs must be retriable. @@ -52,9 +70,9 @@ check_idempotency() { fi # SHA-level idempotency — skip only for terminal statuses (applied, failed, - # no-changes, declined). status=rate-limited is retriable: do not skip on it so - # the retry cron can re-run. - if echo "$comments" | grep -qE "${MARKER_PREFIX}${HEAD_SHA} status=(applied|failed|no-changes|declined)"; then + # no-changes, declined, refused by the channel-pin guard). status=rate-limited is + # retriable: do not skip on it so the retry cron can re-run. + if echo "$comments" | grep -qE "${MARKER_PREFIX}${HEAD_SHA} status=(applied|failed|no-changes|declined|refused)"; then echo "::notice::Already handled CI failure at SHA ${HEAD_SHA} with terminal status — skipping" return 0 fi @@ -219,12 +237,38 @@ $(ssg_declined_body "fix-ci" "$rows")${HOLD_LABEL_NOTE}" \ return 0 } +# fix_ci_channel_pin_guard [base_rev] — refuse the push when the +# pass changed a caller stub's first-party channel pin (#2220): escalate, record a +# terminal `refused` marker for this SHA, and return 1. Returns 0 when clean. +fix_ci_channel_pin_guard() { + local pre="$1" base="${2:-}" out rc=0 + out=$(cpg_scan_pass "$pre" HEAD "$base") || rc=$? + [ "$rc" -eq 0 ] && return 0 + echo "::error::Channel-pin guard: the fix-ci pass changed a caller stub's channel pin ($(printf '%s\n' "$out" | head -1)) — refusing to push (#2220)" + cpg_flag_refused "fix-ci" "$(printf '%s\n' "$out" | sed '1d')" + post_summary "refused" "The pass changed a caller stub's channel pin, so nothing was pushed. See the escalation comment (#2220)." + return 1 +} + main() { if [ -z "$PR_NUMBER" ] || [ -z "$HEAD_SHA" ]; then echo "::error::PR_NUMBER and HEAD_SHA are required" exit 1 fi + # Ignored checks (#2220): drop them before anything else; a dispatch whose + # checks are all ignored starts no pass. Covers relayed failures and retry-cron + # re-dispatches alike. An empty or unparseable CHECKS_JSON is left as it was. + local trigger_checks + if trigger_checks=$(fix_ci_trigger_checks 2>/dev/null) \ + && [ "$(jq 'length' <<<"$CHECKS_JSON" 2>/dev/null || echo 0)" -gt 0 ]; then + if [ "$(jq 'length' <<<"$trigger_checks")" -eq 0 ]; then + echo "::notice::fix-ci: $(jq -r '[.[]?.name // empty] | join(", ")' <<<"$CHECKS_JSON" 2>/dev/null) is not a fix-ci trigger — skipping (#2220)" + exit 0 + fi + CHECKS_JSON="$trigger_checks" + fi + # A budget escalation that cannot apply its hold label fails the run (#2142); # the auto-merge trap below keeps the guard as its last handler. trap hold_label_exit_guard EXIT @@ -266,9 +310,14 @@ main() { # Checkout the PR branch for modification, in an isolated worktree so the # branch switch never overwrites the agent's own prompts/scripts (issue #448). checkout_pr_in_worktree "$PR_NUMBER" "$REPO" - # The head this pass starts from: the sync-stub guard compares against it. - local pre_pass_sha + # The head this pass starts from: the sync-stub and channel-pin guards compare against it. + # base_rev is the PR's base, for the channel-pin guard (#2220). + local pre_pass_sha base_ref base_rev="" pre_pass_sha=$(git rev-parse HEAD 2>/dev/null || true) + base_ref=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq '.base.ref // empty' 2>/dev/null || true) + if [ -n "$base_ref" ] && git fetch -q origin "$base_ref" 2>/dev/null; then + base_rev="origin/${base_ref}" + fi # Backfill the five required description sections into a pre-existing PR whose # body still lacks them (#1805). Idempotent + marker-keyed, so open PRs heal on @@ -357,6 +406,8 @@ main() { post_summary "declined" "The fix changed a synced org-standard stub on this standards-sync PR, so it was not pushed. See the declined comment (#2212)." exit 0 fi + # Channel-pin guard (#2220): never push a pass that repins a caller stub. + fix_ci_channel_pin_guard "$pre_pass_sha" "$base_rev" || exit 1 # push_with_merge_guard exits 0 cleanly if the PR was merged/closed mid-run # (its branch deleted); a genuine push failure still aborts with exit 1. push_with_merge_guard || exit 1 @@ -368,4 +419,7 @@ main() { rm -f "$prompt_file" } -main "$@" +# Source-guard: tests source this to exercise the helpers; CI executes it. +if [ "${BASH_SOURCE[0]:-$0}" = "$0" ]; then + main "$@" +fi diff --git a/scripts/dev-lead-fix-reviews.sh b/scripts/dev-lead-fix-reviews.sh index a6292d6aa..612ac8636 100755 --- a/scripts/dev-lead-fix-reviews.sh +++ b/scripts/dev-lead-fix-reviews.sh @@ -30,6 +30,8 @@ source "$(dirname "$0")/lib/claim-landing.sh" source "$(dirname "$0")/lib/test-tamper-guard.sh" # Sync-stub guard (#2185): a bot pass may not edit a synced org-standard stub. source "$(dirname "$0")/lib/sync-stub-guard.sh" +# Channel-pin guard (#2220): no pass may change a caller stub's channel pin. +source "$(dirname "$0")/lib/channel-pin-guard.sh" # Test-regression guard (#2013): a pass may not push with the suite newly red. source "$(dirname "$0")/lib/test-regression-guard.sh" # Review-thread deferral verifier (#2045): the thread-side sibling of the @@ -3225,6 +3227,24 @@ commit_and_push() { fi ;; esac + # Channel-pin guard (#2220): no pass may change a caller stub's first-party + # channel pin (`uses:` ref or `agent_ref`); the #2157/#1796 passes repinned + # dev-lead.yml to a stale channel to "fix" template-drift. Unlike the sync-stub + # guard this covers every pushing intent — a maintainer who wants a repin makes + # it in a reviewed PR. Ending on the base branch's pin (a rebase, or restoring + # main's pin) is allowed. + case "$intent" in + enable-auto-merge) ;; # never pushes; nothing to guard + *) + local cpg_out cpg_rc=0 + cpg_out=$(cpg_scan_pass "${RESOLUTION_BASE_SHA:-${HEAD_SHA:-}}" HEAD "origin/${BASE_REF:-main}") || cpg_rc=$? + if [ "$cpg_rc" -ne 0 ]; then + echo "::error::Channel-pin guard: the ${intent} pass changed a caller stub's channel pin ($(printf '%s\n' "$cpg_out" | head -1)) — refusing to push (#2220)" + cpg_flag_refused "$intent" "$(printf '%s\n' "$cpg_out" | sed '1d')" + return 4 + fi + ;; + esac # Test-tamper guard (#2013): a bot-driven fix pass must not silently rewrite an # existing test to make its own change pass (the petry-projects/.github#1220 # `13927fc` shape). Refuse the push and escalate, like the no-op guard (rc 3). @@ -3285,7 +3305,7 @@ commit_and_push() { # A rejected push — or one the remote head does not reflect (#2013) — must not # leave this pass's "Fixed" replies standing: retract them before failing. # (commit_and_push returns 3 for the no-op guard and 4 for the sync-stub, - # test-tamper and test-regression guards.) + # channel-pin, test-tamper and test-regression guards.) push_no_clobber || { echo "::error::git push failed — check remote access and branch permissions" >&2 retract_unlanded_claims "$intent" failed || true @@ -3987,7 +4007,7 @@ case "$INTENT_TYPE" in # marker and do not re-enable auto-merge or resolve threads. echo "::warning::fix-reviews produced a net-zero diff — flagged for human, not pushed (#1340)" elif [ "$cp_rc" -eq 4 ]; then - echo "::warning::fix-reviews was refused by a push guard (test tamper/regression or sync-stub) — flagged for human, not pushed (#2013/#2185)" + echo "::warning::fix-reviews was refused by a push guard (test tamper/regression, sync-stub or channel-pin) — flagged for human, not pushed (#2013/#2185/#2220)" else notify_coderabbit_resolve if has_hard_blockers; then @@ -4086,7 +4106,7 @@ case "$INTENT_TYPE" in # re-enable auto-merge or resolve threads. echo "::warning::fix-bot-comment produced a net-zero diff — flagged for human, not pushed (#1340)" elif [ "$cp_rc" -eq 4 ]; then - echo "::warning::fix-bot-comment was refused by a push guard (test tamper/regression or sync-stub) — flagged for human, not pushed (#2013/#2185)" + echo "::warning::fix-bot-comment was refused by a push guard (test tamper/regression, sync-stub or channel-pin) — flagged for human, not pushed (#2013/#2185/#2220)" else notify_coderabbit_resolve if has_hard_blockers; then @@ -4173,6 +4193,12 @@ case "$INTENT_TYPE" in echo "::notice::on-mention: no-op guard aborted the push for PR #${PR_NUMBER} — flagged for human, not merged (#1786)" exit "$rc" fi + if [ "$cp_rc" -eq 4 ]; then + # A push guard refused the pass and flagged the PR; never report it or + # re-enable auto-merge (#2220). + echo "::warning::on-mention was refused by a push guard — flagged for human, not pushed (#2220)" + exit "$rc" + fi if [ "$cp_rc" -eq 0 ]; then post_reviews_terminal "on-mention" "applied" "Changes committed and pushed." else @@ -4225,7 +4251,7 @@ case "$INTENT_TYPE" in # Nothing landed, so a `fixed` disposition would cite a local-only commit; no # thread resolves and auto-merge stays off. if [ "$cp_rc" -eq 4 ]; then - echo "::warning::review-changes was refused by a test guard (tamper/regression) — flagged for human, not pushed (#2013)" + echo "::warning::review-changes was refused by a push guard (test tamper/regression, sync-stub or channel-pin) — flagged for human, not pushed (#2013/#2185/#2220)" resolve_dispositioned_comments "review-changes" failed || _DISPOSITIONS_UNRESOLVED=1 resolve_deferred_bot_threads "review-changes" || rc=1 [ "${_DISPOSITIONS_UNRESOLVED:-0}" -eq 1 ] && rc=1 @@ -4407,6 +4433,10 @@ case "$INTENT_TYPE" in echo "::notice::rebase: no-op guard aborted the push for PR #${PR_NUMBER} — flagged for human, not merged (#1786)" exit "$rc" fi + if [ "$cp_rc" -eq 4 ]; then + echo "::warning::rebase was refused by a push guard — flagged for human, not pushed (#2220)" + exit "$rc" + fi # Post-resolution integrity gate (#1890 AC #7): a corrupted resolution # (#1482/#1485) must never be reported applied and allowed to merge. Escalate # to a human immediately — corruption is not something to retry. diff --git a/scripts/lib/channel-pin-guard.sh b/scripts/lib/channel-pin-guard.sh new file mode 100644 index 000000000..f22d7a748 --- /dev/null +++ b/scripts/lib/channel-pin-guard.sh @@ -0,0 +1,202 @@ +#!/usr/bin/env bash +# channel-pin-guard.sh — a dev-lead pass must never change a caller stub's +# first-party channel pin (#2220). +# +# WHAT IS WRONG WITHOUT THIS +# On #2157 and #1796 a pass "fixed" a red, non-required `template-drift` check +# by re-seeding .github/workflows/dev-lead.yml from the standards template. That +# repinned this repo's caller stub from `dev-lead/v139-ring0` to the legacy +# `dev-lead/v1-stable` channel (a 2026-09-22 release, 70 commits behind) and +# deleted the comment explaining the pin. The tag exists, so nothing failed at +# startup, and `pull_request*` events take the workflow file from the PR, so the +# PR's own dev-lead passes already ran the old release. The prompt guardrail +# ("never SHA-pin a first-party channel ref") does not cover a repin to another +# channel, and nothing in the harness stopped a pass editing its own stub. +# +# THE RULE +# A channel-pinned file is a `.github/workflows/*.yml|*.yaml` file that, at the +# pre-pass head, has a first-party `uses: petry-projects/…@` or an +# `agent_ref: ` whose is a moving channel tag (cpg_is_channel_ref). +# A pass that changes that file's pin list (cpg_pins) is: +# clean the pin list is unchanged (comment/body edits are fine), or equals +# the file's pin list on (a rebase adopting the base +# branch's promotion, or restoring a bad repin to main's pin); +# drift any other change: a repin, an agent_ref change, a SHA pin, or the +# file's deletion. The harness refuses to push and escalates. +# Channel moves are made by moving tags centrally (cut-release.sh), never by a +# pass editing a caller. +# +# PURITY / TESTABILITY (ADR-0004) +# cpg_is_channel_ref, cpg_pins and cpg_refused_body are PURE. cpg_scan_pass is +# the impure gatherer (git only). cpg_flag_refused is the escalation (gh). +# Sourced under `set -euo pipefail`; helpers only `return`, never `exit`. + +set -euo pipefail + +# apply_hold_label / post_hold_failure_note / disable_auto_merge_for_hold (#2142). +if ! declare -F apply_hold_label >/dev/null; then + # shellcheck source=scripts/lib/hold-label.sh + source "$(dirname "${BASH_SOURCE[0]}")/hold-label.sh" +fi + +# Guarded so a script that sources this lib twice (directly and via another +# script) does not trip over the readonly declarations. +if [[ -z "${_CPG_CHANNEL_RE:-}" ]]; then + # /v- (dev-lead/v139-ring0, pr-review/v1-next) and the legacy + # un-majored / (dev-lead/stable). + readonly _CPG_CHANNEL_RE='^[A-Za-z0-9._-]+/(v[0-9]+-)?[A-Za-z0-9]+$' + readonly _CPG_USES_RE='^[[:space:]]*(-[[:space:]]+)?uses:[[:space:]]*["'"'"']?(petry-projects/[^@[:space:]"'"'"']+@[^[:space:]"'"'"'#]+)' + readonly _CPG_AGENT_REF_RE='^[[:space:]]*agent_ref:[[:space:]]*["'"'"']?([^[:space:]"'"'"'#]+)' +fi + +# cpg_is_channel_ref +# 0 when is a moving channel tag. Pure. +cpg_is_channel_ref() { + [[ "${1:-}" =~ $_CPG_CHANNEL_RE ]] +} + +# cpg_pins +# Echoes `uses\t@` for each first-party reusable call and +# `agent_ref\t` for each agent_ref, in file order. Comment lines never +# match. Pure. +cpg_pins() { + local content="${1:-}" line + while IFS= read -r line; do + if [[ "$line" =~ $_CPG_USES_RE ]]; then + printf 'uses\t%s\n' "${BASH_REMATCH[2]}" + elif [[ "$line" =~ $_CPG_AGENT_REF_RE ]]; then + printf 'agent_ref\t%s\n' "${BASH_REMATCH[1]}" + fi + done <<<"$content" +} + +# _cpg_has_channel_pin — 0 when any pin in a cpg_pins list is a channel ref. +_cpg_has_channel_pin() { + local kind val + while IFS=$'\t' read -r kind val; do + [[ -z "${kind:-}" ]] && continue + [[ "$kind" == "uses" ]] && val="${val##*@}" + cpg_is_channel_ref "$val" && return 0 + done <<<"${1:-}" + return 1 +} + +# _cpg_summary — one-line `kind value, kind value` form for messages. +_cpg_summary() { + local pins="${1:-}" + [[ -n "$pins" ]] || { printf ''; return 0; } + printf '%s\n' "$pins" | tr '\t' ' ' | paste -sd ',' - | sed 's/,/, /g' +} + +# _cpg_pins_at — cpg_pins of at ; empty when absent. +_cpg_pins_at() { + local content + content=$(git --literal-pathspecs show "${1}:${2}" 2>/dev/null) || return 0 + cpg_pins "$content" +} + +# cpg_scan_pass [head] [base_rev] +# Echoes the verdict on the first line, then `path\tbefore\tafter` for each +# channel-pinned file whose pins the pass changed (`after` is `` for a +# removed file). Returns 0 for clean, 1 for drift, 2 (echoing `unknown`) when +# does not resolve or the diff cannot be read (fail closed). +# (e.g. origin/main) is optional; when it resolves, ending on the +# base's own pins is clean. Impure. +cpg_scan_pass() { + local pre="${1:-}" head="${2:-HEAD}" base="${3:-}" + if [[ -z "$pre" ]] || ! git cat-file -e "${pre}^{commit}" 2>/dev/null; then + echo "unknown" + return 2 + fi + if [[ -n "$base" ]] && ! git rev-parse -q --verify "${base}^{commit}" >/dev/null 2>&1; then + base="" + fi + local files path before after drift="" + files=$(git -c core.quotePath=false diff --no-renames --name-only "$pre" "$head" -- .github/workflows 2>/dev/null) \ + || { echo "unknown"; return 2; } + while IFS= read -r path; do + case "$path" in + *.yml|*.yaml) ;; + *) continue ;; + esac + before=$(_cpg_pins_at "$pre" "$path") + if git --literal-pathspecs cat-file -e "${head}:${path}" 2>/dev/null; then + after=$(_cpg_pins_at "$head" "$path") + if ! _cpg_has_channel_pin "$before"; then + # A file the base branch added, or newly pinned, arrives with the base's + # own pins when a pass merges or rebases onto it: that is not drift. + if _cpg_has_channel_pin "$after" \ + && ! { [[ -n "$base" ]] && [[ "$after" == "$(_cpg_pins_at "$base" "$path")" ]]; }; then + drift+="${path}"$'\t'"$(_cpg_summary "$before")"$'\t'"$(_cpg_summary "$after")"$'\n' + fi + continue + fi + [[ "$after" == "$before" ]] && continue + if [[ -n "$base" && -n "$after" && "$after" == "$(_cpg_pins_at "$base" "$path")" ]]; then + continue + fi + drift+="${path}"$'\t'"$(_cpg_summary "$before")"$'\t'"$(_cpg_summary "$after")"$'\n' + else + _cpg_has_channel_pin "$before" || continue + drift+="${path}"$'\t'"$(_cpg_summary "$before")"$'\t'""$'\n' + fi + done <<<"$files" + if [[ -z "$drift" ]]; then + echo "clean" + return 0 + fi + echo "drift" + printf '%s' "$drift" + return 1 +} + +# cpg_refused_body +# The PR comment for a refused pass. are `path\tbefore\tafter` lines from +# cpg_scan_pass; empty rows mean the scan could not be verified. Pure. +# shellcheck disable=SC2016 # the backticks are literal Markdown code spans +cpg_refused_body() { + local intent="${1:-}" rows="${2:-}" path before after list="" + while IFS=$'\t' read -r path before after; do + [[ -z "${path:-}" ]] && continue + list+="- \`${path}\`: \`${before}\` → \`${after}\`"$'\n' + done <<<"$rows" + printf '## Refused — a caller stub'"'"'s channel pin is read-only to dev-lead\n\n' + if [[ -n "$list" ]]; then + printf 'The `%s` pass changed the first-party channel pin of a caller stub:\n\n%s\n' "$intent" "$list" + else + printf 'The `%s` pass changed a workflow file, and whether it kept every first-party channel pin could not be verified (the pre-pass head could not be read).\n\n' "$intent" + fi + printf 'dev-lead **did not push** this pass (#2220). A caller stub'"'"'s `uses:` ref and `agent_ref` select which dev-lead/pr-review release runs; they move only when a maintainer moves the channel tag (`scripts/cut-release.sh`) or edits the stub in a reviewed PR. Never repin a stub to clear a check — a red `template-drift` concerns `petry-projects/repo-template`, not this repository'"'"'s stubs.\n' + printf '\nAuto-merge has been disabled.\n' +} + +# cpg_flag_refused +# Escalate a refused pass on PR_NUMBER in REPO: hold label, the refused body +# (once per intent, marker-keyed), auto-merge off. Mirrors flag_sync_stub_drift. +# Never fails the caller. +cpg_flag_refused() { + local intent="$1" rows="${2:-}" + _AM_NEEDS_RESTORE=0 + if [[ "${DEV_LEAD_DRY_RUN:-false}" == "true" ]]; then + echo "[dry-run] channel-pin guard: would refuse on PR #${PR_NUMBER} (${intent}), add ${NEEDS_HUMAN_REVIEW_LABEL:-needs-human-review}, disable auto-merge" + return 0 + fi + if [[ -z "${PR_NUMBER:-}" || -z "${REPO:-}" ]]; then + echo "::warning::channel-pin guard: no PR to escalate on — the push was refused, nothing else done (#2220)" + return 0 + fi + local marker="" + # Label first so the flag comment can say when the PR could not be held (#2142). + apply_hold_label "$REPO" "$PR_NUMBER" || true + if gh api --paginate "repos/${REPO}/issues/${PR_NUMBER}/comments?per_page=100" 2>/dev/null \ + | jq -r '.[].body // ""' 2>/dev/null | grep -qF "$marker"; then + echo "::notice::PR #${PR_NUMBER} already flagged for a channel-pin change for intent=${intent} — not reposting" + post_hold_failure_note "$REPO" "$PR_NUMBER" || true + else + gh pr comment "$PR_NUMBER" --repo "$REPO" --body "${marker} +$(cpg_refused_body "$intent" "$rows")${HOLD_LABEL_NOTE:-}" \ + || echo "::warning::could not post channel-pin flag comment on PR #${PR_NUMBER}" + fi + disable_auto_merge_for_hold "$REPO" "$PR_NUMBER" || true + return 0 +} diff --git a/tests/dev-lead/unit/test_channel_pin_guard.bats b/tests/dev-lead/unit/test_channel_pin_guard.bats new file mode 100644 index 000000000..1d4247a49 --- /dev/null +++ b/tests/dev-lead/unit/test_channel_pin_guard.bats @@ -0,0 +1,384 @@ +#!/usr/bin/env bats +# Unit tests for scripts/lib/channel-pin-guard.sh and its wiring (#2220). +# +# On #2157 and #1796 a dev-lead pass "fixed" a red, non-required `template-drift` +# check by re-seeding .github/workflows/dev-lead.yml from the standards template. +# That repinned this repo's caller stub from `dev-lead/v139-ring0` to the legacy +# `dev-lead/v1-stable` channel (a 2026-09-22 release) and deleted the comment +# explaining the pin. Nothing in the harness stopped a pass editing its own stub. +# This guard does: a pass that changes a first-party channel pin (`uses:` ref or +# `agent_ref`) on a workflow file is refused before the push and escalated. A red +# `template-drift` alone no longer starts a fix pass at all. + +SCRIPT_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/../../.." && pwd)" +LIB="$SCRIPT_DIR/scripts/lib/channel-pin-guard.sh" +REVIEWS="$SCRIPT_DIR/scripts/dev-lead-fix-reviews.sh" +FIX_CI="$SCRIPT_DIR/scripts/dev-lead-fix-ci.sh" +REUSABLE="$SCRIPT_DIR/.github/workflows/dev-lead-reusable.yml" + +STUB=".github/workflows/dev-lead.yml" +REUSABLE_REF="petry-projects/.github-private/.github/workflows/dev-lead-reusable.yml" + +setup() { + # shellcheck source=scripts/lib/channel-pin-guard.sh + source "$LIB" +} + +@test "channel-pin-guard.sh is safe to source under set -euo pipefail" { + run bash -c "set -euo pipefail; source '$LIB'" + [[ "$status" -eq 0 ]] + [[ -z "$output" ]] +} + +# --------------------------------------------------------------------------- +# cpg_is_channel_ref / cpg_pins — pure +# --------------------------------------------------------------------------- + +@test "cpg_is_channel_ref: moving channel tags are channel refs; SHAs, branches and semver are not" { + cpg_is_channel_ref "dev-lead/v139-ring0" + cpg_is_channel_ref "dev-lead/v1-stable" + cpg_is_channel_ref "pr-review/v1-next" + cpg_is_channel_ref "dev-lead/stable" + run cpg_is_channel_ref "main"; [[ "$status" -eq 1 ]] + run cpg_is_channel_ref "v1"; [[ "$status" -eq 1 ]] + run cpg_is_channel_ref "4fd4eac0123456789abcdef0123456789abcdef0"; [[ "$status" -eq 1 ]] + run cpg_is_channel_ref ""; [[ "$status" -eq 1 ]] +} + +@test "cpg_pins: lists first-party uses: refs and agent_ref values, not comments or third-party actions" { + local content + content=$(_stub_body "dev-lead/v139-ring0") + content+=$'\n - uses: actions/checkout@v4\n' + run cpg_pins "$content" + [[ "$status" -eq 0 ]] + [[ "${#lines[@]}" -eq 2 ]] + [[ "${lines[0]}" == "uses"$'\t'"${REUSABLE_REF}@dev-lead/v139-ring0" ]] + [[ "${lines[1]}" == "agent_ref"$'\t'"dev-lead/v139-ring0" ]] +} + +# --------------------------------------------------------------------------- +# cpg_scan_pass — the impure gatherer over .. +# --------------------------------------------------------------------------- + +# _stub_body — this repo's dev-lead.yml shape, pinned to . +_stub_body() { + cat < README.md + _stub_body "dev-lead/v139-ring0" > "$STUB" + printf 'name: lint\non: push\njobs:\n l:\n runs-on: ubuntu-latest\n' > .github/workflows/lint.yml + git add -A; git commit -q -m base + git checkout -q -b feat + echo "feature" > feature.txt + git add -A; git commit -q -m "feat: x" + PRE="$(git rev-parse HEAD)" +} + +@test "cpg_scan_pass: the #2157/#1796 shape — repin v139-ring0 -> v1-stable is refused (drift rc1)" { + _mk_pr + _stub_body "dev-lead/v1-stable" > "$STUB" + git commit -qam "fix(template-drift): re-sync dev-lead.yml to standards baseline with v1-stable channel tag" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] + [[ "${lines[0]}" == "drift" ]] + [[ "${lines[1]}" == "${STUB}"$'\t'* ]] + [[ "${lines[1]}" == *"dev-lead/v139-ring0"* ]] + [[ "${lines[1]}" == *"dev-lead/v1-stable"* ]] +} + +@test "cpg_scan_pass: changing only agent_ref is refused" { + _mk_pr + _stub_body "dev-lead/v139-ring0" "dev-lead/v1-stable" > "$STUB" + git commit -qam "fix: agent_ref" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] + [[ "${lines[0]}" == "drift" ]] +} + +@test "cpg_scan_pass: SHA-pinning a channel-pinned stub is refused" { + _mk_pr + _stub_body "4fd4eac0123456789abcdef0123456789abcdef0" > "$STUB" + git commit -qam "fix: pin to sha" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] +} + +@test "cpg_scan_pass: deleting a channel-pinned stub is refused" { + _mk_pr + git rm -q "$STUB"; git commit -qm "chore: drop stub" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] + [[ "${lines[1]}" == "${STUB}"$'\t'*$'\t'"" ]] +} + +@test "cpg_scan_pass: a comment-only edit to the stub keeps the pin -> clean" { + _mk_pr + sed -i 's/never repin this to next/never repin this/' "$STUB" + git commit -qam "docs: comment" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 0 ]] + [[ "$output" == "clean" ]] +} + +@test "cpg_scan_pass: edits to files without a channel pin are clean" { + _mk_pr + echo more >> README.md + printf ' timeout-minutes: 5\n' >> .github/workflows/lint.yml + git commit -qam "chore: unrelated" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 0 ]] + [[ "$output" == "clean" ]] +} + +@test "cpg_scan_pass: adding a first-party channel pin to a previously unpinned workflow is refused (drift)" { + _mk_pr + _stub_body "dev-lead/v139-ring0" > .github/workflows/lint.yml + git commit -qam "refactor: add channel pin to lint workflow" + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] + [[ "${lines[0]}" == "drift" ]] + [[ "${lines[1]}" == ".github/workflows/lint.yml"$'\t'""$'\t'* ]] + [[ "${lines[1]}" == *"dev-lead/v139-ring0"* ]] +} + +@test "cpg_scan_pass: a pass that changes nothing is clean" { + _mk_pr + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 0 ]] +} + +@test "cpg_scan_pass: restoring the base branch's pin is clean (cleanup of a bad repin)" { + _mk_pr + _stub_body "dev-lead/v1-stable" > "$STUB" + git commit -qam "fix(ci): re-seed dev-lead.yml" # the bad repin, already on the PR + PRE="$(git rev-parse HEAD)" + _stub_body "dev-lead/v139-ring0" > "$STUB" + git commit -qam "revert the repin" + run cpg_scan_pass "$PRE" HEAD main + [[ "$status" -eq 0 ]] + [[ "$output" == "clean" ]] + # Without the base rev the same change is not provably a restore. + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] +} + +@test "cpg_scan_pass: adopting a pin the base branch moved to (rebase) is clean" { + _mk_pr + git checkout -q main + _stub_body "dev-lead/v140-ring0" > "$STUB" + git commit -qam "chore: promote stub to v140" + git checkout -q feat + git merge -q --no-edit main + run cpg_scan_pass "$PRE" HEAD main + [[ "$status" -eq 0 ]] +} + +@test "cpg_scan_pass: a channel-pinned workflow the base branch added arrives clean on a merge" { + _mk_pr + git checkout -q main + _stub_body "dev-lead/v139-ring0" > .github/workflows/new-agent.yml + git add .github/workflows/new-agent.yml + git commit -qm "feat: add a channel-pinned caller stub on main" + git checkout -q feat + git merge -q --no-edit main + run cpg_scan_pass "$PRE" HEAD main + [[ "$status" -eq 0 ]] + [[ "$output" == "clean" ]] + # Without the base rev the new pin is not provably the base's own. + run cpg_scan_pass "$PRE" HEAD + [[ "$status" -eq 1 ]] + # A pin that differs from the base's is still drift. + _stub_body "dev-lead/v1-stable" > .github/workflows/new-agent.yml + git commit -qam "fix(ci): repin the new stub" + run cpg_scan_pass "$PRE" HEAD main + [[ "$status" -eq 1 ]] + [[ "${lines[1]}" == ".github/workflows/new-agent.yml"$'\t'""$'\t'* ]] +} + +@test "cpg_scan_pass: an unresolvable pre-pass head fails closed -> unknown rc2" { + _mk_pr + run cpg_scan_pass "" HEAD + [[ "$status" -eq 2 ]] + [[ "$output" == "unknown" ]] + run cpg_scan_pass "0000000000000000000000000000000000000000" HEAD + [[ "$status" -eq 2 ]] +} + +@test "cpg_refused_body: names the stub, both pins, and says nothing was pushed" { + local rows + rows="${STUB}"$'\t'"uses ${REUSABLE_REF}@dev-lead/v139-ring0, agent_ref dev-lead/v139-ring0"$'\t'"uses ${REUSABLE_REF}@dev-lead/v1-stable, agent_ref dev-lead/v1-stable" + run cpg_refused_body "fix-ci" "$rows" + [[ "$status" -eq 0 ]] + [[ "$output" == *"\`${STUB}\`"* ]] + [[ "$output" == *"dev-lead/v139-ring0"* ]] + [[ "$output" == *"dev-lead/v1-stable"* ]] + [[ "$output" == *"did not push"* ]] + [[ "$output" == *"#2220"* ]] +} + +@test "cpg_refused_body: an unverifiable scan says so" { + run cpg_refused_body "fix-reviews" "" + [[ "$output" == *"could not be verified"* ]] + [[ "$output" == *"did not push"* ]] +} + +# --------------------------------------------------------------------------- +# Harness wiring — fix-reviews (every pushing intent) and fix-ci +# --------------------------------------------------------------------------- + +@test "Wiring: fix-reviews sources the guard and commit_and_push refuses a repin with rc 4 for every pushing intent" { + grep -q 'source "$(dirname "$0")/lib/channel-pin-guard.sh"' "$REVIEWS" + local body block + body=$(awk '/^commit_and_push\(\) \{/,/^}/' "$REVIEWS") + grep -q 'cpg_scan_pass' <<<"$body" + # Not scoped to bot intents: only enable-auto-merge (which never pushes) is exempt. + block=$(grep -B6 -A14 'cpg_scan_pass' <<<"$body") + grep -q 'enable-auto-merge) ;;' <<<"$block" + grep -q 'cpg_flag_refused' <<<"$block" + grep -q 'return 4' <<<"$block" + # The guard runs before the push. + local guard_line push_line + guard_line=$(grep -n 'cpg_scan_pass' <<<"$body" | head -1 | cut -d: -f1) + push_line=$(grep -n 'push_no_clobber ||' <<<"$body" | head -1 | cut -d: -f1) + [[ "$guard_line" -lt "$push_line" ]] +} + +@test "Wiring: on-mention and rebase stop on a guard refusal (rc 4) instead of reporting applied/no-changes" { + local body + body=$(awk '/^ on-mention\)$/,/^ ;;$/' "$REVIEWS") + grep -A14 'commit_and_push "on-mention"' <<<"$body" | grep -q '"\$cp_rc" -eq 4' + body=$(awk '/commit_and_push "rebase" \|\| cp_rc/,/verify_resolution_integrity/' "$REVIEWS") + grep -q '"\$cp_rc" -eq 4' <<<"$body" +} + +@test "Wiring: fix-ci runs the guard before push_with_merge_guard" { + grep -qF 'source "$(dirname "${BASH_SOURCE[0]}")/lib/channel-pin-guard.sh"' "$FIX_CI" + local guard_line push_line + guard_line=$(grep -n 'fix_ci_channel_pin_guard "' "$FIX_CI" | head -1 | cut -d: -f1) + push_line=$(grep -n 'push_with_merge_guard || exit 1' "$FIX_CI" | head -1 | cut -d: -f1) + [[ -n "$guard_line" && -n "$push_line" ]] + [[ "$guard_line" -lt "$push_line" ]] +} + +# fix-ci is sourceable (main runs only when executed), so the guard runs for real +# against a git fixture with gh stubbed out. +_stub_gh() { + STUB_BIN_DIR="$BATS_TEST_TMPDIR/bin" + mkdir -p "$STUB_BIN_DIR" + cat > "$STUB_BIN_DIR/gh" <<'GHEOF' +#!/usr/bin/env bash +echo "gh $*" >> "$GH_CALLS" +case "$*" in + *"/comments"*) echo "[]" ;; + *"pulls/"*) echo '{"base":{"ref":"main"},"state":"open"}' ;; + *) echo "{}" ;; +esac +GHEOF + chmod +x "$STUB_BIN_DIR/gh" + export PATH="$STUB_BIN_DIR:$PATH" + export GH_CALLS="$BATS_TEST_TMPDIR/gh-calls" + : > "$GH_CALLS" +} + +@test "fix-ci: a pass whose diff repins the stub is refused before the push and escalated" { + _mk_pr + _stub_gh + _stub_body "dev-lead/v1-stable" > "$STUB" + git commit -qam "fix(ci): re-seed dev-lead.yml from standards to resolve template-drift check" + run bash -c "export PR_NUMBER=42 HEAD_SHA=$PRE REPO=o/r DEV_LEAD_DRY_RUN=false PROMPTS_DIR='$SCRIPT_DIR/prompts/dev-lead' + source '$FIX_CI' + fix_ci_channel_pin_guard '$PRE' main" + [[ "$status" -ne 0 ]] + [[ "$output" == *"Channel-pin guard"* ]] + grep -q 'issues/42/labels' "$GH_CALLS" # held for a human + grep -q 'pr comment 42' "$GH_CALLS" # escalation comment + grep -q 'disable-auto' "$GH_CALLS" # auto-merge off + grep -q 'status=refused' "$GH_CALLS" # terminal marker for this SHA +} + +@test "fix-ci: a pass that leaves the pin alone passes the guard" { + _mk_pr + _stub_gh + echo fix >> feature.txt + git commit -qam "fix(ci): real fix" + run bash -c "export PR_NUMBER=42 HEAD_SHA=$PRE REPO=o/r DEV_LEAD_DRY_RUN=false PROMPTS_DIR='$SCRIPT_DIR/prompts/dev-lead' + source '$FIX_CI' + fix_ci_channel_pin_guard '$PRE' main" + [[ "$status" -eq 0 ]] + ! grep -q 'pr comment' "$GH_CALLS" +} + +@test "fix-ci: a refused SHA is terminal for idempotency" { + grep -qF 'status=(applied|failed|no-changes|declined|refused)' "$FIX_CI" +} + +# --------------------------------------------------------------------------- +# AC 2 — a red `template-drift` alone dispatches nothing +# --------------------------------------------------------------------------- + +@test "ci-relay: a red template-drift check is not relayed (the job's if: excludes it)" { + local cond + cond=$(awk '/^ ci-relay:$/,/runs-on:/' "$REUSABLE") + [[ -n "$cond" ]] + grep -q "github.event.check_run.name != 'template-drift'" <<<"$cond" +} + +@test "ci-relay: the workflow exclusion and fix-ci's ignored-check list agree" { + source "$FIX_CI" + local name + for name in "${FIX_CI_IGNORED_CHECKS[@]}"; do + awk '/^ ci-relay:$/,/runs-on:/' "$REUSABLE" | grep -qF "github.event.check_run.name != '${name}'" + done +} + +@test "fix-ci: a dispatch carrying only a red template-drift runs no engine and pushes nothing" { + _stub_gh + run env PR_NUMBER=42 HEAD_SHA=abc123 REPO=o/r DEV_LEAD_DRY_RUN=true \ + PROMPTS_DIR="$SCRIPT_DIR/prompts/dev-lead" \ + CHECKS_JSON='[{"name":"template-drift","conclusion":"failure","details_url":"https://github.com/o/r/actions/runs/1/job/2","id":7}]' \ + bash "$FIX_CI" + [[ "$status" -eq 0 ]] + [[ "$output" == *"not a fix-ci trigger"* ]] + [[ "$output" != *"would run engine"* ]] + ! grep -q 'pr comment' "$GH_CALLS" +} + +@test "fix-ci: template-drift alongside a real failure still runs (only the real check is a trigger)" { + _stub_gh + run env PR_NUMBER=42 HEAD_SHA=abc123 REPO=o/r DEV_LEAD_DRY_RUN=true \ + PROMPTS_DIR="$SCRIPT_DIR/prompts/dev-lead" \ + CHECKS_JSON='[{"name":"template-drift","conclusion":"failure","id":7},{"name":"unit","conclusion":"failure","id":8}]' \ + bash "$FIX_CI" + [[ "$status" -eq 0 ]] + [[ "$output" == *"would run engine"* ]] +} + +@test "fix-ci: an empty CHECKS_JSON is not treated as all-ignored (behavior unchanged)" { + _stub_gh + run env PR_NUMBER=42 HEAD_SHA=abc123 REPO=o/r DEV_LEAD_DRY_RUN=true \ + PROMPTS_DIR="$SCRIPT_DIR/prompts/dev-lead" CHECKS_JSON='[]' \ + bash "$FIX_CI" + [[ "$output" != *"not a fix-ci trigger"* ]] +} diff --git a/tests/dev-lead/unit/test_sync_stub_guard_fix_ci.bats b/tests/dev-lead/unit/test_sync_stub_guard_fix_ci.bats index bb4db93e5..ba8f24b43 100644 --- a/tests/dev-lead/unit/test_sync_stub_guard_fix_ci.bats +++ b/tests/dev-lead/unit/test_sync_stub_guard_fix_ci.bats @@ -231,7 +231,7 @@ _remote_feat() { git --git-dir="$REMOTE" rev-parse refs/heads/feat; } # --------------------------------------------------------------------------- @test "Wiring: fix-ci sources the guard and evaluates it before push_with_merge_guard" { - grep -q 'source "$(dirname "$0")/lib/sync-stub-guard.sh"' "$FIX_CI" + grep -qF 'source "$(dirname "${BASH_SOURCE[0]}")/lib/sync-stub-guard.sh"' "$FIX_CI" local body body=$(awk '/^main\(\) \{/,/^}/' "$FIX_CI") local eval_line push_line