fix(worktree): use hard reset to correct file tree when branch base is wrong#1982
Merged
trek-e merged 2 commits intogsd-build:mainfrom Apr 10, 2026
Merged
Conversation
…s wrong (gsd-build#1981) The worktree_branch_check mitigation detects when EnterWorktree creates branches from main instead of the current feature branch, but used git reset --soft to correct it. This only fixed the commit pointer — the working tree still contained main's files, causing silent data loss on merge-back when the agent's commits overwrote feature branch code. Changed to git reset --hard which safely corrects both pointer and file tree (the check runs before any agent work, so no changes to lose). Also removed the broken rebase --onto attempt in execute-phase.md that could replay main's commits onto the feature branch, and added post-reset verification that aborts if the correction fails. Updated documentation from "Windows" to "all platforms" since the upstream EnterWorktree bug affects macOS, Linux, and Windows alike. Closes gsd-build#1981 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…latform Aligns with the workflow file updates — the EnterWorktree base-branch bug affects all platforms, not just Windows. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
trek-e
approved these changes
Apr 10, 2026
Collaborator
trek-e
left a comment
There was a problem hiding this comment.
Review: APPROVE
Summary
The root cause analysis is correct. git reset --soft moves HEAD but leaves working tree and index unchanged — in a freshly created worktree where no work has been done yet, this means the working tree retains the wrong (main's) files while the commit pointer moves. The subsequent commits are made against main's file tree, causing silent data loss on merge-back.
--hard is safe here precisely because the check runs as the first action before any agent work.
Changes verified
All 4 workflow files updated consistently:
execute-phase.md: Removed the brokengit rebase --ontoattempt, replaced soft reset with hard reset + verification guard (exit 1on failure)quick.md: Hard reset + verify guardexecute-plan.md: Documentation line updated to describe hard reset + verifydiagnose-issues.md: Same patternsettings.md: Windows-specific language removed, cross-platform note added
The post-reset verification ([ "3312de5e35a1066dee6c7752cf30a596bd067e30" != "{EXPECTED_BASE}" ] && exit 1) correctly aborts the agent if the correction fails, preventing work on a wrong base.
No issues found
This was referenced Apr 10, 2026
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.
Summary
Fixes the
worktree_branch_checkmitigation that was usinggit reset --softto correct wrong worktree base branches. The soft reset only moved the commit pointer — the working tree still containedmain's files, causing silent data loss when the agent's commits overwrote feature branch code on merge-back.--softto--hardin all 4 workflow files that spawn worktree agents (execute-phase.md, quick.md, execute-plan.md, diagnose-issues.md). This is safe because the check runs as the very first action before any agent work — there are no uncommitted changes to lose.git rebase --ontoattempt in execute-phase.md that could replaymain's commits onto the feature branch if it succeeded.exit 1if the correction fails, preventing the agent from proceeding on a wrong branch base.EnterWorktreebug affects macOS, Linux, and Windows alike (as reported in Worktree isolation branches from main instead of current branch (macOS, not just Windows) #1957).Why
--softwas wronggit reset --softmoves HEAD but preserves the working tree and index. After the reset:main's files ✗main's files ✗The agent then committed diffs against
main's file state on top of the feature branch history. On merge-back, these commits overwrote the feature branch withmain's code — exactly the data loss reported in #1957.Why
--hardis safe hereThe
worktree_branch_checkruns as "FIRST ACTION before any other work" in a freshly-created worktree. At that point the agent has made zero uncommitted changes and done zero work.git reset --hardcorrectly resets the commit pointer, working tree, and index to the expected feature branch state.Closes #1981
Related: #1957
Test plan
stale-colon-refs.test.cjsunrelated to this change)reset --soft.*EXPECTED_BASEreferences in the codebase/gsd-execute-phasewithworkflow.use_worktrees: trueon a feature branch diverged from main — confirm worktree agents operate on the correct file tree🤖 Generated with Claude Code