Repository navigation
[CI] Satya/stale branch archive - #5617
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
🟡 Changes recommended
The workflow permits shell injection, while race conditions and partial API failures can lose commits or leave cleanup permanently stuck.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds an opt-in weekly workflow to archive stale branches, notify owners, and later delete archived refs.
Changes:
- Adds GitHub Actions scheduling, dry-run controls, and action limits.
- Implements branch lifecycle management through GitHub APIs.
- Adds checked-in branch exemption patterns.
File summaries
| File | Description |
|---|---|
.github/workflows/stale-branches-archive.yml |
Runs the stale-branch lifecycle workflow. |
.github/scripts/archive_stale_branches.py |
Implements scanning, archiving, notices, and deletion. |
.github/stale-branch-exemptions.txt |
Defines protected branch names and patterns. |
Review details
Suppressed comments (1)
.github/scripts/archive_stale_branches.py:321
- [verified]
nameis interpolated raw into the REST URL. Git permits#in a branch name such asfeature/#42, buturllibtreats#42as a URI fragment, so this DELETE reaches the wrong ref and leaves cleanup stuck; the same construction is used for the live-branch delete at line 225. Author must URL-encode ref path components before constructing REST paths.
api.write("DELETE", f"/git/refs/heads/{name}")
- Files reviewed: 3/3 changed files
- Comments generated: 4
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
The cleanup can misidentify shared-commit markers and delete incorrectly encoded or concurrently changed refs.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
.github/scripts/archive_stale_branches.py:219
- This endpoint is paginated, but the helper reads only page 1. Once a commit has more than 100 comments and the bot marker is outside that page, the script treats the marker as absent, so it can repost archive/notice comments and fail to honor the intended notice clock. Author must follow the pagination links (or otherwise search all comment pages) before deciding that a marker is missing.
- Files reviewed: 3/3 changed files
- Comments generated: 4
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Destructive branch operations have unresolved pagination, exemption-race, restore, and failure-handling issues.
Review details
Suppressed comments (2)
Previously missed (2) — in code that hasn't changed since the last review.
.github/scripts/archive_stale_branches.py:256
- [verified] This only reads the first 100 commit comments. Once a commit has more comments, the archived/notice marker may be on another page, so the next run can repost notices indefinitely (or fail to find provenance and strand an archive). Author must paginate this endpoint before deciding a marker is absent.
.github/scripts/archive_stale_branches.py:395 - [verified] A user following the notice's "restore the branch" command recreates the original ref at the old archived commit, whose committer date is still more than 30 days old. On the next run this path archives/deletes that restored ref again, so the advertised one-command rescue does not keep it unless the user also creates a new commit or edits the exemption list. Author must detect a restored archive or change the flow/message so restoring the ref actually opts it out.
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
There was a problem hiding this comment.
🟡 Changes recommended
The destructive workflow has confirmed lifecycle, security, and archive-safety issues.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (3)
Previously missed (2) — in code that hasn't changed since the last review.
.github/scripts/archive_stale_branches.py:428
- [verified] Restoring the branch with the command printed in the notice recreates it at the old archived commit, but this live-branch path only checks commit age. On the next weekly run that still-old tip is at least
--stale-daysold, so it is moved to a new archive and deleted again; the advertised restore does not keep the branch. Author must recognize a restored/provenance-marked live branch (or require a new commit or explicit exemption) before re-archiving it.
.github/scripts/archive_stale_branches.py:460 - [verified] Deleting the notice comment does not stop deletion: once
noticeis absent and the archive is at least 30 days old, this path callsgive_noticeagain on the next run, and the later age check can delete the archive. That contradicts the documented per-branch opt-out and “only happens while it stands” message. Author must add a durable opt-out signal or change the lifecycle and user-facing contract so a deleted notice cannot be silently recreated.
.github/scripts/archive_stale_branches.py:365
- [verified] The notice's restore command has the same shell-injection problem: the original branch name is emitted raw into a copy-paste shell command. A branch containing shell metacharacters can change what the maintainer executes when following the notice. Author must emit shell-quoted refs here as well as in the archive comment.
f"```\ngit push origin {branch['name']}:{original}\n```\n\n"
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Lite
There was a problem hiding this comment.
🔵 Needs a closer look
Branch URL handling, fork PR detection, comment pagination, and the documented notice opt-out currently have correctness issues.
Review details
Suppressed comments (5)
Previously missed (1) — in code that hasn't changed since the last review.
.github/scripts/archive_stale_branches.py:241
head.refis only a branch in this repository when the PR head repository is this repository. As written, an open PR from a fork named (for example)feature/fooexempts an unrelated localfeature/foo, potentially preventing its cleanup for as long as the fork PR remains open. Add the head-repository check before addinghead.ref; keep the base branch exemption unconditional.
This issue also appears in the following locations of the same file:
- line 248
- line 268
- line 284
- line 468
.github/scripts/archive_stale_branches.py:248
- The GitHub ref endpoint requires the slash in a branch name to be URL-escaped, but
safe='/'leaves it as a path separator. For a normal branch such asfeature/foo, this can makeref_shareturn 404/None, so every archive attempt is reported as having moved and the branch is never processed. Encode the branch component withsafe=''.
ref = api.get(f"/git/ref/heads/{urllib.parse.quote(name, safe='/')}")
.github/scripts/archive_stale_branches.py:268
- The branches endpoint treats
{branch}as one path parameter, so leaving/unescaped breaks common names such asfeature/foo.still_exemptthen treats the live branch as missing andarchive()leaves it untouched, while archived branches with slashes cannot pass the same guard. Quote the branch name as a single path component.
branch = api.get(f"/branches/{urllib.parse.quote(name, safe='/')}")
.github/scripts/archive_stale_branches.py:472
- The notice text promises that deleting this comment is the per-branch opt-out, but the next run enters this block again once the archive is old enough and posts a fresh notice. The archive will therefore still be deleted after another notice period unless the user keeps deleting comments; either persist an opt-out or change the documented behavior to a reset/pause. Author must make the implementation and contract agree.
if notice is None:
if (today - archived_on).days < args.archive_days:
return None
return give_notice(
api, branch, original, today + dt.timedelta(days=args.notice_days)
.github/scripts/archive_stale_branches.py:284
- This only reads the first 100 commit comments. The workflow deliberately puts markers on commits, and many branch refs can share one commit, so a marker beyond page 1 is not found: archive comments can be duplicated and notice/deletion processing can be skipped or restarted on every run. Paginate the endpoint before searching for markers.
return api.get(f"/commits/{sha}/comments?per_page=100") or []
- Files reviewed: 3/3 changed files
- Comments generated: 0 new
- Review effort level: Lite
ROCm/aiter PR #5617 — [CI] Satya/stale branch archiveAdds an opt-in weekly workflow that moves branches with no commits for 30 days to archive/<date>/<name> refs, gives notice, then deletes the archive ref two weeks later, skipping protected branches, open-PR refs and a checked-in exemption list. Review (advisory): 🔴 HIGH RISK 🔴 [verified] archive_stale_branches.py:468-473 re-posts a fresh delete notice whenever the notice comment is absent and the archive is past 30 days, so the per-branch opt-out promised by the module docstring (lines 11-13, "deleting that comment stops the deletion") and by the notice text itself (lines 379-380, "the deletion only happens while it stands") does not exist — a user who deletes the notice per its own instruction gets a new one the next Monday and the archive ref is deleted 14 days later (confirmed by driving the script against a mock API: run 2 re-noticed, run 3 deleted). Author must make a missing notice on an archive past archive-days mean opted-out, or rewrite the docstring and notice text to say the clock restarts. |
fixed |
There was a problem hiding this comment.
🔵 Needs a closer look
The destructive ref lifecycle has unresolved concurrency and API-rate-limit risks.
Review details
Suppressed comments (1)
.github/scripts/archive_stale_branches.py:394
- [verified] The delete is leased only against the original ref. After
archive/date/nameis created atsha, another writer can move or delete that ordinary branch before_finish_archive; this code will still delete the original and leave no archive of that commit. Author must make archive refs immutable/protected or use an atomic compare-and-swap protocol that verifies the archive before deleting the source.
reason = still_exempt(api, name)
if reason:
return f"archived {name} -> {target}; original kept, {reason}"
if api.delete_branch(name, sha):
return f"archived {name} -> {target}"
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Slash-containing branch names are mishandled, and pre-existing same-SHA archive refs can be incorrectly adopted and deleted.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 3
Weekly, off until STALE_BRANCHES_ARCHIVE is 'true'. A branch with no commits for 30 days is copied to archive/<date>/<name> and its ref removed; 30 days later a deletion notice is posted; 14 days after that the archive ref is deleted. 74 days end to end, restorable at any stage with git push origin archive/<date>/<name>:<name>, which both bot comments print. No stored state: the archive date is in the ref name, the notice date is the bot's notice comment. Deleting that comment opts a branch out. Only refs this workflow made are managed -- the bot's archiving comment naming the ref is the provenance -- and markers name their ref, so two branches on one commit cannot share a clock. Deletion is a leased git push (--force-with-lease), the only server-side compare-and-swap available; protection and open-PR head/base membership are re-checked immediately before it. Exemptions are the checked-in .github/stale-branch-exemptions.txt, consulted at every stage against the original name, so a line added later rescues an already-archived branch. The job runs the policy from the default branch only, and --max-actions caps a run at 50 so the ~700 branches already past 30 days drain over weeks. Verified against a fake repository: staleness, notice and deletion thresholds either side of the boundary; strangers' archive-shaped refs and impossible dates ignored; a collaborator's pasted notice ignored; two refs on one commit kept apart; a branch that moved, or became a PR base, mid-run left alone; a push failure or an unexpected 422 leaves the branch intact and the job red; restore commands survive shell metacharacters in a ref name.
5a5cdd2 to
3f592e8
Compare
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Branch encoding, restoration, opt-out, and comment pagination bugs can prevent or repeat destructive lifecycle actions.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 4
|
LGTM to except do you want to address copilot comments? |


Weekly, off until STALE_BRANCHES_ARCHIVE is 'true'; until then every run reports and writes nothing.
A branch with no commits for 30 days is copied to archive// and its ref removed. 30 days later a deletion notice is posted; 14 days after that the archive ref is deleted. 74 days end to end, restorable at any stage with git push origin archive//:, which both bot comments print.
No stored state: the archive date is in the ref name, the notice date is the bot's comment. Deleting that comment opts a branch out. Only refs this workflow created are managed — its own archiving comment is the provenance — and markers name their ref, so two branches on one commit can't share a clock.
Deletion is a leased git push, the only server-side compare-and-swap available; protection and open-PR head/base membership are re-checked immediately before it. Exemptions are the checked-in .github/stale-branch-exemptions.txt, consulted at every stage against the original name, so a line added later rescues an archived branch without restoring it. The policy runs from the default branch only; --max-actions caps a run at 50 so the ~700 branches already past 30 days drain over weeks.