fix(desktop): say a host is offline when deleting its session

Merged
KI
3h ago
+519-57

Summary

  • Relay calls from the desktop host-service client wait for a fresh sign-in token, so an expired token no longer turns "host is offline" into "Unauthorized".
  • The delete dialog reports an offline host, disables Delete, and links to that host's settings.
  • A relay refusal of a token that could not be refreshed reads as a sign-in problem.

Why / Context

A user replaced a laptop and could not delete the old machine's sessions. The toast said Failed to delete …: Unauthorized.

Sessions live only on their host, so deleting one always goes to that host through the relay. Two things went wrong on top of that:

  1. The relay token rotates hourly. getJwt() returns the cached token and refreshes in the background, so the first call after an idle period goes out with the expired token and the relay answers 401 before it can say the host is offline.
  2. The dialog had no case for an offline host. The relay's SERVICE_UNAVAILABLE fell through as a generic failure.

How It Works

  • resolveHostServiceHeaders waits for a token refresh when the host is reached through the relay and the cached token is missing or past its expiry, then builds the same headers as before. createHostServiceLinks accepts an async headers.
  • The wait ends 5 seconds after the refresh began. The refresh is not cancelled, and callers that arrive later do not wait again. A failed refresh is not retried for 10 seconds.
  • normalizeDestroyWorkspaceError (moved to its own util) adds two kinds, both only for relay-reached hosts: host-offline for SERVICE_UNAVAILABLE, and sign-in-expired for UNAUTHORIZED while the cached token is past its expiry. Both carry a message, so the bulk delete flow shows them with no change.
  • useDestroyDialogState reads the kind from the inspect call it already makes on open. DestroyConfirmPane shows the notice and a "Host settings" link. A delete that fails as offline after confirm shows the same link as a toast action. While the notice is shown, the dialog asks the host again every 5 seconds.

Manual QA

Run in the dev desktop app over CDP, against the local relay, with a session seeded for a roster host that is offline.

  • Delete dialog for the offline host's session shows "The host is offline. Try again when it's back online, or delete the host if it's gone for good. Host settings", and Delete is disabled.

  • "Host settings" closes the dialog and opens /settings/hosts/<hostId> with the Delete host section.

  • Expired cached token, old header path: relay answers 401 Unauthorized.

  • Expired cached token, new header path: token is refreshed first and the relay answers 503 Host is not online.

  • Expired cached token with /api/auth/token blocked: Delete stays enabled and the toast reads "Failed to delete …: Your sign-in couldn't be refreshed. Check your connection, then try again."

  • Delete confirmed before the offline check returns (2.5 s added latency): toast reads "Failed to delete …: The host is offline. Try again when it's back online." with a "Host settings" button, which opens /settings/hosts/<hostId>.

  • Local session: dialog unchanged, Delete enabled.

  • Offline notice open for 11 s: two rechecks sent, none after the dialog closes. Not run: a host that actually comes back, since the dev roster host cannot be brought online.

  • Refresh slowed to 8 s: the relay call waited 5.0 s, the next call waited 0 ms, and the token arrived at 8.5 s.

  • Refresh blocked: the call fails at once, the next call does not retry, and the refresh works again after the cool-down.

Testing

  • bun test for host-service-auth, useDestroyWorkspace, and the DashboardSidebar components (81 pass)
  • tsc --noEmit in apps/desktop and packages/workspace-client
  • bun run check:i18n
  • biome check on the touched folders

Design Decisions

  • No "remove this session" action for an offline host. A session row for an unreachable host is a last-seen copy on the device. Dropping it would not delete anything on the host, the row would return when the host reconnects, and an asleep machine looks the same as a wiped one. Deleting the host is the action for a machine that is gone, and it already exists.
  • No new ownership check. The link only navigates. The host settings page shows Delete host to the host owner or an org owner, and host.delete enforces the same.

Known Limitations

  • Only getHostServiceClientByUrl waits for the token. The workspace view and chat clients still build headers synchronously.
  • With a slow API and an expired token, a relay call waits up to 5 seconds before it goes out with the old token.
  • Turkish and Vietnamese keep "host" untranslated, to match the existing strings in those catalogs.

Summary by cubic

Fixing the delete-session flow so deleting a session on a host that is offline says the host is offline instead of failing with "Unauthorized".

  • Relay calls now wait for a fresh sign-in token before connecting, so the relay can report the host offline instead of rejecting the stale token.
  • The wait is capped at 5 seconds; a failed refresh is not retried for 10 seconds, and the dialog re-checks the host every 5 seconds while offline.
  • The delete dialog reports an offline host, disables Delete, and links to that host's settings.
  • Only relay-reached hosts can read as offline or as a sign-in problem; a local host uses the same error code for its own failures.
  • A refusal of a token that couldn't be refreshed reads as a sign-in problem.

Summary by CodeRabbit

  • New Features

    • Workspace deletion now clearly indicates when its host is offline, disables deletion until a preview is available, and provides a shortcut to host settings.
    • Offline-host and sign-in refresh messages are available in additional languages.
  • Bug Fixes

    • Host-service requests refresh sign-in credentials when needed, while hosts using their own credentials continue to use them.
    • Sign-in refresh requests now time out after 10 seconds, and expired sign-in or offline-host errors are reported more clearly.