Repository navigation
Restore a destroyed thread workspace on request, through the environment provider - #4309
Merged
Merged
Conversation
Archiving a thread retires its environment, and once the provider's grace window passes the managed worktree is removed from disk. The thread then showed "Environment archived" with no composer and, because the banner suppressed Unarchive whenever the environment was gone, no action at all — even though the server could already rebuild the workspace on the branch it held (a send re-provisions with `rebuild: true` and the previous branch name). Server: `POST /threads/:id/restore-environment` runs that same provisioning with `seedWithoutRun`, so the files come back without a turn being started and the thread settles to idle with a live workspace. Restorability is policy, so the server decides it — provider still registered under the same plugin, the machine still present and not being removed, thread live and settled — and reports it as `canRestoreEnvironment` on the thread response. App: the destroyed-environment banner offers Unarchive again while the thread is archived, and Restore workspace once it is live and restorable. Also ships the SDK method and `bb thread restore-environment`. Committed work survives on the branch; uncommitted changes in the removed worktree are still lost, which is tracked separately. Fixes #1710 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
A restore provisions with `seedWithoutRun`, which settled the thread to idle without the `completed` provisioning event that a run start writes, so the timeline projected the still-active row as "Provisioning thread interrupted". The seed path now appends that event in the same transaction as `run.succeeded`. Restoring resolved a full turn execution plan it never used, so a thread whose model or capabilities no longer resolve could not get its workspace back. The startup context's execution is now null for the no-run path, and the restore route no longer resolves one. A detached HEAD records no branch, and the worktree provider then rebuilt on a fresh suggested branch without the detached commits. Restore now refuses when the environment's branch is unknown. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sending a message to a thread whose provider-backed environment had been
destroyed silently rebuilt it: `dispatchTurnDuringReprovision` reprovisioned
and the engine called `create` with `rebuild: true` and `previous`. What came
back depended on whether each provider read those fields. Git worktree reused
the recorded branch, project checkout replayed its original `new` branch input
and reset the thread's branch to its base (dropping its commits from the
branch), and personal workspace handed back an empty folder. The UI hid the
composer for such threads, but the CLI, SDK and parent-thread notifications
all took this path.
Restore is now a capability each environment provider opts into:
- The SDK's `create` context no longer declares `rebuild` or `previous`;
core still passes `rebuild: false` and `previous: null` at runtime so
plugins built against the old contract keep working. A new optional
`restore(context)` receives the creation inputs plus
`previous: { environment, resource }` and decides what restoring means.
- A send to a destroyed environment now fails with
`thread_environment_unavailable`, like the UI. A failed first setup
(`error`) is still retried by a send.
- The engine calls `restore` only when the thread's previous environment is
destroyed; everything else is a fresh `create`.
- `canRestoreEnvironment` and `POST /threads/:id/restore-environment` require
the provider to declare `restore`; the server no longer inspects branches.
- Git worktree restores on the recorded branch and fails when none was
recorded. Project checkout switches back to the recorded branch as an
existing branch, or re-attaches as-is when the thread never picked one.
Personal workspace declares no restore.
- `bb thread tell` explains the refusal, pointing at
`bb thread restore-environment` when the thread can be restored and at a
new thread when it cannot.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
ymichael
force-pushed
the
bb/fix-issue-1710-behavior-thr_bzwvkux8wi
branch
from
September 25, 2026 04:57
4e962c5 to
01765f9
Compare
ymichael
marked this pull request as ready for review
September 25, 2026 17:11
This was referenced Sep 28, 2026
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
Archiving a thread retires its environment. Once the provider's grace window passes, the workspace is removed from disk, and the thread showed only "Environment unavailable": no composer and no action, and the banner even hid Unarchive. Under that, sending a message to such a thread from the CLI, SDK or a child-thread notification silently rebuilt the environment.
dispatchTurnDuringReprovisionreprovisioned, and the engine called the provider'screatewithrebuild: trueandprevious. What came back depended on whether each provider read those fields:{ kind: "new" }branch input and reset the thread's branch to its base, leaving the commit on no branch (reproduced live)Nothing made this behaviour intentional or visible.
Fixes #1710
What changed
Restoring is now an explicit user action, and whether it's possible (and what it means) is decided by each environment provider.
Plugin SDK contract (
packages/plugin-sdk/src/environment-provider.ts,internal/host-policy.ts)create's context no longer declaresrebuildorprevious.createalways means a fresh environment. Core still passesrebuild: false, previous: nullat runtime, so plugins built against the old types keep working.restore(context)on the provider definition. It gets the creationinputsplusprevious: { environment, resource }for the destroyed environment, and returns the same result shape ascreate. It has noexperimental_prefix, likeavailability, because the wholebb.experimental_environmentsregistration is experimental.PluginEnvironmentProviderRestoreContextis exported.mainis already at unpublished0.5.27(npm has0.5.24).Server
POST /threads/:id/restore-environment(routes/threads/actions.ts) provisions withseedWithoutRun: no turn, noclient/turn/requested, and no prompt-history entry. The thread settles toidlewith a live workspace. It doesn't resolve an execution plan (startupexecutionis nullable only on this path), so threads whose model no longer resolves can still restore. The seed path writes thecompletedprovisioning event in the same transaction asrun.succeeded, so the timeline reads "Provisioned thread", not "interrupted".canRestoreEnvironmentonThreadResponseand the route's check (thread-environment-restore.ts): the thread is live and settled, its environment isdestroyed, the same plugin is still registered and declaresrestore, and the machine is active. The server no longer inspects branches; that's the provider's call.environment-engine.ts) callsrestorewhen the thread's previous environment isdestroyed, andcreateotherwise (first setup or retry of a failed one).thread-turn-dispatch.ts): a send to a destroyed environment fails with409 thread_environment_unavailable, matching the UI. A failed first setup (error) is still retried by a send.Providers
restorere-creates the worktree onprevious.environment.branchName(reuse-existing) and fails when no branch was recorded (detached HEAD). It re-adopts an adopted existing worktree path.createalways uses the suggested branch withreset.restorere-attaches{ kind: "existing", name: <recorded branch> }through the guardedgit switch, or re-attaches as-is when the thread never picked a branch. This fixes the reset-to-base bug.restore, because its folder is deleted on teardown.App / SDK / CLI / docs
canRestoreEnvironmentis true.sdk.threads.restoreEnvironmentandbb thread restore-environment [id] [--self] [--json].bb thread tellon a destroyed environment prints a hint: restore it withbb thread restore-environment <id>when restorable, otherwise start a new thread.docs/api_to_audit.md, including stabilization questions forrestore.HOST_DAEMON_PROTOCOL_VERSIONbump.Behaviour changes worth a look
queueParentSystemMessagethrows, and the notification path logs "Failed to queue batched parent turn notifications".restorecan't be rebuilt after destruction.Not covered: uncommitted changes in the removed workspace are lost; only committed work comes back.
How you verified
Tests (fail before, pass after)
apps/server/test/threads/thread-environment-restore.test.ts, 11 tests. They cover:restore(notcreate) with the recorded branchidlewith acompletedeventrestoremakes the thread non-restorable and gets a 409plugins/environment-git-worktree/server.test.ts: restore on the recorded branch withreuse-existing, refusal with no branch, re-adopting an existing path.plugins/environment-project-checkout/server.test.ts: restore switches to the thread's branch asexisting(notnew), re-attaches as-is without a branch, and refuses when no branch was recorded.apps/cli/src/__tests__/command-output/thread-tell.test.ts: the restore hint, the new-thread hint, and no hint for unrelated reasons.tests/integration/fake/multi-thread/environment-isolation.test.ts: rewritten from "a send rebuilds the worktree" to "sends are refused until the thread restores it". It restores through the route, then sends.ThreadPromptContextBanner.test.tsx: Unarchive alongside the environment-gone status, Restore workspace when restorable, and the pending state.Commands, after rebasing onto
origin/mainpnpm exec turbo run typecheck lint --filter='...[origin/main]': all green. Lint has warnings only (app: 201, the same asmain).pnpm exec turbo run testfor the plugin SDK, all three environment plugins, the Plugin Guide, CLI (862), SDK and server-contract: all pass.@bb/server: 3220 passed. Twoinstall-machine-scripttests timed out under heavy machine load and pass when run alone (47/47).Manual, on a
pnpm start:worktreebuild with real Haiku turns, via the source CLI and headless browsercanRestoreEnvironmentmain)mainAlso confirmed on a
mainbuild that sending to a thread with a destroyed worktree used to rebuild it silently.🤖 Generated with Claude Code