Skip to content

Take a project checkout's base and existing branches from origin when the clone lacks them - #5855

Merged
ymichael merged 1 commit into
get-bb:mainfrom
alexandre-vl:fix/project-checkout-origin-branches
Oct 20, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
alexandre-vl:fix/project-checkout-origin-branches

Conversation

@alexandre-vl

@alexandre-vl alexandre-vl commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

Thanks @ymichael for rebuilding this on main.

What was wrong

A project bb clones onto a machine holds only the remote's default branch, and the picker lists origin's branches there without origin/. The host passed the bare name to git. On main at 81b48d3, after #5856, a new branch from dev fails with fatal: invalid reference: dev, and an existing release with Cannot checkout missing branch release.

What changed

  • The host looks a branch up locally, then as origin/<name> (locateBranch, on the shared hasRef).
  • An existing branch that only origin holds is switched to with git switch --track origin/<name>, so the switch does not depend on checkout.guess.
  • A new branch starts from origin/<base> when no local branch has it. The host reports that ref, and the provider records it as the merge base in place of the requested name. dev does not resolve in that clone, so a recorded dev would leave the thread's diff empty. Preserve new checkout branch comparison bases #5856's other cases are unchanged.
  • A new branch tracks nothing, as the worktree host's worktree add --no-track -B makes one. Otherwise a branch made from origin/dev would take it as its upstream.

How you verified

At ebe71b6:

Fixes #5851

AGENT GENERATED

@github-actions

Copy link
Copy Markdown
Contributor

Thanks @alexandre-vl for the PR. This repo approves new contributors before it reviews their PRs, so this PR now closes on its own. That is expected. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to this PR, and explain what it changes and why. A maintainer then adds you to the allow list and can reopen the PR.

@ymichael

Copy link
Copy Markdown
Collaborator

/approve @alexandre-vl

@github-actions

Copy link
Copy Markdown
Contributor

Approved @alexandre-vl for PRs. The name now stays on the allow list. The author can open a PR now.

@ymichael ymichael reopened this Oct 20, 2026
… the clone lacks them

A project bb clones onto a machine holds only the remote's default branch locally, and the picker names origin's branches there without their origin/ prefix. A new branch from dev failed with "fatal: invalid reference: dev", and an existing release branch with "Cannot checkout missing branch release".

The host now takes each from origin when no local branch has it. An existing branch is switched to tracking origin's, and a new branch starts from origin/<base> and reports that ref as its merge base, which the provider records in place of the requested name. A new branch tracks nothing, as the worktree host makes one.

Fixes get-bb#5851

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the fix/project-checkout-origin-branches branch from 0ea1a79 to ebe71b6 Compare October 20, 2026 20:10
@ymichael
ymichael merged commit 809fea2 into get-bb:main Oct 20, 2026
23 checks passed
@alexandre-vl
alexandre-vl deleted the fix/project-checkout-origin-branches branch October 20, 2026 20:33
ymichael added a commit that referenced this pull request Oct 20, 2026
…ck (#5907)

## Human comments

## What was wrong

#5855 fixed fresh-clone branch resolution, but its integration test only
checked an empty initial diff. That could not prove committed thread
changes remain visible. The README also still described the selected
name as the recorded base rather than the resolved ref.

## What changed

Bring over the remaining test and documentation coverage from #5885:
- Extend the fresh-clone integration test to commit thread.txt and
assert that the diff contains exactly that file.
- Extend the existing provider contract test table to verify that
selecting dev records the host-resolved origin/dev.
- Document local-first branch lookup, origin fallback, tracking
behavior, and the resolved diff base.

No production code or wire contracts change.

## How you verified

- Turbo plugin server tests: 27 passed.
- Turbo project-checkout integration tests: 3 passed.
- Turbo lint/typecheck for the plugin and integration packages completed
successfully.
- Temporarily changed the server to record the requested dev instead of
origin/dev: the provider test failed and the integration test failed
with an empty diff instead of A\tthread.txt\n. Restored production code
byte for byte and reran both suites successfully.
- Formatted all three changed files and ran git diff --check.

Follow-up to #5855; carries over coverage and documentation from #5885.

> AGENT GENERATED
brsbl added a commit that referenced this pull request Oct 20, 2026
#5855 made mergeBaseBranch required in the attach response, and the fake
host rejects replies that omit it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
brsbl added a commit that referenced this pull request Oct 20, 2026
…5913)

## Human comments

## What was wrong

#5855 made `mergeBaseBranch` required in the checkout plugin's attach
response, but two server test stubs still reply without it. The fake
plugin host rejects those replies, so two provider-orchestration tests
fail. Main stayed green because `@bb/server#test` hashes only
`plugins/*/skills/**`, not the plugin sources the tests import, so the
server-2 shard reused a cached result from before the contract change.

## What changed

- The two checkout attach stubs in `provider-orchestration.test.ts` now
return `mergeBaseBranch: null`.
- `@bb/server#test` now hashes all of `plugins/*/**` (except
`node_modules` and `dist`), so a plugin contract change reruns the
server tests.

## How you verified

- Uncached run with only the cache-input fix
([run](https://github.com/get-bb/bb/actions/runs/39497575205)): server-2
missed the cache and failed the same two tests on Ubuntu and Windows,
reproducing the bug on main's code.
- Remote CI on this PR's head passed with the server shards uncached:
`provider-orchestration.test.ts` ran and passed on Ubuntu (1715 tests)
and Windows (1079 tests).
- Slop Cop (security, architecture, code quality, performance): no P0 or
P1 findings.

### Follow-ups (P2, not in this PR)

- `@bb/integration-tests#test` loads built-in plugins from `plugins/*`
too, and its inputs don't include them either.
- Locally, `plugins/*/.turbo/` and `plugins/*/.bundled-runtime/` are
gitignored but hashed by the new glob, so `@bb/server#test` misses the
local cache after any plugin build. CI checkouts are fresh, so CI is
unaffected.
- Plugin-only PRs now schedule the server test shards, which is the cost
of catching contract breaks like #5855's.

BB-Thread-ID: thr_eigbyjruhs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
ymichael added a commit that referenced this pull request Nov 18, 2026
## Human comments

## What was wrong

Choosing between a local branch and `origin/<name>` had no single owner.
The rule "use `origin/<name>` unless the local branch is ahead or has
diverged" existed three times: `resolveDefaultWorktreeBaseBranch` in the
server (`apps/server/src/services/projects/worktree-base-branch.ts`), an
identical copy in the git-worktree plugin, and a third version written
as `merge-base --is-ancestor` in 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:

- **Project checkout bases new branches on a stale local branch.**
Reproduced live on a source dev instance at `1fa400dec78`. In a clone
where `main` is 1 behind `origin/main`, `bb thread spawn
--environment-provider project-checkout --environment-inputs
'{"branch":{"kind":"new","baseBranch":"main"}}'` created
`bb/reply-with-ok-thr_…` at `d167c6c` (local `main`). `origin/main` was
at `bd9d253`, and the environment recorded `mergeBaseBranch: main`. This
is the #1770/#1917 bug in a provider those fixes never reached.
- **The diff cache misses moves of `origin/<base>`.** The host watch set
(`packages/host-workspace/src/git.ts` `fingerprintFor`) covered
`refs/heads/<base>` and `refs/remotes/<base>`. Since #7425, though, the
Git plugin resolves a bare `<base>` against
`refs/remotes/origin/<base>`. For a non-default base such as `release`,
a fetch that moves `origin/release` therefore left the refs fingerprint
unchanged, and the merge base stayed cached.

## What changed

- `packages/environment-provider-host/src/workspace-git.ts` now owns the
rule:
- `resolveBranchRef(cwd, name)` → `{ ref, source: "local" | "origin" |
"as-given" }`. It makes one `for-each-ref` read of the candidates, then
compares them only when both local and origin exist.
- `readDefaultBranchRefs(...).baseBranch` applies the same internal
`chooseBranchRef` to the default branch.
  - `branchRefCandidates(name)` lists the refs the resolver reads.
- All three are exported from `bb-environment-provider-host/git` and
`/workspace-git`. The worktree plugin was the only user of
`readBranchRelation`, so it is deleted.
- Callers moved onto it:
- git-worktree `host/base-branch.ts`: named and default bases.
`fetchFailureFallback` is now `source === "origin"`.
- Git plugin `resolveMergeBaseBranch`: resolve the ref, then one
`rev-parse`.
- project-checkout `planSwitch`: new-branch base. Switching to an
*existing* branch keeps local-first lookup, because that is your own
branch.
- host-workspace `fingerprintFor`: watches
`branchRefCandidates(mergeBaseBranch)`, which now includes
`refs/remotes/origin/<base>`.
- **Server/daemon:** `host.inspect_git_source` returns
`defaultWorktreeBaseBranch`, resolved on the host. The server stops
recomputing it: the route passes the field through and
`projectSourceHasWorktreeBase` reads it.
`apps/server/src/services/projects/worktree-base-branch.ts` and its test
are deleted. `projectBranchesResponseSchema` keeps the same public
`defaultWorktreeBaseBranch` field. **Wire change:
`HOST_DAEMON_PROTOCOL_VERSION` 266 → 267.**
- **Guard:** `bb-environment-provider-host/testing/branch-ref-cases`
provides 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:
- the resolver itself
(`packages/environment-provider-host/test/branch-ref.test.ts`)
- the worktree named and default bases
(`plugins/environment-git-worktree/host/base-branch.test.ts`)
- the Git plugin merge base
(`plugins/git/host/merge-base-branch.test.ts`)
- the project-checkout new branch
(`plugins/environment-project-checkout/host.test.ts`)
- the daemon's `inspect_git_source`
(`apps/host-daemon/test/command/host-branches-dispatch.test.ts`)
- the host-workspace watch set
(`packages/host-workspace/test/shared-refs-branch-ref.test.ts`)
- **Owner guard:** `scripts/check-branch-ref-owner.mjs` runs in the CI
plan job next to the provider-literal ratchet. Source outside
`workspace-git.ts` fails it when it builds `refs/remotes/origin/${…}` or
`` `origin/${…}` `` refs, concatenates `"origin/" +`, or runs
`merge-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:
  - marketplace and plugin-update clones (fresh clones, server-side)
  - bb-app's self-update fast-forward check
  - the branch picker's origin-default pin
- project-checkout's switch to an *existing* branch, which keeps local
first by design

A lint rule would not do this job: CI lints only packages that have a
`lint` script, and `apps/server`, `apps/host-daemon`, `packages/bb-app`
and the two environment plugins have none. Fixture tests are in
`packages/scripts/test/branch-ref-owner.test.mjs`. Against the pre-#6336
worktree copy, the pre-#7425 and #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.ts`
`hasCommitsOnlyOnBranch`) resolves the stored base through
`resolveBranchRef`. Before, a stored bare base such as `main` was read
as-is. If origin squash-merged the branch and its remote branch was
pruned, cleanup compared against the stale local `main` and 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 in `host.test.ts`.
- The host watch set's default-branch refs and project-checkout's
`switch --track` now use `branchRefCandidates` and the full origin ref,
instead of building them by hand.
- Docs: `docs/worktrees.md` still said explicit names are used as-is
(out of date since #6336). The project-checkout README described
local-wins for new branches. Both now describe the shared rule.

### Root cause history

| | Issue | Fixed by | Site patched | Prevented by one resolver + table?
|
|---|---|---|---|---|
| closed | #1770 `--base-branch` resolves the stale local ref | #2152 |
server spawn | Yes. The equal and behind rows. |
| closed | #1917 the same, for named bases in the worktree plugin |
#6336 | worktree plugin | Yes. It re-implemented #2152's rule; the table
fails on the pre-#6336 code (below). |
| — | regression from #6336: fetch failure on an auto-chosen origin |
#6393 | worktree plugin | No. Fetch fallback is separate policy; it now
keys off `source === "origin"`. |
| closed | #5851 project checkout on a fresh clone: `invalid reference:
dev` | #5855 | project-checkout | Yes. The origin-only row. |
| — | diffs against remote-only defaults | #6359 | Git plugin | Yes. The
origin-only row. |
| closed | #7114 Diff tab prefers stale local base | #7425 | Git plugin
(third copy) | Yes. The table fails on the pre-#7425 code (below). |
| unfiled | project-checkout new branch from stale local base | this PR
| project-checkout | Fixed here, reproduced live. |
| unfiled | diff cache misses `origin/<base>` moves | this PR |
host-workspace watch set | Fixed here. |
| open | #3955 worktrees provisioned from stale refs | partly
#6336/#6393 | — | Only the provisioning half. The long-lived-turn drift
half is a separate feature, so this PR does not close it. |
| closed | #5373 marketplace add for a non-default branch | #5375 |
marketplace clone | No. That is a server-owned clone with its own
fallback, deliberately left separate. |

Copies deleted:
- the server's `resolveDefaultWorktreeBaseBranch` and its test
- the worktree plugin's `resolveDefaultWorktreeBaseBranch` and its
named-base `hasRef` logic (#6336)
- the Git plugin's `merge-base --is-ancestor` copy (#7425)
- project-checkout's local-wins new-branch base (#5855's fallback)
- `readBranchRelation` and `revParseRef`

## Decisions for review

1. **Project-checkout new branches start from `origin/<base>` when the
local base is equal or behind.** You will see:
- New branches include upstream commits that your local `main` has not
pulled yet.
- In the common equal case, the recorded merge base and Git panel base
now read `origin/main` instead of `main`. 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.
2. **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

- **Guard on main before the fix** (the same table, production code
untouched):
- project-checkout: `local equals origin` and `local behind origin`
failed (`mergeBaseBranch: "release"`, HEAD at the stale local commit).
- host-workspace watch set: `origin only`, `local equals origin` and
`local behind origin` failed (the fingerprint did not change when
`origin/release` moved).
- worktree and Git plugin: all passed, since their bugs were already
fixed.
- **Guard against the historical bugs:** I temporarily restored the old
logic.
- Pre-#6336 worktree named base (always as-given): `origin only`, `local
equals origin` and `local behind origin` failed.
  - Pre-#7425 Git plugin (local first): `local behind origin` failed.
- **After the fix:** every table row passes at every caller (resolver
14, worktree 14, Git plugin 9, project-checkout 20, host-workspace 8,
daemon inspect 26 including 5 table rows).
- `node scripts/check-branch-ref-owner.mjs` passes, and the fixture
tests pass with it (5).
- Existing suites pass: `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 typecheck` passes on the affected packages, and
oxlint passes on the changed files.
- **Live, on a source dev instance with an isolated data dir:** I
spawned the same project-checkout thread on a fresh clone with `main`
behind `origin/main`.
  - Before (main): branch at `d167c6c` (stale), `mergeBaseBranch: main`.
- After: branch at `bd9d253` (`origin/main`), `mergeBaseBranch:
origin/main`, and `GET /projects/:id/branches` returns
`defaultWorktreeBaseBranch: "origin/main"` from the daemon.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Project checkout on a fresh clone fails for any branch but the default: "fatal: invalid reference: dev"

2 participants