Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/dev-lead-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/…@<channel>` 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`
Expand Down
2 changes: 2 additions & 0 deletions prompts/dev-lead/fix-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <agent> <version> --channel <name>`; (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
Expand Down
80 changes: 67 additions & 13 deletions scripts/dev-lead-fix-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:-}"
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -219,12 +237,38 @@ $(ssg_declined_body "fix-ci" "$rows")${HOLD_LABEL_NOTE}" \
return 0
}

# fix_ci_channel_pin_guard <pre_pass_sha> [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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
38 changes: 34 additions & 4 deletions scripts/dev-lead-fix-reviews.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading