From 16fca5f2d7584efaeaed48e6dd0f292ff383bdf9 Mon Sep 17 00:00:00 2001 From: don-petry <{}+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:24:29 +0000 Subject: [PATCH 1/6] =?UTF-8?q?checkpoint(dev-lead):=20INCOMPLETE=20?= =?UTF-8?q?=E2=80=94=20partial=20work=20for=20issue=20#2220=20(stage=20tim?= =?UTF-8?q?eout=20=E2=80=94=20unreviewed,=20do=20not=20merge)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev-lead-reusable.yml | 7 +- AGENTS.md | 10 + prompts/dev-lead/fix-ci.md | 2 + scripts/dev-lead-fix-ci.sh | 76 +++- scripts/dev-lead-fix-reviews.sh | 34 +- scripts/lib/channel-pin-guard.sh | 193 ++++++++++ scripts/template_stub_drift.sh | 30 +- .../dev-lead/unit/test_channel_pin_guard.bats | 351 ++++++++++++++++++ tests/template_stub_drift.bats | 24 ++ 9 files changed, 699 insertions(+), 28 deletions(-) create mode 100644 scripts/lib/channel-pin-guard.sh create mode 100644 tests/dev-lead/unit/test_channel_pin_guard.bats diff --git a/.github/workflows/dev-lead-reusable.yml b/.github/workflows/dev-lead-reusable.yml index b1195b705..2ff1937ef 100644 --- a/.github/workflows/dev-lead-reusable.yml +++ b/.github/workflows/dev-lead-reusable.yml @@ -637,13 +637,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 b43db0bee..4c1ddef1b 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 carry no `file=` anchor. Before #2220 they did, so GitHub pinned them to the PR's own + same-named file, and 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. + - **Reference rows — `agent-ingress.yml` (#1729, AC #12):** `.github/workflows/agent-ingress.yml` is enrolled in the drift manifest for **post-collapse byte-identity coverage**, but `seed-repo-template.sh` deliberately never seeds it (it lives in the `--emit-workflow`-only 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..1e4a87aff 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" # 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" +# 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 a03c7c006..90cd2283e 100755 --- a/scripts/dev-lead-fix-reviews.sh +++ b/scripts/dev-lead-fix-reviews.sh @@ -24,6 +24,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" # PR issue-comment disposition verifier (#1813): the issue-comment sibling of @@ -3103,6 +3105,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). @@ -3163,7 +3183,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 @@ -3767,7 +3787,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 @@ -3953,6 +3973,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 @@ -4187,6 +4213,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..a9a34ed3e --- /dev/null +++ b/scripts/lib/channel-pin-guard.sh @@ -0,0 +1,193 @@ +#!/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") + _cpg_has_channel_pin "$before" || continue + if git --literal-pathspecs cat-file -e "${head}:${path}" 2>/dev/null; then + after=$(_cpg_pins_at "$head" "$path") + [[ "$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 + 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/scripts/template_stub_drift.sh b/scripts/template_stub_drift.sh index d31987346..107f93af3 100644 --- a/scripts/template_stub_drift.sh +++ b/scripts/template_stub_drift.sh @@ -305,30 +305,32 @@ template_drift_annotate() { case "$status" in DRIFTED) drifted=1 + # No `file=` anchor (#2220): GitHub would pin it to the PR's own + # same-named file; the drift is in TEMPLATE_REPO. if [ "$matched" = "ahead-unchecked" ]; then # shellcheck disable=SC2016 - printf '::error file=%s::Template stub %s has DRIFTED from the published baseline `%s` (committed blob %s != expected %s), but it could NOT be compared against standards HEAD, so it may be AHEAD of the channel rather than drifted. Do not re-seed yet: re-run with STANDARDS_REF=. If that reports ALIGNED, promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding, which would regress the template (#1926).\n' \ - "$file" "$file" "$n_ref" "${committed:0:12}" "${expected:0:12}" + printf '::error::Template stub %s in %s has DRIFTED from the published baseline `%s` (committed blob %s != expected %s), but it could NOT be compared against standards HEAD, so it may be AHEAD of the channel rather than drifted. Do not re-seed yet: re-run with STANDARDS_REF=. If that reports ALIGNED, promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding, which would regress the template (#1926).\n' \ + "$file" "$TEMPLATE_REPO" "$n_ref" "${committed:0:12}" "${expected:0:12}" elif [ -n "$head_sha" ]; then # HEAD is newer than the channel and the file matches neither, so it may # have been synced from a standards commit between them (#2237 AC #2). # shellcheck disable=SC2016 - printf '::error file=%s::Template stub %s has DRIFTED: its committed blob %s matches neither the published baseline `%s` (expected %s) nor standards HEAD%s. It may have been synced from a standards commit between `%s` and HEAD. Before re-seeding, check: re-run with STANDARDS_REF= for the standards commits in that range that touched this file'"'"'s source. If one reports ALIGNED, re-seed with STANDARDS_REF= and promote standards (scripts/cut-standards-release.sh in the standards repo); do not re-seed from the channel, which would regress the template. If none does, re-seed via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ - "$file" "$file" "${committed:0:12}" "$n_ref" "${expected:0:12}" "$head_note" "$n_ref" + printf '::error::Template stub %s in %s has DRIFTED: its committed blob %s matches neither the published baseline `%s` (expected %s) nor standards HEAD%s. It may have been synced from a standards commit between `%s` and HEAD. Before re-seeding, check: re-run with STANDARDS_REF= for the standards commits in that range that touched this file'"'"'s source. If one reports ALIGNED, re-seed with STANDARDS_REF= and promote standards (scripts/cut-standards-release.sh in the standards repo); do not re-seed from the channel, which would regress the template. If none does, re-seed via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ + "$file" "$TEMPLATE_REPO" "${committed:0:12}" "$n_ref" "${expected:0:12}" "$head_note" "$n_ref" else - printf '::error file=%s::Template stub %s has DRIFTED from the standards-derived baseline (committed blob %s != expected %s). Re-seed it via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ - "$file" "$file" "${committed:0:12}" "${expected:0:12}" + printf '::error::Template stub %s in %s has DRIFTED from the standards-derived baseline (committed blob %s != expected %s). This is not a file in this pull request: never edit this repository'"'"'s own %s (or its channel pin) to clear it. Re-seed %s via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ + "$file" "$TEMPLATE_REPO" "${committed:0:12}" "${expected:0:12}" "$file" "$TEMPLATE_REPO" fi ;; AHEAD) # shellcheck disable=SC2016 - printf '::warning file=%s::Template stub %s is ahead of `%s`: its committed blob %s matches the seed emission at standards HEAD%s, not the channel baseline %s. Promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding — a re-seed from the channel would regress the template. This is a propagation lag, not template corruption, so it does not fail the check (#1926).\n' \ - "$file" "$file" "$n_ref" "${committed:0:12}" "$head_note" "${expected:0:12}" + printf '::warning::Template stub %s in %s is ahead of `%s`: its committed blob %s matches the seed emission at standards HEAD%s, not the channel baseline %s. Promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding — a re-seed from the channel would regress the template. This is a propagation lag, not template corruption, so it does not fail the check (#1926).\n' \ + "$file" "$TEMPLATE_REPO" "$n_ref" "${committed:0:12}" "$head_note" "${expected:0:12}" ;; ALIGNED) if [ "$matched" = "N-1" ]; then - printf '::notice file=%s::Template stub %s matches the immediately-preceding published standards version (N-1), not the current one (N) — propagation pending, accepted as ALIGNED (#1448 AC #8).\n' \ - "$file" "$file" + printf '::notice::Template stub %s in %s matches the immediately-preceding published standards version (N-1), not the current one (N) — propagation pending, accepted as ALIGNED (#1448 AC #8).\n' \ + "$file" "$TEMPLATE_REPO" fi ;; MISSING) @@ -337,11 +339,11 @@ template_drift_annotate() { # NOT seeded yet (#1729 AC #12). Emit a non-actionable notice — never # the generic re-seed remedy, which can never clear here and would push # a live executing ingress into every new repo ahead of the pilot. - printf '::notice file=%s::Template reference %s is expected but intentionally NOT seeded into %s yet (ADR-0007 / #1729) — seed-repo-template.sh deliberately never emits it until the pilot repo collapse lands. This is NOT actionable: do not hand-seed it to silence this. It will be seeded in the same change that retires the per-role stubs it replaces. See AGENTS.md -> Template drift guard.\n' \ - "$file" "$file" "$TEMPLATE_REPO" + printf '::notice::Template reference %s is expected but intentionally NOT seeded into %s yet (ADR-0007 / #1729) — seed-repo-template.sh deliberately never emits it until the pilot repo collapse lands. This is NOT actionable: do not hand-seed it to silence this. It will be seeded in the same change that retires the per-role stubs it replaces. See AGENTS.md -> Template drift guard.\n' \ + "$file" "$TEMPLATE_REPO" else - printf '::warning file=%s::Template stub %s is MISSING from %s (expected blob %s) — re-seed via scripts/seed-repo-template.sh.\n' \ - "$file" "$file" "$TEMPLATE_REPO" "${expected:0:12}" + printf '::warning::Template stub %s is MISSING from %s (expected blob %s) — re-seed via scripts/seed-repo-template.sh.\n' \ + "$file" "$TEMPLATE_REPO" "${expected:0:12}" fi ;; esac 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..65cea419e --- /dev/null +++ b/tests/dev-lead/unit/test_channel_pin_guard.bats @@ -0,0 +1,351 @@ +#!/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: 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: 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 -qE 'status=\(applied\|failed\|no-changes\|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/template_stub_drift.bats b/tests/template_stub_drift.bats index e88b287e4..f93197939 100644 --- a/tests/template_stub_drift.bats +++ b/tests/template_stub_drift.bats @@ -141,6 +141,30 @@ setup() { rm -f "$tsv" } +# #2220: a `file=` annotation lands on the PR's OWN same-named file, so a drifted +# repo-template dev-lead.yml showed up as "this repo's dev-lead.yml drifted" and a +# dev-lead pass repinned this repo's caller stub to clear it. The annotation must +# name repo-template, carry no `file=`, and say this repo's stubs are not the fix. +@test "annotate: a DRIFTED row names repo-template, has no file= anchor, and rules out editing this repo's stub (#2220)" { + tsv="$(mktemp)" + printf '%s\t%s\t%s\t%s\n' ".github/workflows/dev-lead.yml" "DRIFTED" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "$EXPECTED" >> "$tsv" + run template_drift_annotate "$tsv" + [ "$status" -ne 0 ] + [[ "$output" == *"::error::"* ]] + [[ "$output" != *"file="* ]] + [[ "$output" == *"petry-projects/repo-template"* ]] + [[ "$output" == *"not a file in this pull request"* ]] + rm -f "$tsv" +} + +@test "annotate: a MISSING row has no file= anchor either (#2220)" { + tsv="$(mktemp)" + printf '%s\t%s\t%s\t%s\n' "CLAUDE.md" "MISSING" "" "$EXPECTED" >> "$tsv" + run template_drift_annotate "$tsv" + [[ "$output" != *"file="* ]] + rm -f "$tsv" +} + @test "annotate: an all-ALIGNED set passes (exit 0, no error annotation)" { tsv="$(mktemp)" printf '%s\t%s\t%s\t%s\n' ".github/workflows/dev-lead.yml" "ALIGNED" "$EXPECTED" "$EXPECTED" >> "$tsv" From 47c47060e7ce6b64cc863722ed74593b88a53a11 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:44:31 +0000 Subject: [PATCH 2/6] fix(channel-pin-guard): detect when a workflow gains a first-party channel pin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A pass that adds a first-party channel pin to a previously unpinned workflow was not caught by the guard, because it skipped any file without a channel pin before the pass. Now the guard detects and refuses this case. Adds test: cpg_scan_pass detects when a workflow without a channel pin gains a first-party channel pin — it is refused as drift. Fixes #2220. Co-Authored-By: Claude Haiku 4.5 --- scripts/lib/channel-pin-guard.sh | 8 +++++++- tests/dev-lead/unit/test_channel_pin_guard.bats | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/lib/channel-pin-guard.sh b/scripts/lib/channel-pin-guard.sh index a9a34ed3e..d678bef2c 100644 --- a/scripts/lib/channel-pin-guard.sh +++ b/scripts/lib/channel-pin-guard.sh @@ -120,15 +120,21 @@ cpg_scan_pass() { *) continue ;; esac before=$(_cpg_pins_at "$pre" "$path") - _cpg_has_channel_pin "$before" || continue 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 + if _cpg_has_channel_pin "$after"; 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" diff --git a/tests/dev-lead/unit/test_channel_pin_guard.bats b/tests/dev-lead/unit/test_channel_pin_guard.bats index 65cea419e..1b4f0a0dc 100644 --- a/tests/dev-lead/unit/test_channel_pin_guard.bats +++ b/tests/dev-lead/unit/test_channel_pin_guard.bats @@ -152,6 +152,17 @@ _mk_pr() { [[ "$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 From c5b3ffabb8aa29488a84da16d618b60ba22a2972 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:38:01 +0000 Subject: [PATCH 3/6] test(template-drift): align rebased expectations with unanchored annotations (#2220) Co-Authored-By: Claude Sonnet 5.5 --- tests/template_stub_drift.bats | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/template_stub_drift.bats b/tests/template_stub_drift.bats index f93197939..c68210024 100644 --- a/tests/template_stub_drift.bats +++ b/tests/template_stub_drift.bats @@ -372,7 +372,7 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/workflows/agent-shield.yml" "AHEAD" "$HEAD_BLOB" "$EXPECTED" "HEAD" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" "1234567890abcdef1234567890abcdef12345678" [ "$status" -eq 0 ] # a propagation lag, not corruption - [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] + [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] [[ "$output" != *"::error"* ]] [[ "$output" == *'is ahead of `standards/v1-stable`'* ]] [[ "$output" == *"cut-standards-release.sh"* ]] @@ -397,9 +397,9 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/CODEOWNERS" "DRIFTED" "$HAND_EDIT" "$EXPECTED" "" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" [ "$status" -ne 0 ] - [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] - [[ "$output" == *"::error file=.github/CODEOWNERS::"*"Re-seed it via scripts/seed-repo-template.sh"* ]] - [[ "$output" != *"::error file=.github/workflows/agent-shield.yml"* ]] + [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] + [[ "$output" == *"::error::Template stub .github/CODEOWNERS in "*"Re-seed it via scripts/seed-repo-template.sh"* ]] + [[ "$output" != *"::error::Template stub .github/workflows/agent-shield.yml in "* ]] } @test "annotate: a DRIFTED row tagged ahead-unchecked fails WITHOUT the bare re-seed remedy (#1926)" { @@ -407,7 +407,7 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/CODEOWNERS" "DRIFTED" "$HAND_EDIT" "$EXPECTED" "ahead-unchecked" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" [ "$status" -ne 0 ] # still drift until proven otherwise - [[ "$output" == *"::error file=.github/CODEOWNERS::"* ]] + [[ "$output" == *"::error::Template stub .github/CODEOWNERS in "* ]] [[ "$output" == *"could NOT be compared against standards HEAD"* ]] [[ "$output" == *"STANDARDS_REF="* ]] [[ "$output" != *"Re-seed it via scripts/seed-repo-template.sh"* ]] @@ -505,9 +505,9 @@ _drv_row() { [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'AHEAD\tHEAD')" ] [ "$(_drv_row .github/workflows/dependabot-automerge.yml)" = "$(printf 'DRIFTED\t')" ] [ "$(_drv_row CLAUDE.md)" = "$(printf 'MISSING\t')" ] - [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"*'is ahead of `standards/v1-stable`'* ]] - [[ "$output" == *"::error file=.github/workflows/dependabot-automerge.yml::"* ]] - [[ "$output" != *"::error file=.github/workflows/agent-shield.yml"* ]] + [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "*'is ahead of `standards/v1-stable`'* ]] + [[ "$output" == *"::error::Template stub .github/workflows/dependabot-automerge.yml in "* ]] + [[ "$output" != *"::error::Template stub .github/workflows/agent-shield.yml in "* ]] } @test "driver: only would-be DRIFTED files pay for a HEAD emission (#1926 AC #1)" { @@ -524,7 +524,7 @@ _drv_row() { run main [ "$status" -eq 0 ] [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'AHEAD\tHEAD')" ] - [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] + [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] [[ "$output" != *"::error"* ]] } @@ -534,7 +534,7 @@ _drv_row() { [ "$status" -ne 0 ] # With nothing newer than N, a file that matches only some other ref is plain drift. [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'DRIFTED\t')" ] - [[ "$output" == *"::error file=.github/workflows/agent-shield.yml::"*"Re-seed it via"* ]] + [[ "$output" == *"::error::Template stub .github/workflows/agent-shield.yml in "*"Re-seed it via"* ]] run grep -c "@${DRV_HEAD_SHA}\$" "$DRV_EMIT_LOG" [ "$output" = "0" ] } @@ -545,7 +545,7 @@ _drv_row() { [ "$status" -ne 0 ] [[ "$output" == *"::warning::Could not resolve the standards default-branch HEAD commit"* ]] [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'DRIFTED\tahead-unchecked')" ] - [[ "$output" == *"::error file=.github/workflows/agent-shield.yml::"*"could NOT be compared against standards HEAD"* ]] + [[ "$output" == *"::error::Template stub .github/workflows/agent-shield.yml in "*"could NOT be compared against standards HEAD"* ]] [[ "$output" != *"Re-seed it via scripts/seed-repo-template.sh"* ]] # Files that match the channel are unaffected by the failed lookup. [ "$(_drv_row .github/workflows/auto-rebase.yml)" = "$(printf 'ALIGNED\tN')" ] From 244b374d6e0f15f607b7e0763018f5834ca676e4 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 02:01:32 +0000 Subject: [PATCH 4/6] fix(dev-lead): update status-4 guard refusal messages to include channel-pin When fix-bot-comment or review-changes guards refuse due to channel-pin violations, the status-4 handlers now report all possible guard refusals (test tamper/regression, sync-stub, and channel-pin) to match fix-reviews. Previously these handlers were misleading operators by only mentioning test and sync-stub guards, omitting the channel-pin guard that could have triggered the refusal. Co-Authored-By: Claude Haiku 4.5 --- scripts/dev-lead-fix-reviews.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/dev-lead-fix-reviews.sh b/scripts/dev-lead-fix-reviews.sh index 90cd2283e..4c1698d3e 100755 --- a/scripts/dev-lead-fix-reviews.sh +++ b/scripts/dev-lead-fix-reviews.sh @@ -3886,7 +3886,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 @@ -4031,7 +4031,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 From 3205d1809b7667c2a71b854204840644ead0eb5a Mon Sep 17 00:00:00 2001 From: Don Petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 09:43:38 -0500 Subject: [PATCH 5/6] fix(dev-lead): make fix-ci sourceable and drop the template-drift edits from #2220 - scripts/dev-lead-fix-ci.sh: resolve lib/dev-lead-pr-body.sh and lib/sync-stub-guard.sh from BASH_SOURCE[0] instead of $0, so the script loads its libs when a bats test sources it (the source guard added for the channel-pin guard tests already assumes this). - tests/dev-lead/unit/test_channel_pin_guard.bats: the idempotency test pinned an out-of-date status list with an ERE that could not match; assert the script's literal list (now including `declined`) with -F. - tests/dev-lead/unit/test_sync_stub_guard_fix_ci.bats: the wiring test pinned the old `$0` form of the sync-stub-guard source line. - scripts/template_stub_drift.sh, tests/template_stub_drift.bats: restored to main. The annotation change moves to issue #2215. Test-Change-Justification: the wiring assertion in test_sync_stub_guard_fix_ci.bats pinned the literal `$(dirname "$0")` source line; it now pins the `${BASH_SOURCE[0]}` form the graphite-app review thread on scripts/dev-lead-fix-ci.sh:20 (PR #2238) asked for. The assertion still checks that fix-ci sources the guard. Co-Authored-By: Claude Opus 5.5 --- scripts/dev-lead-fix-ci.sh | 4 +- scripts/template_stub_drift.sh | 30 ++++++------ .../dev-lead/unit/test_channel_pin_guard.bats | 2 +- .../unit/test_sync_stub_guard_fix_ci.bats | 2 +- tests/template_stub_drift.bats | 46 +++++-------------- 5 files changed, 29 insertions(+), 55 deletions(-) diff --git a/scripts/dev-lead-fix-ci.sh b/scripts/dev-lead-fix-ci.sh index 1e4a87aff..16de53426 100755 --- a/scripts/dev-lead-fix-ci.sh +++ b/scripts/dev-lead-fix-ci.sh @@ -14,10 +14,10 @@ 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" diff --git a/scripts/template_stub_drift.sh b/scripts/template_stub_drift.sh index 107f93af3..d31987346 100644 --- a/scripts/template_stub_drift.sh +++ b/scripts/template_stub_drift.sh @@ -305,32 +305,30 @@ template_drift_annotate() { case "$status" in DRIFTED) drifted=1 - # No `file=` anchor (#2220): GitHub would pin it to the PR's own - # same-named file; the drift is in TEMPLATE_REPO. if [ "$matched" = "ahead-unchecked" ]; then # shellcheck disable=SC2016 - printf '::error::Template stub %s in %s has DRIFTED from the published baseline `%s` (committed blob %s != expected %s), but it could NOT be compared against standards HEAD, so it may be AHEAD of the channel rather than drifted. Do not re-seed yet: re-run with STANDARDS_REF=. If that reports ALIGNED, promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding, which would regress the template (#1926).\n' \ - "$file" "$TEMPLATE_REPO" "$n_ref" "${committed:0:12}" "${expected:0:12}" + printf '::error file=%s::Template stub %s has DRIFTED from the published baseline `%s` (committed blob %s != expected %s), but it could NOT be compared against standards HEAD, so it may be AHEAD of the channel rather than drifted. Do not re-seed yet: re-run with STANDARDS_REF=. If that reports ALIGNED, promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding, which would regress the template (#1926).\n' \ + "$file" "$file" "$n_ref" "${committed:0:12}" "${expected:0:12}" elif [ -n "$head_sha" ]; then # HEAD is newer than the channel and the file matches neither, so it may # have been synced from a standards commit between them (#2237 AC #2). # shellcheck disable=SC2016 - printf '::error::Template stub %s in %s has DRIFTED: its committed blob %s matches neither the published baseline `%s` (expected %s) nor standards HEAD%s. It may have been synced from a standards commit between `%s` and HEAD. Before re-seeding, check: re-run with STANDARDS_REF= for the standards commits in that range that touched this file'"'"'s source. If one reports ALIGNED, re-seed with STANDARDS_REF= and promote standards (scripts/cut-standards-release.sh in the standards repo); do not re-seed from the channel, which would regress the template. If none does, re-seed via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ - "$file" "$TEMPLATE_REPO" "${committed:0:12}" "$n_ref" "${expected:0:12}" "$head_note" "$n_ref" + printf '::error file=%s::Template stub %s has DRIFTED: its committed blob %s matches neither the published baseline `%s` (expected %s) nor standards HEAD%s. It may have been synced from a standards commit between `%s` and HEAD. Before re-seeding, check: re-run with STANDARDS_REF= for the standards commits in that range that touched this file'"'"'s source. If one reports ALIGNED, re-seed with STANDARDS_REF= and promote standards (scripts/cut-standards-release.sh in the standards repo); do not re-seed from the channel, which would regress the template. If none does, re-seed via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ + "$file" "$file" "${committed:0:12}" "$n_ref" "${expected:0:12}" "$head_note" "$n_ref" else - printf '::error::Template stub %s in %s has DRIFTED from the standards-derived baseline (committed blob %s != expected %s). This is not a file in this pull request: never edit this repository'"'"'s own %s (or its channel pin) to clear it. Re-seed %s via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ - "$file" "$TEMPLATE_REPO" "${committed:0:12}" "${expected:0:12}" "$file" "$TEMPLATE_REPO" + printf '::error file=%s::Template stub %s has DRIFTED from the standards-derived baseline (committed blob %s != expected %s). Re-seed it via scripts/seed-repo-template.sh — do not hand-edit the template; edit standards/ instead.\n' \ + "$file" "$file" "${committed:0:12}" "${expected:0:12}" fi ;; AHEAD) # shellcheck disable=SC2016 - printf '::warning::Template stub %s in %s is ahead of `%s`: its committed blob %s matches the seed emission at standards HEAD%s, not the channel baseline %s. Promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding — a re-seed from the channel would regress the template. This is a propagation lag, not template corruption, so it does not fail the check (#1926).\n' \ - "$file" "$TEMPLATE_REPO" "$n_ref" "${committed:0:12}" "$head_note" "${expected:0:12}" + printf '::warning file=%s::Template stub %s is ahead of `%s`: its committed blob %s matches the seed emission at standards HEAD%s, not the channel baseline %s. Promote standards (scripts/cut-standards-release.sh in the standards repo) instead of re-seeding — a re-seed from the channel would regress the template. This is a propagation lag, not template corruption, so it does not fail the check (#1926).\n' \ + "$file" "$file" "$n_ref" "${committed:0:12}" "$head_note" "${expected:0:12}" ;; ALIGNED) if [ "$matched" = "N-1" ]; then - printf '::notice::Template stub %s in %s matches the immediately-preceding published standards version (N-1), not the current one (N) — propagation pending, accepted as ALIGNED (#1448 AC #8).\n' \ - "$file" "$TEMPLATE_REPO" + printf '::notice file=%s::Template stub %s matches the immediately-preceding published standards version (N-1), not the current one (N) — propagation pending, accepted as ALIGNED (#1448 AC #8).\n' \ + "$file" "$file" fi ;; MISSING) @@ -339,11 +337,11 @@ template_drift_annotate() { # NOT seeded yet (#1729 AC #12). Emit a non-actionable notice — never # the generic re-seed remedy, which can never clear here and would push # a live executing ingress into every new repo ahead of the pilot. - printf '::notice::Template reference %s is expected but intentionally NOT seeded into %s yet (ADR-0007 / #1729) — seed-repo-template.sh deliberately never emits it until the pilot repo collapse lands. This is NOT actionable: do not hand-seed it to silence this. It will be seeded in the same change that retires the per-role stubs it replaces. See AGENTS.md -> Template drift guard.\n' \ - "$file" "$TEMPLATE_REPO" + printf '::notice file=%s::Template reference %s is expected but intentionally NOT seeded into %s yet (ADR-0007 / #1729) — seed-repo-template.sh deliberately never emits it until the pilot repo collapse lands. This is NOT actionable: do not hand-seed it to silence this. It will be seeded in the same change that retires the per-role stubs it replaces. See AGENTS.md -> Template drift guard.\n' \ + "$file" "$file" "$TEMPLATE_REPO" else - printf '::warning::Template stub %s is MISSING from %s (expected blob %s) — re-seed via scripts/seed-repo-template.sh.\n' \ - "$file" "$TEMPLATE_REPO" "${expected:0:12}" + printf '::warning file=%s::Template stub %s is MISSING from %s (expected blob %s) — re-seed via scripts/seed-repo-template.sh.\n' \ + "$file" "$file" "$TEMPLATE_REPO" "${expected:0:12}" fi ;; esac diff --git a/tests/dev-lead/unit/test_channel_pin_guard.bats b/tests/dev-lead/unit/test_channel_pin_guard.bats index 1b4f0a0dc..9c83d0e9d 100644 --- a/tests/dev-lead/unit/test_channel_pin_guard.bats +++ b/tests/dev-lead/unit/test_channel_pin_guard.bats @@ -309,7 +309,7 @@ GHEOF } @test "fix-ci: a refused SHA is terminal for idempotency" { - grep -qE 'status=\(applied\|failed\|no-changes\|refused\)' "$FIX_CI" + grep -qF 'status=(applied|failed|no-changes|declined|refused)' "$FIX_CI" } # --------------------------------------------------------------------------- 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 diff --git a/tests/template_stub_drift.bats b/tests/template_stub_drift.bats index c68210024..e88b287e4 100644 --- a/tests/template_stub_drift.bats +++ b/tests/template_stub_drift.bats @@ -141,30 +141,6 @@ setup() { rm -f "$tsv" } -# #2220: a `file=` annotation lands on the PR's OWN same-named file, so a drifted -# repo-template dev-lead.yml showed up as "this repo's dev-lead.yml drifted" and a -# dev-lead pass repinned this repo's caller stub to clear it. The annotation must -# name repo-template, carry no `file=`, and say this repo's stubs are not the fix. -@test "annotate: a DRIFTED row names repo-template, has no file= anchor, and rules out editing this repo's stub (#2220)" { - tsv="$(mktemp)" - printf '%s\t%s\t%s\t%s\n' ".github/workflows/dev-lead.yml" "DRIFTED" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "$EXPECTED" >> "$tsv" - run template_drift_annotate "$tsv" - [ "$status" -ne 0 ] - [[ "$output" == *"::error::"* ]] - [[ "$output" != *"file="* ]] - [[ "$output" == *"petry-projects/repo-template"* ]] - [[ "$output" == *"not a file in this pull request"* ]] - rm -f "$tsv" -} - -@test "annotate: a MISSING row has no file= anchor either (#2220)" { - tsv="$(mktemp)" - printf '%s\t%s\t%s\t%s\n' "CLAUDE.md" "MISSING" "" "$EXPECTED" >> "$tsv" - run template_drift_annotate "$tsv" - [[ "$output" != *"file="* ]] - rm -f "$tsv" -} - @test "annotate: an all-ALIGNED set passes (exit 0, no error annotation)" { tsv="$(mktemp)" printf '%s\t%s\t%s\t%s\n' ".github/workflows/dev-lead.yml" "ALIGNED" "$EXPECTED" "$EXPECTED" >> "$tsv" @@ -372,7 +348,7 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/workflows/agent-shield.yml" "AHEAD" "$HEAD_BLOB" "$EXPECTED" "HEAD" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" "1234567890abcdef1234567890abcdef12345678" [ "$status" -eq 0 ] # a propagation lag, not corruption - [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] + [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] [[ "$output" != *"::error"* ]] [[ "$output" == *'is ahead of `standards/v1-stable`'* ]] [[ "$output" == *"cut-standards-release.sh"* ]] @@ -397,9 +373,9 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/CODEOWNERS" "DRIFTED" "$HAND_EDIT" "$EXPECTED" "" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" [ "$status" -ne 0 ] - [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] - [[ "$output" == *"::error::Template stub .github/CODEOWNERS in "*"Re-seed it via scripts/seed-repo-template.sh"* ]] - [[ "$output" != *"::error::Template stub .github/workflows/agent-shield.yml in "* ]] + [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] + [[ "$output" == *"::error file=.github/CODEOWNERS::"*"Re-seed it via scripts/seed-repo-template.sh"* ]] + [[ "$output" != *"::error file=.github/workflows/agent-shield.yml"* ]] } @test "annotate: a DRIFTED row tagged ahead-unchecked fails WITHOUT the bare re-seed remedy (#1926)" { @@ -407,7 +383,7 @@ setup() { printf '%s\t%s\t%s\t%s\t%s\n' ".github/CODEOWNERS" "DRIFTED" "$HAND_EDIT" "$EXPECTED" "ahead-unchecked" >> "$tsv" run template_drift_annotate "$tsv" "standards/v1-stable" [ "$status" -ne 0 ] # still drift until proven otherwise - [[ "$output" == *"::error::Template stub .github/CODEOWNERS in "* ]] + [[ "$output" == *"::error file=.github/CODEOWNERS::"* ]] [[ "$output" == *"could NOT be compared against standards HEAD"* ]] [[ "$output" == *"STANDARDS_REF="* ]] [[ "$output" != *"Re-seed it via scripts/seed-repo-template.sh"* ]] @@ -505,9 +481,9 @@ _drv_row() { [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'AHEAD\tHEAD')" ] [ "$(_drv_row .github/workflows/dependabot-automerge.yml)" = "$(printf 'DRIFTED\t')" ] [ "$(_drv_row CLAUDE.md)" = "$(printf 'MISSING\t')" ] - [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "*'is ahead of `standards/v1-stable`'* ]] - [[ "$output" == *"::error::Template stub .github/workflows/dependabot-automerge.yml in "* ]] - [[ "$output" != *"::error::Template stub .github/workflows/agent-shield.yml in "* ]] + [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"*'is ahead of `standards/v1-stable`'* ]] + [[ "$output" == *"::error file=.github/workflows/dependabot-automerge.yml::"* ]] + [[ "$output" != *"::error file=.github/workflows/agent-shield.yml"* ]] } @test "driver: only would-be DRIFTED files pay for a HEAD emission (#1926 AC #1)" { @@ -524,7 +500,7 @@ _drv_row() { run main [ "$status" -eq 0 ] [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'AHEAD\tHEAD')" ] - [[ "$output" == *"::warning::Template stub .github/workflows/agent-shield.yml in "* ]] + [[ "$output" == *"::warning file=.github/workflows/agent-shield.yml::"* ]] [[ "$output" != *"::error"* ]] } @@ -534,7 +510,7 @@ _drv_row() { [ "$status" -ne 0 ] # With nothing newer than N, a file that matches only some other ref is plain drift. [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'DRIFTED\t')" ] - [[ "$output" == *"::error::Template stub .github/workflows/agent-shield.yml in "*"Re-seed it via"* ]] + [[ "$output" == *"::error file=.github/workflows/agent-shield.yml::"*"Re-seed it via"* ]] run grep -c "@${DRV_HEAD_SHA}\$" "$DRV_EMIT_LOG" [ "$output" = "0" ] } @@ -545,7 +521,7 @@ _drv_row() { [ "$status" -ne 0 ] [[ "$output" == *"::warning::Could not resolve the standards default-branch HEAD commit"* ]] [ "$(_drv_row .github/workflows/agent-shield.yml)" = "$(printf 'DRIFTED\tahead-unchecked')" ] - [[ "$output" == *"::error::Template stub .github/workflows/agent-shield.yml in "*"could NOT be compared against standards HEAD"* ]] + [[ "$output" == *"::error file=.github/workflows/agent-shield.yml::"*"could NOT be compared against standards HEAD"* ]] [[ "$output" != *"Re-seed it via scripts/seed-repo-template.sh"* ]] # Files that match the channel are unaffected by the failed lookup. [ "$(_drv_row .github/workflows/auto-rebase.yml)" = "$(printf 'ALIGNED\tN')" ] From c3a7928f014e7dd71d059239274fbeafa82b7f7f Mon Sep 17 00:00:00 2001 From: Don Petry <36422719+don-petry@users.noreply.github.com> Date: Sun, 11 Oct 2026 12:15:04 -0500 Subject: [PATCH 6/6] fix(dev-lead): channel-pin guard accepts a pinned workflow the base branch added cpg_scan_pass skipped the base-branch exemption when a workflow file had no channel pin at the pre-pass head. A pass that merged or rebased onto a base that had added a channel-pinned workflow (or newly pinned an existing one) was reported as drift, so the push was refused, the PR flagged and auto-merge disabled, although the file carried the base's own pins. Apply the same base comparison on that path. A pin that differs from the base's is still drift. Raised by the graphite-app review thread on scripts/lib/channel-pin-guard.sh (PR #2238). AGENTS.md: the template-drift note said the check's annotations no longer carry a `file=` anchor. That change was dropped from this PR and is tracked in issue #2215, so the note now says the anchor is still there. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 6 ++--- scripts/lib/channel-pin-guard.sh | 5 ++++- .../dev-lead/unit/test_channel_pin_guard.bats | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 3bcc1aaca..5ab89356f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -524,9 +524,9 @@ PR-triggered check on the existing Lint workflow — **no new cron/scheduled wor - **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 carry no `file=` anchor. Before #2220 they did, so GitHub pinned them to the PR's own - same-named file, and 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` + 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. diff --git a/scripts/lib/channel-pin-guard.sh b/scripts/lib/channel-pin-guard.sh index d678bef2c..f22d7a748 100644 --- a/scripts/lib/channel-pin-guard.sh +++ b/scripts/lib/channel-pin-guard.sh @@ -123,7 +123,10 @@ cpg_scan_pass() { 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 - if _cpg_has_channel_pin "$after"; 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 diff --git a/tests/dev-lead/unit/test_channel_pin_guard.bats b/tests/dev-lead/unit/test_channel_pin_guard.bats index 9c83d0e9d..1d4247a49 100644 --- a/tests/dev-lead/unit/test_channel_pin_guard.bats +++ b/tests/dev-lead/unit/test_channel_pin_guard.bats @@ -195,6 +195,28 @@ _mk_pr() { [[ "$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