fix(api): resolve owner's relay when dispatching automations

Merged
SA
3mo ago
+87-70

Automations broke for anyone whose host moved to relay2.

apps/api/src/app/api/automations/dispatch/[id]/route.ts passed relayUrl: env.RELAY_URL — the default (Fly) relay, hardcoded. The MCP route and Slack agent already resolve getRelayUrl(userId), but automation dispatch never did. So when an owner's host tunnels into an overridden relay via relay-url-override, their scheduled automations are dispatched at the default relay, which no longer holds their host, and the run can't reach the machine (programmatic access silently fails).

Fix: resolve getRelayUrl(automation.ownerUserId), matching the other two server-side paths. getRelayUrl reads the same flag the desktop/host use; posthog-node evaluates it remotely (no personalApiKey), so the owner's stored email resolves the targeting correctly.

Found while porting the internal team to relay2 — the host moved, automations kept dialing Fly.


Summary by cubic

Dispatches automations using the owner’s resolved relay instead of the default, and centralizes relay selection in the API so hosts and clients use the same endpoint. Fixes broken automations when hosts moved to relay2 and prevents relay mismatches across desktop, CLI, and host-service.

  • Bug Fixes

    • Automation dispatch now uses getRelayUrl(automation.ownerUserId) instead of env.RELAY_URL, ensuring runs reach hosts on overridden relays.
  • Refactors

    • Added API host.relayEndpoint that returns the user’s relay (reads relay-url-override, falls back to env.RELAY_URL).
    • Desktop renderer and CLI query this endpoint; host-service resolves the relay at connect time; the desktop main process now only supplies a fallback.

Summary by CodeRabbit

  • New Features

    • Relay connections now resolve the appropriate endpoint dynamically through the host service.
    • Automation dispatches use the automation owner’s configured relay endpoint.
    • Desktop and CLI clients retrieve relay endpoint settings through the host API.
  • Bug Fixes

    • Added reliable fallback to the default relay endpoint when the host service is unavailable or no custom endpoint is configured.
    • Improved connection logging when endpoint resolution fails.