sync records a layer base from a stale local trunk it could not fast-forward, and the next sync replays trunk commits
Version: observed with gh-stack v0.1.0; the code quoted below is from the v0.1.1 tree, where the same paths are present. Git worktree layout: the trunk (main) checked out in the primary working tree, the stack branches in a linked worktree.
Setup
- A stack
main <- layer-1 <- layer-2 <- layer-3, submitted, PRs open. - The primary working tree has
mainchecked out and behindorigin/main. - Squash-merge layer-1's PR on GitHub, so
origin/mainadvances.
Steps and observed output
-
From the stack worktree, run
gh stack sync. The trunk fast-forward fails:UpdateBranchRef(internal/git/gitops.go) runsgit branch -f main <sha>, which git refuses whilemainis checked out in the primary tree:⚠ Could not update local main: failed to run git: fatal: cannot force update the branch 'main' checked out at '<primary tree>' Rebasing the stack onto origin/main instead; local main is unchanged.The cascade rebase onto
origin/mainsucceeds. This first sync works becauseresolveRebaseOldBasestill accepts the merged layer's old tip as the rebase boundary. AfterwardsupdateBaseSHAs(cmd/utils.go) records layer-2'sbaseby resolving the trunk branch name, which is the stale localmainSHA.canUpdateBaseaccepts it because that SHA is an ancestor of the rebased layer. -
Commit on layer-2, then run
gh stack syncagain.
Actual
✗ Conflict detected rebasing layer-2 onto origin/main
All branches restored to their original state.
gh stack rebase then stops on conflicts in the files the trunk changed since the stale SHA (changelogs and every workspace manifest in our repo), and git log -1 REBASE_HEAD shows a trunk commit (a release commit), not a stack commit.
Cause of the second failure: after step 1's rebase the merged layer's old tip is no longer an ancestor of layer-2, so resolveRebaseOldBase falls through to the recorded base and the cascade runs git rebase --onto origin/main <stale local main> layer-2, replaying every commit the trunk gained since that SHA (18 in our case).
Expected
Either record the base as the SHA the layer was actually rebased onto (the trunk target's SHA, already carried in trunkTarget on the fallback path), or have resolveRebaseOldBase reject a recorded base that is also an ancestor of newBase, since such a boundary can only replay trunk commits.
Workaround
Fast-forward the local trunk before syncing, so sync prints Trunk main fast-forwarded instead of the fallback line. After the stale base is already recorded: gh stack rebase --abort, then a manual git rebase --onto <new parent tip> <old parent tip> <branch> per layer and a lease push; the stack linkage on GitHub survives.
Related
#169 and #485 look like the same symptom class after a squash merge. This report isolates the worktree-pinned-trunk path that writes the stale base.
might be relevant, might not, don't have time to verify - claude got a little eager and filed the issue on my behalf (sorry)