Repository navigation
fix(git): keep HEAD on a live branch while renaming a worktree branch - #1088
Draft
agent-manager-bots[bot] wants to merge 1 commit into
Draft
agent-manager-bots[bot] wants to merge 1 commit into
agent-manager-bots[bot] wants to merge 1 commit into
Conversation
git branch -m deletes the old ref before it repoints the worktree HEAD. A git command running in the worktree in that gap reads HEAD as a missing branch and fails with "ambiguous argument 'HEAD'". That is what TestRenameWorktreeBranchLetsAgentKeepWorking hit on main CI run 43958080305. The rename now copies the branch (config and reflog come along), locks the old ref at the copied tip, points HEAD at the copy, then deletes the old ref at that tip. HEAD names an existing branch at every step. A commit that moves the old ref before the lock makes the rename stop and drop the copy, so no commit is left behind on the old branch. A reference-transaction hook test checks HEAD after every ref change of a rename. It fails on the old code and passes now.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
7 of 8 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TestRenameWorktreeBranchLetsAgentKeepWorkingfailed on main in run 43958080305 (test-linux, merge of #1063) withfatal: ambiguous argument 'HEAD': unknown revisionfrom the helper agent, and passed on the next main run. The failure is real product behavior that the test catches, so this fixes the rename.Root cause
RenameWorktreeBranchrangit branch -m old new. Git does that in steps: it deletesrefs/heads/old, writesrefs/heads/new, then repoints each worktree HEAD. Between the first and last step the worktree HEAD names a branch that does not exist. Any git command running in that worktree at that moment (the test's agent loop runsgit statusandgit rev-parse HEAD, a real agent does the same) fails. The window is short, so it shows up roughly once in a few hundred CI runs.TestPendingRenameLetsAgentKeepWorkingininternal/executiongoes through the same function with the same agent loop, so it shares this cause.Fix
The rename now:
git branch -c, which carries the branch config and reflog like-mdoesgit update-ref --stdinwithverifyandprepare)git symbolic-refbranch.<old>.*configHEAD names an existing branch at every step. A commit that moves the old ref before the lock makes the rename stop and drop the copy, and the agent's commit stays on its branch. Product behavior is the same: a display-name change still renames the worktree branch.
A commit running at the exact moment of the rename can still be refused with git's usual
cannot lock ref, as it could withgit branch -m. The rename is refused the same way and reported to the session.Tests
New
TestRenameWorktreeBranchNeverLeavesHeadOnAMissingBranchinstalls areference-transactionhook that reads the worktree HEAD after every ref change during a rename. It fails 50/50 on main and passes 50/50 with this change. It also checks the branch config moved and the old config is gone.Stress, same commands before (main at d8d389f) and after:
go test -race -count=200 -run '^TestRenameWorktreeBranchLetsAgentKeepWorking$' ./internal/gitgo test -race -count=100 -run <same> ./internal/gitin parallelgo test -race -count=100 -cpu=1,4 -run <same> ./internal/gitgo test -race -count=100 -run '^TestPendingRenameLetsAgentKeepWorking$' ./internal/execution-count=50git rev-parse --abbrev-ref HEADin the worktree, 5 timesThe plain test loop never hit the window locally, which matches its CI rate. The two amplified runs were throwaway tests and are not part of this PR. The rename was also checked on a reftable repo.
go build ./...,go vet ./...andgo test -race -p 4 ./internal/git ./internal/sessioncmd ./internal/execution ./internal/ui/dialogs/applypass.Not covered: the Windows
Permission deniedon.git/configseen in run 42834593853 comes from config rewrites and is a separate issue.