SuperAPI AI Directory

pr-cleanup

General & other

View repository

Updated 10-15 23:45

About this skill

PR & Worktree Cleanup Sweep (pr-cleanup)

[!NOTE]

This skill targets the personal repository layout (~/github and ~/github/kevmoo/*) and invokes the unified kscripts AOT CLI.

Quick Start & Prerequisites

Ensure kscripts is installed and up to date on PATH. If kscripts is missing, install it from the canonical Git remote:

dart install 'kevmoo_scripts@{git: https://github.com/kevmoo/scripts.dart}'

Thereafter upkeep update dart_install keeps it current -- it re-reads the source descriptor from the installed bundle's own pubspec.lock and reinstalls from the same remote.

[!WARNING]

Do not run dart install ~/github/kevmoo/scripts.dart. Installing from a local path builds whatever branch that checkout is on, and it replaces the app-bundles/kevmoo_scripts/git/<sha>/ bundle with a local/ one -- which permanently repoints upkeep update dart_install at that checkout instead of the remote. Only use a path install to test uncommitted work, and reinstall from Git afterwards.

1. Read-Only Pre-Flight Sweep (Always Start Read-Only)

Never pass --apply on the initial scan. Execute the 3 read-only collectors in parallel to build a unified view of ~/github:

  1. Merged/Closed PRs, Stale Worktrees & Dangling Remote Branches:
    kscripts gh-clean -l 50 --markdown
    
  2. Open Active GitHub PRs & Reviewer/CI Next Steps:
    kscripts gh-view -d 14 --markdown
    
    (Omit -d 14 if the user asks for all open PRs regardless of recency, or adjust -d <days> to match the user's window).
  3. Dart SDK Gerrit Worktrees & CL Status (when ~/github/dart-sdk exists):
    kscripts gerrit-view -p ~/github/dart-sdk
    
    (Automatically resolves ~/github/dart-sdk/core/main/sdk on bare-clone layouts, matches worktrees and local branches via Change-Id: footers, commit SHAs, and branch.*.gerritissue, reconstructs in_reply_to comment chains so only leaf comments are evaluated for resolution, and outputs reviewers, CQ dry run status, and next steps).
  4. Open Assigned GitHub Issues (optional — run when asked for broader backlog/next-steps triage):
    kscripts gh-issues --linked-prs --markdown
    

2. Active Worktree & Session Ownership Cross-Reference

Before proposing to delete any worktree, local branch, or unpushed commit:

  1. Inspect Dirty / Unpushed Worktrees:
    • For any worktree flagged by kscripts gh-clean as having uncommitted changes or unpushed commits, run:
      git -C <worktree_path> status -s
      git -C <worktree_path> log -n 2 --oneline
      
    • Squash-Merge Ancestry Check: If a merged PR branch still reports unpushed commits past PR HEAD, fetch origin/main first and check whether the branch is an ancestor of origin/main:
      git -C <repo_path> fetch origin main --quiet
      git -C <repo_path> rev-list --count origin/main..<branch>
      
      If the count is 0, the branch was reset onto the squash-merge commit and is safe to delete via git -C <repo_path> branch -D <branch>.
  2. Cross-Reference Active Sessions & Local Tasks:
    • Check whether any worktree is actively in use by another running terminal or agent session before proposing deletion.
    • If a worktree belongs to an active session or an open PR/CL, classify it in Bucket A (🚫 DO NOT TOUCH).
    • If a worktree belongs to a waiting/idle session whose PR/CL has already merged, include pruning its VCS state in Bucket B and reconcile any linked local tasks or waiting sessions according to your workspace rules.

3. Three-Bucket Classification & Report

Present the findings in a structured Markdown report (or artifact for large sweeps) divided into 3 buckets:

  • Bucket A — 🚫 Active / Hands-Off (Open PRs, Active Gerrit CLs, In-Flight Worktrees):
    • List open PRs from kscripts gh-view and active Gerrit CLs from kscripts gerrit-view with their CI status, reviewer state (⏳ Awaiting vs 🔔 Ping Reviewer vs 🔄 Re-request Review vs ❌ Action Needed), and local worktree mapping.
    • When kscripts gh-view flags 🔄 **Re-request Review** (@reviewer), that human reviewer previously submitted a review (COMMENTED, CHANGES_REQUESTED, or DISMISSED) and was dropped from GitHub's reviewRequests. Even if an @reviewer PTAL comment was posted, the PR is not in their GitHub Review Queue (review-requested:@me) until re-requested via gh pr edit <PR> -R <owner/repo> --add-reviewer <login>.
    • Three-State CHANGES_REQUESTED Disambiguation (Zero Extra gh Calls):
      1. 🔴 **Changes Requested** (@reviewer): Author has not yet pushed a fix or replied since the review (⚠️ Action Needed — author's ball).
      2. 🔄 **Re-request Review** (@reviewer): Author pushed a fix or replied after the review and resolved threads, but @reviewer was dropped from reviewRequests (⚠️ Action Needed — run --add-reviewer <login>).
      3. 🟡 **Re-review Requested** (@reviewer): @reviewer is already in reviewRequests (🟡 In Review Queue — reviewer's ball). Even though GitHub's raw reviewDecision remains CHANGES_REQUESTED, do not re-run --add-reviewer or run pr-triage. Only if another maintainer has already APPROVED (🟢 Approved (@approver)) and the prior CHANGES_REQUESTED review is a stale veto on an older commit, surface the option (gated via ask_question) to dismiss the stale review.
  • Bucket B — ✅ Safe to Prune Immediately (Merged PRs/CLs & Clean Worktrees):
    • Clean worktrees (git worktree remove), merged local feature branches (git branch -D), dangling remote head branches on writable repos, and trunk fast-forwards (--ff-only).
    • List any linked tasks or waiting sessions whose PRs have landed so they can be reconciled after VCS cleanup.
  • Bucket C — ⚠️ Requires Explicit Confirmation (Closed-Unmerged PRs, Dirty Worktrees, Unpushed Experiments):
    • Detail the exact modified/untracked files (git status -s) or unmerged commits so the user can decide whether to discard or keep each item.

4. Approval Gate & Execution

Gate execution via ask_question with explicit bucket choices:

  • (Recommended) Execute Bucket B only (prune merged PRs, clean worktrees & remote branches)
  • Execute Bucket B + Bucket C (also force-remove dirty/closed-unmerged worktrees)
  • Stop here (keep read-only report only)

Once approved:

  1. Execute Bucket B Pruning:
    • If any merged-PR worktree was held back in Bucket A (because an active session is still using it), run kscripts gh-clean -R <owner/repo> --apply for each approved Bucket B repository individually so the Bucket A worktree is not touched.
    • Only run unscoped kscripts gh-clean -l 50 --apply when zero merged-PR worktrees were reclassified into Bucket A.
  2. Execute Bucket C Pruning (only if Bucket C was explicitly approved):
    • Run git -C <parent_repo> worktree remove --force <worktree_path> and git -C <parent_repo> branch -D <branch>.
  3. Reconcile Linked Tasks & Waiting Sessions:
    • After pruning landed PRs/CLs, verify and close any linked local tasks or notify waiting sessions as configured in your local workspace rules.