Repository navigation
fix(workspaces): scope a departure's ownership scan to the workspaces it leaves - #10751
Conversation
… it leaves Leaving a workspace takes its Project lock exclusively and then asks which workspaces the departing member owns shared resources in. That query scanned every row the member had ever created, in every workspace, and only the answer for the workspaces being left was used. For a member who has created a lot it takes tens of seconds, with the Project lock held throughout: a second departure or a file write in the same Project waits out the 5s lock timeout and fails with "Project is changing; retry the operation", and a retry that arrives after the slow departure commits is refused because the member has already left. Both ownership scans now take the ids the departure hands off (its workspaces, and their Projects), so the lookup reads only those rows. Callers that need every workspace or Project (account deletion, the departure preview, organization departure's Project pass) still scan unscoped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
@myxamediyar Server fix looks right. A few things to fold in here or in the follow-up:
I have a client-only draft of the first two on |
…st re-fetch races the leave Leaving the open workspace moves to the next one, and the visit that records re-fetches the workspace list. When that re-fetch reads the server before the leave commits, it overwrites the optimistic removal and the left workspace shows in the switcher again until the leave settles. useOrderedWorkspacesQuery now hides any workspace whose leave is pending, tracked through a leave mutation key. The leave removes the workspace from the lists again on success, and onSettled returns the list re-fetch, so the leave stays pending until the list is current. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks, folded the client side in here:
One correction on the diagnosis: the leave confirm already refuses every dismissal while it is pending ( |
|
@cubic-dev-ai review this PR |
@myxamediyar I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 9 files
Reply with feedback, questions, or to request a fix.
Turn on auto-fix | Re-trigger cubic
…efused Leaving the open workspace moves to the next one before the server answers. When the server refuses the leave (the last admin, the billing account), the rollback restored the workspace list but left the user on the other workspace. A refusal now takes the user back to the path they left, unless they have moved on to another workspace since. It pushes that path directly: the navigation away may still be in flight, and the rollback makes the left workspace active again, so switchWorkspace would do nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@cubic-dev-ai review this PR |
@myxamediyar I have started the AI code review. It will take a few minutes to complete. |
… the leave hook test check:test-patterns refuses a new wall-clock wait in a unit test, and the refused-leave navigation test flushed React Query with sleep(1). It now yields with flushMacrotask, which is a zero-delay turn. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@cubic-dev-ai review this PR |
@myxamediyar I have started the AI code review. It will take a few minutes to complete. |
Summary
Leaving a workspace (or being removed from one) could take tens of seconds and fail with "Project is changing; retry the operation", then "Insufficient permissions" on the next retry.
The departure takes the workspace's Project lock exclusively, then asks which workspaces the departing member owns shared resources in. That query scanned every row the member had ever created, across every workspace, and only the answer for the workspaces being left was used. For a member who has created a lot it is slow, and the Project lock is held the whole time:
This scopes both ownership scans (
listSharedResourceWorkspaceIdsForUser,listSharedResourceProjectIdsForUser) to the ids the departure hands off (its workspaces, and their Projects), so the lookup reads only those rows. Callers that need every workspace or Project (account deletion, the departure preview, organization departure's Project pass) still scan unscoped, unchanged.The client side no longer invites the retry either:
useOrderedWorkspacesQueryhides any workspace whose leave is pending, success removes it from the lists again, andonSettledreturns the list re-fetch so the leave stays pending until the list is current.A follow-up will move the resource handoff off the request entirely: revoke access in a short transaction and hand off from the outbox.
Type of Change
Testing
shared-resource-handoff.integration.ts: a member owns resources in two workspaces and two Projects. The scoped scans return only the departing workspace and its Project, the departure hands off only those, and the member's folder in the other workspace stays theirs. It fails on the pre-fix code (the scan returns both workspaces); the other 48 cases in the file pass either way.workspace.test.tsx: the switcher keeps a left workspace hidden when a list re-fetch lands before the leave commits (fails on the optimistic cache edit alone), and a refused leave brings it back.bun run type-check, biome,check:react-query,check:test-patterns, related unit suites (81 files), andbun run check:audits(73/74;check:unused-exportsonly flags the docs app's generated.content/registry, which a fresh worktree has not built).Reviewers: on the server, the only behavior change is which rows the two scans read. Each departure call site passes the exact ids it already filtered the result by, so the handoff decisions are unchanged. On the client, the pending-leave filter in
useOrderedWorkspacesQueryis the piece to check.Checklist
🤖 Generated with Claude Code