Repository navigation
Add an environment lifecycle ordering test and fix the two bugs it found - #7553
Merged
ymichael merged 6 commits intoNov 16, 2026
Merged
Conversation
Drives the real server environment engine, the real Worktree and Project checkout plugins and real git repositories in temporary directories through orderings of create, attach, archive, unarchive, remove, restore, failed setup and daemon disconnects. After each ordering it checks that nothing in use was removed, no work was lost, nothing was orphaned and everything settled. Two orderings fail on main today and are recorded as known failures. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Git prints Windows paths with forward slashes, so the orphan check mistook the main checkout for a stray worktree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Unarchiving a thread after its environment's delete was requested but before the removal started left the request pending forever: the sweep skipped environments with live threads and never cleared it, and the next archive removed the worktree at once, skipping both graces. The sweep now withdraws a removal that has not started once a live thread uses the environment again. When the same daemon instance reconnected during worktree setup, the server refused to re-send the attach because #5961's setup marker was unfinished, and failed the thread. A different daemon instance already interrupts provisioning when it opens its session, so a provisioning environment resumed on socket open belongs to the connected daemon, which joins the setup still running. Only that resume path re-sends; every other path keeps the refusal. The ordering test now has no known failures and checks that a restarted daemon is never asked to re-run an unfinished setup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
When the sweep withdraws a requested removal because a thread uses the environment again, it now adds a "Workspace deletion cancelled" notice to each live thread's timeline, so the dropped delete is visible where the user looks. The reconnect resume gets its own entry point. The server-move release keeps calling resumeEnvironmentProvisioningForHost exactly as on main, so that path and its tests are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
force-pushed
the
bb/environment-lifecycle-ordering-test-thr_kjdz7m4cpu
branch
from
November 14, 2026 13:33
781580b to
d2864b5
Compare
A step or sweep tick now finishes only when no fake-daemon work is in flight (turns included), the periodic sweep has finished or reached a held operation, and nothing the server advances on its own is still pending. The one-second escape that treated pending provisioning or removal as settled is gone. The fake daemon now answers thread.stop. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sweep still withdraws a requested removal that has not started once a thread uses the environment again, but no longer adds a notice to the thread's timeline. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ymichael
deleted the
bb/environment-lifecycle-ordering-test-thr_kjdz7m4cpu
branch
November 16, 2026 19:12
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
Past environment bugs (#3650, #4325, #6828, #6892, #7194, #7242, #7311 and others) were each caught after release, one ordering at a time. No test drove the real environment engine, the real Worktree and Project checkout plugins, and real git together through orderings of user and machine events. The ordering test added here found two bugs on main, and this PR fixes both.
DELETE /environments/:idonly setsteardownStatus: "running"; the removal happens on the next sweep. If a thread was unarchived before that sweep,sweepProviderEnvironmentInSlotreturned early for an environment with live threads and never cleared the request. The environment stayed inteardownindefinitely: restore refused it ("not destroyed") and cleanup refused it ("still has live threads"). The next archive skipped both the 30 s archive undo grace and the 5-minute retire grace, and removed the worktree, uncommitted files included, within a second.environment.attach. When the daemon reconnected as the same instance (Preserve provisioning across host reconnects #3650's case), the server re-sent the attach, butrunEnvironmentProvisionCommandrefused because the marker was unfinished. The thread failed with "An earlier setup has an unknown outcome…", even though the setup was still running and finished seconds later. The Preserve provisioning across host reconnects #3650 test seeds a path the provider doesn't own, so it never writes the marker. Proof runs show the failure starts at Bound environment teardown retries and add guarded recovery #5961.What changed
apps/server/src/services/environments/environment-engine.tsteardownStatus: "running"while stillready; starting a removal setserror), it withdraws the request (teardownStatusandretireAtback to null), and normal retirement applies. The withdrawal is silent: nothing is added to the thread's timeline. A removal that has already started is untouched (Record a removed workspace even if its thread was unarchived mid-removal #6892's path).resumeEnvironmentProvisioningForReconnectedDaemon, called only from the daemon socket-open path, may re-send an attach whose setup marker is unfinished. This is safe because the session-open route, which is awaited before the daemon opens its socket, already interrupts every provisioning environment when a different daemon instance replaces the session. An environment still provisioning at socket open therefore had its setup sent to this same daemon, andRuntimeManager.ensureEnvironmentjoins the in-flight setup (or returns the finished entry).resumeEnvironmentProvisioningForHostkeeps main's signature and behaviour. Every other path keeps Bound environment teardown retries and add guarded recovery #5961's refusal: the periodic sweep, retries, and the server-move release.apps/server/src/ws/daemon-protocol.ts: socket open calls the new resume.packages/templates/src/templates/bb-guide-environments.md:bb environment deletenotes that unarchiving a thread before removal starts cancels the request.apps/server/test/services/environments/provider-orchestration.test.ts: archive,DELETE /environments/:id, unarchive; then the sweep keeps the environment ready, and never callsremove.apps/server/test/environments/environment-lifecycle-orderings.test.ts: the ordering suite.Real code, real data. A migrated SQLite database (
createMigratedConnection), the real server environment engine and routes, and the real Worktree and Project checkout plugins. Their host halves run in host-entry harnesses against real git repositories (with a bareorigin) in a freshmkdtempdirectory, reached throughplugin.host.callon a fake daemon.Fake daemon. It runs turns, answers
environment.attachby inspecting the real workspace, can hold or fail any host operation, and can disconnect, reconnect, or restart as a new instance.Ten orderings built from create, attach a second thread, archive, unarchive, remove or retire, restore, failed setup, and daemon disconnect, reconnect and restart, on one or two machines.
Rules checked after every ordering, compared by rule and subject only, never messages, counts or timings:
removed-in-usework-lostorphaned-worktree/-branch/-claim/-rowunsettled-environment/unsettled-thread/thread-failed/create-racedsetup-rerun: a restarted daemon is never asked to re-run a setup whose outcome is unknown. This guards Bound environment teardown retries and add guarded recovery #5961, and it fails if the restart interrupt at session open is removed.There are no known failures.
No protocol, wire, schema, CLI, SDK or plugin API changes. No new
@bb/dbquery functions. No existing test is changed: the server-move code and its tests,internal-events-tool-calls.test.ts, and #5961's tests are identical to main.What a user would see differently, beyond the bugs going away (decisions for Michael):
bb environment showreturns toLifecycle: active. Before, the environment was stuck inteardownand the next archive removed it within a second, skipping both graces.Run time: about 25 s of test time for the ordering file (10 orderings), in its own isolated worker.
How you verified
Live, before and after, using
pnpm start:worktreefrom this checkout on fresh dev data dirs (never a copy of~/.bb), the real host daemon, real Claude Code turns, and temp git repos with anorigin:bb thread archive,bb environment delete,bb thread unarchive.teardown / running.restore-environment→ 409 "Thread must be settled…";environment cleanup→ 409 "still has live threads". When the thread was archived again, the worktree and an uncommittednotes.txtwere gone within 1 s.Lifecycle: active. The thread's timeline gets no new entry. On the next archive the worktree andnotes.txtsurvived 60 s, withretireAt5 minutes out..bb-env-setup.sh; the server was paused with SIGSTOP for 40 s so the daemon's lease expired.host_daemon_sessionsshows the sameinstance_idreconnecting..bb-env-setup.shran once.instance_id, so its session was replaced by the same instance. The thread went idle with Claude's "ok", the setup marker finished, and the script's own run log has exactly one line.apps/server/src/services/server-move/environment.ts(zero diff against main). Its call,resumeEnvironmentProvisioningForHost, keeps main's signature and behaviour (setupRunsOnConnectedDaemon: false). The existing, unmodifiedtest/server-move/freeze-work.test.tsasserts that a move release calls it with{ hostId }. The existing, unmodified Bound environment teardown retries and add guarded recovery #5961 test intest/environments/environment-provisioning.test.ts("does not replace an unresolved RuntimeManager setup with a new successful attempt") asserts that this path still refuses an unfinished setup. Both pass.Proof runs of the test (kept out of this PR). The test was run on each listed fix's merge commit and its parent, and the violations were diffed:
thread-failed: Aorphaned-claim: env(B)refs/worktree/*ref →work-lost: worktree-ref in Aorphaned-row: env(A)unsettled-environment: env(B)orphaned-branch: env(B)create-raced: create C,thread-failed: Aunsettled-thread: AThe restart ordering and the
setup-rerunrule were added after the proof runs. They guard the reconnect fix and were checked by the mutation above rather than against history.Local: targeted suites pass:
test/environments,test/services/environments, thread provisioning recovery, internal events tool calls, periodic sweeps, public environment routes, environment providers, live commands andtest/server-move(375 tests), plus@bb/thread-viewand@bb/templatestests.turbo run typecheck lint:type-awarefor@bb/serverand@bb/thread-viewpass, andoxfmt --checkis clean. CI runs the full suites.🤖 Generated with Claude Code