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
103 changes: 76 additions & 27 deletions .github/workflows/branch_archive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ name: Branch Archive (to the Gut)
# (the Heart ↔ vitals template). Reimplementing the push would put the Gut's
# namespace convention in two places.
#
# AUTH. One pyautolabs-bot App token per run, scoped to the source repo and
# PyAutoGut with contents + workflows write. workflows: write is the point:
# archived history routinely contains `.github/workflows/*` changes, and
# GitHub refuses any push introducing those without it (the PAT this replaced
# lacked that scope, so such branches could not be archived at all). git
# authenticates through `gh auth setup-git`; no URL carries a credential.
#
# `dry_run: true` resolves, mints, fetches the branch and then runs
# `git push --dry-run` of the real archive ref and of the source delete —
# no pack is sent, no ref changes — and stops. It proves the credential
# end to end without archiving or deleting anything.
#
# Filing the condemned.md entry is deliberately NOT done here — that index is
# Mind state, written by the hygiene conductor's condemn pass with a human
# deciding the transit clock. Some material (original project history) should
Expand All @@ -48,6 +60,10 @@ on:
description: "Delete the source branch once the archive is verified"
type: boolean
default: false
dry_run:
description: "Prove the credential only: push --dry-run the archive ref and the delete, change nothing"
type: boolean
default: false

permissions:
contents: read
Expand Down Expand Up @@ -78,32 +94,25 @@ jobs:
repository: ${{ github.repository_owner }}/PyAutoGut
path: .gut
fetch-depth: 1
# Load-bearing. With the default (true), checkout writes an
# `http.https://github.com/.extraheader` auth header into this
# clone's config, and that header WINS over credentials embedded in
# a remote URL. The archive push then goes out as
# github-actions[bot] — which has no access to a sibling repo — and
# fails 403 no matter what `git remote set-url` was told. Leaving it
# on cost one run to discover; the dry run caught it before any
# delete, which is the only reason that was cheap.
# Keeps checkout's GITHUB_TOKEN auth header out of this clone's
# config, where it would win over the App credential below and send
# the archive push out as github-actions[bot] (no sibling access).
persist-credentials: false

- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install --quiet pyyaml

- name: Archive, verify, then delete
- name: Validate the request
id: req
env:
PAT: ${{ secrets.PAT_PYAUTOLABS }}
REPO: ${{ inputs.repo }}
NAME: ${{ inputs.name }}
run: |
set -euo pipefail
repo='${{ inputs.repo }}'
branch='${{ inputs.branch }}'
name='${{ inputs.name }}'
owner='${{ github.repository_owner }}'

[ -n "${PAT:-}" ] || { echo "::error::PAT_PYAUTOLABS is not set."; exit 1; }
repo="$REPO"
name="$NAME"

# Same boundary as the sweep: a repo the body map does not class as a
# development repo is not one this may touch.
Expand All @@ -119,8 +128,37 @@ jobs:
*[!a-zA-Z0-9._-]*|""|.*) echo "::error::archive name must be a plain slug (a-z 0-9 . _ -)."; exit 1 ;;
esac

src="https://x-access-token:${PAT}@github.com/${repo}.git"
gut="https://x-access-token:${PAT}@github.com/${owner}/PyAutoGut.git"
# The token's scope: the bare name of the (validated) source repo.
echo "source=${repo#*/}" >> "$GITHUB_OUTPUT"

- name: Mint App token (source repo + PyAutoGut, contents + workflows write)
id: app_token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
owner: PyAutoLabs
repositories: |
PyAutoGut
${{ steps.req.outputs.source }}
permission-contents: write
permission-workflows: write

- name: Archive, verify, then delete
env:
GH_TOKEN: ${{ steps.app_token.outputs.token }}
run: |
set -euo pipefail
repo='${{ inputs.repo }}'
branch='${{ inputs.branch }}'
name='${{ inputs.name }}'
owner='${{ github.repository_owner }}'

# gh as git's credential helper for github.com (reads GH_TOKEN).
gh auth setup-git

src="https://github.com/${repo}.git"
gut="https://github.com/${owner}/PyAutoGut.git"
ref="refs/heads/archive/condemned/${name}"

# Refuse to delete a repo's default branch, whatever it is called.
Expand All @@ -130,8 +168,8 @@ jobs:
exit 1
fi

git config --global user.email "actions@github.com"
git config --global user.name "PyAuto Branch Archive"
git config --global user.email "346909844+pyautolabs-bot[bot]@users.noreply.github.com"
git config --global user.name "pyautolabs-bot[bot]"

echo "::group::Fetch $repo@$branch"
cd .gut
Expand All @@ -142,14 +180,25 @@ jobs:
echo " $repo@$branch = $sha ($count commits)"
echo "::endgroup::"

echo "::group::Archive into the Gut"
# Confirm no inherited credential is about to hijack the push. An
# extraheader here means the archive would go out as the workflow
# identity instead of the PAT — the 403 that cost run #1.
if git config --local --get-regexp 'http\..*\.extraheader' >/dev/null 2>&1; then
echo "::error::the Gut checkout carries a persisted credential; it would override the PAT."
exit 1
if [ '${{ inputs.dry_run }}' = 'true' ]; then
echo "::group::Dry run — prove the credential, change nothing"
# --dry-run negotiates with the remote (the write-access check)
# but sends no pack and moves no ref. Workflow-file scope is
# enforced only on a real pack, so this proves contents write.
git ls-remote origin "refs/heads/archive/condemned/*" >/dev/null
git push --dry-run origin "__to_archive__:${ref}"
git push --dry-run "$src" --delete "$branch"
echo "::endgroup::"
{
echo "## Branch archive — dry run"
echo
echo "Credential proven for \`$repo@$branch\` ($sha, $count commits) → \`$ref\` on the Gut and the source delete."
echo "Nothing was archived or deleted."
} >> "$GITHUB_STEP_SUMMARY"
exit 0
fi

echo "::group::Archive into the Gut"
# The Gut owns its own namespace and its own refusal-to-overwrite.
PYAUTO_GUT_REMOTE=origin bin/pyauto-gut archive __to_archive__ "$name"
echo "::endgroup::"
Expand Down
59 changes: 48 additions & 11 deletions .github/workflows/branch_sweep_all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ name: Branch Sweep (org-wide)
#
# WHY CENTRAL. A repo's own GITHUB_TOKEN reaches only that repo, so a per-repo
# sweeper means one workflow file per repo — 19 of them, drifting apart the
# moment one is edited. PAT_PYAUTOLABS already exists for exactly this kind of
# cross-repo work (spawn_drift.yml and arxiv_papers.yml use it), so the sweep
# is written once here and pointed at each repo in turn.
# moment one is edited. Instead the job mints a one-hour token from the org's
# `pyautolabs-bot` GitHub App (installed on every repo, so a repo born
# tomorrow is reachable with no token edit), and the sweep is written once
# here and pointed at each repo in turn.
#
# Audit mode also runs `git push --dry-run origin HEAD` per repo — no pack is
# sent and no ref changes — so an audit dispatch proves the token could
# delete before anyone asks it to.
#
# THE GATE IS STRUCTURAL, NOT PROCEDURAL. Deleting branches across 19 repos on
# one click is not something a report should be able to talk you into, so:
Expand Down Expand Up @@ -121,20 +126,39 @@ jobs:
echo "Mode: $mode"
echo "Targets:"; for t in $targets; do echo " $t"; done

- name: Mint App token (every installed repo — delete branches)
# Owner-wide (no `repositories:`) because the targets are every
# sweepable repo; contents: write is the delete, pull-requests: read
# is branch_sweep.sh's open-PR and squash-merge lookups. Minted
# immediately before the loop: installation tokens live one hour.
id: app_token
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
owner: PyAutoLabs
permission-contents: write
permission-pull-requests: read

- name: Sweep each repo
env:
# Cross-repo work needs more reach than this repo's GITHUB_TOKEN has.
GH_TOKEN: ${{ secrets.PAT_PYAUTOLABS }}
PAT: ${{ secrets.PAT_PYAUTOLABS }}
GH_TOKEN: ${{ steps.app_token.outputs.token }}
run: |
set -uo pipefail
mode='${{ steps.plan.outputs.mode }}'
targets='${{ steps.plan.outputs.targets }}'

if [ -z "${PAT:-}" ]; then
echo "::error::PAT_PYAUTOLABS is not set — cannot reach sibling repos."
exit 1
fi
# gh as git's credential helper for github.com (reads GH_TOKEN):
# clones use plain URLs and no credential is ever written into a
# remote URL or a clone's config.
gh auth setup-git
# Fetches spawn a detached auto-gc/maintenance that can still be
# writing into a clone when the loop removes it ("rm: Directory not
# empty" failed the 2026-09-27 run and the first App-token audit).
# Run it in the foreground instead.
git config --global gc.autoDetach false
git config --global maintenance.autoDetach false

echo "## Branch sweep (org-wide) — \`$mode\`" >> "$GITHUB_STEP_SUMMARY"
echo >> "$GITHUB_STEP_SUMMARY"
Expand All @@ -145,12 +169,25 @@ jobs:
echo "::group::$slug"
# Full history: containment is an ancestry question and a shallow
# clone answers it wrong in the safe-looking direction.
if ! git clone --quiet "https://x-access-token:${PAT}@github.com/${slug}.git" "work/$name"; then
if ! git clone --quiet "https://github.com/${slug}.git" "work/$name"; then
echo "::warning::clone failed for $slug — skipping"
printf '### %s\n\n_clone failed — skipped_\n\n' "$slug" >> "$GITHUB_STEP_SUMMARY"
failed=1; echo "::endgroup::"; continue
fi

# Audit proves the delete credential without using it: an
# up-to-date `push --dry-run` still needs the receive-pack
# handshake, which GitHub grants only to a token that can write.
if [ "$mode" = "audit" ]; then
if git -C "work/$name" push --dry-run --quiet origin HEAD; then
echo "push credential: ok (push --dry-run)" | tee -a "work/$name.cred"
else
echo "::error::the App token cannot push to $slug — a delete would fail"
echo "push credential: REFUSED (push --dry-run)" >> "work/$name.cred"
failed=1
fi
fi

bin/branch_sweep.sh \
--repo "$PWD/work/$name" \
--owner "${slug%%/*}" \
Expand All @@ -161,10 +198,10 @@ jobs:

{
printf '### %s\n\n```\n' "$slug"
[ ! -f "work/$name.cred" ] || cat "work/$name.cred"
cat "work/$name.log"
printf '```\n\n'
} >> "$GITHUB_STEP_SUMMARY"
# The clone carries a credentialed remote; do not leave it lying about.
rm -rf "work/$name"
echo "::endgroup::"
done
Expand Down
11 changes: 9 additions & 2 deletions skills/repo_cleanup/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ workflow `GITHUB_TOKEN` that does have `contents: write`:
sweep themselves with their own `GITHUB_TOKEN`.

**Every other development repo** is swept centrally instead, by PyAutoBrain's
`branch_sweep_all.yml` using `PAT_PYAUTOLABS`. Same dispatch/poll/report loop,
`branch_sweep_all.yml`, which mints a one-hour `pyautolabs-bot` GitHub App
token (contents write on every installed repo, so a new repo needs no token
change). An audit run also `push --dry-run`s each repo, proving the delete
credential without using it. Same dispatch/poll/report loop,
with `repos` naming the targets. Two things about it are not negotiable from
the chat side:

Expand Down Expand Up @@ -191,7 +194,11 @@ Disjoint or unpinned → route it to the Gut instead of the delete bucket:
dispatch `branch_archive.yml` (PyAutoBrain) with `repo`, `branch`, `name`, and
`delete_after`. It archives via `pyauto-gut archive`, **verifies the ref landed
on the Gut with a fresh `ls-remote`**, and only then deletes the source branch —
never on the strength of the push's own exit code.
never on the strength of the push's own exit code. It authenticates with a
`pyautolabs-bot` App token scoped to the source repo and PyAutoGut, carrying
workflows write, so branches whose history touches `.github/workflows/` archive
too. `dry_run: true` proves the credential (`push --dry-run` of the archive ref
and the delete) and changes nothing.

It does not file the `condemned.md` entry. That is Mind state and a judgement:
material like original project history should be held **undated** (no
Expand Down
Loading