Repository navigation
Resolve local-vs-origin branch refs with one shared resolver - #7719
Merged
ymichael merged 4 commits intoNov 18, 2026
Merged
Conversation
ymichael
force-pushed
the
bb/class-fix-one-shared-git-branch-resolver-thr_6nbskxh9wg
branch
from
November 18, 2026 00:06
599dc52 to
2d81ca0
Compare
The rule "use origin/<name> unless the local branch is ahead or has diverged" lived in three copies (server, git-worktree plugin, Git plugin), and project-checkout used a fourth, local-wins rule for new-branch bases. Export resolveBranchRef and DefaultBranchRefs.baseBranch from bb-environment-provider-host/git, route every caller through it, have the daemon return the resolved default base (protocol 267), and run one shared case table against every caller. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add scripts/check-branch-ref-owner.mjs, run in the CI plan job: source outside workspace-git.ts may not build origin/<name> refs or run merge-base --is-ancestor unless allowlisted with a reason. Owned-branch cleanup now resolves the stored base through resolveBranchRef, so a bare local base that trails origin no longer hides a squash merge. The host watch set and project-checkout's existing-branch switch use the shared candidates and the full origin ref. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
An exact per-file count failed CI on harmless edits to an allowlisted file, and a stale entry failed it when the pattern was removed. Neither failure meant a real bug, so each allowlisted file is now exempt with its reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/class-fix-one-shared-git-branch-resolver-thr_6nbskxh9wg
branch
from
November 18, 2026 04:39
ac9baeb to
47adaa0
Compare
ymichael
deleted the
bb/class-fix-one-shared-git-branch-resolver-thr_6nbskxh9wg
branch
November 18, 2026 04:58
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.
Human comments
What was wrong
Choosing between a local branch and
origin/<name>had no single owner. The rule "useorigin/<name>unless the local branch is ahead or has diverged" existed three times:resolveDefaultWorktreeBaseBranchin the server (apps/server/src/services/projects/worktree-base-branch.ts), an identical copy in the git-worktree plugin, and a third version written asmerge-base --is-ancestorin the Git plugin (#7425). A fourth site, project-checkout's new-branch base, used a different rule: the local branch always won. Each fix touched one site, and the others kept the old behavior. Two bugs are still live on main:1fa400dec78. In a clone wheremainis 1 behindorigin/main,bb thread spawn --environment-provider project-checkout --environment-inputs '{"branch":{"kind":"new","baseBranch":"main"}}'createdbb/reply-with-ok-thr_…atd167c6c(localmain).origin/mainwas atbd9d253, and the environment recordedmergeBaseBranch: main. This is the --base-branch resolves against the stale local ref, so spawned worktrees silently start behind #1770/spawn --base-branch <name> resolves the stale local ref; only remote-prefixed names are fetched #1917 bug in a provider those fixes never reached.origin/<base>. The host watch set (packages/host-workspace/src/git.tsfingerprintFor) coveredrefs/heads/<base>andrefs/remotes/<base>. Since Git: use origin's base branch when the local one only trails it #7425, though, the Git plugin resolves a bare<base>againstrefs/remotes/origin/<base>. For a non-default base such asrelease, a fetch that movesorigin/releasetherefore left the refs fingerprint unchanged, and the merge base stayed cached.What changed
packages/environment-provider-host/src/workspace-git.tsnow owns the rule:resolveBranchRef(cwd, name)→{ ref, source: "local" | "origin" | "as-given" }. It makes onefor-each-refread of the candidates, then compares them only when both local and origin exist.readDefaultBranchRefs(...).baseBranchapplies the same internalchooseBranchRefto the default branch.branchRefCandidates(name)lists the refs the resolver reads.bb-environment-provider-host/gitand/workspace-git. The worktree plugin was the only user ofreadBranchRelation, so it is deleted.Callers moved onto it:
host/base-branch.ts: named and default bases.fetchFailureFallbackis nowsource === "origin".resolveMergeBaseBranch: resolve the ref, then onerev-parse.planSwitch: new-branch base. Switching to an existing branch keeps local-first lookup, because that is your own branch.fingerprintFor: watchesbranchRefCandidates(mergeBaseBranch), which now includesrefs/remotes/origin/<base>.Server/daemon:
host.inspect_git_sourcereturnsdefaultWorktreeBaseBranch, resolved on the host. The server stops recomputing it: the route passes the field through andprojectSourceHasWorktreeBasereads it.apps/server/src/services/projects/worktree-base-branch.tsand its test are deleted.projectBranchesResponseSchemakeeps the same publicdefaultWorktreeBaseBranchfield. Wire change:HOST_DAEMON_PROTOCOL_VERSION266 → 267.Guard:
bb-environment-provider-host/testing/branch-ref-casesprovides one case table on real Git repos: local only, origin only, equal, local behind, local ahead, diverged,origin/<name>,refs/heads/<name>, missing. It also derives the default-branch subset. The same table runs against every caller:packages/environment-provider-host/test/branch-ref.test.ts)plugins/environment-git-worktree/host/base-branch.test.ts)plugins/git/host/merge-base-branch.test.ts)plugins/environment-project-checkout/host.test.ts)inspect_git_source(apps/host-daemon/test/command/host-branches-dispatch.test.ts)packages/host-workspace/test/shared-refs-branch-ref.test.ts)Owner guard:
scripts/check-branch-ref-owner.mjsruns in the CI plan job next to the provider-literal ratchet. Source outsideworkspace-git.tsfails it when it buildsrefs/remotes/origin/${…}or`origin/${…}`refs, concatenates"origin/" +, or runsmerge-base --is-ancestor. Tests and fixtures are exempt. Five files are allowlisted, each with a reason. The allowlist exempts whole files, with no per-file counts, so a harmless edit to an allowlisted file never fails CI:A lint rule would not do this job: CI lints only packages that have a
lintscript, andapps/server,apps/host-daemon,packages/bb-appand the two environment plugins have none. Fixture tests are inpackages/scripts/test/branch-ref-owner.test.mjs. Against the pre-Resolve named worktree bases against origin and reuse non-git parent folders #6336 worktree copy, the pre-Git: use origin's base branch when the local one only trails it #7425 and Git: use origin's base branch when the local one only trails it #7425 Git plugin copies, and the old project-checkout code, the check fails on each file.Owned-branch cleanup (
plugins/environment-git-worktree/host/worktree.tshasCommitsOnlyOnBranch) resolves the stored base throughresolveBranchRef. Before, a stored bare base such asmainwas read as-is. If origin squash-merged the branch and its remote branch was pruned, cleanup compared against the stale localmainand kept the branch. It erred toward keeping, never toward deleting. Origin is only chosen when it contains the local branch, so a resolved base can only reveal merges that really happened. A new test, which fails without the change, is inhost.test.ts.The host watch set's default-branch refs and project-checkout's
switch --tracknow usebranchRefCandidatesand the full origin ref, instead of building them by hand.Docs:
docs/worktrees.mdstill said explicit names are used as-is (out of date since Resolve named worktree bases against origin and reuse non-git parent folders #6336). The project-checkout README described local-wins for new branches. Both now describe the shared rule.Root cause history
--base-branchresolves the stale local refsource === "origin".invalid reference: devorigin/<base>movesCopies deleted:
resolveDefaultWorktreeBaseBranchand its testresolveDefaultWorktreeBaseBranchand its named-basehasReflogic (Resolve named worktree bases against origin and reuse non-git parent folders #6336)merge-base --is-ancestorcopy (Git: use origin's base branch when the local one only trails it #7425)readBranchRelationandrevParseRefDecisions for review
Project-checkout new branches start from
origin/<base>when the local base is equal or behind. You will see:mainhas not pulled yet.origin/maininstead ofmain. The commit is the same.This is what new worktrees already do. Local commits are still kept when the local branch is ahead or has diverged. The provider does not fetch first, so "origin" means the last fetched ref, as before.
Daemon resolves, server passes through. This needed the protocol bump instead of keeping a shared pure function on both sides of the server/daemon line. It means the server's displayed default base is computed by the same code that creates the worktree.
How you verified
local equals originandlocal behind originfailed (mergeBaseBranch: "release", HEAD at the stale local commit).origin only,local equals originandlocal behind originfailed (the fingerprint did not change whenorigin/releasemoved).origin only,local equals originandlocal behind originfailed.local behind originfailed.node scripts/check-branch-ref-owner.mjspasses, and the fixture tests pass with it (5).workspace-git.test.ts,host.test.ts(worktree, 63),workspace-reads.test.ts,git.test.ts,host-workspace.test.ts,contract.test.ts, and the server tests for workspace routing, named base branch, project default and thread default policy.pnpm exec turbo run typecheckpasses on the affected packages, and oxlint passes on the changed files.mainbehindorigin/main.d167c6c(stale),mergeBaseBranch: main.bd9d253(origin/main),mergeBaseBranch: origin/main, andGET /projects/:id/branchesreturnsdefaultWorktreeBaseBranch: "origin/main"from the daemon.🤖 Generated with Claude Code