| 1 | import type { RemoteConnectionStatus } from "./types"; |
| 2 | |
| 3 | export type RemoteConnectionErrorKind = |
| 4 | | "connection_failed" |
| 5 | | "auth_failed" |
| 6 | | "host_key_rejected" |
| 7 | | "host_key_mismatch" |
| 8 | | "degraded"; |
| 9 | |
| 10 | export type RemoteConnectionErrorSummaryKey = `remote.error.summary.${RemoteConnectionErrorKind}`; |
| 11 | |
| 12 | export function remoteConnectionErrorKind(status?: RemoteConnectionStatus): RemoteConnectionErrorKind { |
| 13 | if (status?.state === "degraded") return "degraded"; |
| 14 | return status?.errorDetails?.code ?? "connection_failed"; |
| 15 | } |
| 16 | |
| 17 | export function remoteConnectionErrorSummaryKey(status?: RemoteConnectionStatus): RemoteConnectionErrorSummaryKey { |
| 18 | return `remote.error.summary.${remoteConnectionErrorKind(status)}`; |
| 19 | } |
| 20 | |
| 21 | export function isRemoteHostKeyMismatch(status?: RemoteConnectionStatus): boolean { |
| 22 | return remoteConnectionErrorKind(status) === "host_key_mismatch"; |
| 23 | } |
| 24 | |
| 25 | export function isRemoteTerminalFailure(status?: RemoteConnectionStatus): boolean { |
| 26 | return status?.state === "stopped" && Boolean(status.error); |
| 27 | } |
| 28 | |
| 29 | export function isRemoteDegradedWarning(status?: RemoteConnectionStatus): boolean { |
| 30 | return status?.state === "degraded" && Boolean(status.error); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * A local runtime on the serve host owns the session. Mirrors the refusal |
| 35 | * wordings desktop/remote_tab.go accepts in remoteSessionTakenOver plus the |
| 36 | * transcript API's conflict surface ("remote transcript read failed (HTTP 409)"), |
| 37 | * which is how a spectator's Follow request fails. |
| 38 | */ |
| 39 | export function isRemoteTakeoverError(error: unknown): boolean { |
| 40 | const message = error instanceof Error ? error.message : String(error ?? ""); |
| 41 | return /\(HTTP 409\)/.test(message) |
| 42 | || /taken over by a local Reasonix|writer is owned by another runtime|in use by another Reasonix process/.test(message); |
| 43 | } |
| 44 |