Skip to content

Script the Local Half of Post-Merge Cleanup in pr_review.py cleanup #3184

Closed

Description

@ptr727

Problem

pr_review.py cleanup <number> scripts only the remote half of post-merge cleanup: verify-then-delete of the merged head branch on GitHub. The local half is still prose: removing the task worktree, deleting the local task branch, and fast-forwarding the base clone. That prose is repo-worktree "Listing and Cleanup" and drive-pr step 4. It says to run git worktree remove <path> and git branch -D <branch> "from the base clone", but gh-write-guard denies both against a primary checkout.

So a session that follows the skills literally ends with the remote branch gone and the local branch still in the maintainer's primary clone. The maintainer then has to run git branch -D by hand after every drive. That is the outcome the maintainer asked not to have.

A path through the guard does work, but no skill states it, so each session has to rediscover it:

  1. git -C <primary> worktree add --detach <helper> origin/develop, as its own call
  2. git -C <helper> worktree remove <task-worktree>, if it is still registered
  3. git -C <helper> branch -D <task-branch>
  4. git -C <helper> worktree remove <helper>, since a linked worktree can remove itself
  5. git -C <primary> pull --ff-only origin develop, which the guard allows

Proposal

Give pr_review.py cleanup the local half too, so one guard-allowed command (Bash(python3 scripts/pr_review.py:*)) finishes the whole cleanup. It would take a --checkout <primary clone>, or find the clone from the worktree list. Before deleting anything it verifies, from live state:

  • the pull request is merged into develop from a head in the same repository, as the remote half already checks;
  • the local branch tip equals the pull request's headRefOid, so a branch with unpushed commits is never deleted;
  • the task worktree, where one is registered, is clean and resolves to that head.

It then removes the worktree and the branch through a temporary detached helper worktree, removes the helper, and fast-forwards the base clone, stopping and reporting where the base clone is dirty or cannot fast-forward. It reads back each removal, and keeps the existing exit contract: 0 done, 1 refusal, 2 unconfirmed.

repo-worktree "Listing and Cleanup" and drive-pr step 4 then name the command instead of the raw git steps.

Example

With a constructed task example-task merged as pull request 42, pr_review.py cleanup 42 --repo owner/example --checkout ~/repos/example would:

  • delete origin/example-task;
  • remove the example-task worktree;
  • delete the local example-task branch;
  • leave ~/repos/example clean at origin/develop, with git worktree list showing only the primary.

Related: #1682 and #1693 cover the remote-delete half of the same guard and procedure mismatch.

Activity

  1. added
    enhancement
    on Oct 17, 2026
  2. ptr727 commented on Oct 17, 2026

    @ptr727
    OwnerAuthor

    Correction to the premise, measured against the installed gh-write-guard: it does not deny git branch -D <branch> or an unforced git worktree remove <path> against a primary checkout. Rule 6 denies only the forced remove and push. What actually stops an agent finishing the local half is that none of the raw git steps is on the permission allowlist, while python3 scripts/pr_review.py:* is. So an attended agent has to prompt for each step and an unattended one cannot run them. The proposal holds for that reason. The helper-worktree route in the issue body is not needed, since the script runs git itself.

    In progress on feature/agent-cleanup-local-half: cleanup <n> --checkout <primary> for the local half, and cleanup --sweep --checkout <primary> [--apply] for what crashed or abandoned sessions leave behind.

  3. added a commit that references this issue on Oct 17, 2026
    eba7b6a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions