fix(trpc): let a member re-register a host whose owners all left the org

Draft
HA
20d ago
+79-0

Summary

Follow-up to #7457. Customer report: an org owner cannot re-register a machine whose host row belongs to an account that has left the org.

host.ensure inserts with onConflictDoNothing. If the v2_hosts row already exists, only a host owner may report metadata, and isHostOwner correctly ignores owners who are no longer org members. So for a host whose owners all left, nobody can register that machine again (FORBIDDEN: Only a host owner can report its build). The host is also hidden from everyone, because host.list is filtered by v2_users_hosts.

#7457 deletes such hosts in cleanupRemovedMember, but only when a member who can reach the host leaves. That misses hosts orphaned before #7457 shipped, and hosts where the leaver was the only member.

Change: when ensure hits an existing row that has no owner still in the org, it deletes that row and inserts a fresh one for the caller as owner. The delete cascades workspaces and access rows, which is the same result the removal sweep would have had. A host that still has an owner in the org is never replaced.

Security note: any org member can trigger this for an orphaned machine id. That gives them no access to the old data (it is deleted). It only does what the existing removal policy already does.

No tRPC input/output change.

Test plan

  • bun test src/router/host in packages/trpc: 18 pass, 2 new (orphan is replaced and granted; host with an owner in the org is never replaced)
  • bun run typecheck in packages/trpc
  • biome check packages/trpc/src/router/host
  • Real app on a Neon branch: delete a member's membership row directly (old-style removal), then register their machine id as another member

Summary by cubic

Fixes host registration for machines whose owners all left the organization. host.ensure previously refused with "Only a host owner can report its build" when the org's v2_hosts row was owned only by a departed member. #7457 sweeps such hosts when a member is removed, but older orphans were never cleaned up.

Now, when the existing row has no owner who is still an org member, ensure deletes the row (cascading its workspaces and access rows, as the removal sweep does) and registers the machine fresh for the caller.