Cloud: let every team member reach the team's VMs at once

Merged
AU
2mo ago
+2004-76

Follow-up to #13307. Two accounts on the same Cloud team can both see a team VM in the machine list, but only the account that created it can open it. Team VMs join their creator's personal Freestyle VPC, and each Mac's WireGuard tunnel attaches only to its own account's VPC. A teammate's Mac has no route to the VM's private address and fails with privateRouteRequired.

With this change, a team VM created from an updated Mac lives in a VPC owned by the team, and every member's tunnel is attached to it. All members can link, attach and forward ports to the VM at the same time.

The team gets its own VPC rather than attaching teammates to the creator's personal one. A personal VPC lets its members reach each other, so attaching a teammate to it would expose the creator's personal VMs and Mac.

There is no database migration. Freestyle is the only record of team networks and of which tunnels are attached to them, so production needs the web deploy and the Mac release and nothing else.

Backend (web/)

  • A team's VPC is found in Freestyle by its slug, cmux-team-net-<hash of team id>, the same way the personal VPC slug is derived. Nothing about team networks is stored in Postgres.

  • The team VPC has no members-reach-each-other rule. Each team VM gets an ingress rule from the team VPC, so tunnels and other team VMs can reach it, but team VMs can't reach members' Macs.

  • Create, base, fork and restore place a VM on the team network only when all of these hold:

    • the client sends X-Cmux-Private-Network-Routing: team-networks;
    • the VM is billed to a team rather than the caller's personal account;
    • the caller is a current member of that team in the Stack directory;
    • the team already has a VPC in Freestyle, or has at least two members.

    Otherwise the VM goes on the personal network exactly as before. The span attribute cmux.vm.network.team_fallback records no_capability, solo_team, not_member, directory_error or directory_timeout.

  • Membership is checked before the Freestyle lookup, on reuse as well as on create, so a caller who left the team gets not_member and their personal network. Only creating a team VPC needs two members; the last remaining member keeps an existing one. A directory error or timeout falls back to the personal network. A Freestyle lookup error fails the create rather than risk a second team VPC.

  • The Stack directory lookup has a 3 s deadline through Effect.timeout, which interrupts the lookup and aborts the AbortSignal handed to the directory. The Stack SDK doesn't accept a signal, so the abort is checked before each SDK call and can't cancel a request already in flight.

  • Tunnel enroll and read look up the VPC of every team the caller belongs to and attach the tunnel to each one it isn't already attached to. They then detach any other non-home attachment, which can only belong to a team the caller has left. If any team lookup fails, nothing is detached on that pass. The response keeps network as the home network for older clients and adds networks[], home first. A failed or overlapping attach is logged and skipped, with a readable status, code and message chain; enrollment never fails because of it.

  • The reconcile cron detaches tunnels from team VPCs their owner no longer belongs to, or whose tunnel was revoked. It pages through teams that have live team-billed VMs, 50 at a time within a 60 s budget, after the existing status and heal work. For each team it reads the VPC and its tunnel list from Freestyle and matches the tunnel ids against cloud_vm_tunnels to find the owner. Tunnels with no row are skipped, because the Freestyle account also holds tunnels that other environments issued. A failed lookup or detach skips that team until the next run.

  • Revoking a tunnel or an access grant, and deleting an account, need no extra cleanup: deleting the Freestyle tunnel removes its attachments. Account deletion still deletes only the personal VPC. Team VPCs survive it.

  • Freestyle returns a generic CONFLICT for every 409. The attach passes no pinned address or remote CIDRs, so only a 409 CONFLICT is treated as a network overlap; any other 409 is a provider error.

  • One gap: the cron only visits teams with a live team-billed VM, so a former member's attachment to a team with no live VMs stays until that team has a VM again or that member's Mac re-enrolls. The attachment grants a route into a VPC with nothing in it.

  • The mock driver is unchanged; it never had private networking.

Mac (CmuxCloud)

  • WireGuard AllowedIPs is the union of every returned network CIDR.
  • VMClient requests send the capability header.
  • When a machine's private address falls outside the hub's routes, the hub re-enrolls once. The refresh is single-flight and throttled to 15 s. If the routes changed, the hub restarts with the new config and keeps its leases, and the private route is resolved again.
  • Enrollment's file and network work runs in a @concurrent static helper, off the hub actor.

Team VMs created before this change, or from a Mac without it, stay in their creator's personal VPC and remain reachable only by that account. Both Macs need this build: the creator's to place the VM on the team network, and each teammate's to route the team CIDR.

#14515 (team scope during auth bootstrap) and #14600 (personal VMs in implicit team lists) touch the same list and create paths but not network placement. #14600 also edits tunnel/route.ts, repository.ts and workflows.ts, so whichever lands second may need a small rebase.

No v2 socket method was added or allowlisted; the existing tunnel status payload gains a network_cidrs field. Localization audit: no user-facing strings were added or changed.

Verification

At 31e86ac5fe (tables and migration dropped):

  • git diff 4c5272e915 HEAD -- web/db is empty.
  • The eight suites this touches (vm-private-network, vm-team-private-network, vm-workflows, vm-freestyle-provider, vm-provider-gateway-private-networking, account-deletion-lock, vm-route-auth, vm-cron-reconcile-route): 265 pass, 71 skip, 0 fail. The skips are dbTest cases gated on CMUX_DB_TEST=1, including the new ones for the two repository queries (tunnels by provider id, and paged teams with live team-billed VMs). Those run against Postgres in CI only.
  • New coverage:
    • vm-workflows.test.ts: the cron detaches removed, revoked and departed members' tunnels, and leaves tunnels with no row alone. A team whose lookup or directory call fails doesn't block the others. The cron stops at the budget, and pages by team.
    • vm-freestyle-provider.test.ts: the team VPC lookup by slug (404 is "none") and its tunnel list come straight from Freestyle.
    • account-deletion-lock.test.ts: account deletion deletes the Freestyle tunnel and only the personal VPC, and never calls detach.
    • vm-private-network.test.ts: enroll attaches missing team VPCs, detaches stale ones, and detaches nothing when a lookup fails.
  • bun run typecheck, bun run lint:complexity (no baseline growth) and bun run db:check pass. eslint on the changed files reports 0 errors; its 3 warnings are already on main.
  • The tag's dev backend is redeployed at 31e86ac5fe.

At 4ef6be30e7:

  • Live bug found during dogfood, now fixed. VmProviderGatewayLive called the Freestyle driver's attachTunnelNetwork and detachTunnelNetwork unbound. this was undefined, so every team attach threw a TypeError before any request was made, and the skip warning logged the cause as {}. The gateway now calls them on the driver object.
    • Regression: web/tests/vm-provider-gateway-private-networking.test.ts drives the real FreestyleProvider through the live gateway with a fake Freestyle client. It fails on d1835573c7 (test only) with TypeError: undefined is not an object (evaluating 'this.client'), and passes on 4ef6be30e7 (fix).
    • The earlier tests missed this because they used object-literal fakes or called the driver method bound.

Earlier:

  • Regression: web/tests/vm-team-private-network.test.ts (bun test --isolate) fails 0/2 on 6dc370b0d5 (test only), with assertion errors on the attach calls and the create network options. It passes 2/2 on c0870872ea (fix).
  • Regression for team network reuse: on 7d95dfb065 (test only), three resolveOwnerNetwork tests fail with assertion errors: reuse by the sole current member, which the old code allowed without consulting the directory; a removed member; and a directory failure with an existing network. They pass on 7ec85c49f3 and still pass at 31e86ac5fe.
  • swift test --package-path Packages/macOS/CmuxCloud (at 2d860b5dac): 11 tests pass. They cover the tunnel payload decode and CIDR union, plus hub refresh: single-flight, throttle, idle stop, and stop during a refresh.
  • ./scripts/sync-test-wiring --check and the four scripts/verify-local.py checks pass.
  • App-host CI lane at 2d860b5dac (job 108736504435): all six CloudPrivateRouteSelectionTests cases ran and passed, including "refreshes hub routes before selecting a newly added team address" and "still rejects an address that remains outside refreshed routes". The lane ran 234 tests in 15 suites, 0 failed.
  • Build: fleet builds of 0b5acc5563 and 2d860b5dac exited 65 in the app's merge-module step, with a type mismatch in BrowserDesignModeToolbarButton.body. This branch doesn't touch that view. The cause is #14507's app module flags combined with the fleet's --swift-frontend-workaround on the worker's Xcode 26.3, which #14772 also hit.
    • Resubmitted with CMUX_RELOAD_APP_EMIT_MODULE=1 as job be1f03e641a3fdcc6294738f. It compiled (cmux_build 605 s), but the artifact upload got a controller 507 Insufficient Storage, so there is no HQ link.
    • The same build is installed on both test Macs. Every commit since changes only web/, so the app is unchanged.
  • Live test at 4ef6be30e7, against the tag's dev backend, signed in as the first account with a two-member team selected:
    • A new team VM landed on a team VPC (10.82.45.0/24), separate from the account's personal VPC (10.16.170.0/24). That VPC's slug matches the one 31e86ac5fe derives, so the no-migration backend finds it without a row.
    • After the fix, the Mac's tunnel routes both, 10.16.170.0/24, fd98:deb9:4c94::/64, 10.82.45.0/24, fd80:9c15:5030::/64, and a machine workspace opens on the team VM. Before the fix, only the personal range was routed.
  • Not yet run: the second account opening the same VM from the other Mac at the same time. That Mac has the build but isn't signed in yet.

— Gatehouse (callsign pending), run run_team_shared_private_network_20260926

🤖 Generated with Claude Code