Skip to content

Restore a destroyed thread workspace on request, through the environment provider - #4309

Merged
ymichael merged 3 commits into
mainfrom
bb/fix-issue-1710-behavior-thr_bzwvkux8wi
Sep 25, 2026
Merged

ymichael merged 3 commits into
mainfrom
bb/fix-issue-1710-behavior-thr_bzwvkux8wi

Conversation

@ymichael

@ymichael ymichael commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

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. dispatchTurnDuringReprovision reprovisioned, and the engine called the provider's 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 { kind: "new" } branch input and reset the thread's branch to its base, leaving the commit on no branch (reproduced live)
  • personal workspace returned an empty folder

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 declares rebuild or previous. create always means a fresh environment. Core still passes rebuild: false, previous: null at runtime, so plugins built against the old types keep working.
  • New optional restore(context) on the provider definition. It gets the creation inputs plus previous: { environment, resource } for the destroyed environment, and returns the same result shape as create. It has no experimental_ prefix, like availability, because the whole bb.experimental_environments registration is experimental. PluginEnvironmentProviderRestoreContext is exported.
  • No SDK version bump in this PR: main is already at unpublished 0.5.27 (npm has 0.5.24).

Server

  • POST /threads/:id/restore-environment (routes/threads/actions.ts) provisions with seedWithoutRun: no turn, no client/turn/requested, and no prompt-history entry. The thread settles to idle with a live workspace. It doesn't resolve an execution plan (startup execution is nullable only on this path), so threads whose model no longer resolves can still restore. The seed path writes the completed provisioning event in the same transaction as run.succeeded, so the timeline reads "Provisioned thread", not "interrupted".
  • canRestoreEnvironment on ThreadResponse and the route's check (thread-environment-restore.ts): the thread is live and settled, its environment is destroyed, the same plugin is still registered and declares restore, and the machine is active. The server no longer inspects branches; that's the provider's call.
  • The engine (environment-engine.ts) calls restore when the thread's previous environment is destroyed, and create otherwise (first setup or retry of a failed one).
  • Sends no longer rebuild (thread-turn-dispatch.ts): a send to a destroyed environment fails with 409 thread_environment_unavailable, matching the UI. A failed first setup (error) is still retried by a send.

Providers

  • git worktree: restore re-creates the worktree on previous.environment.branchName (reuse-existing) and fails when no branch was recorded (detached HEAD). It re-adopts an adopted existing worktree path. create always uses the suggested branch with reset.
  • project checkout: restore re-attaches { kind: "existing", name: <recorded branch> } through the guarded git switch, or re-attaches as-is when the thread never picked a branch. This fixes the reset-to-base bug.
  • personal workspace: no restore, because its folder is deleted on teardown.

App / SDK / CLI / docs

  • The banner offers Unarchive while the thread is archived, and Restore workspace once it is live and canRestoreEnvironment is true.
  • sdk.threads.restoreEnvironment and bb thread restore-environment [id] [--self] [--json].
  • A failed bb thread tell on a destroyed environment prints a hint: restore it with bb thread restore-environment <id> when restorable, otherwise start a new thread.
  • Updated the threads guide template, the bb-cli skill and command index, the plugin-authoring references, the Plugin Guide surface, and docs/api_to_audit.md, including stabilization questions for restore.
  • No server/daemon wire change, so no HOST_DAEMON_PROTOCOL_VERSION bump.

Behaviour changes worth a look

  • A child-thread notification to a parent whose environment was destroyed no longer rebuilds the parent. queueParentSystemMessage throws, and the notification path logs "Failed to queue batched parent turn notifications".
  • Sending to a destroyed personal workspace now errors instead of silently running in an empty folder.
  • Third-party providers without restore can't be rebuilt after destruction.
  • For a detached-HEAD worktree, Restore is offered and the provider then fails with "no branch checked out". Hiding it upfront would need a second SDK hook.

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 calls the provider's restore (not create) with the recorded branch
    • no turn is requested
    • provisioning runs to idle with a completed event
    • restoring works when turn settings don't resolve
    • a provider without restore makes the thread non-restorable and gets a 409
    • a send to a destroyed environment gets a 409, and the provider is never called
    • archive, live-workspace, missing-provider and machine-lifecycle refusals
  • plugins/environment-git-worktree/server.test.ts: restore on the recorded branch with reuse-existing, refusal with no branch, re-adopting an existing path.
  • plugins/environment-project-checkout/server.test.ts: restore switches to the thread's branch as existing (not new), 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/main

  • pnpm exec turbo run typecheck lint --filter='...[origin/main]': all green. Lint has warnings only (app: 201, the same as main).
  • pnpm exec turbo run test for the plugin SDK, all three environment plugins, the Plugin Guide, CLI (862), SDK and server-contract: all pass.
  • @bb/server: 3220 passed. Two install-machine-script tests timed out under heavy machine load and pass when run alone (47/47).
  • The integration file passes (2/2).

Manual, on a pnpm start:worktree build with real Haiku turns, via the source CLI and headless browser

Provider Send after destroy canRestoreEnvironment Restore Result
git worktree 409 + restore hint true CLI and UI button new worktree on the same branch; the agent sees the committed file
project checkout (thread started a new branch; user switched the checkout back to main) 409 + restore hint true CLI checkout back on the thread's branch at the same commit; before this PR the branch was reset to main
personal workspace 409 + "start a new thread" false refused (409), no button as intended

Also confirmed on a main build that sending to a thread with a destroyed worktree used to rebuild it silently.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 3 commits September 24, 2026 21:51
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
ymichael force-pushed the bb/fix-issue-1710-behavior-thr_bzwvkux8wi branch from 4e962c5 to 01765f9 Compare September 25, 2026 04:57
@ymichael ymichael changed the title Let a thread rebuild the workspace its archiving destroyed Restore a destroyed thread workspace on request, through the environment provider Sep 25, 2026
@ymichael
ymichael marked this pull request as ready for review September 25, 2026 17:11
@ymichael
ymichael merged commit 90971b1 into main Sep 25, 2026
18 checks passed
@ymichael
ymichael deleted the bb/fix-issue-1710-behavior-thr_bzwvkux8wi branch September 25, 2026 17:11
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.

Archived threads cannot resume in the original chat

1 participant