Skip to content

Support existing worktree selection and separate branch picker policy - #3932

Merged
ymichael merged 13 commits into
mainfrom
bb/reuse-worktree-option-disappeared-from-composer-thr_ua4ddg6avu
Sep 19, 2026
Merged

ymichael merged 13 commits into
mainfrom
bb/reuse-worktree-option-disappeared-from-composer-thr_ua4ddg6avu

Conversation

@ymichael

@ymichael ymichael commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The worktree provider could create a new worktree but could not select an existing one. Attaching a path also needed to distinguish an external worktree from one BB already managed: recreating provider metadata for an existing environment could lose its cleanup identity. Meanwhile, the generic branch picker mixed branch selection with worktree-specific default behavior.

What changed

  • Normalize supplied worktree paths and the managed-storage root before matching Git paths. Trailing slashes and symlink aliases resolve correctly while main checkouts and managed worktrees remain excluded from adoption.
  • Add existing-worktree selection to the worktree plugin. Discover worktrees only when that section is selected, scope results to the project and host, and handle loading, errors, retries, and stale responses.
  • Make section selection immediate, support searching paths and branches, and keep picker dimensions stable while loading. Rows show the full path with left truncation and a second line for branch or detached status; the selected value shows parent-dir/dir.
  • Look up an existing environment before provider creation. Preserve ownership, resource metadata, merge base, and cleanup identity on reuse, including the late-binding race. External adopted paths remain externally owned; existing managed worktrees retain their cleanup policy. Reject incompatible managed-path ownership.
  • Keep worktree default-base resolution in the plugin, separate from discovery. Retain explicit local and remote branch choices. The generic branch hook retains its existing return shape; the generic picker uses its label for the heading and its placeholder for an empty selection.
  • Add the optional provider callback experimental_existingPath for reuse before creation, validate its registration, and document it in the Plugin Guide inventory and API audit. Bump the SDK to 0.4.106. There are no server/daemon wire changes.

Composer environment reuse restoration landed separately in #3929; this branch is rebased onto that merge.

How you verified

  • Path normalization: all three new real-Git host regression tests failed before the fix. After the fix, pnpm exec turbo run test typecheck --filter=bb-plugin-environment-git-worktree passed all 95 plugin tests and the plugin typecheck. Coverage includes trailing slashes, symlink aliases, managed-root aliases, and missing/prunable worktrees. git diff --check passed.

  • After rebasing onto Restore the reuse-existing-environment choice in the thread composer #3929: 120 targeted app tests (including composer reuse), all 92 plugin tests, and app/plugin typechecks passed.

  • Turbo plugin tests: 92 passed, covering discovery, selection/search, stale responses, provider adoption, and cleanup. Re-ran the 21 plugin UI tests after the final path-label changes; passed.

  • Turbo targeted server tests: 76 passed across provider orchestration, shared workspace paths, path claims, and managed-storage recognition.

  • Turbo targeted core picker tests: 17 passed. Branch-hook tests also passed during the branch verification.

  • App and worktree-plugin typechecks passed. git diff --check passed.

  • Ran an isolated pnpm start:worktree app and exercised it through the matching BB CLI and browser: adopted and reused an external worktree, reused a managed worktree, compared lifecycle metadata before/after reuse, archived threads and ran cleanup. External files remained; the managed worktree was removed. Verified default creation, foreign-project refusal, ordinary checkout creation, and submitting a thread from the UI.

  • Browser checks confirmed no proactive worktree discovery, immediate mode checkmarks, search/Enter selection, error recovery, two-line rows, left truncation, and the selected parent/directory label. Desktop picker dimensions stayed 288 × 367 across loading and loaded states.

  • Verification covered desktop web and one live host; cross-host response races were covered by component tests. Mobile/native and the full repository test suite were not run.

AGENT GENERATED

ymichael and others added 11 commits September 18, 2026 20:49
Before environments moved into plugins, bb wrote managed workspaces to
`<dataDir>/worktrees` and `<dataDir>/personal-workspaces`, and
`isBbManagedWorkspacePath` tested exactly those two roots. Since #3227
each environment plugin writes under `<dataDir>/plugins/<id>/host-data`,
so the test has matched nothing for ten days.

The guard it feeds refuses a user-supplied path that sits inside bb's
own storage without being a recorded workspace of the asking project —
the case where another project is mid-provision at that path, or where
the directory is an orphan of an environment that was destroyed. Both
`update_environment_directory` and thread creation with an unmanaged
path accept arbitrary paths from a user, and both silently stopped
refusing these.

Match any plugin's process data directory, using the `host-data` /
`bridge-data` convention that `ensurePluginProcessDataDir` already owns,
alongside the pre-plugin roots so existing workspaces keep their
protection.

Repairing the path test alone would have broken every worktree creation.
The provider-create path in the engine runs the same guard against the
path the provider just produced, before that path is bound to its
environment row, so a correct test would refuse bb's own fresh
workspaces. Split the two uses: `foreignProjectOwnedPathRefusal` for a
provider-produced path, which only asks whether another project owns it,
and `suppliedWorkspacePathRefusal` for a path a user chose, which asks
that and the storage question.

Clause two also claimed the path was "owned by another project" when the
orphan case has no owner at all; it now reports what the check actually
knows.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The composer could only ask the worktree provider for a fresh worktree.
A worktree made by hand outside bb was invisible, even though the
provider contract has been able to attach an unowned path since #3227.

Discovery lives in the plugin. A `listWorktrees` host method parses
`git worktree list --porcelain -z` in the project checkout and drops the
main checkout, bare repositories and anything under the plugin's own
worktrees root, so what is left is what bb did not create. A plugin RPC
exposes that list to the plugin's own composer control, which now offers
"New worktree" alongside each discovered worktree. No core change, no
new daemon RPC, no HOST_DAEMON_PROTOCOL_VERSION bump.

The inputs schema becomes a union so `{ kind: "existing", path }` joins
the existing `{ branch }` shape. The branch member keeps its exact old
form, so selections persisted on existing environments still parse.

Creating on an existing path re-resolves it against the repository,
refuses a prunable worktree, claims the path through core, and returns
it with `ownsPath: false` and an `{ adopted: true }` resource. Removal
returns early for that resource, so archiving a thread never deletes a
directory the user made. That early return is the whole safety property
of this change and is tested from both sides.

Verified in the app with a hand-made worktree: it appeared in the
composer, a thread created on it landed on the existing path with
`managed: false`, and archiving that thread left both the directory and
its git registration intact.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Core mints an environment row per thread, then binds it to the path the
provider produced. When the project already has an environment at that
path, `bindEnvironmentPath` discards the new row and hands the existing
one to the thread — but the write that followed still applied the
attaching create's `ownsPath`. Attaching to a worktree bb created and
owns therefore cleared `providerOwnsPath`, and bb stopped cleaning up its
own worktree when the thread was archived.

Apply `ownsPath` only when the bind kept the row core reserved. An
environment's ownership of its directory is settled when it is created;
a later attach has no standing to change it, in either direction — the
opposite mistake would arm bb to delete a directory a user made.

The test drives a provider create onto a path an owned environment
already holds and asserts the row keeps `providerOwnsPath`. It fails
with "expected false to be true" without the fix.

Render the worktree picker the way the project checkout plugin renders
its branch picker: a composer pill built from the shared Popover, Button
and branch-picker primitives, with New worktree and an Existing worktree
section, instead of the bare `<select>` it shipped with. Plugins can
depend on @bb/shared-ui directly, which the checkout plugin already
does.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The control rendered two pills: its own worktree selector and bb's
branch picker beside it. The project checkout provider solves the same
problem with one pill whose popover carries two sections — the mode on
top, the list that mode needs below.

Do the same here. One trigger, "Work in:" offering New worktree and
Existing worktree, then either "Branch from:" with the repository's
branches or "Existing worktree:" with the discovered ones. Branch
options come from experimental_useBranches with the shared search box,
so the branch list keeps its filtering without a second control.

The trigger names the whole selection: "New worktree", "New worktree
from: <branch>", or the chosen worktree's directory. Existing worktree
is disabled, with the reason, when discovery found nothing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The list was fetched once when the control mounted. Branches already
refresh on open, so a worktree added while the composer was up appeared
in one section and not the other until something remounted.

Fetch on open as well, alongside the branch refresh.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Folding the branch choice into one picker changed copy and iconography
that nobody asked to change: the trigger became "New worktree" / "New
worktree from: <branch>" behind a FolderGit glyph, where the branch
picker it replaced read "Branch from: <branch>" behind GitMerge.

Put both back. A reused worktree reads "Reuse: <directory>" in the same
place, since that selection has no base branch to name.

Naming the default branch needs the branch the workspace would start
from, which the plugin could not see; `experimental_useBranches` now
returns `defaultBaseBranch` from the query it already runs, so a control
can label a default selection "Branch from: main" rather than inventing
a word for it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ymichael
ymichael force-pushed the bb/reuse-worktree-option-disappeared-from-composer-thr_ua4ddg6avu branch from 52f7dd6 to 7950cb3 Compare September 19, 2026 03:51
@ymichael
ymichael merged commit 693d61d into main Sep 19, 2026
16 checks passed
@ymichael
ymichael deleted the bb/reuse-worktree-option-disappeared-from-composer-thr_ua4ddg6avu branch September 19, 2026 04:24
ymichael added a commit that referenced this pull request Sep 22, 2026
…ugin branch pickers (#4062)

## Human comments

## What was wrong

When environment selection moved into plugins (#3227 for project
checkout, #3932 for
worktrees), both branch pickers stopped rendering through `BranchPicker`
and silently
dropped behavior that component still implements. Three regressions
followed, all
confirmed by driving the pickers in a dev app at 1440×900, 470×844 and
390×844:

1. **Compact drawer height.** `plugins/environment-git-worktree/app.tsx`
pinned the
option list with an unconditional `h-80`, so the compact drawer measured
a flat
390 × 399 in every state — 399 px of sheet for two rows. Every other
branch picker
uses `min-h-0 max-h-[60vh] … md:max-h-80`, which sizes to content below
`md`.
2. **Search debounce.** Both plugins pass `useDeferredValue(query)`
straight into
`experimental_useBranches`, which is not a debounce. Typing `feature`
issued 7
`GET /api/v1/projects/:id/branch-options` requests — 7 host round trips
running
   `git branch`. `BranchPicker` debounces the same query at 120 ms.
3. **Trigger label.** The worktree chip rendered `Branch from: main` in
one span
instead of marking the prefix `data-promptbox-hide-compact`. In a
promptbox under
30 rem the prefix is supposed to disappear; instead it consumed the
chip, which read
`Branch from: ...` at 470 px and `Branc...` at 390 px, hiding the branch
entirely.

## What changed

- `plugins/environment-git-worktree/app.tsx` — option list `h-80` →
`md:h-80`, so the
compact drawer sizes to content while the desktop popover keeps the
stable 288 × 367
dimensions #3932 introduced. The trigger renders prefix and value as
separate spans,
the prefix carrying `data-promptbox-hide-compact` and the value the
usual emphasis,
  matching the project-checkout chip.
- `apps/app/src/hooks/queries/branch-search-debounce.ts` (new),
`project-queries.ts`, `environment-queries.ts` — the debounce now lives
in the two
  hooks that own branch requests, `useProjectSourceBranches` and
`useEnvironmentMergeBaseBranches`, behind a shared
`useDebouncedBranchSearchQuery`.
A component holding this policy is what let the regression happen:
callers that
stopped rendering `BranchPicker` silently started hitting the host per
keystroke.
At the query boundary no caller can forget it, and the first render is
still
  immediate — only changes wait.
- `apps/app/src/components/pickers/BranchPicker.tsx` — drops its own
debounce and
reports search queries to consumers as they are typed; both consumers
feed them
back into the hooks above. Callers keep filtering cached results off the
immediate
  query, so lists still narrow on each keystroke.

- `packages/plugin-sdk/src/app-contract.ts`, `docs/api_to_audit.md` —
record that
`experimental_useBranches` debounces `query`, since that decides whether
a plugin
should debounce its own search input. No plugin source changed: the
plugins never
held the debounce, they call `experimental_useBranches` and the
implementation is
  app-side, so nothing needs rebuilding against a new SDK.

No wire, CLI, or configuration changes, and no new public plugin API
members.

## How you verified

Measured in an isolated source dev app (fixture repo: 6 branches, 2
existing
worktrees), driven through headless Chromium:

| Check | Before | After |
| --- | --- | --- |
| Worktree drawer, no search match | 390 × 399 | 390 × 266 |
| Worktree drawer, branches listed | 390 × 399 | 390 × 382 |
| Worktree drawer, 2 existing worktrees | 390 × 399 | 390 × 284 |
| Worktree popover, desktop | 288 × 367 | 288 × 367 |
| `branch-options` requests for 7 keystrokes, worktree | 7 | 1 |
| `branch-options` requests for 6 keystrokes, project checkout | 6 | 1 |
| Worktree chip at 470 px | `Branch from: ...` | `main` |

The after values match the project-checkout picker in the same states
(266 px empty,
382 px listed), and the pre-#3932 worktree picker also issued 1 request
for 7
keystrokes.

Tests:

- `usePluginBranchPickerState.test.tsx` and
`environment-queries.test.tsx` — new
debounce tests for both request paths; with the debounce removed they
fail with 8
  and 5 requests instead of 2.
- `plugins/environment-git-worktree/app.test.tsx` — new compact-prefix
test asserting
the prefix span carries `data-promptbox-hide-compact` and the value does
not; the
  existing chip assertions move to the split-span text.
- `pnpm exec turbo run test typecheck
--filter=bb-plugin-environment-git-worktree` —
  96 tests, 5 files, typecheck clean.
- `pnpm exec turbo run test --filter=@bb/app -- src/components/plugin
src/components/pickers src/components/secondary-panel/git-diff
src/hooks/queries` — 850 tests across the pickers, plugin bridge,
git-diff panel, and query hooks.
- `pnpm exec turbo run typecheck lint --filter=@bb/app` — clean.

Not covered: headless Chromium only, so the WebKit/iOS-Simulator drawer
checks the UI
guidance asks for were not run, and the full repository suite was not
run. The
merge-base picker's debounce is covered by the new `environment-queries`
test rather
than a live measurement. Unrelated
and untouched: branch-picker drawers have no bottom safe-area inset
because
`BRANCH_PICKER_CONTENT_CLASS_NAME`'s `p-0` overrides the drawer padding,
which has been
true since #325 for all three pickers.

> AGENT GENERATED

---------

Co-authored-by: Claude Opus 5 (1M context) <[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.

1 participant