Repository navigation
fix: preserve provider sessions across environment switches - #1212
SawyerHood merged 5 commits into
Conversation
7c311e4 to
9e7db5d
Compare
|
I went back through the diff and pulled the non-obvious rationale into this PR discussion.
The regression tests cover the real directory-update path, transcript/provider continuity, cross-environment handoff ordering, exact-source fork reuse, isolated personal forks, and rejection of a different unmanaged environment. Live QA was run against the pre-fix and fixed builds. The PR contains no screenshot artifacts. |
|
🚨 SLOP COP 🚨 · I am SlopCop. I started the security, code quality, and performance reviews. I will also check the test path. |
| ? [] | ||
| : [...(commandsByThreadId.get(args.threadId) ?? [])], | ||
| ); | ||
| await Promise.all(inFlightOldCommands); |
There was a problem hiding this comment.
🚨 slopcop/review — This wait can cause a deadlock.
The control queue waits for an old command. The old command can then place its release behind this wait. Neither request can finish.
Wait outside the queue. Then enter the queue again to release the old owner. Add a cross-environment control test.
| command.environmentId, | ||
| options.runtimeManager, | ||
| ); | ||
| await options.runtimeManager.releaseThreadFromOtherEnvironments({ |
There was a problem hiding this comment.
🚨 slopcop/review — These handlers require the target runtime before they release the old owner.
An existing database environment can be absent from the daemon. The command then fails, and the old provider turn continues.
Find and stop the old owner first. Apply the same fix to thread.plan.cancel. Add tests for an unloaded target runtime.
|
|
||
| // A directory switch changes the runtime cwd, not the provider thread's | ||
| // conversation identity. The host daemon releases the old runtime owner | ||
| // before the next turn is dispatched through `thread/resume` with the new |
There was a problem hiding this comment.
🚨 slopcop/review — This server behavior needs the new daemon ownership controls.
An old daemon accepts the same command but does not release the old provider owner. Two runtimes can then own one provider session.
Increase HOST_DAEMON_PROTOCOL_VERSION and update its contract test.
|
|
||
| await Promise.all( | ||
| staleEntries.map((entry) => | ||
| entry.runtime.stopThread({ threadId: args.threadId }), |
There was a problem hiding this comment.
🚨 slopcop/review — This handoff always calls stopThread.
A rename or goal clear after a directory switch can interrupt the active old turn.
Route non-turn controls to the current owner. Transfer ownership only when a new turn starts. Add an active-turn control test.
SawyerHood
left a comment
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
ELI5: The patch keeps one chat when a thread moves to another folder. It is like moving a game without losing your place.
I found four high-severity defects:
- The server needs a host daemon protocol increase for the new ownership rule.
- Stop and Plan cancel can miss the old owner when the target runtime is absent.
- Rename and goal clear can stop an active old turn.
- The new control queue can deadlock across two environments.
I left one inline comment for each defect.
Architecture: The router and runtime manager both keep per-thread promise queues. One runtime-owner service would reduce lock-order errors and duplicate state.
Three parallel GPT-5.6 workers reviewed security, quality, architecture, and performance. One GPT-5.6 reviewer verified the combined findings.
git diff --check passed. GitHub reports no pull request checks.
I did not execute the fork code or start a browser test. The environment has no execution sandbox for untrusted code.
I used a comment review only. I did not approve the pull request or request changes.
Follow-up to the environment-switch session fix. The per-thread control lane could wedge, two controls released the old owner too late, two controls stopped a live turn, and the wire change had no protocol bump. - Wait for old-environment thread commands outside the control lane. A thread command takes that same lane for its own release step, so a wait inside the lane could hold the lane against the command it waits for. A control command carrying the old environment id and a turn submit carrying the new one then hung forever, and every later start or submit on that thread hung with them. - Release the old owner before `thread.stop` looks up its target runtime. A moved thread usually has no runtime in its new environment, so the lookup threw and the old turn kept running. - Cancel a plan in whichever environment runs the expected turn, instead of requiring the new environment's runtime. - Stop the ownership handoff in `thread.rename` and `thread.archive`. Neither moves the provider session, and the switch happens mid-turn, so both could interrupt the old turn. A goal clear now reports the busy environment instead of stopping that turn. - Bump HOST_DAEMON_PROTOCOL_VERSION to 87. The server now sends `turn.submit` with a provider thread id where it sent `thread.start`, and a daemon without the release would own one provider session twice. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
) I hit this when the agent switched from my personal working directory to another directory with `update_environment_directory`. The switch worked, but the session did not migrate with it. The transcript was still there; the next message started a new provider session. The same state also broke forks: side chats returned HTTP 409, and Fork into new thread returned the same personal-workspace error. ## Repro 1. Start a thread in a personal workspace. 2. Switch it to another directory with `update_environment_directory`. 3. Ask a follow-up question. 4. Try Reply in side chat or Fork into new thread. 5. Before this change, the transcript context was lost and both fork actions failed. ## Fix - Keep the provider thread ID when the environment changes. - Release the old runtime before resuming in the new environment. - Serialize starts and submits per thread across environment lanes so a move cannot deadlock the handoff. - Let a source-derived fork reuse only its exact switched unmanaged environment. - Give isolated forks from personal projects a personal workspace. - Keep the existing project and host boundary checks. ## Verification I ran the flow in the desktop app against both builds. The pre-fix build lost the transcript context and showed the 409 on both fork actions. The fixed build preserved the original opening message, the side chat replied `SIDE_CHAT_OK`, and the forked thread replied `FORK_OK`. 59 focused host-daemon tests, 37 focused server tests, and 76 DB event tests pass. The affected packages typecheck and build; formatting and diff checks pass. The PR contains no screenshot artifacts. --------- Co-authored-by: Sawyer Hood <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
## Human comments ## What was wrong A Personal thread that moved into an attached folder with `update_environment_directory` could not spawn Personal children: every variant returned `409 Personal project threads must reuse a personal workspace` (#7256). The cause is a reuse rule from c35a710 (May 27) that let Personal threads share only bb-created personal workspaces. It was written before threads could move (79f409a, June 24); moving made "a Personal thread in an attached folder" a normal state, but the rule was never revisited. Forks hit the same 409 and got a fork-only exception in #1212 (`allowUnmanagedPersonalProjectReuseEnvironmentId`); children of moved parents hit it now. The rule's other half, "standard project threads cannot reuse personal workspaces", has been unreachable since #3227: personal workspaces always belong to the Personal project, so the earlier "Environment belongs to a different project" check rejects that case first. ## What changed - Delete the reuse rule (`assertReuseWorkspaceProjectCompatibility`) and the fork-only exception plumbing in `thread-request-eligibility.ts`, `thread-environment-placement.ts` and `thread-create.ts`. - Children of a moved Personal parent now share the parent's folder, the same way forks already did. A parent still in its personal workspace keeps sharing it as before, and `--machine` on another host still gets a fresh workspace (#5207). - Deviation from the issue's proposed fix and this PR's first commit: instead of falling back to a fresh, empty personal workspace when the parent has moved, the child follows the parent. That keeps children and forks consistent and removes an exception rather than adding one. It also makes `--environment <parent env id>` work. The first commit's guard is reverted. - Still blocked: starting a new Personal thread at an arbitrary path (`--environment <path>`, a separate 400 check), and a standard project thread sharing a personal workspace (different-project check, now covered by a test). - No wire, schema, CLI or doc changes. ## How you verified - Tests: - The three `environment-providers.test.ts` cases now expect the child to share the attached parent's environment. They fail on main with the 409. - A new case asserts that a standard project thread reusing a personal workspace still gets 409 "Environment belongs to a different project". - The fork test that asserted the exception's narrowness is removed along with the exception. - `pnpm exec turbo run test --filter=@bb/server -- test/threads test/public`: 117 files, 1275 tests pass. `turbo run typecheck --filter=@bb/server` passes; oxlint and prettier are clean on the changed files. - Live, on a `pnpm start:worktree` server, a claude-code Personal thread moved itself to `/tmp/bb7258-scratch`: | From the moved parent | main | this PR | |---|---|---| | child, no env flag | 409 | shares `/tmp/bb7258-scratch` | | child, `--new-environment personal` | 409 | shares `/tmp/bb7258-scratch` | | child, `--environment-provider personal-workspace` | 409 | shares `/tmp/bb7258-scratch` | | child, `--environment <parent env id>` | 409 | shares `/tmp/bb7258-scratch` | | fork (no flags / `--environment <parent env id>`) | shares | shares `/tmp/bb7258-scratch` | | fork `--new-environment personal` | fresh workspace | fresh workspace | A running child and a running fork each replied `/private/tmp/bb7258-scratch` to `pwd`. Fixes #7256 > AGENT GENERATED 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- slopcop:rule=new-issue-autopilot run=run_wgn1lcuqmw71ng1k sha=issue-7256 kind=summary --> --------- Co-authored-by: SawyerHood <[email protected]> Co-authored-by: Michael Yong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
I hit this when the agent switched from my personal working directory to another directory with
update_environment_directory.The switch worked, but the session did not migrate with it. The transcript was still there; the next message started a new provider session.
The same state also broke forks: side chats returned HTTP 409, and Fork into new thread returned the same personal-workspace error.
Repro
update_environment_directory.Fix
Verification
I ran the flow in the desktop app against both builds. The pre-fix build lost the transcript context and showed the 409 on both fork actions. The fixed build preserved the original opening message, the side chat replied
SIDE_CHAT_OK, and the forked thread repliedFORK_OK.59 focused host-daemon tests, 37 focused server tests, and 76 DB event tests pass. The affected packages typecheck and build; formatting and diff checks pass. The PR contains no screenshot artifacts.