Repository navigation
Scope the workspace path claim to the project - #1049
Conversation
|
🚨 SLOP COP 🚨 · I am the SlopCop. I am reviewing this pull request against the security, code quality, performance, architecture, and end-to-end rules. The stack contains PR #1049 only. No pull request was skipped. |
|
🚨 SLOP COP 🚨 · The security phase is complete. It found two possible cross-project hazards around cleanup and branch changes on one physical path. I will verify both hazards against the cleanup and workspace logic before the final review. |
|
🚨 SLOP COP 🚨 · The quality and architecture phase is complete. It confirmed the branch hazard and found an index regression plus a stale tool description. The final review will include only findings that remain valid after verification. |
|
🚨 SLOP COP 🚨 · The performance phase is complete. It confirmed a full environment scan in a host-thread query after the index change. The exact project, host, and path lookup still uses the new unique index. |
|
🚨 SLOP COP 🚨 · The end-to-end phase passed on the development application at the pull request SHA. Two projects created idle threads on one host path. The server returned different project-owned environment IDs. |
| const existing = findEnvironmentByHostPath(deps.db, args.hostId, args.path); | ||
| const existing = findProjectEnvironmentByHostPath( | ||
| deps.db, | ||
| args.request.projectId, |
There was a problem hiding this comment.
🚨 slopcop/review — P1: Keep the active-thread guard across projects.
This project-scoped lookup hides an environment from another project. A branch request can then run a checkout on the shared directory.
The current guard checks only the environment that this lookup returns.
Check all host and path environments before checkout. Return 409 when any nonterminal thread uses the directory.
Add a cross-project branch test that verifies no checkout command runs.
| uniqueIndex("environments_host_path_idx").on(table.hostId, table.path), | ||
| // A workspace path is claimed per project, not globally. Two projects may | ||
| // point at the same folder; each gets its own environment for it. | ||
| uniqueIndex("environments_project_host_path_idx").on( |
There was a problem hiding this comment.
🚨 slopcop/review — P1: Protect shared physical paths from managed cleanup.
This index lets a managed environment and an unmanaged environment use one path across projects.
Cleanup checks only the managed environment ID. The daemon can remove the directory while another project still uses it.
Reject unmanaged aliases to managed paths. Add a host and path guard before each destroy command.
Add a test where cleanup must preserve an active unmanaged alias.
| const existingEnvironment = findEnvironmentByHostPath( | ||
| const existingEnvironment = findProjectEnvironmentByHostPath( | ||
| deps.db, | ||
| args.thread.projectId, |
There was a problem hiding this comment.
🚨 slopcop/review — P3: Update the tool description for project-scoped reuse.
This lookup now reuses an environment only in the current project. The tool description still promises reuse of any host and path environment.
State the project limit in the description so the agent contract matches the behavior.
| @@ -0,0 +1,2 @@ | |||
| DROP INDEX IF EXISTS `environments_host_path_idx`;--> statement-breakpoint | |||
| CREATE UNIQUE INDEX IF NOT EXISTS `environments_project_host_path_idx` ON `environments` (`project_id`,`host_id`,`path`); | |||
There was a problem hiding this comment.
🚨 slopcop/review — P2: Retain a host-leading environment index.
The replacement index starts with project_id. A query that filters only host_id now scans all environments.
The host session paths call listHostThreadIds, which uses that filter.
Use a host-leading unique tuple or add a host and path index. Verify the plan uses an index search.
SawyerHood
left a comment
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
I found two P1 safety defects, one P2 index regression, and one P3 contract mismatch.
- P1: Shared managed and unmanaged aliases can let cleanup remove an active directory.
- P1: A project-scoped lookup lets a branch checkout bypass the active-thread guard from another project.
- P2: The project-leading unique index causes host-only queries to scan all environments.
- P3: The directory tool description still promises global environment reuse.
The focused server tests passed with 24 tests. The server and database type checks passed.
The browser test also passed for two projects with idle threads on one host path. That test does not cover the two unsafe operations.
Fix the physical path safety defects before merge. Also restore a host-leading index and update the tool description.
SlopCop review of #1049 found that project-scoping the environment claim removed guards that keyed off "the one environment at this path". The claim is per project, but the directory is one physical folder, so questions about the folder itself must stay cross-project. Add listEnvironmentsByHostPath and two guards built on it: - A branch checkout no longer runs while any project has a live thread in the directory. The previous guard only saw the requesting project's environment, and a project with no environment at the path skipped it entirely by falling through to direct-unmanaged. - Attaching in place to another project's bb-managed worktree is refused, in both thread create and update_environment_directory. Cleanup of the owning environment deletes that directory. Also restore a host-leading index. The replacement unique index starts with project_id, so host-only filters such as listHostThreadIds began scanning the table. Verified with EXPLAIN QUERY PLAN: all four affected queries now search an index. Update the update_environment_directory description to state that reuse is project-scoped, so the agent contract matches the behavior. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Addressed all four SlopCop findings in b897f39. Notes on each: P1 — cross-project branch checkout (thread-create.ts). Confirmed, and slightly wider than reported. The guard only saw the requesting project's environment, but a project with no environment at the path skipped the check entirely by falling through to P1 — managed/unmanaged aliasing (schema.ts). Confirmed as reachable. Managed worktree paths embed the environment ID, so two managed environments still cannot collide, but an unmanaged environment in another project could now attach in place to a managed worktree — and its cleanup deletes the directory. I took the narrower of the two suggested fixes: reject the alias rather than add a host+path guard before each destroy. That keeps the invariant "a managed path has exactly one environment", so the destroy precondition stays valid as-is instead of every future destroy path needing to remember a new check. Enforced in both thread create and P2 — host-leading index. Confirmed with P3 — tool description. Fixed. It now states that reuse is project-scoped, that another project may hold its own environment for the same directory, and that a managed worktree owned by another project is refused. Thanks — the two P1s were real regressions introduced by the scoping change. |
|
🚨 SLOP COP 🚨 · I am SlopCop. I am reviewing this pull request for security, code quality, performance, architecture, and end-to-end behavior. The stack contains PR #1049 only. The author has write access, and the pull request is not a draft. I skipped no pull requests. |
|
🚨 SLOP COP 🚨 · The performance phase is complete. It found a medium N+1 query pattern in shared-path branch checks. It also found a low-cost duplicate-index concern. I will verify both findings before the final review. |
|
🚨 SLOP COP 🚨 · The end-to-end phase passed on the development application at the pull request SHA. A Personal thread changed to the test directory. A standard-project thread then used the same host path successfully. The browser showed the completed standard-project thread without the old path-claim error. The database held separate project-owned environment rows. |
|
🚨 SLOP COP 🚨 · The security phase is complete. It found three possible physical-path alias or race hazards. I will compare them with the base branch before the final review. Only pull-request regressions will become findings. |
|
🚨 SLOP COP 🚨 · The code-quality and architecture phase is complete. It found two shared-path consistency risks. It also confirmed the avoidable database query cost. I will compare each risk with the base branch before the final review. |
| deps: ThreadCreateDeps, | ||
| args: ExistingUnmanagedEnvironmentIntentByHostPathArgs, | ||
| ): ExistingUnmanagedEnvironmentIntentResult | null { | ||
| const existing = findProjectEnvironmentByHostPath( |
There was a problem hiding this comment.
🚨 slopcop/review — [P2] Keep branch metadata consistent across project aliases
This lookup selects only the current project environment. A branch checkout changes one physical Git tree, but settlement updates only this row. Other project aliases retain the old branchName, which the sidebar, runtime configuration, and fork logic can later use.
Update the physical branch fields for all live aliases, or refresh an alias before reuse. Add a two-project test that checks out through one alias and verifies both rows.
| db: DbConnection, | ||
| args: HostPathArgs, | ||
| ): boolean { | ||
| return listEnvironmentsByHostPath(db, args.hostId, args.path).some( |
There was a problem hiding this comment.
🚨 slopcop/review — [P3] Replace the N+1 path check with a targeted query
This code loads all matching environments and runs one thread query for each row. The foreign-managed check also filters loaded rows in JavaScript. This conflicts with the repository data-access rule and makes a branch request perform 1 + N checks.
Use a filtered LIMIT 1 query for the managed predicate. Use one environment-to-thread join or EXISTS query for live threads.
| args: HostPathArgs & { projectId: string }, | ||
| ): Environment | null { | ||
| return ( | ||
| listEnvironmentsByHostPath(db, args.hostId, args.path).find( |
There was a problem hiding this comment.
🚨 slopcop/review — [P1] Reserve managed target paths before host completion
This exact-path lookup cannot see a managed environment while its path is still NULL. Another project can attach after the host creates the worktree but before the owner stores its path. The old global index rejected the owner update, but the new project-scoped index accepts both rows. Cleanup can then delete a directory used by the alias thread.
Store the managed target path, or an equivalent atomic claim, before host dispatch. Add a test that attempts a foreign attachment while managed provisioning is pending and requires rejection.
SawyerHood
left a comment
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
I found three pull-request regressions.
- P1: A managed path remains unclaimed until host completion. The new index can then accept a dangerous cross-project alias.
- P2: A branch checkout updates one environment row but leaves sibling project aliases with stale branch metadata.
- P3: The new safety checks filter rows in JavaScript and perform an N+1 thread query.
The development application passed the shared-path workflow. A Personal thread and a standard-project thread used one host path with distinct ready environments.
The focused server tests passed with 27 tests. The database migration tests passed with 39 tests. Server and database type checks passed.
GitHub CI passed. The final GPT-5.6 gate returned REQUEST CHANGES, but this review uses the required comment-only action.
Please fix the P1 and P2 consistency defects before merge. The P3 change also enforces the repository data-access rule.
Second SlopCop round on #1049. P1: a managed environment stores its path only after the host reports provisioning success, so between worktree creation and that report the row claims nothing. The old global index made the owner's later path write fail; the project-scoped index accepts both rows instead, leaving an alias that cleanup can delete underneath. Defend the workspace roots rather than the row: bb derives every managed path from <dataDir>/worktrees and <dataDir>/personal-workspaces, so a path under either belongs to a managed environment whether or not it has been stored yet. A project may still attach to a managed path it already owns, which keeps same-project reuse working. P3: replace the load-all-and-filter-in-JavaScript checks with targeted queries, per the repository data-access rule. findForeignManagedEnviron- mentAtHostPath filters in SQL, and hasLiveThreadAtHostPath is one join instead of one thread query per environment at the path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Round two addressed in 815ef09. Migrations also squashed into a single 0086 (9bda88f) — 0086 and 0087 both rebuilt the P1 — managed path unclaimed until host completion. Confirmed. I defended the workspace roots instead of the row. bb derives every managed path from P3 — N+1. Agreed, and it conflicted with the explicit AGENTS.md data-access rule. P2 — branch metadata across aliases. I'm not fixing this one, and I'd rather say why than silently skip it.
So this is not a new class of staleness, and the proposed fix would make things worse: writing branch metadata into another project's environment row from a checkout that project never requested creates a cross-project write where today there is only a per-project cached read. Happy to reconsider if you see a path where a stale read causes something worse than a briefly wrong branch label. All checks green; the only local failure is a |
A folder used by a Personal thread could never be opened as a project. Every attempt failed with "Workspace path is already attached to a different project", and no supported action released the claim. The environments table had a global unique index on (host_id, path), so one host folder belonged to exactly one environment and therefore one project. When a Personal thread called update_environment_directory, the new unmanaged environment claimed the folder for the Personal project permanently. A later thread create in a real project looked the environment up by host and path alone, saw a different projectId, and threw a 409. The claim was global, but a project is the unit that owns work. Two projects can already point at the same source folder; only the environment index disagreed. Scope the claim to the project instead: - The unique index becomes (project_id, host_id, path). The new index is weaker than the old one, so existing rows always satisfy it. - findEnvironmentByHostPath becomes findProjectEnvironmentByHostPath and takes a projectId. - Thread create and update_environment_directory scope their lookups, which removes both cross-project failures. Each project now gets its own environment row for a shared folder. This removes the error class, so adopt-on-create, an error naming the owner, and a release action are no longer needed. The migration DDL is idempotent because migrate.test.ts replays the newest migrations after deleting ledger rows. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
SlopCop review of #1049 found that project-scoping the environment claim removed guards that keyed off "the one environment at this path". The claim is per project, but the directory is one physical folder, so questions about the folder itself must stay cross-project. Add listEnvironmentsByHostPath and two guards built on it: - A branch checkout no longer runs while any project has a live thread in the directory. The previous guard only saw the requesting project's environment, and a project with no environment at the path skipped it entirely by falling through to direct-unmanaged. - Attaching in place to another project's bb-managed worktree is refused, in both thread create and update_environment_directory. Cleanup of the owning environment deletes that directory. Also restore a host-leading index. The replacement unique index starts with project_id, so host-only filters such as listHostThreadIds began scanning the table. Verified with EXPLAIN QUERY PLAN: all four affected queries now search an index. Update the update_environment_directory description to state that reuse is project-scoped, so the agent contract matches the behavior. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
0086 and 0087 both changed the environments indexes and neither has shipped. Regenerate them as a single migration so upgrading installs do one index rebuild instead of two, and so the index set is readable in one file. No schema change: the resulting indexes and query plans are identical. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Second SlopCop round on #1049. P1: a managed environment stores its path only after the host reports provisioning success, so between worktree creation and that report the row claims nothing. The old global index made the owner's later path write fail; the project-scoped index accepts both rows instead, leaving an alias that cleanup can delete underneath. Defend the workspace roots rather than the row: bb derives every managed path from <dataDir>/worktrees and <dataDir>/personal-workspaces, so a path under either belongs to a managed environment whether or not it has been stored yet. A project may still attach to a managed path it already owns, which keeps same-project reuse working. P3: replace the load-all-and-filter-in-JavaScript checks with targeted queries, per the repository data-access rule. findForeignManagedEnviron- mentAtHostPath filters in SQL, and hasLiveThreadAtHostPath is one join instead of one thread query per environment at the path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The managed-path guard needs the host data directory to recognize bb's own workspace roots, and I reached for ensureHostSessionReadyForWork to get it. That throws a 502 when no daemon session is open, which turned a reuse that never touches the host into a host-unavailable error, raised as an HTTP error rather than a tool failure. Read the data directory without demanding readiness instead. When no session is open it is null, which skips only the workspace-root check; the row-based checks still run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
5006f0c to
ca578b5
Compare
Fixes get-bb#1043. ## Problem A folder used by a Personal thread could never be opened as a project. Every attempt failed with `Workspace path is already attached to a different project`, and nothing in the UI or CLI said which project owned the folder or offered a way to release it. The reporter had to edit the database by hand. ## Root cause The `environments` table had a **global** unique index on `(host_id, path)`. One host folder therefore belonged to exactly one environment, and so to exactly one project. When a Personal thread called `update_environment_directory`, it created an unmanaged environment under the Personal project. That row claimed the folder permanently. A later thread create in a real project looked the environment up by host and path alone, saw a different `projectId`, and threw a 409 in `thread-create.ts`. The claim was global, but a project is the unit that owns work. Two projects can already point at the same source folder — only the environment index disagreed. ## Fix Scope the workspace-path claim to the project: - `packages/db/src/schema.ts` — the unique index becomes `(project_id, host_id, path)`, with migration `0086_project_scoped_environment_path.sql`. The new index is weaker than the old one, so existing rows always satisfy it. - `packages/db/src/data/environments.ts` — `findEnvironmentByHostPath` becomes `findProjectEnvironmentByHostPath` and takes a `projectId`. - `apps/server/src/services/threads/thread-create.ts` and `thread-environment-directory.ts` — both lookups are project-scoped, which removes the two cross-project failure branches. Each project now gets its own environment row for a shared folder. A Personal thread that visits a folder no longer blocks a project from using it, and the reverse also works. This removes the error class the issue describes, so the report's other suggestions (adopt-on-create, an error that names the owner, a release action) are no longer needed. ## Notes - The migration DDL is idempotent (`IF EXISTS` / `IF NOT EXISTS`) because `migrate.test.ts` replays the newest migrations after deleting ledger rows. - No `HOST_DAEMON_PROTOCOL_VERSION` bump: no session payload, WebSocket message, host RPC command, or result changed. ## Tests - New test in `thread-create-shared-workspace-path.test.ts`: a project thread starts in a folder the Personal project already claims and gets its own environment. It reproduced the reported 409 before the fix. - New test in `internal-events-tool-calls.test.ts`: `update_environment_directory` switches into a folder another project uses and leaves the other project's claim alone. - `@bb/db` 359 pass, `@bb/server` 1325 pass. Repo-wide `turbo run typecheck` passes (57 tasks). - `internal-skill-trees.test.ts` fails locally on a file-mode comparison (420 vs 436). It fails identically without these changes — a local umask difference, unrelated. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Fixes #1043.
Problem
A folder used by a Personal thread could never be opened as a project. Every attempt failed with
Workspace path is already attached to a different project, and nothing in the UI or CLI said which project owned the folder or offered a way to release it. The reporter had to edit the database by hand.Root cause
The
environmentstable had a global unique index on(host_id, path). One host folder therefore belonged to exactly one environment, and so to exactly one project.When a Personal thread called
update_environment_directory, it created an unmanaged environment under the Personal project. That row claimed the folder permanently. A later thread create in a real project looked the environment up by host and path alone, saw a differentprojectId, and threw a 409 inthread-create.ts.The claim was global, but a project is the unit that owns work. Two projects can already point at the same source folder — only the environment index disagreed.
Fix
Scope the workspace-path claim to the project:
packages/db/src/schema.ts— the unique index becomes(project_id, host_id, path), with migration0086_project_scoped_environment_path.sql. The new index is weaker than the old one, so existing rows always satisfy it.packages/db/src/data/environments.ts—findEnvironmentByHostPathbecomesfindProjectEnvironmentByHostPathand takes aprojectId.apps/server/src/services/threads/thread-create.tsandthread-environment-directory.ts— both lookups are project-scoped, which removes the two cross-project failure branches.Each project now gets its own environment row for a shared folder. A Personal thread that visits a folder no longer blocks a project from using it, and the reverse also works.
This removes the error class the issue describes, so the report's other suggestions (adopt-on-create, an error that names the owner, a release action) are no longer needed.
Notes
IF EXISTS/IF NOT EXISTS) becausemigrate.test.tsreplays the newest migrations after deleting ledger rows.HOST_DAEMON_PROTOCOL_VERSIONbump: no session payload, WebSocket message, host RPC command, or result changed.Tests
thread-create-shared-workspace-path.test.ts: a project thread starts in a folder the Personal project already claims and gets its own environment. It reproduced the reported 409 before the fix.internal-events-tool-calls.test.ts:update_environment_directoryswitches into a folder another project uses and leaves the other project's claim alone.@bb/db359 pass,@bb/server1325 pass. Repo-wideturbo run typecheckpasses (57 tasks).internal-skill-trees.test.tsfails locally on a file-mode comparison (420 vs 436). It fails identically without these changes — a local umask difference, unrelated.🤖 Generated with Claude Code