fix(desktop): derive v2 workspace locality from workspace.hostId, not joined v2Hosts

Merged
SA
6mo ago
+70-116

Summary

  • Workspaces that loaded while Electric was still syncing v2Hosts were routed through the cloud relay instead of the local host service. The locality check in v2-workspace/layout.tsx (and 5 sibling sites) leftJoined v2Workspaces → v2Hosts and compared joined.hostMachineId === machineId — when Electric hadn't synced the host row, the joined value was null, the equality failed, and the entire WorkspaceTrpcProvider got pointed at ${RELAY_URL}/hosts/....
  • v2Workspaces.hostId is notNull and FKs to v2Hosts.machineId, so the workspace already carries the value the join was producing. Switched locality to workspace.hostId === machineId everywhere. Routing-only paths drop the v2Hosts join entirely; metadata-display paths (sidebar / list) keep an innerJoin for host name + isOnline.
  • Side-fixes for the same root cause: useAccessibleV2Workspaces now stays innerJoin (cleaner; rows briefly hide while Electric catches up but workspaces remain routable via direct URL); DashboardSidebarPortsList synthesizes a local-host port-query target so local ports show without a synced v2Hosts row; DevicePicker synthesizes "this device" so the picker never loses the local entry.

Test plan

  • bun run lint:fix — clean
  • bun run typecheck — clean
  • bun test (desktop) — 1870 pass
  • Manually clear v2Hosts in tanstack-db inspector, navigate to /v2-workspace/$workspaceId, confirm WorkspaceTrpcProvider connects to http://127.0.0.1:<port> (not the relay) and the workspace loads
  • DevicePicker: with v2Hosts cleared, "this device" still appears as default
  • Sidebar ports: with v2Hosts cleared, local ports still render
  • Don't regress remote routing: open a workspace whose hostId is a different machine; confirm it still routes via ${RELAY_URL}/hosts/<key>

Summary by cubic

Fixes v2 workspace routing by deriving locality from v2Workspaces.hostId === machineId, avoiding relay misrouting when v2Hosts hasn’t synced. Also keeps local ports and “this device” visible even if the local host row is missing.

  • Bug Fixes

    • Route workspace TRPC, notifications, terminal, and “Open in” via local host when hostId === machineId; otherwise use the relay URL.
    • Prevent local ports from disappearing by synthesizing a local host target from machineId + activeHostUrl.
    • Ensure DevicePicker always shows “this device” using machineId when the local v2Hosts row is missing.
  • Refactors

    • Removed v2Hosts leftJoin from routing-only queries; compare hostId directly.
    • Kept innerJoin to v2Hosts only where host metadata (name, isOnline) is displayed.
    • Simplified hostType derivation to hostId === machineId ? "local-device" : "remote-device".

Summary by CodeRabbit

  • Refactor
    • Simplified workspace-to-host relationship handling across the application.
    • Streamlined local and remote device classification logic.
    • Optimized database queries for improved performance.