Skip to content

fix(workspaces): scope a departure's ownership scan to the workspaces it leaves - #10751

Merged
waleedlatif1 merged 5 commits into
stagingfrom
fix/scope-departure-ownership-scan
Dec 4, 2026
Merged

waleedlatif1 merged 5 commits into
stagingfrom
fix/scope-departure-ownership-scan

Conversation

@myxamediyar

@myxamediyar myxamediyar commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • a second departure, or a file write in the same Project, waits out the 5s lock timeout and gets 409 "Project is changing"
  • a retry that arrives after the slow departure commits gets 403 "Insufficient permissions", because the member has already left

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:

  • Leave and remove teammate are optimistic (cherry-picked from 421f5b9). Confirming closes the modal, the workspace drops out of every cached workspace list (and pins), the sidebar moves to the next workspace at once, and a refusal rolls back with the server's reason. A removed teammate drops out of the permissions and members caches the same way.
  • Leaving the open workspace navigates 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 would overwrite the optimistic removal, so useOrderedWorkspacesQuery hides any workspace whose leave is pending, success removes it from the lists again, and onSettled returns 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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

  • New integration case in 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.
  • Re-ran the integration suites over these paths against PostgreSQL: shared resource handoff, v2 member management, run stops on removal, Project foundation, file versions, creator lifetime, organization removal lock order, SCIM managed membership, deleted identity, offboarding, storage sweep, private purge, file search owners (15 files, 290 tests).
  • 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.
  • Drove the leave in a local browser with the leave request held for 3s: the modal closed and the view moved at once, the switcher never listed the left workspace (40 samples across the hold and after), and a refused leave (last admin) showed its toast and restored the workspace.
  • bun run type-check, biome, check:react-query, check:test-patterns, related unit suites (81 files), and bun run check:audits (73/74; check:unused-exports only 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 useOrderedWorkspacesQuery is the piece to check.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

🤖 Generated with Claude Code

… 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>
@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Dec 4, 2026 12:49am UTC

Request Review

@waleedlatif1

waleedlatif1 commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

@myxamediyar Server fix looks right. A few things to fold in here or in the follow-up:

  • Optimistic leave (client): useLeaveWorkspace should drop the workspace from every workspaceKeys.lists() cache in onMutate (and from pinnedWorkspaceIds), then roll back on error. The header confirm modal can close on click, and the sidebar can switch to the next workspace right away. Today the modal can be closed while the request is pending and Leave clicked again; that second request is what times out with "Project is changing", and the retry 403s once the first one commits.
  • Remove teammate: useRemoveWorkspaceMember has the same shape. Drop the user from workspaceKeys.permissions and workspaceKeys.members optimistically, and roll back on refusal.
  • Org member removal: fine to keep it pessimistic, since its dialog shows refusals and credential impact inline. It benefits from this scan scoping too.
  • Account deletion still runs the unscoped workspace scan up to 3 times in one transaction. The scans are needed for discovery, but a partial index on workspace_files (user_id, workspace_id) for the shared contexts would make the unscoped scan cheap everywhere.

I have a client-only draft of the first two on fix/leave-workspace (421f5b9) if you want to cherry-pick it.

waleedlatif1 and others added 2 commits December 3, 2026 16:03
…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>
@myxamediyar

Copy link
Copy Markdown
Contributor Author

Thanks, folded the client side in here:

  • Optimistic leave + remove teammate: cherry-picked 421f5b9 as is.
  • On top of it (6abfe22): driving the leave in a browser showed the left workspace coming back in the switcher right after the modal closed. Leaving the open workspace navigates to the next one, and the visit it records invalidates workspaceKeys.lists(). When that re-fetch reads the server before the leave commits, it overwrites the optimistic removal. useOrderedWorkspacesQuery now hides workspaces with a pending leave (leave mutation key + useMutationState), success removes the workspace from the lists again, and onSettled returns the list re-fetch so the leave stays pending until the list is current. The regression is in workspace.test.tsx and fails on the cherry-pick alone. With the leave request held for 3s in a local browser run, the switcher never listed the workspace again.
  • Remove teammate: nothing re-fetches the permissions/members keys mid-request, so the snapshot rollback holds as is.
  • Org member removal: left pessimistic.
  • Account deletion index: I'd rather do this in its own PR. The slow scan's stats don't say which of the eight UNION branches dominates, so I'd confirm with EXPLAIN (ANALYZE, BUFFERS) for a heavy user first. If it is workspace_files, the partial index predicate has to match context IN (...) AND chat_id IS NULL for the planner to use it, and it's a CONCURRENTLY migration on a big table.

One correction on the diagnosis: the leave confirm already refuses every dismissal while it is pending (pending sets dismissDisabled, wired to this modal since v0.9.44), and the confirm button is disabled, so the request that timed out couldn't have come from the same modal. It must have come from a second page load or tab. The optimistic flow still removes the reason to try again.

@myxamediyar

Copy link
Copy Markdown
Contributor Author

@greptile

@myxamediyar

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@myxamediyar I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@myxamediyar

Copy link
Copy Markdown
Contributor Author

@greptile

@myxamediyar

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@myxamediyar I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

… 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>
@myxamediyar

Copy link
Copy Markdown
Contributor Author

@greptile

@myxamediyar

Copy link
Copy Markdown
Contributor Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@myxamediyar I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@myxamediyar
myxamediyar marked this pull request as ready for review December 4, 2026 01:02

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 62291f2 into staging Dec 4, 2026
68 of 83 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/scope-departure-ownership-scan branch December 4, 2026 01:24

This branch was previously deployed

1 inactive deployment
Preview — 00e9b723 Deployed Dec 4, 2026 by vercel[bot]
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.

2 participants