// What the Sessions list needs to know about the machines a person added over // SSH, kept apart from drawing: how a device's connection reads as one status, // and how a session read from another machine becomes a list row. // // A REMOTE ROW CAN NEVER BE A LOCAL ONE. Its id is "remote:" and the server's // key, which is the device's id and the session's id together, so the same // session id, title or folder on two machines stays two rows and neither can // equal a local session's id. The list keys, pins and hides local sessions by // their own id and never meets a remote one by accident. // // AGES ARE THE OTHER MACHINE'S. Two clocks differ, so every time a device sends // is moved by the difference between this machine's now and the device's own // `now` that came with the answer (remoteNow); without one the times stand. import type { Row } from "./components/SessionList"; import type { DeviceWorkspaceSubject } from "./device-workspace"; import type { MessageKey } from "./i18n"; import type { Provider, WaitingBlock } from "./types"; /** How the deck reports one device's connection (GET /api/devices). */ export interface DeviceLink { state: "offline" | "connecting" | "connected"; checkedAt?: number | null; /** The type of the last answer a read or check got, or null before one. */ last?: string | null; /** A person disconnected it: nothing asks it again until they press Connect. */ paused?: boolean; /** For a last answer of no_remote_ccdeck: whether ccdeck is missing there * (not_installed) or there but not running (not_running). */ detail?: "not_installed" | "not_running"; /** A press on Install or Start that the deck is carrying out there now. */ busy?: "installing" | "starting"; } export interface DeviceInfo { id: string; name: string; alias?: string; host?: string; user?: string; port?: number; link: DeviceLink; } /** One session as a device sent it (the deck's remoteSessions). */ export interface RemoteSession { key: string; id: string; device?: { id: string; name: string }; provider: string | null; cwd: string | null; project: string | null; name: string | null; model: string | null; state: "waiting" | "working" | "idle" | "ended"; since: number | null; startedAt: number | null; lastEventAt: number | null; waiting: { kind: string; message: string; since: number | null; tool: string | null } | null; } /** What the Devices dialog opens on: a device (on its new-session form), or * the add form; nothing is the first device, or the add form when there is none. */ export interface DevicesTarget { id?: string; startNew?: boolean; add?: boolean } export type DeviceStatus = "connected" | "connecting" | "setting-up" | "offline" | "paused" | "needs-setup" | "error"; /** The answers that say something on the device itself has to change, or a * person has to decide, before connecting again can work. */ export const NEEDS_SETUP: ReadonlySet = new Set([ "no_remote_ccdeck", "version_too_old", "ssh_missing", "host_key_unknown", "host_key_changed", "trust_expired", "auth_failed", "tailscale_approval", ]); export interface StatusOf { status: DeviceStatus; /** The last answer's type for a status that has one to word. */ reason: string | null; /** For ccdeck missing there: not_installed or not_running. */ detail?: string | null; } /** One status for a device's link: what its body says and which action it offers. */ export function deviceStatus(link: DeviceLink | null | undefined): StatusOf { const last = link?.last ?? null; // Installing or starting ccdeck there outranks whatever the last read said. if (link?.busy) return { status: "setting-up", reason: link.busy }; if (link?.state === "connecting") return { status: "connecting", reason: null }; if (last && NEEDS_SETUP.has(last)) { return { status: "needs-setup", reason: last, ...(last === "no_remote_ccdeck" && link?.detail ? { detail: link.detail } : {}) }; } if (last === "failed") return { status: "error", reason: last }; if (link?.state === "connected") return { status: "connected", reason: null }; if (link?.paused) return { status: "paused", reason: null }; return { status: "offline", reason: last === "unreachable" ? last : null }; } /** How a state is drawn: the mark's tone, the same set the Devices dialog and * the Sessions list's device lines use. */ export type StateTone = "active" | "ok" | "done" | "warn" | "err" | "idle"; export interface StateWord { key: MessageKey; tone: StateTone } /** A device's connection as one word and one tone, for every place that says * it: the Sessions list's device line and the Devices dialog say the same. */ export function deviceWord({ status, reason, detail }: StatusOf): StateWord { if (status === "connected") return { key: "board.devices.connected", tone: "ok" }; if (status === "connecting") return { key: "board.devices.connecting", tone: "active" }; if (status === "setting-up") return { key: reason === "installing" ? "board.devices.installing" : "board.devices.starting", tone: "active" }; // ccdeck missing there is said for what it is, beside its Install or Start. if (status === "needs-setup" && reason === "no_remote_ccdeck" && detail === "not_installed") return { key: "board.devices.noCcdeck", tone: "warn" }; if (status === "needs-setup" && reason === "no_remote_ccdeck" && detail === "not_running") return { key: "board.devices.ccdeckStopped", tone: "warn" }; if (status === "needs-setup") return { key: "board.devices.needsSetup", tone: "warn" }; if (status === "error") return { key: "board.devices.readFailed", tone: "err" }; if (status === "paused") return { key: "board.devices.disconnected", tone: "idle" }; return reason === "unreachable" ? { key: "board.devices.unreachableShort", tone: "err" } : { key: "board.devices.offline", tone: "idle" }; } export const REMOTE_PREFIX = "remote:"; export const remoteRowId = (key: string): string => REMOTE_PREFIX + key; export const isRemoteRowId = (id: string): boolean => id.startsWith(REMOTE_PREFIX); /** The device's id and the session's, from a remote row id; null for any other * id. A device id never holds a colon, so the first one after the prefix ends it. */ export function parseRemoteRowId(id: string): { deviceId: string; sessionId: string } | null { if (!isRemoteRowId(id)) return null; const rest = id.slice(REMOTE_PREFIX.length); const colon = rest.indexOf(":"); if (colon <= 0 || colon === rest.length - 1) return null; return { deviceId: rest.slice(0, colon), sessionId: rest.slice(colon + 1) }; } /** The list id of the remote row a Workspace subject has open, or null for a new session. */ export const remoteRowOf = (subject: DeviceWorkspaceSubject): string | null => subject.action === "open" ? remoteRowId(`${subject.id}:${subject.sessionId}`) : null; function providerOf(value: string | null): Provider | undefined { return value === "claude" || value === "codex" ? value : undefined; } function basename(path: string | null): string | null { if (!path) return null; const parts = path.split(/[\\/]+/).filter(Boolean); return parts.at(-1) ?? null; } /** The waiting block a device's session carries, when it is a stop that needs * a person (a permission or a question); idle is a turn that ended. */ function blockOf(session: RemoteSession, shift: (t: number | null) => number | null, fallback: number): WaitingBlock | null { const w = session.waiting; if (!w || (w.kind !== "permission" && w.kind !== "asked")) return null; return { kind: w.kind, message: w.message, since: shift(w.since) ?? fallback } as WaitingBlock; } /** * A device's session as a list row. `remoteNow` is the device's clock when it * answered (null when it did not say), `now` this machine's. * * Working is a running turn, a permission or a question is Needs you, and * idle or ended is Done. A remote row is never Done ยท new: whether a finished * turn was looked at is kept on this machine, for its own sessions. */ export function toRemoteRow(session: RemoteSession, remoteNow: number | null, now: number): Row { const skew = remoteNow != null && Number.isFinite(remoteNow) ? now - remoteNow : 0; const shift = (t: number | null) => (t == null ? null : t + skew); const startedAt = shift(session.startedAt) ?? shift(session.lastEventAt) ?? now; const lastActivity = shift(session.lastEventAt) ?? shift(session.since) ?? startedAt; const block = blockOf(session, shift, lastActivity); const running = session.state === "working" || (session.state === "waiting" && block !== null); const folder = basename(session.cwd); const row: Row = { sessionId: remoteRowId(session.key), provider: providerOf(session.provider), label: session.name ?? session.project ?? folder ?? session.id, state: running ? "active" : "done", waiting: block, toolCount: 0, cost: 0, startedAt, lastActivity, plan: null, planTitle: "", cwd: session.cwd ?? undefined, modelId: session.model ?? undefined, device: session.device ?? undefined, remoteId: session.id, }; if (session.name && (session.project ?? folder)) row.folder = session.project ?? folder ?? undefined; return row; } /** One device as the list draws it. */ export interface DeviceView { id: string; name: string; link: DeviceLink; status: DeviceStatus; reason: string | null; rows: Row[]; /** The type of the last failed read of a connected device, or null. */ error: string | null; } export interface DeviceSessions { rows: readonly RemoteSession[]; remoteNow: number | null; error: string | null; } /** Rows in the list's order: by group, then the newest first. */ function ordered(rows: Row[], rank: (row: Row) => number): Row[] { return rows.map((row, i) => ({ row, i, g: rank(row) })) .sort((a, b) => a.g - b.g || b.row.lastActivity - a.row.lastActivity || a.i - b.i) .map(x => x.row); } /** The device views for the list, in the order devices were returned, each with * the rows read from it (none until it was read). `rank` is the list's own. */ export function deviceViews( devices: readonly DeviceInfo[], sessions: Readonly>, now: number, rank: (row: Row) => number, ): DeviceView[] { return devices.map(device => { const { status, reason } = deviceStatus(device.link); const read = status === "connected" ? sessions[device.id] : undefined; // What Workspace cannot attach to is not listed: a finished session, or a // tool other than Claude Code and Codex. const open = read ? read.rows.filter(s => s.state !== "ended" && providerOf(s.provider)) : []; const rows = ordered(open.map(s => toRemoteRow(s, read!.remoteNow, now)), rank); return { id: device.id, name: device.name, link: device.link, status, reason, rows, error: read?.error ?? null }; }); } const SETUP_KEYS: Readonly> = { auth_failed: "board.devices.auth", no_remote_ccdeck: "board.devices.remote", version_too_old: "board.devices.update", ssh_missing: "board.devices.sshMissing", host_key_changed: "board.devices.keyChanged", trust_expired: "board.devices.expired", unreachable: "board.devices.unreachable", failed: "board.devices.readFailed", }; /** The sentence for an answer that needs a person, the Devices dialog's own; * the short "needs setup" for the ones it words in its own steps. ccdeck * missing on the device is said two ways: install it, or start it. */ export function needsSetupKey(reason: string | null, detail?: string | null): MessageKey { if (reason === "no_remote_ccdeck" && detail === "not_installed") return "board.devices.notInstalled"; if (reason === "no_remote_ccdeck" && detail === "not_running") return "board.devices.notRunning"; return (reason && SETUP_KEYS[reason]) || "board.devices.needsSetup"; }