feat(desktop,host-service): owners and creators can delete a project

KI
2mo ago
+2985-334

Links

Summary

  • A project copy on a device can be deleted by:
    • an owner: an organization owner, or an owner of that device;
    • its creator, as long as no one else is using it.
  • "Using it" means another user has a workspace in the project on that device that isn't archived. A creator's own workspaces don't block anything; they are deleted with the project.
  • The host service now enforces this rule in project.remove, so it's no longer only a desktop safeguard.
  • The delete dialog lists every device with a copy, and you choose which ones to delete. Unselected devices keep their copy.

Why / Context

Before this PR, only organization owners could delete a project. A member couldn't remove a project they had just created on their own machine: Settings showed Delete disabled, and the sidebar menu had no Delete item. The old dialog also deleted from every reachable device at once, which is too broad once non-owners can delete.

How It Works

Host service

  • Migration 0037_project_creator adds projects.created_by_user_id. persistLocalProject sets it from the caller's user id, and a later re-setup doesn't overwrite it. Projects created before this change have no creator, so only owners can delete them.
  • Adopted worktrees now record the acting user as their creator. Without this, a creator's own adopted workspaces would count as someone else using the project.
  • project.remove calls requireProjectDeletionAccess:
    • A creator whose project has no other users' workspaces is allowed with no network call.
    • Anyone else needs the API to confirm they are an owner, through host.authorizeProjectDeletion.
    • A request with no user id is rejected.
  • Workspaces with no recorded creator count as someone else's, to stay on the safe side.
  • project.list and project:changed now include createdByUserId.

API

  • host.authorizeProjectDeletion is built the same way as authorizeUpdate. The host calls it with its owner credential, and it answers whether the requester is an organization owner or a device owner.

Desktop

  • useProjectDeletionHosts works out, for each device, whether this user can delete there and how many of other people's workspaces that would remove. It uses data the desktop already syncs, with no extra requests per device:
    • each copy's creator, from the new creatorByHostId on the merged project;
    • other users' workspaces, from the synced host workspaces;
    • ownership, from the organization and device member lists.
  • In the dialog, each device row shows one of:
    • "In use by other people": you're the creator, but someone else has a workspace there;
    • "Owner access required";
    • "Offline";
    • for owners, "Deletes N workspaces other people are using".
  • Default selection is the local device if it's online and deletable. Otherwise it's the only online device you can delete from. With several such remote devices, nothing is preselected.
  • The selection doesn't change while the dialog is open. A ref guard blocks double submission, and a partial failure keeps the dialog open.

Manual QA Checklist

First commit (owner rule), tested in the dev desktop app over CDP. It used a disposable org, a local relay, and two host-service instances with real relay tunnels and real git operations:

  • Before fix: a member's local project had Delete disabled, and the sidebar menu had no Delete
  • Local delete and remote-only delete worked, and the repository folder was kept
  • A remote device going offline while the dialog is open disables its checkbox
  • Project on local, owned-remote and shared-remote devices: only local was preselected, and the shared remote was disabled for the member
  • Org owner on a shared host: clean worktree removed; dirty worktree and repository kept
  • Several remote devices and no local copy: nothing preselected. Disconnecting or reconnecting a device doesn't change the selection

Creator rule, tested in the same setup. The shared device ran with its real owner's credential; the member's session made the requests:

  • Creator, no one else using it: Delete is enabled and the device preselected. Deleting worked, and the host never called the API
  • Creator, another user has a workspace: Settings Delete is disabled with the tooltip, and the dialog row shows "In use by other people". A direct project.remove returns FORBIDDEN after the host calls host.authorizeProjectDeletion with machineId: qa-7768-shared
  • Same project with a copy on a device the member owns: only that copy is deleted; the shared copy and its 2 workspaces are kept
  • After the other user's workspace is archived: the creator can delete, with no API call
  • Project from before creator tracking: "Owner access required", and a direct project.remove returns FORBIDDEN
  • Organization owner: the dialog shows "Deletes 2 workspaces other people are using". The API authorizes, and the project and its workspaces are deleted
  • Local project created through Add project → Create new project records the creator. Deleting from the sidebar's "Delete Project…" removes the row and keeps the repository
  • Out-of-date warning (feefa4b7a7): as org owner, open the dialog while no one else uses the project, then add another user's workspace without the desktop noticing. Delete is refused with "Other people are using this project on this device. Review the warning and try again." The dialog stays open and refreshes to "Deletes 1 workspace other people are using". The second Delete succeeds
  • Found during this run and fixed in 2d47a5c4e7: the dialog waited on the global host-workspaces isReady. That never settles when cloud sandbox brokering doesn't, so Delete stayed disabled with a device selected

Setup shortcuts: the fixture projects were created through each device's project.create with the relay's user header. Workspace rows were inserted or archived directly in the device's SQLite, and organization roles were changed in the QA database.

Testing

  • packages/host-service: project-deletion-access.test.ts has 7 tests, including creator, others' workspaces, archived rows, rows with no creator, rejection with no user id, and the owner path through the API. bun test src/trpc/router/project src/db src/projects src/workspaces: 89 pass.
  • packages/trpc: project-deletion-access.test.ts has 5 tests: a credential that isn't the host owner is rejected, a request can't cross organizations, organization owners and device owners are allowed, and anyone else is refused.
  • apps/desktop: 14 isolated hook and dialog tests, plus the policy and selection unit tests. bun test src/renderer/routes/_authenticated src/renderer/hooks/host-*: 1270 pass.
  • tsc --noEmit passes in host-service, trpc and desktop. Biome passes on the changed files. bun run check:i18n passes, with all 17 locales translated.

Design Decisions

  • The creator is stored per device, on the host. Projects are local to each device, and v2_projects has no creator column. Storing the creator on each host follows how workspaces already record theirs.
  • The desktop works out eligibility from synced data instead of querying each device, so the sidebar doesn't add relay traffic for every project.
  • The creator check runs on the host with no network call. Only the owner path asks the API, so a creator can still delete their own unused project on their own machine even if the API can't be reached.

Risks / Rollout

  • Deploy order: the API must ship host.authorizeProjectDeletion before a host with this change is released. If a new host talks to an older API, owners who aren't the creator get an error when they delete, while creators still work. The normal order (API deploys on merge, host ships with the next desktop release) handles this.
  • Older desktops against a new host: an older desktop sends the user id header, so the host applies the same rule. It may show Delete for an organization owner on a host they can't access, and the host rejects the request with FORBIDDEN.
  • Older hosts against a new desktop: there's no createdByUserId, so only the owner rule applies, which is the same as the first commit.
  • Rollback: revert the PR. The added column is nullable and nothing else reads it.

Known Limitations

  • Organization owners still need access to a host to reach a private project on it. This PR doesn't add org-wide project discovery.
  • "Using it" is based on workspaces, not live terminal or agent sessions.
  • Owners need the API to delete a project that isn't their own unused one. That includes every project created before this change, since none has a recorded creator. If the API can't be reached, the host refuses with a readable PRECONDITION_FAILED rather than a 500 (258d64ebcf). Creators of unused projects can still delete offline.
  • This guards against accidentally deleting a project other people are using; it doesn't stop someone doing it deliberately. The owner's warning can't be out of date: the desktop sends the count it showed, and the host refuses if more exist (feefa4b7a7).
  • The "in use" check and the delete aren't atomic. If another user creates a workspace during the seconds git worktree remove runs, the cascade removes that workspace's row and leaves its worktree on disk. The same race already existed for owners on main.
  • The rule protects project deletion only. The host has no per-user check on archiving or deleting individual workspaces, so a user with access to the device could archive other people's workspaces first. That gap predates this PR.
  • Deleting only a remote copy still navigates away from the project (settings redirect, or leaving the active project in the sidebar).

Summary by CodeRabbit

  • New Features
    • Choose which eligible online devices to delete a project from. Project creators can delete their copies when no one else is using them; owners can delete eligible copies across devices.
    • Review device availability and whether other people are using the project before confirming deletion.
  • Updates
    • Deletion details clarify that unselected devices and devices where deletion fails keep their copies. Selected devices’ workspaces are removed for their users; worktrees with uncommitted changes remain on disk.
    • Workspace deletion warns when it affects other people. Deletion messaging is updated across supported languages.