Skip to content

Restore the reuse-existing-environment choice in the thread composer - #3929

Merged
ymichael merged 7 commits into
mainfrom
bb/split-composer-reuse-restoration-into-its-own-pr-thr_2iiuw7rwx5
Sep 19, 2026
Merged

ymichael merged 7 commits into
mainfrom
bb/split-composer-reuse-restoration-into-its-own-pr-thr_2iiuw7rwx5

Conversation

@ymichael

@ymichael ymichael commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The environments-into-plugins migration (#3227) replaced the composer's fixed workspace rows with plugin-provided environment provider rows, and the "Existing worktree" row was dropped along with them. ReuseEnvironmentPicker only renders once the selection is already a reuse value, so reuse:<id> became unreachable from the root composer — reuse mode could only be entered when a thread seeded it (the thread "+" button or the workspace header). Choosing an existing environment for a new thread from the root composer was impossible.

This PR is the reuse-restoration half of a branch split; the external Git worktree adoption work ships separately.

What changed

App-only, seven files under apps/app/src, no SDK/server/daemon/wire changes.

  • components/pickers/EnvironmentPicker.tsx — adds an optional onSelectReuse prop and a ReuseEnvironmentOption row ("Reuse existing", REUSE_ENVIRONMENT_ICON_NAME) rendered in its own CommandGroup below the provider rows, separated by a CommandSeparator. The row is omitted entirely when onSelectReuse is not wired, and marked selected while the active value parses as type: "reuse". The label shares its verb with the selected-state pill ("Reuse") and the reuse dropdown heading ("Reuse an existing environment") so the three surfaces read as one option. The row is always offered rather than disabled-when-empty: the reuse dropdown already explains an empty project, and a disabled row is a dead end.

  • components/pickers/EnvironmentPicker.tsx (menu width) — the popover was pinned to w-80 for every variant. Every variant now sizes to content between min-w-52 and max-w-80, including the machine-search menu: the search input stretches to whatever width the menu has, so there was no reason to pin it. Measured across the picker stories the searching menu settles at 208–309px instead of a flat 320px, and holds its width while results filter because the floor covers the search placeholder. This matches ModelReasoningPicker, which already sizes to content. With the floor unconditional, the isLoading arm that re-applied min-w-52 became dead and was removed.

  • components/promptbox/NewThreadComposer.tsx — handleSelectReuse sets REUSE_VALUE_WITHOUT_ENVIRONMENT through the existing changeEnvironment path.

  • components/promptbox/NewThreadPromptBox.tsx — threads onSelectReuse through NewThreadEnvironmentConfig to EnvironmentSlot.

  • views/root-compose-environment-selection.ts — resolveRootComposeEffectiveEnvironmentValue no longer rewrites a pending reuse (null environment id) selection to the fallback value when the project has nothing to reuse. Selecting "Reuse existing" now holds reuse mode and shows the "Pick an environment" dropdown instead of silently snapping back to the default provider.

  • components/promptbox/NewThreadEnvironmentOptions.stories.tsx — wires onSelectReuse into the strip and drops the now-obsolete "reuse unavailable" story row.

  • components/pickers/EnvironmentPicker.stories.tsx — no fixture drove the machine-grouped menu with onSelectReuse, the combination the real composer always produces, so the row's placement below every machine group, its selected state, and its behaviour beside a disabled offline machine had no visual coverage. Adds four stories over the existing machine fixtures: reuse offered, reuse selected, offline machine, and the machine-search variant.

  • Tests: EnvironmentPicker.test.tsx and RootComposeView.test.ts (see below).

  • components/pickers/ReuseEnvironmentPicker.tsx — the reuse dropdown rendered every option in a plain DropdownMenu with no filter. Replaced with the cmdk Command the other pickers use: a search input appears once the list reaches seven entries, and each whitespace-separated term is matched against the environment label, name, branch, path, machine name, and the titles of the threads living in it.

  • views/root-compose-environment-selection.ts + NewThreadComposer.tsx — "New thread in environment" seeds reuse:<id> directly, but the resolver only kept such a value when the id appeared in the thread-derived option list. That list comes from the sidebar's non-archived threads, so a seeded environment that no live thread points at was silently replaced by the default provider, and knowing the exact id saved no work. The composer now resolves that id through the existing GET /environments/:id query (no new route), only when it is absent from the thread-derived list: the selection is held while the lookup is pending, kept once the environment returns, and dropped only when it is gone or destroyed. The resolved environment is appended to the picker options so the trigger shows its real label instead of "Pick an environment".

  • components/pickers/ReuseEnvironmentPicker.stories.tsx — new stories for the empty, short, selected, disabled, and searchable states.

How you verified

Tests added, all failing before these changes and passing after:

  • EnvironmentPicker.test.tsx — the row is offered alongside the providers and invokes onSelectReuse; the row reports aria-current="true" while a reuse:<id> value is active; the row is omitted when onSelectReuse is not supplied.
  • RootComposeView.test.ts — resolveRootComposeEffectiveEnvironmentValue holds "reuse" both with an empty reuseThreadOptions list and with one available, rather than falling back. Four more cover the seeded path: a seeded environment absent from the thread-derived list is kept, a pending lookup holds the selection, a missing environment falls back, and a lookup for a different id does not rescue an unrelated reuse value.
  • ReuseEnvironmentPicker.test.ts — filterReuseThreadOptions returns everything for a blank query, matches branch/thread-title/machine-name, ignores case, requires every whitespace-separated term to match, and returns nothing when no option matches.

Commands run from the worktree:

  • pnpm exec turbo run typecheck lint --filter=@bb/app — 5/5 tasks successful, Found 191 warnings and 0 errors. Warning count and the per-file NewThreadComposer.tsx warning count (4) are identical to origin/main with these files reverted, so no new lint warnings are introduced. The preserve-manual-memoization warning on that hook is pre-existing (inferred setCommandState, unrelated to the added dependency).
  • pnpm exec turbo run test --filter=@bb/app -- --run src/components/pickers/EnvironmentPicker.test.tsx src/views/RootComposeView.test.ts src/components/pickers/ReuseEnvironmentPicker.test.ts — 3 files, 110 tests passed.

Visual check via Ladle (promptbox--environment-options--overview) in headless Chrome:

  • Opening the project checkout picker lists Project checkout (current), Worktree, then Reuse existing below a separator, with aria-disabled="false" on the reuse row.
  • The menu measures 208px wide instead of the former fixed 320px.
  • Swept every picker story and measured the popover: grouped menu 208px, grouped menu with reuse 253px, offline machine 309px, machine-search 208px — all previously a flat 320px. Typing into the machine search and clearing it again held 208px at every step, so content sizing does not make the menu jitter while filtering. The loading menu still measures 208px with the isLoading width arm removed.
  • The "project · reuse empty" row renders Reuse + Pick an environment rather than reverting to a provider, confirming reuse mode is held before an environment is chosen.

Reuse-picker search driven in headless Chrome against its new story: with eight environments the search input appears; payment narrows to the one matching on branch and thread title; build-box narrows to the two on that machine; zzzz renders the "No environments match." empty state. With three environments no search input renders, selecting a row closes the menu, and the empty-options variant shows only "Nothing to reuse yet.".

AGENT GENERATED

ymichael and others added 7 commits September 18, 2026 19:19
The environments-into-plugins migration (#3227) replaced the composer's
fixed workspace rows with plugin-provided environment provider rows. The
"Existing worktree" row was dropped with them, so `reuse:<id>` values
became unreachable from the picker: the ReuseEnvironmentPicker only
renders once the selection is already a reuse value, which now only
happens when a thread seeds it (the thread "+" button or the workspace
header). Reusing an environment from the root composer was impossible.

Add the row back as "Reuse existing" rather than "Existing worktree",
since environments are no longer worktree-coupled, and select any
provider's environment through the existing reuse selection. The label
shares its verb with the selected-state pill ("Reuse") and the reuse
dropdown's heading ("Reuse an existing environment"), so the three
surfaces read as one option. The row sits in its own section below the
provider rows and is disabled with "No environments to reuse yet" when
the project has nothing to reuse, matching the pre-migration affordance.

Verified in the app: with a project that has a live git-worktree
environment, the picker offers the row, selecting it reveals the
environment dropdown, and submitting creates a thread on the existing
environment id rather than provisioning a new one. A project with no
environments shows the row disabled with its reason.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The popover primitive defaults to `w-96`, and the environment menu has
only ever clamped it: `max-w-80` capped it at 320px, and #3563's switch
to `w-80` replaced it with the same 320px. Either way a three-row
provider menu rendered as wide as the machine search menu it never
shows.

Override the primitive's width instead of clamping it, and keep the
fixed 320px only for the searchable multi-machine menu, where a
content-sized box would resize as the machine list filters.

Measured in Ladle: the provider menu drops from 320px to its 208px
`min-w-52` floor, the machine-grouped menu is also 208px with host
names, status dots and provider rows still on one line each, and the
searchable menu stays at 320px.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Enablement was derived from the sidebar thread payload: the row went
disabled whenever no thread in the project currently pointed at an
environment. That answered the wrong question. It read as empty while
the sidebar query was still settling, and it read as empty for a project
whose worktrees are all still on disk but whose threads have been
archived.

Always offer the row. The reuse dropdown behind it already renders
"Nothing to reuse yet." for the genuinely empty case, so the empty state
keeps a home and the row stops making a claim it cannot support.

The list itself is still thread-derived, so an environment whose threads
are all archived remains absent from the dropdown. Fixing that needs a
project-scoped environments query and is left for separate work.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Selecting the reuse row on a project with nothing to reuse did nothing
at all: core resolved a bare `reuse` value back to the project-checkout
fallback whenever the option list was empty, so the menu closed and the
pill never changed. The rule was invisible while the row was disabled in
exactly that case — the disabled row and this fallback were covering for
each other, and removing one exposed the other.

Let a bare `reuse` stand. The reuse pill appears, the environment picker
reads "Pick an environment", its dropdown explains "Nothing to reuse
yet.", and submit blocks on "Select an environment." A selection that
names a missing environment still falls back, and the loading guard on
that path is unchanged.

Covers the branch with a test; nothing exercised a bare `reuse` value
before.

Also drops the `project · reuse unavailable` story. It documented a
disabled reuse row that no longer exists, and seeded it with a
`host:<id>:local` value that `parseEnvironmentValue` stopped recognising
in #3227.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The picker stories drove the machine-grouped menu without `onSelectReuse`,
so no fixture rendered the combination the real composer always produces:
multiple machines plus a wired reuse row. That left the row's placement
below every machine group, its selected state, and its behaviour beside a
disabled offline machine without any visual coverage.

Add four stories over the existing machine fixtures: the grouped menu with
reuse offered, the same menu with a `reuse:<id>` value active, an offline
machine whose provider rows stay disabled while reuse stays selectable, and
the machine-search variant.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Content sizing was applied only when the machine search input was hidden,
so the searching variant kept a fixed `w-80` and still rendered wider than
its contents. The search input stretches to whatever width the menu has,
so there was no reason to pin it.

Drop the conditional and size every variant to content between `min-w-52`
and `max-w-80`. Measured across the picker stories the searching menu now
settles at 208-309px instead of a flat 320px, and holds its width while
results filter because the floor covers the search placeholder. This also
matches `ModelReasoningPicker`, which already sizes to content.

With the floor now unconditional, the `isLoading` arm that re-applied
`min-w-52` was dead; the loading menu still measures 208px without it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The reuse dropdown rendered every option in a plain DropdownMenu with no
filter, so a project with many live environments gave a long unsearchable
list. Replace it with the cmdk Command the other pickers use, showing a
search input once the list reaches seven entries and matching each
whitespace-separated term against the environment label, name, branch,
path, machine name, and the titles of the threads living in it.

Separately, "New thread in environment" seeds `reuse:<id>` directly, but
`resolveRootComposeEffectiveEnvironmentValue` only kept such a value when
the id appeared in the thread-derived option list. That list is built from
the sidebar's non-archived threads, so a seeded environment that no live
thread points at was silently replaced by the default provider, and
knowing the exact environment id saved no work.

Resolve that case through the existing `GET /environments/:id` query
instead: the composer looks the seeded id up only when it is absent from
the thread-derived list, holds the selection while that lookup is pending,
keeps it once the environment comes back, and falls back only when the
environment is gone or destroyed. The resolved environment is appended to
the picker options so the trigger shows its real label rather than "Pick
an environment". No new route, and no lookup at all when the seeded id is
already in the list.

Adds picker stories for the empty, short, selected, disabled, and
searchable states.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ymichael
ymichael merged commit 9b3d306 into main Sep 19, 2026
16 checks passed
@ymichael
ymichael deleted the bb/split-composer-reuse-restoration-into-its-own-pr-thr_2iiuw7rwx5 branch September 19, 2026 03:43
ymichael added a commit that referenced this pull request Sep 19, 2026
…#3932)

## 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 #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

---------

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