PR #10178 review: say a host is offline when deleting its session

PR #10178 Review workspace Author: Kitenite Head: 68005ccd

Summary

A user who replaced a laptop could not delete the old machine's sessions; the toast said "Unauthorized". The PR makes relay-bound host-service calls wait for a fresh sign-in JWT, so the relay can answer "Host is not online" instead of rejecting a stale token. It adds host-offline and sign-in-expired error kinds. The delete dialog now shows an offline notice, disables Delete, and links to the host's settings page.

Verdict

Minor issues

The approach is sound and the relay behaviour it depends on is confirmed. Two edges need tightening: the offline mapping trusts every SERVICE_UNAVAILABLE, and a failed token refresh is retried with no backoff on every relay call.

Findings

Minor

Every SERVICE_UNAVAILABLE reads as "host offline", for any host kind

apps/desktop/src/renderer/hooks/host-service/useDestroyWorkspace/utils/normalizeDestroyWorkspaceError/normalizeDestroyWorkspaceError.ts:33 and …/useDestroyDialogState/useDestroyDialogState.ts:85,97

The mapping is keyed only on the tRPC code. But host-service itself also throws SERVICE_UNAVAILABLE: in packages/host-service/src/trpc/worker-abort.ts:35 (process shutting down) and router/host/cloud-api-error.ts:80 (the cloud API is unreachable). For a local host target, the dialog would then say "delete the host if it's gone for good" and link to the settings of this machine. For a sandbox target, hostId is the workspace id (useWorkspaceHostUrl.ts:50), so "Host settings" would open /settings/hosts/<workspaceId>, a page that does not exist. The cleanup router does not throw that code today, so this is latent. But the dialog has no guard if that changes.

Fix: set isHostOffline and offer the link only when hostTarget.status === "ready" && hostTarget.kind === "remote". Or derive hostId for the link only from a remote target.

Minor

A failed refresh is retried with no backoff, and can stall calls that still had a valid token

apps/desktop/src/renderer/lib/host-service-auth.ts:63, apps/desktop/src/renderer/lib/auth-client.ts:109

About 100 renderer files use getHostServiceClientByUrl, so every relay call now goes through ensureFreshJwt(). jwtIsFresh() is already false in the last 60 s before expiry, so a token the relay would still accept now waits for /token. If the API is slow or unreachable, each such call waits up to 10 s. When a refresh fails, jwtRefreshInFlight is cleared, so the next relay call starts another 10 s attempt. Also, getHostServiceHeaders → getJwt() starts one more background refresh right after the awaited one fails, which doubles the /token requests. The PR's Known Limitations section mentions the 10 s wait, but not that it repeats for every call.

Fix: after a refresh fails, record the time. For a short cool-down (say 30 s), make resolveHostServiceHeaders skip the await and use the cached token. Also skip the await while the cached token has not actually expired yet (only in the leeway window), and let the background refresh run.

Nit

sign-in-expired also fires when there is no token at all

normalizeDestroyWorkspaceError.ts:45, call sites in useDestroyWorkspace.ts:90,105

!jwtIsFresh() is true when jwt is null (signed out, or the JWT was never seeded). The user then sees "Check your connection", which is the wrong hint. This is rare, and the message is still better than "Unauthorized".

Fix (optional): pass getJwt() !== null && !jwtIsFresh(), or accept the current behaviour as is.

No issues with structure, i18n or tests. The new util is in its own folder with a co-located test. New strings use msg/Trans, and all 17 catalogs are filled. Async headers is supported by httpBatchStreamLink. Hosts reached with their own secret (local, sandbox) skip the wait, and a unit test covers that.

Evidence

Relay (API level): verified

Ran against the local API (:3001) and the local relay (:3013) at 68005ccd. The script signed in with local dev credentials, minted a JWT from /api/auth/token, and registered a roster host (host.ensure) that never connected. It then called workspaceCleanup.inspect through the relay:

sign-in 200 | org set: true | /api/auth/token 200 | jwt minted: true
host.ensure (roster host, never connected) 200
GET relay workspaceCleanup.inspect with expired token (old header path): HTTP 401 code=UNAUTHORIZED message="Unauthorized"
GET relay workspaceCleanup.inspect with freshly minted token (new header path): HTTP 503 code=SERVICE_UNAVAILABLE message="Host is not online"

This confirms the premise: with a token the relay accepts, an offline host comes back as SERVICE_UNAVAILABLE, which the new code maps to host-offline. Caveat: the "expired" token was made by rewriting exp on a real token, so its signature is also invalid. The 401 shows the relay refuses it, but it does not prove the cause is the expiry alone.

Unit tests: verified

Ran bun test on host-service-auth.test.ts, useDestroyWorkspace/ and the whole DashboardSidebar/ tree: 227 pass, 0 fail. This covers the new normalizeDestroyWorkspaceError kinds, the refresh-before-headers path, the secret-host skip, and the DestroyConfirmPane offline notice (Delete disabled, "Host settings" calls back).

Desktop UI (delete dialog, toast action): not verified

I could not run the desktop app long enough to open the dialog. The box has 8 GB of RAM, and its memory guard killed the Electron dev process three times:

The only screenshot I have is from the third attempt, taken before the UI painted. It shows nothing about the change:

Blank dark desktop window while the renderer was still loading

The dialog's offline notice, the Host settings navigation and the offline toast action are covered only by the author's manual QA and the unit test above. The author also lists the toast action as not run.

Questions for the author

Test / CI notes