Repository navigation
chore: stop concurrent agents colliding over git branches - #2230
Conversation
A branch can be checked out in exactly one worktree. Nothing collected finished agent worktrees, so each one held its branch name hostage: `gh pr checkout` in a sweep/resolve agent aborted outright, and agents improvised local branch names and pushed from those. - Check out PR heads DETACHED (`gh pr checkout --detach`) and push by refspec (`git push origin HEAD:<branch>`). A detached HEAD claims no name, so it cannot collide with a concurrent agent or a leftover worktree. - New `.claude/scripts/gc-worktrees.sh` collects finished worktrees. Four conditions, all required: under .claude/worktrees/, branch on a settled PR with no open PR, clean tree, not locked. Resolves merged-ness from GitHub PR state rather than `git branch --merged`, which is a permanent no-op under squash-merge. - Pins *.sh to LF: under core.autocrlf=true a CR rides into the collector's here-strings and its exact branch-name matching silently never matches. Ports optave/data-analytics-pipeline-svc#651.
Greptile SummaryThe PR prevents concurrent agents from claiming the same Git branch and adds conservative cleanup for completed agent worktrees.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains; both previously reported detached-push state-loss paths are corrected at the current head. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[Agent worktree] --> B[Checkout PR head detached]
B --> C[Validate HEAD against PR head OID]
C --> D[Apply fixes or conflict resolution]
D --> E[Re-derive PR head branch]
E --> F[Push HEAD to explicit remote ref]
G[Housekeep] --> H[Inspect agent worktrees]
H --> I{Scoped, settled, clean, unlocked?}
I -->|Yes| J[Remove worktree]
I -->|No| K[Keep and report]
Reviews (4): Last reviewed commit: "fix(skills): re-derive PR branch name at..." | Re-trigger Greptile |
| # Push BY REFSPEC: Phase 1 checked out detached (so the checkout could not collide with | ||
| # another worktree holding this branch), and a bare `git push origin "HEAD:$HEAD_BRANCH"` from a detached HEAD fails | ||
| # with "You are not currently on a branch". HEAD:<branch> is still a normal | ||
| # fast-forward-only push — a non-fast-forward is rejected exactly as before. | ||
| echo "Pushing to origin/$HEAD_BRANCH..." |
There was a problem hiding this comment.
Fixed — Phase 7 now re-derives HEAD_BRANCH from .codegraph/resolve/head-branch at the top of its own block, matching every other phase's pattern. It previously only restored BASE_BRANCH, so $HEAD_BRANCH was unset by the time this separate Bash invocation ran.
| head=$(gh pr view <number> --json headRefName -q .headRefName) | ||
| oid=$(gh pr view <number> --json headRefOid -q .headRefOid) | ||
| gh pr checkout <number> --detach | ||
| # Fail closed if HEAD is not the PR head (stale fetch, or the head moved as you started). | ||
| [ "$(git rev-parse HEAD)" = "$oid" ] || { echo "ABORT: HEAD is not PR <number>'s head"; exit 1; } | ||
| ``` | ||
|
|
||
| Every later `git push origin "HEAD:$head"` in this skill then becomes `git push origin "HEAD:$head"` (and the commitlint-amend exception, `git push --force-with-lease origin "HEAD:$head"`) — a bare `git push` fails from a detached HEAD with *"You are not currently on a branch"*. Re-read `headRefOid` immediately before pushing; on a live PR the sha you captured here may no longer be the head. |
There was a problem hiding this comment.
There was a problem hiding this comment.
Fixed — every push in /sweep now re-derives the branch name inline via gh pr view <number> --json headRefName -q .headRefName in the same Bash call as the push, instead of relying on a $head shell variable captured in an earlier, separate Bash tool call (which does not persist).
Phase 7 only restored BASE_BRANCH from state, leaving HEAD_BRANCH unset and the push refspec empty on a detached HEAD.
Bash tool calls do not share shell state, so a $head variable captured during Step 2a's detached checkout is gone by the time a later step pushes — the refspec silently expands to the invalid 'HEAD:' and fixes go unpushed. Every push now re-derives the branch name via gh in the same call.
Ports optave/data-analytics-pipeline-svc#651 into this repo, and follows the foundry change in optave/ops-development-tool#32.
The failure
A branch can be checked out in exactly one worktree. Nothing collected finished agent worktrees, so each one held its branch name hostage:
gh pr checkoutin a parallel/sweepor/resolveagent aborts on that, and the agent then improvises a local branch name of its own and pushes from it — unspecified, and easy to get wrong.What changed
.claude/scripts/gc-worktrees.sh.claude/worktrees/, branch on a settled PR with no open PR, clean tree, not locked./sweepgh pr checkout --detach, assertHEAD == headRefOid, push by refspec./resolvegit push origin HEAD:$HEAD_BRANCH./housekeep.gitattributes*.shto LF.Two things reviewers should look at
Squash-merge makes the obvious staleness test a permanent no-op. The natural rule is
git merge-base --is-ancestor <branch> origin/main. Under squash-merge a landed branch's tip is never an ancestor of the default branch, so that test looks right, reads right in review, and collects nothing, forever. The script resolves merged-ness from GitHub PR state instead, and detects the default branch rather than assumingmain.Orphans are reported, not hidden. A deliberate divergence from #651, where a worktree whose branch never had a PR is counted as "still in flight" and never named. Here the dry-run reports it as its own
orphaned (no PR)bucket and lists the branches — those pin their names indefinitely and are usually the residue of exactly the improvised branch names this PR stops agents creating. Raised upstream on #651.Verification
bash -nclean on the script;--dry-runexercised against a live 19-worktree repo (12 collected, 1 in flight, 6 orphaned; every open-PR branch vetoed, no uncommitted work touched).git push. A half-patched skill is worse than an untouched one, and each repo had drifted enough from the common template that exact-text patching left gaps. All repos in this rollout pass.Note
.claude/worktrees/is not cleaned by this PR. Preview withbash .claude/scripts/gc-worktrees.sh --dry-run.