When you start a thread in bb, you can run it in your project's existing checkout or in a fresh managed worktree — a separate working copy on disk with its own branch. Worktrees let bb work on multiple things in parallel without touching your main checkout, and they make it easy to throw away whatever the agent does without affecting the rest of your work.
You can pair a worktree with a .worktreeinclude file that lists the local
files each new worktree needs, and with a setup script that bb runs the
first time the worktree is created — useful for installing dependencies,
generating secrets, or anything else you need before the agent starts.
You can also add a teardown script that releases resources outside the
worktree before bb removes it.
A managed worktree is a git worktree of your project's repo, on a fresh
branch. Under the hood it's git worktree add plus some bookkeeping:
- It shares the repo's
.gitstate with your main checkout — cheap to create, no full clone. - It gets its own branch so multiple threads can run in parallel.
- It lives at
<BB_DATA_DIR>/plugins/environment-git-worktree/host-data/worktrees/<thread-id>/<repo-name>— for example,~/.bb/plugins/environment-git-worktree/host-data/worktrees/thr_abc.../myrepo. - bb removes it once no thread uses it; see Cleanup.
Worktrees are created by bb's built-in Worktree plugin, which is enabled by default. Disabling it in Settings → Installed plugins leaves existing worktrees alone but stops bb from making new ones: a thread that asks for one waits until the plugin is running again.
In the app, pick Worktree in the environment picker when starting a thread.
From the CLI:
pnpm bb thread spawn \
--project <project-id> \
--new-environment worktree \
--prompt "..."Omit --base-branch for bb's smart default. A bare name such as main uses
origin/main when the local branch is equal to it or behind it, and the local
branch when it is ahead or has diverged. origin/main and full refs such as
refs/heads/main are used exactly as given.
New worktree branches start at the selected base without tracking it as an
upstream, even when Git's branch.autoSetupMerge is enabled. Publish with
git push -u origin HEAD to set the upstream to the task's remote branch.
Reusing an existing branch preserves its upstream configuration.
A new worktree checks out tracked files only. Your .env, your local
certificates, and anything else git ignores stay behind in your main checkout.
Commit a .worktreeinclude file at the root of your repo to list what a
worktree needs. It uses gitignore syntax — one pattern per line, # for
comments, ! to negate an earlier pattern:
# Local credentials the agent needs
.env
.env.*
!.env.example
certs/bb copies every untracked file in the source checkout that matches a pattern,
after it creates the worktree and before it runs .bb-env-setup.sh. Your
setup script can therefore read the copied files.
Contract:
- bb copies files. It does not create symlinks, and each worktree gets its own copy — an edit inside the worktree does not change your main checkout.
- bb never replaces anything the worktree already has. If the branch tracks a file at that path, the tracked file wins and bb reports the skip.
- bb skips symlinks in the source checkout rather than copying their targets, and it never writes through a symlink in the worktree.
- A pattern that matches nothing, an unreadable file, or a failed copy is reported in the provisioning transcript. Provisioning continues.
- Large directories such as
node_modulesare copied file by file, which is slow. Install dependencies in.bb-env-setup.shinstead.
Drop a file named .bb-env-setup.sh at the root of your project. If bb finds
one after an environment provider creates a path it owns (ownsPath: true), bb
runs the script inside that path before handing the thread to the agent. Core
owns this policy for every provider. Attaching a project checkout or personal
workspace (ownsPath: false) does not run either hook. Provider-specific
preparation, including .worktreeinclude copying, finishes before setup starts.
Use it for anything the agent will need in a fresh checkout — install
dependencies, sync local state, generate tokens, etc. To bring local files in
from your main checkout, prefer .worktreeinclude above.
#!/usr/bin/env bash
set -euo pipefail
pnpm installContract:
- The script runs with
env bash, working directory set to the new worktree. - It gets the daemon's sanitized environment overlaid with your login shell's
environment (including its
PATH), plus the variables you configured for the machine and project. Teardown gets the same environment. - In a new worktree, setup starts while the thread's title, and so its branch name, is still being generated: HEAD is detached at the base, and bb creates the branch alongside setup. The branch exists before the agent starts. Don't read the branch name in setup.
- stdin is closed. stdout and stderr stream into the thread's provisioning transcript in the app.
- A non-zero exit, a signal, or a timeout (15 minutes) fails provisioning and the thread doesn't start.
- A bash script on every platform. On native Windows bb runs it with the bash that Git for Windows installs and fails with a message naming Git for Windows when there is none.
A project can keep up to four warm worktrees per checkout: worktrees already
created at the project's default base with .worktreeinclude copied and
.bb-env-setup.sh finished. A new worktree thread on the default base takes
one instead of waiting for git and the setup script, in the same order as a
fresh worktree: in one call to the machine, bb asks the remote for its current
default base (git ls-remote), checks the warm worktree and fast-forwards it
to that commit; it then hands the warm worktree to the thread, reruns setup
while the thread's title is generated if the worktree moved, and names the
branch from the title last. A warm worktree at the remote's commit starts the thread without running
setup again. A warm worktree behind it is fast-forwarded to that commit and
.bb-env-setup.sh runs again before the thread starts, so a thread never
starts on older code than a fresh worktree would. If bb restarts before the
handover finishes, that warm worktree is deleted and the thread gets another
one, or a fresh worktree. A setup script that skips work whose inputs did not
change (this repository's skips pnpm install when the lockfile is unchanged)
makes that second run short. If the remote can't be reached, bb uses the last
fetched default base instead, as a fresh worktree would, and the thread's
provisioning log says it may be out of date. The log names the commit the
thread starts on. Named bases, existing worktrees and restores always build a
fresh worktree, and so does a thread that finds no usable warm worktree.
Turn it on per project in the Worktree plugin's settings, or with
bb worktree pool set <project-id> <size> (0 turns it off and deletes the
idle ones); bb worktree pool status lists them. Every machine with a checkout
keeps the project's count unless you give that machine its own: pick a count
next to the machine in settings, or run
bb worktree pool set <project-id> <size> --host <host-id> (project as the
size makes the machine follow the project's count again). While a project keeps warm
worktrees, bb fetches its default base every two minutes and replaces warm
worktrees that are no longer on it (the default branch was rewritten); a warm
worktree that is only behind is kept and moved forward when a thread takes it.
Refills run one at a time per machine
after a handoff, idle worktrees are replaced after eight hours, and a failed
one is deleted and retried after ten minutes. Warm worktrees are environments
held by the Worktree plugin (bb environment list --held-by plugin:environment-git-worktree); threads cannot join them, and deleting the
project or removing the machine removes them. Disabling or uninstalling the
Worktree plugin releases them, and they are then removed like any unused
worktree once the plugin is enabled again.
You don't need to clean up worktrees by hand. The Worktree plugin watches every worktree it made and removes the ones nothing is using:
- Deleting the last thread on a worktree removes it right away.
- Archiving the last thread starts a five-minute grace period. Unarchive a thread within it and the worktree is kept; let it elapse and the worktree goes.
- An archived thread keeps running for 30 seconds so Undo can restore it;
see
bb guide thread-lifecycle.
Removal runs .bb-env-teardown.sh inside the worktree first, then stops
every process whose working directory is inside the worktree — the agent's
provider process, its background jobs (dev servers, MCP servers, nohup
jobs), and any process you started there yourself, such as a shell you cd'd
into the worktree or an editor terminal. Each process gets SIGTERM, then
SIGKILL after a short grace period. Then git worktree remove --force runs
and the directory is deleted.
bb then deletes the branch it created for the worktree, but only when nothing would be lost: no other worktree has it checked out, and every commit on it is on a remote, a tag, or the base it started from, or the branch was squash-merged into that base. Otherwise the branch is kept, so work you committed but never pushed survives the worktree, even if another local branch also has it. Branches you check out or create yourself inside a worktree are never deleted, and neither are the branches of worktrees bb did not create. Restoring an archived thread whose branch was deleted recreates it from the remote branch when one exists, or from the base. If you want to keep uncommitted work, commit and push (or open a PR) from inside the worktree before letting the thread go, and move your own shells out of the worktree first if you want to keep them.
Commit a file named .bb-env-teardown.sh at the project root when setup
creates resources outside the worktree. For example, the script can remove a
database, a proxy registration, a container, or a port reservation.
#!/usr/bin/env bash
set -euo pipefail
docker rm -f "my-project-${USER}"Contract:
- bb runs the script before calling a provider to remove a path it owns, including cleanup after failed setup. Attached paths do not run it.
- bb runs
env bash .bb-env-teardown.shfrom the worktree before it removes the worktree, so the script can read tracked and generated files. - stdin is closed. bb records stdout and stderr in the server lifecycle logs.
- The script gets a separate 15-minute timeout.
- A non-zero exit, a signal, or a timeout reports a failure. It never stops bb from removing the worktree.
- The script receives the same sanitized environment as the setup script.
- A bash script on every platform. On native Windows bb runs it with the bash that Git for Windows installs and fails with a message naming Git for Windows when there is none.
A server restart does not rerun a hook that already started. An unreachable daemon leaves cleanup pending for retry.
A few quick checks:
- If worktree creation fails, look at the thread's provisioning transcript
in the app. Failures from
git worktree add(dirty source checkout, invalid base branch, conflicting branch name) show up there with the exact git error. - If
.bb-env-setup.shdoesn't seem to run, make sure it's committed to the branch you're working from. A file that exists only in the working copy of your main checkout won't appear in the new worktree. - If your setup script hangs, remember stdin is closed. Anything that prompts for input will time out at 15 minutes.
- Run
bash .bb-env-setup.shmanually in a clean clone to verify it works outside bb before debugging through the provisioning transcript. - Run
bash .bb-env-teardown.shmanually before you delete a test worktree. Confirm that repeated runs do not fail or remove shared resources.
Core applies the same setup and teardown policy when a project checkout is freshly
cloned onto a new machine and the environment provider reports that it owns the
checkout. .bb-env-setup.sh must succeed before the environment is ready.
.bb-env-teardown.sh runs before removal with its own 15-minute timeout; a failure
is reported but does not prevent removal. A user-maintained checkout attached to
BB remains unowned and runs neither hook.
Fresh machine clones do not apply .worktreeinclude, because the new host has
no local source checkout; supply local files and secrets through Machine
environment settings. Restoring a machine filesystem does not rerun
.bb-env-setup.sh.