From eab8f490220ef2042e74dc72364f9470b7682aeb Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:51:22 -0600 Subject: [PATCH] fix(app): drive server health and plugin server state with explicit machines (#61763) --- packages/app/src/components-lab/lab.tsx | 3 +- packages/app/src/home/model.ts | 3 +- packages/app/src/home/projects/actions.tsx | 3 +- packages/app/src/home/projects/controller.tsx | 3 +- packages/app/src/home/projects/view.tsx | 59 +-- .../app/src/new-session/project/controller.ts | 11 +- packages/app/src/new-session/route.tsx | 3 +- .../app/src/providers/connect/remote.test.ts | 5 +- packages/app/src/runtime/plugin/host-apis.tsx | 3 +- .../app/src/runtime/plugin/mounted-session.ts | 3 +- .../app/src/runtime/plugin/server-shell.tsx | 8 +- packages/app/src/runtime/plugin/servers.tsx | 71 +++- .../src/runtime/server/health-machine.test.ts | 227 +++++++++++ .../app/src/runtime/server/health-machine.ts | 357 ++++++++++++++++++ .../app/src/runtime/server/health.test.ts | 89 +++-- packages/app/src/runtime/server/health.ts | 200 ++++------ .../app/src/runtime/server/machine.test.ts | 21 +- packages/app/src/runtime/server/machine.ts | 17 +- .../src/runtime/server/plugin-machine.test.ts | 128 +++++++ .../app/src/runtime/server/plugin-machine.ts | 170 +++++++++ .../app/src/runtime/server/registry.test.ts | 5 +- packages/app/src/runtime/server/registry.tsx | 34 +- packages/app/src/runtime/server/runtime.tsx | 13 +- packages/app/src/servers/connect/dialog.tsx | 14 +- packages/app/src/servers/connect/pairing.ts | 2 +- packages/app/src/servers/connect/screen.tsx | 4 +- .../src/servers/registry/controller.test.ts | 11 +- .../app/src/servers/registry/controller.ts | 13 +- .../src/servers/registry/management.test.ts | 25 +- .../app/src/servers/registry/management.ts | 8 +- .../app/src/servers/registry/plugin-row.tsx | 40 +- .../app/src/servers/registry/row.stories.tsx | 34 +- packages/app/src/servers/registry/row.tsx | 80 ++-- .../src/session/resolution-machine.test.ts | 12 +- .../app/src/session/resolution-machine.ts | 6 +- packages/app/src/session/route-error.tsx | 3 +- .../src/session/server-unavailable-panel.tsx | 3 +- .../src/settings/servers/inventory.test.ts | 5 +- .../app/src/settings/servers/inventory.ts | 4 +- packages/app/src/settings/servers/servers.tsx | 11 +- .../app/src/settings/servers/switcher.tsx | 6 +- .../app/src/shell/commands/session-search.tsx | 5 +- packages/app/src/shell/routes/routes.tsx | 3 +- .../app/src/shell/titlebar/inbox-model.ts | 5 +- .../src/workspaces/selection/picker.test.ts | 5 +- .../test-browser/plugin-server-stream.test.ts | 7 +- packages/app/test-browser/ssh-health.test.ts | 205 +++++----- .../gui-plugins/src/ssh/dialog.stories.tsx | 2 +- 48 files changed, 1432 insertions(+), 517 deletions(-) create mode 100644 packages/app/src/runtime/server/health-machine.test.ts create mode 100644 packages/app/src/runtime/server/health-machine.ts create mode 100644 packages/app/src/runtime/server/plugin-machine.test.ts create mode 100644 packages/app/src/runtime/server/plugin-machine.ts diff --git a/packages/app/src/components-lab/lab.tsx b/packages/app/src/components-lab/lab.tsx index a7df208ee43..2a2b83c4876 100644 --- a/packages/app/src/components-lab/lab.tsx +++ b/packages/app/src/components-lab/lab.tsx @@ -31,6 +31,7 @@ import { ModelsProvider } from "@/providers/models/models" import { Contribution } from "@/runtime/plugin/render" import { ServerProvider, useServer } from "@/runtime/server/current" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerHealthy } from "@/runtime/server/health-machine" import { useGlobal } from "@/runtime/server/runtime" import { SessionRouteFrame } from "@/session/session-frame" import { SessionUIProvider } from "@/shell/routes/session-ui-provider" @@ -526,7 +527,7 @@ function Workspace(props: { children: JSX.Element }) { const healthy = createMemo((picked?: ServerConnection.Any) => picked && servers.list.some((conn) => ServerConnection.key(conn) === ServerConnection.key(picked)) ? picked - : servers.list.find((conn) => global.servers.health[ServerConnection.key(conn)]?.healthy), + : servers.list.find((conn) => isServerHealthy(global.servers.health[ServerConnection.key(conn)])), ) return ( diff --git a/packages/app/src/home/model.ts b/packages/app/src/home/model.ts index c7f69fada52..9f0dd8082c7 100644 --- a/packages/app/src/home/model.ts +++ b/packages/app/src/home/model.ts @@ -1,6 +1,7 @@ import { useGlobal, useServerCtx } from "@/runtime/server/runtime" import { type HomeProjectSelection, useLayout } from "@/shell/state/layout" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerFailed } from "@/runtime/server/health-machine" import { createProjectSessionActions } from "@/shell/tabs/project-sessions" import { toggleHomeProjectSelection } from "@/shell/layout/helpers" import { createEffect, createMemo } from "solid-js" @@ -88,7 +89,7 @@ export function createHomeController() { select: (conn: ServerConnection.Any, directory: string) => { const key = ServerConnection.key(conn) - if (global.servers.health[key]?.healthy === false) return + if (isServerFailed(global.servers.health[key])) return if ( !global diff --git a/packages/app/src/home/projects/actions.tsx b/packages/app/src/home/projects/actions.tsx index 8247689618b..91f1d4805b3 100644 --- a/packages/app/src/home/projects/actions.tsx +++ b/packages/app/src/home/projects/actions.tsx @@ -3,6 +3,7 @@ import { useDialog } from "@opencode/ui/context/dialog" import { useLanguage } from "@/runtime/i18n/language" import { usePlatform } from "@/runtime/platform/platform" import { ServerConnection } from "@/runtime/server/registry" +import { isServerUnauthorized } from "@/runtime/server/health-machine" import { useGlobal } from "@/runtime/server/runtime" import { useSettingsSurface } from "@/settings/surface" import { errorMessage } from "@/shell/layout/helpers" @@ -34,7 +35,7 @@ export function createProjectActions() { // Resume the requested action with the saved connection after an expired HTTP pairing or plugin sign-in. function authenticate(conn: ServerConnection.Any, onConnected?: (conn: ServerConnection.Any) => void) { - if (conn.type !== "http" || !global.servers.health[ServerConnection.key(conn)]?.unauthorized) + if (conn.type !== "http" || !isServerUnauthorized(global.servers.health[ServerConnection.key(conn)])) return ServerConnection.authenticate(conn, () => onConnected?.(conn)) editServer(conn, onConnected) diff --git a/packages/app/src/home/projects/controller.tsx b/packages/app/src/home/projects/controller.tsx index 6b6813327c2..7fbbe36bf42 100644 --- a/packages/app/src/home/projects/controller.tsx +++ b/packages/app/src/home/projects/controller.tsx @@ -4,6 +4,7 @@ import { useSettingsCommand } from "@/settings/command" import { useLanguage } from "@/runtime/i18n/language" import { usePlatform } from "@/runtime/platform/platform" import { ServerConnection } from "@/runtime/server/registry" +import { isServerFailed } from "@/runtime/server/health-machine" import { homeProjectDirectories } from "@/shell/layout/helpers" import { Persist, persisted } from "@/runtime/persistence/storage" import { createResource } from "solid-js" @@ -90,7 +91,7 @@ export function createHomeProjectsController(home: HomeController) { choose: (conn: ServerConnection.Any) => { if (actions.authenticate(conn, (next) => choose(next))) return - if (home.server.health(conn)?.healthy === false) return + if (isServerFailed(home.server.health(conn))) return choose(conn) }, hide: actions.hide, diff --git a/packages/app/src/home/projects/view.tsx b/packages/app/src/home/projects/view.tsx index 94dce50f1ef..80b4d0a3ea9 100644 --- a/packages/app/src/home/projects/view.tsx +++ b/packages/app/src/home/projects/view.tsx @@ -20,7 +20,14 @@ import { ServerConnection } from "@/runtime/server/registry" import { useLanguage } from "@/runtime/i18n/language" import { ServerRowMenuView, serverMenuLabels } from "@/servers/registry/row-menu" import { ServerHealthIndicator } from "@/servers/registry/row" -import { type ServerHealth } from "@/runtime/server/health" +import { + isServerFailed, + isServerHealthy, + isServerIncompatible, + isServerUnauthorized, + selectServerVersion, + type ServerHealth, +} from "@/runtime/server/health-machine" import { ProjectMenuItems } from "./menu" import { CollapsedSidebarToggleSpace } from "@/shell/sidebar-toggle" import "./view.css" @@ -32,7 +39,10 @@ const serverContextMenuID = (server: ServerConnection.Any) => `server:${ServerCo // Plugin servers ask for sign-in themselves; an HTTP server that rejects its saved credentials needs a new // pairing link or password. const signInRequired = (server: ServerConnection.Any, health: ServerHealth | undefined) => - (server.type === "plugin" && server.authenticationRequired) || (server.type === "http" && !!health?.unauthorized) + (server.type === "plugin" && server.status.type === "auth") || + (server.type === "http" && isServerUnauthorized(health)) + +const connecting = (server: ServerConnection.Any) => server.type === "plugin" && server.status.type === "starting" const projectContextMenuID = (server: ServerConnection.Any, directory: string) => `project:${ServerConnection.key(server)}:${directory}` @@ -201,7 +211,7 @@ function HomeProjectsPanel(props: HomeProjectsViewProps) { size="large" class="titlebar-icon [&_[data-slot=icon-svg]]:text-v2-icon-icon-muted" icon={} - disabled={props.serverHealth(props.servers[0])?.healthy === false} + disabled={isServerFailed(props.serverHealth(props.servers[0]))} onClick={() => props.onChooseProject(props.servers[0])} aria-label={props.language.t("home.project.add")} /> @@ -236,11 +246,7 @@ function HomeProjectsPanel(props: HomeProjectsViewProps) { 1 || - props.servers.some( - (server) => - signInRequired(server, props.serverHealth(server)) || - (server.type === "plugin" && server.connecting), - ) + props.servers.some((server) => signInRequired(server, props.serverHealth(server)) || connecting(server)) } fallback={ @@ -253,7 +259,7 @@ function HomeProjectsPanel(props: HomeProjectsViewProps) { type="button" data-action="home-add-project-row" class="mt-1 disabled:opacity-60" - disabled={props.serverHealth(server())?.healthy === false} + disabled={isServerFailed(props.serverHealth(server()))} onClick={() => props.onChooseProject(server())} > @@ -270,11 +276,10 @@ function HomeProjectsPanel(props: HomeProjectsViewProps) { {(item) => { const projects = () => props.projectsForServer(item) - const healthy = () => !!props.serverHealth(item)?.healthy + const healthy = () => isServerHealthy(props.serverHealth(item)) const hasProjects = () => projects().length > 0 const collapsed = () => props.collapsed(item) const authentication = () => signInRequired(item, props.serverHealth(item)) - const connecting = () => item.type === "plugin" && item.connecting return (
@@ -286,7 +291,7 @@ function HomeProjectsPanel(props: HomeProjectsViewProps) { collapsed={collapsed()} health={props.serverHealth(item)} /> - +
- +
@@ -376,9 +383,9 @@ function HomeServerRow(props: { collapsed: boolean health: ServerHealth | undefined }) { - const healthy = () => !!props.health?.healthy + const healthy = () => isServerHealthy(props.health) const authentication = () => signInRequired(props.server, props.health) - const incompatible = () => !!props.health?.incompatible + const incompatible = () => isServerIncompatible(props.health) const canToggle = () => healthy() && props.projectsForServer(props.server).length > 0 const contextMenuID = () => serverContextMenuID(props.server) onCleanup(() => { @@ -397,7 +404,7 @@ function HomeServerRow(props: { value={ authentication() ? props.language.t("server.status.authentication") - : props.language.t("server.row.incompatible", { version: props.health?.version ?? "1" }) + : props.language.t("server.row.incompatible", { version: selectServerVersion(props.health) ?? "1" }) } >
- +
} aria-label={props.language.t("home.project.add")} - disabled={props.health?.healthy === false && !authentication()} + disabled={isServerFailed(props.health) && !authentication()} onClick={() => props.onChooseProject(props.server)} /> @@ -570,7 +573,7 @@ function HomeProjectSlot(props: HomeProjectListProps & { worktree: string }) { } function HomeProjectEmpty(props: HomeProjectsViewProps & { server: ServerConnection.Any }) { - const unreachable = () => props.serverHealth(props.server)?.healthy === false + const unreachable = () => isServerFailed(props.serverHealth(props.server)) return ( props.serverHealth(props.server)?.healthy === false + const serverUnreachable = () => isServerFailed(props.serverHealth(props.server)) const contextMenuID = () => projectContextMenuID(props.server, props.project.worktree) onCleanup(() => { diff --git a/packages/app/src/new-session/project/controller.ts b/packages/app/src/new-session/project/controller.ts index bb650edd305..3678f99ce71 100644 --- a/packages/app/src/new-session/project/controller.ts +++ b/packages/app/src/new-session/project/controller.ts @@ -4,6 +4,7 @@ import { type ServerCtx, useGlobal } from "@/runtime/server/runtime" import { useServer } from "@/runtime/server/current" import { useServerSDK } from "@/runtime/server/client" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerFailed } from "@/runtime/server/health-machine" import { useWorkspaceLocation } from "@/workspaces/location" import { workspaceSelectionDestination } from "@/workspaces/paths" import { useTabs } from "@/shell/tabs/tabs" @@ -45,11 +46,11 @@ export function createComposerProjectControls(props: { draftId: string; worktree // Servers that are down or still connecting contribute nothing. const ready = createMemo(() => - servers.list.filter((connection) => { - if (connection.type === "plugin" && connection.state !== "ready") return false - - return global.servers.health[ServerConnection.key(connection)]?.healthy !== false - }), + servers.list.filter( + (connection) => + !!ServerConnection.http(connection) && + !isServerFailed(global.servers.health[ServerConnection.key(connection)]), + ), ) const projects = createMemo(() => { diff --git a/packages/app/src/new-session/route.tsx b/packages/app/src/new-session/route.tsx index 35e8cfcff05..3f49c80c1da 100644 --- a/packages/app/src/new-session/route.tsx +++ b/packages/app/src/new-session/route.tsx @@ -8,6 +8,7 @@ import { ModelsProvider } from "@/providers/models/models" import { ComposerPersistenceProvider } from "@/composer/persistence" import { ServerProvider, useServer } from "@/runtime/server/current" import { ServerConnection } from "@/runtime/server/registry" +import { isServerIncompatible } from "@/runtime/server/health-machine" import { useTabs, type DraftTab } from "@/shell/tabs/tabs" import { SessionUIProvider } from "@/shell/routes/session-ui-provider" import NewSession from "@/new-session/screen" @@ -62,7 +63,7 @@ function ResolvedDraftContent(props: { draft: DraftTab }) { return ( diff --git a/packages/app/src/providers/connect/remote.test.ts b/packages/app/src/providers/connect/remote.test.ts index ae6aac62999..9a18dd78000 100644 --- a/packages/app/src/providers/connect/remote.test.ts +++ b/packages/app/src/providers/connect/remote.test.ts @@ -5,9 +5,8 @@ const contributed = { type: "plugin", key: "ssh:production", plugin: "ssh", - state: "ready", - connecting: false, - authenticationRequired: false, + status: { type: "ready", http: { url: "http://127.0.0.1:4096" } }, + authenticate: () => false, managed: true, http: { url: "http://127.0.0.1:4096" }, } as const diff --git a/packages/app/src/runtime/plugin/host-apis.tsx b/packages/app/src/runtime/plugin/host-apis.tsx index 875bc10c23d..06d0b9cdfde 100644 --- a/packages/app/src/runtime/plugin/host-apis.tsx +++ b/packages/app/src/runtime/plugin/host-apis.tsx @@ -40,6 +40,7 @@ import { same } from "@/runtime/persistence/equality" import { Persist, persisted, removePersisted } from "@/runtime/persistence/storage" import { useGlobal, type ServerCtx } from "@/runtime/server/runtime" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerIncompatible } from "@/runtime/server/health-machine" import { useDirectoryPicker } from "@/workspaces/selection/picker" import { SessionRouteKey, SessionStateKey, type ServerScope } from "@/runtime/server/scope" import { findSessionTab, tabKey, useTabs } from "@/shell/tabs/tabs" @@ -540,7 +541,7 @@ export function createPluginAttachment(apis: HostApis) { return ServerConnection.builtin(listed()) }, get compatible() { - return !global.servers.health[key]?.incompatible + return !isServerIncompatible(global.servers.health[key]) }, get connected() { return global.serverCtx(key)?.sdk.connection.status() === "connected" diff --git a/packages/app/src/runtime/plugin/mounted-session.ts b/packages/app/src/runtime/plugin/mounted-session.ts index 6ae9f24e8d6..7977306bb78 100644 --- a/packages/app/src/runtime/plugin/mounted-session.ts +++ b/packages/app/src/runtime/plugin/mounted-session.ts @@ -28,6 +28,7 @@ import type { ComposerStateValue } from "@/composer/state" import { useLocal } from "@/providers/models/selection" import { useServer } from "@/runtime/server/current" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerIncompatible } from "@/runtime/server/health-machine" import { sessionInWorkspace, sessionProject, type SessionModel } from "@/session/model" import { useFile } from "@/workspaces/files/model" import { useWorkspaceLocation } from "@/workspaces/location" @@ -83,7 +84,7 @@ export function createMountedSession(session: SessionModel) { return ServerConnection.builtin(server.conn) }, get compatible() { - return !server.health?.incompatible + return !isServerIncompatible(server.health) }, get connected() { return server.ctx.sdk.connection.status() === "connected" diff --git a/packages/app/src/runtime/plugin/server-shell.tsx b/packages/app/src/runtime/plugin/server-shell.tsx index 055b3232692..13956b2bf1f 100644 --- a/packages/app/src/runtime/plugin/server-shell.tsx +++ b/packages/app/src/runtime/plugin/server-shell.tsx @@ -93,12 +93,12 @@ export function PluginServerEndpoints() { createEffect(() => { const endpoints = servers.list.flatMap((conn) => { // Main knows the built-in server first-hand; a contributed server has no endpoint until it is ready. - if (conn.type === "sidecar" || (conn.type === "plugin" && conn.state !== "ready")) return [] - const password = conn.http.password + const http = ServerConnection.http(conn) - const endpoint = { id: ServerConnection.key(conn), url: conn.http.url } + if (conn.type === "sidecar" || !http) return [] + const endpoint = { id: ServerConnection.key(conn), url: http.url } - return [password ? { ...endpoint, password } : endpoint] + return [http.password ? { ...endpoint, password: http.password } : endpoint] }) const value = JSON.stringify(endpoints) diff --git a/packages/app/src/runtime/plugin/servers.tsx b/packages/app/src/runtime/plugin/servers.tsx index 91242be1d29..8a7499fa7fa 100644 --- a/packages/app/src/runtime/plugin/servers.tsx +++ b/packages/app/src/runtime/plugin/servers.tsx @@ -1,9 +1,12 @@ import { + batch, createContext, createEffect, createMemo, createRenderEffect, createSignal, + onCleanup, + untrack, useContext, type ParentProps, } from "solid-js" @@ -12,6 +15,14 @@ import { Schema } from "effect" import { MenuItem, Server, type ServerEntry } from "@opencode/plugin/gui" import { Persistence } from "@/runtime/persistence/schema" import { Persist, persisted } from "@/runtime/persistence/storage" +import { + canAuthenticatePluginServer, + createPluginServerState, + selectPluginStatus, + transitionPluginServer, + type PluginEntrySnapshot, + type PluginServerEvent, +} from "@/runtime/server/plugin-machine" import type { ServerConnection } from "@/runtime/server/registry" import { usePluginHost } from "./host" import { renamedFrom } from "./stores" @@ -19,7 +30,7 @@ import { renamedFrom } from "./stores" /** A server a plugin contributes. `key` is `${plugin}:${entry.id}`. */ export type PluginServer = { readonly key: string; readonly plugin: string; readonly entry: ServerEntry } -// Endpoint of a contributed server before its plugin reports one. +// Where a contributed server's event stream starts before its plugin reports an endpoint; it never answers. const offline = { url: "http://127.0.0.1:0" } // The last complete inventory of each plugin that contributes servers, without endpoints or credentials. @@ -120,13 +131,15 @@ function createPluginServers() { // Routes key on the connection object. Keep one per key across entry updates so reconnecting // never unmounts an open conversation or composer. - const connections = new Map void; value: ServerConnection.Plugin }>() + const connections = new Map>() const list = createMemo(() => { const listed = entries().filter((item) => item.entry.listed !== false) const keys = new Set(listed.map((item) => item.key)) - connections.forEach((_, key) => { - if (!keys.has(key)) connections.delete(key) + connections.forEach((item, key) => { + if (keys.has(key)) return + item.dispose() + connections.delete(key) }) return listed.map((item) => { @@ -145,6 +158,8 @@ function createPluginServers() { }) }) + onCleanup(() => connections.forEach((item) => item.dispose())) + return { ready: host.serversReady, /** Contributed servers the app lists, as stable connections. */ @@ -160,7 +175,25 @@ function createPluginServers() { function connection(initial: PluginServer) { const [current, setCurrent] = createSignal(initial) + const [machine, setMachine] = createSignal(createPluginServerState(snapshot(initial.entry))) const entry = () => current().entry + let timer: ReturnType | undefined + + const dispatch = (event: PluginServerEvent) => { + const result = transitionPluginServer(untrack(machine), event) + setMachine(result.state) + + result.effects.forEach((effect) => { + if (effect.type === "timeout.cancel") return clearTimeout(timer) + + // Out of the update that reported the server ready: the callbacks navigate. + if (effect.type === "notify.connected") return queueMicrotask(() => effect.callbacks.forEach((run) => run())) + + timer = setTimeout(() => dispatch({ type: "connect.timed-out", attempt: effect.attempt }), effect.timeoutMs) + const settled = (ready: boolean) => dispatch({ type: "connect.settled", attempt: effect.attempt, ready }) + void Promise.try(() => untrack(entry).connect?.() ?? false).then(settled, () => settled(false)) + }) + } const value: ServerConnection.Plugin = { type: "plugin", @@ -172,14 +205,8 @@ function connection(initial: PluginServer) { get label() { return entry().label }, - get state() { - return entry().state - }, - get connecting() { - return entry().state === "starting" - }, - get authenticationRequired() { - return entry().state === "auth" + get status() { + return selectPluginStatus(machine()) }, get managed() { return !!entry().reconnect @@ -193,10 +220,28 @@ function connection(initial: PluginServer) { return (signal: AbortSignal) => entry().reconnect?.(signal) ?? Promise.resolve(entry().http ?? offline) }, + authenticate: (onConnected) => { + if (!canAuthenticatePluginServer(untrack(machine))) return false + dispatch({ type: "authenticate", onConnected }) + + return true + }, connect: () => entry().connect?.() ?? Promise.resolve(entry().state === "ready"), } - return { update: (server: PluginServer) => setCurrent(server), value } + return { + update: (server: PluginServer) => + batch(() => { + setCurrent(server) + dispatch({ type: "entry.updated", entry: snapshot(server.entry) }) + }), + dispose: () => dispatch({ type: "disposed" }), + value, + } +} + +function snapshot(entry: ServerEntry): PluginEntrySnapshot { + return { state: entry.state, http: entry.http } } /** MenuItem "server.row" contributions that apply to a server. */ diff --git a/packages/app/src/runtime/server/health-machine.test.ts b/packages/app/src/runtime/server/health-machine.test.ts new file mode 100644 index 00000000000..b59dceba654 --- /dev/null +++ b/packages/app/src/runtime/server/health-machine.test.ts @@ -0,0 +1,227 @@ +import { expect, test } from "bun:test" +import { + canUseServer, + createServerHealthState, + isServerFailed, + selectServerHealthRank, + selectServerHealthRecord, + selectServerIndicator, + SERVER_HEALTH_POLL_MS, + transitionServerHealth, + type ServerHealth, + type ServerHealthEffect, + type ServerHealthEvent, + type ServerHealthInput, + type ServerHealthState, + type ServerProbeResult, +} from "./health-machine" +import { ServerKey } from "./persistence" + +const key = (name: string) => ServerKey.make(`http://${name}`) + +const endpoint = (name: string, password?: string, managed = false): ServerHealthInput => ({ + type: "endpoint", + key: key(name), + http: { url: `http://${name}`, password }, + managed, +}) + +const staged = (name: string, stage: Extract["stage"]): ServerHealthInput => ({ + type: "staged", + key: key(name), + stage, +}) + +const synced = (...servers: ServerHealthInput[]): ServerHealthEvent => ({ type: "servers.synced", servers }) + +const answered = (name: string, attempt: number, result: ServerProbeResult): ServerHealthEvent => ({ + type: "probe.settled", + key: key(name), + attempt, + result, +}) + +const poll: ServerHealthEvent = { type: "poll.due" } + +function replay(events: readonly ServerHealthEvent[], state: ServerHealthState = createServerHealthState()) { + return events.reduce<{ state: ServerHealthState; effects: ServerHealthEffect[] }>( + (result, event) => { + const next = transitionServerHealth(result.state, event) + + return { state: next.state, effects: [...result.effects, ...next.effects] } + }, + { state, effects: [] }, + ) +} + +const health = (state: ServerHealthState, name: string) => selectServerHealthRecord(state)[key(name)] + +const probes = (effects: readonly ServerHealthEffect[]) => + effects.flatMap((effect) => (effect.type === "probe" ? [`${effect.key} #${effect.attempt}`] : [])) + +const notified = (effects: readonly ServerHealthEffect[]) => effects.filter((effect) => effect.type === "notify.probe") + +test("a slow server's answer never reverts a faster server's newer answer", () => { + const start = replay([synced(endpoint("fast"), endpoint("slow")), answered("fast", 1, { type: "unreachable" })]) + + // The poll comes due while the slow probe still runs: only the idle server probes again. + const polled = transitionServerHealth(start.state, poll) + expect(probes(polled.effects)).toEqual(["http://fast #2"]) + + const end = replay( + [answered("fast", 2, { type: "healthy" }), answered("slow", 1, { type: "unreachable" })], + polled.state, + ) + + expect(health(end.state, "fast")).toEqual({ type: "healthy" }) + expect(health(end.state, "slow")).toEqual({ type: "unreachable" }) +}) + +test("a probe that a retry replaced never reaches the listener", () => { + const retried = replay([synced(endpoint("s", "pw1")), { type: "retry.requested", resolve: () => {} }]) + const current = transitionServerHealth(retried.state, answered("s", 2, { type: "healthy" })) + expect(notified(current.effects)).toEqual([ + { type: "notify.probe", probe: { key: key("s"), auth: "http://s\npw1", result: { type: "healthy" } } }, + ]) + + // The replaced probe answers 401 late: it neither signs the server out nor changes its health. + const late = transitionServerHealth(current.state, answered("s", 1, { type: "unauthorized" })) + expect(late).toEqual({ state: current.state, effects: [] }) + expect(health(late.state, "s")).toEqual({ type: "healthy" }) +}) + +test.each([ + { name: "disposed", event: { type: "disposed" } as const }, + { name: "unlisted", event: synced() }, + { name: "moved to a new endpoint", event: synced(endpoint("s", "pw2")) }, + { name: "waiting for its plugin", event: synced(staged("s", "auth")) }, +])("a probe of a server that was $name never reaches the listener", (input) => { + const left = replay([synced(endpoint("s", "pw1")), input.event]) + const late = transitionServerHealth(left.state, answered("s", 1, { type: "unauthorized" })) + + expect(late).toEqual({ state: left.state, effects: [] }) +}) + +test("a check in flight does not read as a failed server", () => { + const moved = replay([synced(endpoint("tunnel", undefined, true), endpoint("plain"))]).state + const tunnel = health(moved, "tunnel") + const plain = health(moved, "plain") + + expect(tunnel).toEqual({ type: "checking", managed: true }) + expect(plain).toEqual({ type: "checking", managed: false }) + ;[tunnel, plain].forEach((value) => { + expect(canUseServer(value)).toBe(true) + expect(isServerFailed(value)).toBe(false) + expect(selectServerHealthRank(value)).toBe(1) + }) + expect(selectServerIndicator(tunnel)).toBe("connecting") + expect(selectServerIndicator(plain)).toBe("unknown") +}) + +test("a contributed server that is not ready reports its stage and is never probed", () => { + const stages = ["stopped", "starting", "auth", "failed", "incompatible"] as const + + const expected: ServerHealth[] = [ + { type: "stopped" }, + { type: "starting" }, + { type: "auth" }, + { type: "unreachable" }, + { type: "incompatible" }, + ] + + const seen = stages.map((stage, index) => { + const result = replay(stages.slice(0, index + 1).map((item) => synced(staged("ssh", item)))) + + expect(probes(result.effects)).toEqual([]) + + return health(result.state, "ssh") + }) + + expect(seen).toEqual(expected) + // The inbox stops waiting for a server that cannot answer yet. + seen.forEach((value) => expect(canUseServer(value)).toBe(false)) + expect(seen.map(selectServerIndicator)).toEqual(["unknown", "connecting", "auth", "unreachable", "incompatible"]) +}) + +test("a background poll keeps the last answer until the next one arrives", () => { + const known = replay([synced(endpoint("s")), answered("s", 1, { type: "healthy", version: "2.0.0" })]).state + const polled = transitionServerHealth(known, poll) + + expect(probes(polled.effects)).toEqual(["http://s #2"]) + expect(health(polled.state, "s")).toEqual({ type: "healthy", version: "2.0.0" }) + // A probe already in flight is not started again. + expect(transitionServerHealth(polled.state, poll)).toEqual({ state: polled.state, effects: [] }) +}) + +test("a server joining the list leaves the probe of an unchanged one running", () => { + const first = replay([synced(endpoint("a"))]) + const joined = transitionServerHealth(first.state, synced(endpoint("a"), endpoint("b"))) + + expect(probes(joined.effects)).toEqual(["http://b #1"]) + expect(health(transitionServerHealth(joined.state, answered("a", 1, { type: "healthy" })).state, "a")).toEqual({ + type: "healthy", + }) +}) + +test("the poll runs only while a server has an endpoint", () => { + const listed = replay([synced(staged("ssh", "auth"))]) + expect(listed.effects).toEqual([]) + + const ready = transitionServerHealth(listed.state, synced(endpoint("ssh"))) + expect(ready.effects).toContainEqual({ type: "timer.start", intervalMs: SERVER_HEALTH_POLL_MS }) + + expect(transitionServerHealth(ready.state, synced()).effects).toEqual([{ type: "timer.stop" }]) +}) + +test("retry probes every server again and resolves once each answers", () => { + const log: string[] = [] + const resolve = () => log.push("resolved") + + const start = replay([ + synced(endpoint("a"), endpoint("b"), staged("ssh", "auth")), + answered("a", 1, { type: "unreachable" }), + ]) + + const retried = transitionServerHealth(start.state, { type: "retry.requested", resolve }) + expect(probes(retried.effects)).toEqual(["http://a #2", "http://b #2"]) + expect(retried.effects).toContainEqual({ type: "timer.start", intervalMs: SERVER_HEALTH_POLL_MS }) + + // The probe the retry replaced answers first: the caller keeps waiting. + const stale = transitionServerHealth(retried.state, answered("b", 1, { type: "healthy" })) + expect(stale.state).toBe(retried.state) + + const partial = transitionServerHealth(retried.state, answered("a", 2, { type: "healthy" })) + expect(partial.effects).not.toContainEqual(expect.objectContaining({ type: "waiters.resolve" })) + + const done = transitionServerHealth(partial.state, answered("b", 2, { type: "healthy" })) + done.effects.forEach((effect) => effect.type === "waiters.resolve" && effect.resolvers.forEach((run) => run())) + expect(log).toEqual(["resolved"]) + expect(done.state.waiters).toEqual([]) +}) + +test.each([ + { name: "is unlisted", event: synced(endpoint("a")) }, + { name: "waits for its plugin", event: synced(endpoint("a"), staged("b", "starting")) }, + { name: "is disposed", event: { type: "disposed" } as const }, +])("retry resolves when the server it waits for $name", (input) => { + const retried = replay([synced(endpoint("a"), endpoint("b")), answered("a", 1, { type: "healthy" })]) + + const waiting = replay( + [{ type: "retry.requested", resolve: () => {} }, answered("a", 2, { type: "healthy" })], + retried.state, + ) + + expect(waiting.state.waiters).toHaveLength(1) + + const result = transitionServerHealth(waiting.state, input.event) + expect(result.effects).toContainEqual(expect.objectContaining({ type: "waiters.resolve" })) + expect(result.state.waiters).toEqual([]) +}) + +test("retry with no server to probe resolves at once", () => { + const resolve = () => {} + + expect( + transitionServerHealth(replay([synced(staged("ssh", "auth"))]).state, { type: "retry.requested", resolve }).effects, + ).toEqual([{ type: "waiters.resolve", resolvers: [resolve] }]) +}) diff --git a/packages/app/src/runtime/server/health-machine.ts b/packages/app/src/runtime/server/health-machine.ts new file mode 100644 index 00000000000..9e4b64b0df6 --- /dev/null +++ b/packages/app/src/runtime/server/health-machine.ts @@ -0,0 +1,357 @@ +import { endpointCredentials } from "./machine" +import type { PluginServerStatus } from "./plugin-machine" +import type { ServerConnection } from "./registry" + +export const SERVER_HEALTH_POLL_MS = 10_000 + +/** The answer of one `/api/info` probe. */ +export type ServerProbeResult = + | { readonly type: "healthy"; readonly version?: string; readonly hostname?: string } + | { readonly type: "unauthorized" } + | { readonly type: "unreachable" } + +/** + * What the app knows about a server's health. `checking` waits for the first answer from the current endpoint; a + * contributed server that is not ready reports its plugin's stage instead. + */ +export type ServerHealth = + | ServerProbeResult + | { readonly type: "checking"; readonly managed: boolean } + | { readonly type: "incompatible" } + | { readonly type: "starting" } + | { readonly type: "auth" } + | { readonly type: "stopped" } + +type PluginStage = Exclude + +/** A listed server, as plain values: the health checks keep the endpoint they started with. */ +export type ServerHealthInput = + | { readonly type: "staged"; readonly key: ServerConnection.Key; readonly stage: PluginStage } + | { + readonly type: "endpoint" + readonly key: ServerConnection.Key + readonly http: ServerConnection.HttpBase + /** The plugin re-resolves the endpoint, so a new one shows as connecting until it answers. */ + readonly managed: boolean + } + +type Endpoint = { readonly http: ServerConnection.HttpBase; readonly auth: string; readonly managed: boolean } + +/** + * One server. Every probe carries the slot's `attempt`; an answer for another attempt is stale. `checking` and + * `refreshing` have a probe in flight, `settled` waits for the next poll. + */ +export type ServerHealthSlot = + | { readonly type: "staged"; readonly stage: PluginStage; readonly attempt: number } + | { readonly type: "checking"; readonly endpoint: Endpoint; readonly attempt: number } + | { + readonly type: "settled" + readonly endpoint: Endpoint + readonly result: ServerProbeResult + readonly attempt: number + } + | { + readonly type: "refreshing" + readonly endpoint: Endpoint + readonly result: ServerProbeResult + readonly attempt: number + } + +/** A `retry` caller, waiting for each server's probe from at least the given attempt. */ +type RetryWaiter = { + readonly resolve: () => void + readonly targets: ReadonlyMap +} + +export type ServerHealthState = { + readonly entries: ReadonlyMap + readonly waiters: readonly RetryWaiter[] +} + +/** An accepted probe answer, with the endpoint credentials it used. */ +export type ServerProbe = { + readonly key: ServerConnection.Key + readonly auth: string + readonly result: ServerProbeResult +} + +export type ServerHealthEvent = + | { readonly type: "servers.synced"; readonly servers: readonly ServerHealthInput[] } + | { readonly type: "poll.due" } + | { readonly type: "retry.requested"; readonly resolve: () => void } + | { + readonly type: "probe.settled" + readonly key: ServerConnection.Key + readonly attempt: number + readonly result: ServerProbeResult + } + | { readonly type: "disposed" } + +export type ServerHealthEffect = + | { + readonly type: "probe" + readonly key: ServerConnection.Key + readonly attempt: number + readonly http: ServerConnection.HttpBase + } + | { readonly type: "notify.probe"; readonly probe: ServerProbe } + | { readonly type: "timer.start"; readonly intervalMs: number } + | { readonly type: "timer.stop" } + | { readonly type: "waiters.resolve"; readonly resolvers: readonly (() => void)[] } + +export type ServerHealthTransition = { + readonly state: ServerHealthState + readonly effects: readonly ServerHealthEffect[] +} + +export function createServerHealthState(): ServerHealthState { + return { entries: new Map(), waiters: [] } +} + +export function transitionServerHealth(state: ServerHealthState, event: ServerHealthEvent): ServerHealthTransition { + if (event.type === "servers.synced") return synced(state, event.servers) + + if (event.type === "poll.due") return polled(state) + + if (event.type === "retry.requested") return retried(state, event.resolve) + + if (event.type === "probe.settled") return answered(state, event) + + // Disposed: stop polling and release every `retry` caller. + if (!state.entries.size && !state.waiters.length) return { state, effects: [] } + + return { + state: createServerHealthState(), + effects: [ + ...(polling(state) ? [{ type: "timer.stop" as const }] : []), + ...resolved(state.waiters.map((waiter) => waiter.resolve)), + ], + } +} + +/** Every listed server's health, keyed by server. */ +export function selectServerHealthRecord(state: ServerHealthState) { + return Object.fromEntries([...state.entries].map(([key, slot]) => [key, selectServerHealth(slot)])) +} + +// Builds a new object on every call: the driver hands it to a store, which adopts and later changes it. +export function selectServerHealth(slot: ServerHealthSlot): ServerHealth { + if (slot.type === "staged") return slot.stage === "failed" ? { type: "unreachable" } : { type: slot.stage } + + if (slot.type === "checking") return { type: "checking", managed: slot.endpoint.managed } + + return { ...slot.result } +} + +export function isServerHealthy(health: ServerHealth | undefined) { + return health?.type === "healthy" +} + +/** The server can be used: it answered, or nothing says it cannot (not checked yet, or a check is in flight). */ +export function canUseServer(health: ServerHealth | undefined) { + return health === undefined || health.type === "healthy" || health.type === "checking" +} + +/** The server's last answer, or its plugin, says it cannot be used. A check in flight is not a failure. */ +export function isServerFailed(health: ServerHealth | undefined) { + return health?.type === "unreachable" || health?.type === "unauthorized" || health?.type === "incompatible" +} + +export function isServerUnauthorized(health: ServerHealth | undefined) { + return health?.type === "unauthorized" +} + +export function isServerIncompatible(health: ServerHealth | undefined) { + return health?.type === "incompatible" +} + +export function selectServerVersion(health: ServerHealth | undefined) { + return health?.type === "healthy" ? health.version : undefined +} + +export function selectServerHostname(health: ServerHealth | undefined) { + return health?.type === "healthy" ? health.hostname : undefined +} + +/** Sort rank: healthy first, then servers still settling, then failed ones. */ +export function selectServerHealthRank(health: ServerHealth | undefined) { + if (health?.type === "healthy") return 0 + + if (isServerFailed(health)) return 2 + + return 1 +} + +/** The status mark a server row shows. */ +export function selectServerIndicator(health: ServerHealth | undefined) { + if (health === undefined || health.type === "stopped") return "unknown" + + if (health.type === "auth" || health.type === "unauthorized") return "auth" + + if (health.type === "starting") return "connecting" + + if (health.type === "checking") return health.managed ? "connecting" : "unknown" + + return health.type +} + +export type ServerIndicator = ReturnType + +function synced(state: ServerHealthState, servers: readonly ServerHealthInput[]): ServerHealthTransition { + const steps = servers.map((input) => { + const slot = state.entries.get(input.key) + + return { key: input.key, slot, next: listed(slot, input) } + }) + + const entries = + steps.length === state.entries.size && steps.every((step) => step.next === step.slot) + ? state.entries + : new Map(steps.map((step) => [step.key, step.next])) + + return settle( + state, + entries, + steps.flatMap((step) => + step.next.type === "checking" && step.next.attempt !== step.slot?.attempt ? [probe(step.key, step.next)] : [], + ), + ) +} + +function listed(slot: ServerHealthSlot | undefined, input: ServerHealthInput): ServerHealthSlot { + if (input.type === "staged") { + if (slot?.type === "staged" && slot.stage === input.stage) return slot + + // The probe in flight, if any, keeps its attempt and so can never land. + return { type: "staged", stage: input.stage, attempt: slot?.attempt ?? 0 } + } + + const auth = endpointCredentials(input.http) + + // Another endpoint is another target: its previous answer says nothing about the new one. + if (!slot || slot.type === "staged" || slot.endpoint.auth !== auth) + return { + type: "checking", + endpoint: { http: input.http, auth, managed: input.managed }, + attempt: (slot?.attempt ?? 0) + 1, + } + + if (slot.endpoint.managed === input.managed) return slot + + return { ...slot, endpoint: { ...slot.endpoint, managed: input.managed } } +} + +// A probe still in flight keeps running: a server slower than the poll interval still gets its answer. +function polled(state: ServerHealthState): ServerHealthTransition { + const due = [...state.entries].flatMap(([key, slot]) => + slot.type === "settled" ? [{ key, next: { ...slot, type: "refreshing" as const, attempt: slot.attempt + 1 } }] : [], + ) + + if (!due.length) return { state, effects: [] } + + const entries = new Map(state.entries) + due.forEach((item) => entries.set(item.key, item.next)) + + return { state: { entries, waiters: state.waiters }, effects: due.map((item) => probe(item.key, item.next)) } +} + +// A retry replaces any probe in flight, so one that hangs cannot hold the caller. +function retried(state: ServerHealthState, resolve: () => void): ServerHealthTransition { + const restarted = [...state.entries].flatMap(([key, slot]) => { + if (slot.type === "staged") return [] + + const next: ServerHealthSlot = + slot.type === "checking" + ? { ...slot, attempt: slot.attempt + 1 } + : { ...slot, type: "refreshing", attempt: slot.attempt + 1 } + + return [{ key, next }] + }) + + if (!restarted.length) return { state, effects: resolved([resolve]) } + + const entries = new Map(state.entries) + restarted.forEach((item) => entries.set(item.key, item.next)) + + return { + state: { + entries, + waiters: [ + ...state.waiters, + { resolve, targets: new Map(restarted.map((item) => [item.key, item.next.attempt])) }, + ], + }, + effects: [ + ...restarted.map((item) => probe(item.key, item.next)), + { type: "timer.start", intervalMs: SERVER_HEALTH_POLL_MS }, + ], + } +} + +function answered( + state: ServerHealthState, + event: Extract, +): ServerHealthTransition { + const slot = state.entries.get(event.key) + + // A superseded, unlisted or disposed probe changes nothing and reaches no listener. + if (!slot || (slot.type !== "checking" && slot.type !== "refreshing") || slot.attempt !== event.attempt) + return { state, effects: [] } + + return settle( + state, + new Map(state.entries).set(event.key, { + type: "settled", + endpoint: slot.endpoint, + result: event.result, + attempt: slot.attempt, + }), + [{ type: "notify.probe", probe: { key: event.key, auth: slot.endpoint.auth, result: event.result } }], + ) +} + +/** Applies new entries: starts or stops the poll and resolves the `retry` callers whose probes all answered. */ +function settle( + state: ServerHealthState, + entries: ServerHealthState["entries"], + effects: readonly ServerHealthEffect[], +): ServerHealthTransition { + if (entries === state.entries) return { state, effects } + + const remaining = state.waiters.map((waiter) => { + const targets = [...waiter.targets].filter(([key, attempt]) => !answeredSince(entries.get(key), attempt)) + + return targets.length === waiter.targets.size ? waiter : { resolve: waiter.resolve, targets: new Map(targets) } + }) + + const next = { entries, waiters: remaining.filter((waiter) => waiter.targets.size > 0) } + + return { + state: next, + effects: [ + ...effects, + ...(!polling(state) && polling(next) + ? [{ type: "timer.start" as const, intervalMs: SERVER_HEALTH_POLL_MS }] + : []), + ...(polling(state) && !polling(next) ? [{ type: "timer.stop" as const }] : []), + ...resolved(remaining.flatMap((waiter) => (waiter.targets.size ? [] : [waiter.resolve]))), + ], + } +} + +// A server that left the list or has no endpoint has nothing left to answer. +function answeredSince(slot: ServerHealthSlot | undefined, attempt: number) { + return !slot || slot.type === "staged" || (slot.type === "settled" && slot.attempt >= attempt) +} + +function polling(state: ServerHealthState) { + return [...state.entries.values()].some((slot) => slot.type !== "staged") +} + +function probe(key: ServerConnection.Key, slot: { readonly endpoint: Endpoint; readonly attempt: number }) { + return { type: "probe" as const, key, attempt: slot.attempt, http: slot.endpoint.http } +} + +function resolved(resolvers: readonly (() => void)[]): ServerHealthEffect[] { + return resolvers.length ? [{ type: "waiters.resolve", resolvers }] : [] +} diff --git a/packages/app/src/runtime/server/health.test.ts b/packages/app/src/runtime/server/health.test.ts index 9767ab689dd..a7861f3412c 100644 --- a/packages/app/src/runtime/server/health.test.ts +++ b/packages/app/src/runtime/server/health.test.ts @@ -9,6 +9,18 @@ const server: ServerConnection.HttpBase = { const info = (version = "1.2.3") => Response.json({ version, pid: 1, urls: [server.url], paths: { tmp: "/tmp/opencode" } }) +// SAFETY: `checkServerHealth` only calls `fetch(input, init)`, which these fakes implement; none reads `preconnect`. +const fakeFetch = (handler: (input: RequestInfo | URL, init?: RequestInit) => Promise) => + handler as typeof globalThis.fetch + +// Puts back the `AbortSignal.timeout` a test replaced, or removes the stand-in when the runtime had none. +function restoreTimeout(descriptor: PropertyDescriptor | undefined) { + if (descriptor) return void Object.defineProperty(AbortSignal, "timeout", descriptor) + + const signal: Partial = AbortSignal + delete signal.timeout +} + function abortFromInput(input: RequestInfo | URL, init?: RequestInit) { if (init?.signal) return init.signal @@ -21,14 +33,14 @@ describe("checkServerHealth", () => { test.each([undefined, "secret"])("reads /api/info authenticating with only the password (%s)", async (password) => { const requests: { path: string; authorization: string | null }[] = [] - const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const fetch = fakeFetch(async (input, init) => { const url = input instanceof URL ? input : new URL(input instanceof Request ? input.url : input) requests.push({ path: url.pathname, authorization: new Headers(init?.headers).get("authorization") }) return info("2.0.0") - }) as typeof globalThis.fetch + }) - expect(await checkServerHealth({ ...server, password }, fetch)).toEqual({ healthy: true, version: "2.0.0" }) + expect(await checkServerHealth({ ...server, password }, fetch)).toEqual({ type: "healthy", version: "2.0.0" }) expect(requests).toEqual([ { path: "/api/info", authorization: password ? `Basic ${btoa(`opencode:${password}`)}` : null }, ]) @@ -37,13 +49,16 @@ describe("checkServerHealth", () => { test("reports rejected credentials without retrying", async () => { let calls = 0 - const fetch = (async () => { + const fetch = fakeFetch(async () => { calls++ - return Response.json({ _tag: "UnauthorizedError", message: "Authentication required" }, { status: 401 }) - }) as unknown as typeof globalThis.fetch + return new Response(`{"_tag":"UnauthorizedError","message":"Authentication required"}`, { + status: 401, + headers: { "content-type": "application/json" }, + }) + }) - expect(await checkServerHealth(server, fetch)).toEqual({ healthy: false, unauthorized: true }) + expect(await checkServerHealth(server, fetch)).toEqual({ type: "unauthorized" }) expect(calls).toBe(1) }) @@ -59,13 +74,9 @@ describe("checkServerHealth", () => { }, }) - const fetch = (async () => info()) as unknown as typeof globalThis.fetch + const fetch = fakeFetch(async () => info()) - await checkServerHealth(server, fetch).finally(() => { - if (timeout) Object.defineProperty(AbortSignal, "timeout", timeout) - - if (!timeout) delete (AbortSignal as Partial).timeout - }) + await checkServerHealth(server, fetch).finally(() => restoreTimeout(timeout)) expect(timeoutMs).toBe(30_000) }) @@ -79,39 +90,37 @@ describe("checkServerHealth", () => { let aborted = false - const fetch = ((input: RequestInfo | URL, init?: RequestInit) => - new Promise((_resolve, reject) => { - const signal = abortFromInput(input, init) - signal?.addEventListener( - "abort", - () => { - aborted = true - reject(new DOMException("Aborted", "AbortError")) - }, - { once: true }, - ) - })) as unknown as typeof globalThis.fetch + const fetch = fakeFetch( + (input, init) => + new Promise((_resolve, reject) => { + const signal = abortFromInput(input, init) + signal?.addEventListener( + "abort", + () => { + aborted = true + reject(new DOMException("Aborted", "AbortError")) + }, + { once: true }, + ) + }), + ) const result = await checkServerHealth(server, fetch, { timeoutMs: 10, - }).finally(() => { - if (timeout) Object.defineProperty(AbortSignal, "timeout", timeout) - - if (!timeout) delete (AbortSignal as Partial).timeout - }) + }).finally(() => restoreTimeout(timeout)) expect(aborted).toBe(true) - expect(result).toEqual({ healthy: false }) + expect(result).toEqual({ type: "unreachable" }) }) test("uses provided abort signal", async () => { let signal: AbortSignal | undefined - const fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const fetch = fakeFetch(async (input, init) => { signal = abortFromInput(input, init) return info() - }) as unknown as typeof globalThis.fetch + }) const abort = new AbortController() await checkServerHealth(server, fetch, { @@ -124,13 +133,13 @@ describe("checkServerHealth", () => { test("retries transient failures and eventually succeeds", async () => { let count = 0 - const fetch = (async () => { + const fetch = fakeFetch(async () => { count += 1 if (count < 3) throw new TypeError("network") return info() - }) as unknown as typeof globalThis.fetch + }) const result = await checkServerHealth(server, fetch, { retryCount: 2, @@ -138,16 +147,16 @@ describe("checkServerHealth", () => { }) expect(count).toBe(3) - expect(result).toEqual({ healthy: true, version: "1.2.3" }) + expect(result).toEqual({ type: "healthy", version: "1.2.3" }) }) - test("returns unhealthy when retries are exhausted", async () => { + test("returns unreachable when retries are exhausted", async () => { let count = 0 - const fetch = (async () => { + const fetch = fakeFetch(async () => { count += 1 throw new TypeError("network") - }) as unknown as typeof globalThis.fetch + }) const result = await checkServerHealth(server, fetch, { retryCount: 2, @@ -155,6 +164,6 @@ describe("checkServerHealth", () => { }) expect(count).toBe(3) - expect(result).toEqual({ healthy: false }) + expect(result).toEqual({ type: "unreachable" }) }) }) diff --git a/packages/app/src/runtime/server/health.ts b/packages/app/src/runtime/server/health.ts index 0edc4a36784..751c71da64c 100644 --- a/packages/app/src/runtime/server/health.ts +++ b/packages/app/src/runtime/server/health.ts @@ -1,22 +1,21 @@ import { usePlatform } from "@/runtime/platform/platform" import { ServerConnection } from "@/runtime/server/registry" import { authTokenFromCredentials } from "./api" +import { + createServerHealthState, + selectServerHealthRecord, + transitionServerHealth, + type ServerHealth, + type ServerHealthEvent, + type ServerHealthInput, + type ServerProbe, + type ServerProbeResult, +} from "./health-machine" import { endpointCredentials } from "./machine" import { ClientError, isUnauthorizedError, OpenCode } from "@opencode/client" -import { Match } from "effect" -import { Accessor, createEffect, createSignal, onCleanup } from "solid-js" +import { Accessor, batch, createComputed, createSignal, onCleanup, untrack } from "solid-js" import { createStore, reconcile } from "solid-js/store" -export type ServerHealth = { - healthy: boolean - version?: string - /** The server machine's name, when the server reports one. */ - hostname?: string - incompatible?: boolean - checking?: boolean - unauthorized?: boolean -} - interface CheckServerHealthOptions { timeoutMs?: number signal?: AbortSignal @@ -32,9 +31,10 @@ const defaultRetryDelayMs = 100 const cacheMs = 750 +// `at` is when the probe started, then when it answered: a probe that hangs is shared only briefly. const healthCache = new Map< string, - { at: number; done: boolean; fetch: typeof globalThis.fetch; promise: Promise } + { at: number; fetch: typeof globalThis.fetch; promise: Promise } >() /** A signal that aborts after a timeout, and how to stop its timer early when one runs. */ @@ -92,11 +92,13 @@ function retryable(error: unknown, signal?: AbortSignal) { return /network|fetch|econnreset|econnrefused|enotfound|timedout/i.test(error.message) } +const unreachable = { type: "unreachable" } as const + export async function checkServerHealth( server: ServerConnection.HttpBase, fetch: typeof globalThis.fetch, opts?: CheckServerHealthOptions, -): Promise { +): Promise { const timeout = opts?.signal ? undefined : timeoutSignal(opts?.timeoutMs ?? defaultTimeoutMs) const signal = opts?.signal ?? timeout?.signal const retryCount = opts?.retryCount ?? defaultRetryCount @@ -108,15 +110,15 @@ export async function checkServerHealth( } : undefined - const next = (count: number, retry: boolean) => { - if (count >= retryCount || !retry) return Promise.resolve({ healthy: false } as const) + const next = (count: number, retry: boolean): Promise => { + if (count >= retryCount || !retry) return Promise.resolve(unreachable) return wait(retryDelayMs * (count + 1), signal) .then(() => attempt(count + 1)) - .catch(() => ({ healthy: false })) + .catch(() => unreachable) } - const attempt = async (count: number): Promise => { + const attempt = async (count: number): Promise => { const current = await OpenCode.make({ baseUrl: server.url, fetch, @@ -124,15 +126,15 @@ export async function checkServerHealth( }) .server.info({ signal }) .then((status) => ({ - data: { healthy: true as const, version: status.version, hostname: status.hostname }, + data: { type: "healthy" as const, version: status.version, hostname: status.hostname }, })) .catch((error) => ({ error })) if ("data" in current) return current.data - if (signal?.aborted) return { healthy: false } + if (signal?.aborted) return unreachable - if (isUnauthorizedError(current.error)) return { healthy: false, unauthorized: true } + if (isUnauthorizedError(current.error)) return { type: "unauthorized" } return next(count, retryable(current.error, signal)) } @@ -140,16 +142,6 @@ export async function checkServerHealth( return attempt(0).finally(() => timeout?.clear?.()) } -const pollMs = 10_000 - -// What a contributed server that is not ready reports: failed and incompatible are known; any other stage is unknown. -const stageHealth = (stage: ServerConnection.Plugin["state"]) => - Match.value(stage).pipe( - Match.when("failed", (): ServerHealth | undefined => ({ healthy: false })), - Match.when("incompatible", (): ServerHealth | undefined => ({ healthy: false, incompatible: true })), - Match.orElse((): ServerHealth | undefined => undefined), - ) - export function useCheckServerHealth() { const platform = usePlatform() const fetcher = platform.fetch ?? globalThis.fetch @@ -159,128 +151,86 @@ export function useCheckServerHealth() { const hit = healthCache.get(key) const now = Date.now() - if (hit && hit.fetch === fetcher && (!hit.done || now - hit.at < cacheMs)) return hit.promise + if (hit && hit.fetch === fetcher && now - hit.at < cacheMs) return hit.promise const promise = checkServerHealth(http, fetcher).finally(() => { - const next = healthCache.get(key) - - if (!next || next.promise !== promise) return - next.done = true - next.at = Date.now() + if (healthCache.get(key)?.promise === promise) healthCache.set(key, { at: Date.now(), fetch: fetcher, promise }) }) - healthCache.set(key, { at: now, done: false, fetch: fetcher, promise }) + healthCache.set(key, { at: now, fetch: fetcher, promise }) return promise } } -/** One finished probe, with the endpoint credentials it used. */ -export type ServerProbe = { key: ServerConnection.Key; auth: string; health: ServerHealth } - -export const useServerHealth = ( - servers: Accessor, - enabled: Accessor, - onProbe?: (probe: ServerProbe) => void, -) => { - return createServerHealth(servers, enabled, useCheckServerHealth(), onProbe) +export const useServerHealth = (servers: Accessor, onProbe?: (probe: ServerProbe) => void) => { + return createServerHealth(servers, useCheckServerHealth(), onProbe) } export function createServerHealth( servers: Accessor, - enabled: Accessor, - check: (http: ServerConnection.HttpBase) => Promise, + check: (http: ServerConnection.HttpBase) => Promise, onProbe?: (probe: ServerProbe) => void, ) { + const [machine, setMachine] = createSignal(createServerHealthState()) const [status, setStatus] = createStore>({}) - const [attempt, setAttempt] = createSignal(0) - const endpoints = new Map() - // Callers of `retry` wait for the probe round it starts. - const waiting: (() => void)[] = [] + let timer: ReturnType | undefined - createEffect(() => { - attempt() + const dispatch = (event: ServerHealthEvent) => + batch(() => { + const prev = untrack(machine) + const result = transitionServerHealth(prev, event) - if (!enabled()) { - endpoints.clear() - setStatus(reconcile({})) - waiting.splice(0).forEach((resolve) => resolve()) + // The selector builds new objects, which the store adopts and later updates in place. + if (result.state.entries !== prev.entries) setStatus(reconcile(selectServerHealthRecord(result.state))) + setMachine(result.state) - return - } + result.effects.forEach((effect) => { + if (effect.type === "probe") { + const settled = (answer: ServerProbeResult) => + dispatch({ type: "probe.settled", key: effect.key, attempt: effect.attempt, result: answer }) - // Snapshot transport fields synchronously so a newly established SSH tunnel - // invalidates both the old result and any probe still using the old endpoint. - const list = servers().map((conn) => ({ - key: ServerConnection.key(conn), - managed: conn.type === "plugin" && conn.managed, - http: conn.http, - stage: conn.type === "plugin" ? conn.state : undefined, - })) + void Promise.try(() => check(effect.http)).then(settled, () => settled(unreachable)) - for (const conn of list) { - if (conn.stage && conn.stage !== "ready") { - endpoints.delete(conn.key) - setStatus(conn.key, reconcile(stageHealth(conn.stage))) - continue - } + return + } - const endpoint = endpointCredentials(conn.http) + if (effect.type === "notify.probe") return onProbe?.(effect.probe) - if (conn.managed && endpoints.get(conn.key) !== endpoint) { - setStatus(conn.key, reconcile({ healthy: false, checking: true })) - } + if (effect.type === "waiters.resolve") return effect.resolvers.forEach((resolve) => resolve()) - endpoints.set(conn.key, endpoint) - } - - for (const key of endpoints.keys()) { - if (!list.some((conn) => conn.key === key)) endpoints.delete(key) - } - - let dead = false - - const refresh = async () => { - const results: Record = {} - await Promise.all( - list.map(async (conn) => { - if (conn.stage && conn.stage !== "ready") { - results[conn.key] = stageHealth(conn.stage) - - return - } - - // The endpoint can change while the probe runs; the listener matches the result to the credentials it used. - const auth = endpointCredentials(conn.http) - const result = await check(conn.http) - results[conn.key] = result - onProbe?.({ key: conn.key, auth, health: result }) - - if (!dead) setStatus(conn.key, reconcile(result)) - }), - ) - - if (dead) return - setStatus(reconcile(results)) - } - - void refresh().finally(() => { - if (!dead) waiting.splice(0).forEach((resolve) => resolve()) - }) - const id = setInterval(() => void refresh(), pollMs) - onCleanup(() => { - dead = true - clearInterval(id) + clearInterval(timer) + timer = + effect.type === "timer.start" + ? setInterval(() => dispatch({ type: "poll.due" }), effect.intervalMs) + : undefined + }) }) + + // Runs during updates, so a server's health is known on the render that lists it. Reading the endpoint here tracks a + // saved password and a plugin server's stage and endpoint. + createComputed(() => { + const list = servers().map(healthInput) + + untrack(() => dispatch({ type: "servers.synced", servers: list })) }) + onCleanup(() => dispatch({ type: "disposed" })) + return { status, - /** Probes every server now instead of at the next poll; resolves when that round finishes. */ - retry: () => - new Promise((resolve) => { - waiting.push(resolve) - setAttempt((value) => value + 1) - }), + /** Probes every server now instead of at the next poll; resolves when each has answered or left the list. */ + retry: () => new Promise((resolve) => dispatch({ type: "retry.requested", resolve })), } } + +function healthInput(conn: ServerConnection.Any): ServerHealthInput { + const key = ServerConnection.key(conn) + + if (conn.type !== "plugin") + return { type: "endpoint", key, http: { url: conn.http.url, password: conn.http.password }, managed: false } + + if (conn.status.type === "ready") return { type: "endpoint", key, http: conn.status.http, managed: conn.managed } + + return { type: "staged", key, stage: conn.status.type } +} diff --git a/packages/app/src/runtime/server/machine.test.ts b/packages/app/src/runtime/server/machine.test.ts index 81395c3c166..3effca6d29f 100644 --- a/packages/app/src/runtime/server/machine.test.ts +++ b/packages/app/src/runtime/server/machine.test.ts @@ -36,8 +36,7 @@ const rejected = (password: string): ServerControllersEvent => ({ type: "health.reported", key, auth: `${url}\n${password}`, - healthy: false, - unauthorized: true, + result: { type: "unauthorized" }, }) // Controllers dispose long after the run that created them, so every run appends to one log. @@ -114,6 +113,24 @@ test("a rejection of credentials the server has since replaced changes nothing", expect(selectServerCtx(late.state, key)?.name).toBe("ctx2") }) +test("a contributed server has credentials to check only while it is ready, never its placeholder endpoint", () => { + const plugin = (status: ServerConnection.Plugin["status"]): ServerConnection.Plugin => ({ + type: "plugin", + key: "ssh:box", + plugin: "ssh", + status, + managed: true, + http: { url: "http://127.0.0.1:0" }, + authenticate: () => false, + }) + + expect(serverCredentials(plugin({ type: "auth" }))).toEqual({ controller: "", auth: undefined }) + expect(serverCredentials(plugin({ type: "ready", http: { url, password: "pw" } }))).toEqual({ + controller: "", + auth: `${url}\npw`, + }) +}) + test("new HTTP credentials dispose the old controller before the new one starts", () => { expect(run(live("pw1"), synced(http("pw2"))).log).toEqual(["dispose ctx1", "create ctx2"]) }) diff --git a/packages/app/src/runtime/server/machine.ts b/packages/app/src/runtime/server/machine.ts index 79ba5842bde..eeb7170f776 100644 --- a/packages/app/src/runtime/server/machine.ts +++ b/packages/app/src/runtime/server/machine.ts @@ -1,3 +1,4 @@ +import type { ServerProbeResult } from "./health-machine" import type { ServerConnection } from "./registry" /** What a server's controller is bound to, as plain values read from its listed connection. */ @@ -59,8 +60,7 @@ export type ServerControllersEvent = readonly type: "health.reported" readonly key: ServerConnection.Key readonly auth: string - readonly healthy: boolean - readonly unauthorized: boolean + readonly result: ServerProbeResult } | { readonly type: "disposed" } @@ -94,12 +94,12 @@ export function endpointCredentials(http: ServerConnection.HttpBase) { } export function serverCredentials(conn: ServerConnection.Any): ServerCredentials { + if (conn.type === "plugin") + return { controller: "", auth: conn.status.type === "ready" ? endpointCredentials(conn.status.http) : undefined } + const endpoint = endpointCredentials(conn.http) - return { - controller: conn.type === "http" ? endpoint : "", - auth: conn.type === "plugin" && conn.state !== "ready" ? undefined : endpoint, - } + return { controller: conn.type === "http" ? endpoint : "", auth: endpoint } } export function createServerControllersState(): ServerControllersState { @@ -220,13 +220,14 @@ function reported( // A server that accepts the rejected credentials again, such as after its password was restored, gets a controller. if (state.type === "unauthorized") { - if (event.healthy && event.auth === state.credentials.auth) return start(state, state.conn, state.credentials, []) + if (event.result.type === "healthy" && event.auth === state.credentials.auth) + return start(state, state.conn, state.credentials, []) return { state, effects: [] } } // A probe of credentials the server has since replaced says nothing about the current ones. - if (!event.unauthorized || event.auth !== state.credentials.auth) return { state, effects: [] } + if (event.result.type !== "unauthorized" || event.auth !== state.credentials.auth) return { state, effects: [] } return { state: { diff --git a/packages/app/src/runtime/server/plugin-machine.test.ts b/packages/app/src/runtime/server/plugin-machine.test.ts new file mode 100644 index 00000000000..95f3208055f --- /dev/null +++ b/packages/app/src/runtime/server/plugin-machine.test.ts @@ -0,0 +1,128 @@ +import { expect, test } from "bun:test" +import { + canAuthenticatePluginServer, + createPluginServerState, + PLUGIN_CONNECT_TIMEOUT_MS, + selectPluginStatus, + transitionPluginServer, + type PluginEntrySnapshot, + type PluginServerEffect, + type PluginServerEvent, + type PluginServerState, + type PluginServerStatus, +} from "./plugin-machine" + +const http = { url: "http://127.0.0.1:4100", password: "secret" } + +const entry = (state: PluginEntrySnapshot["state"], endpoint?: PluginEntrySnapshot["http"]): PluginServerEvent => ({ + type: "entry.updated", + entry: { state, http: endpoint }, +}) + +const settled = (attempt: number, ready: boolean): PluginServerEvent => ({ type: "connect.settled", attempt, ready }) + +function replay(events: readonly PluginServerEvent[], state = createPluginServerState({ state: "auth" })) { + return events.reduce<{ state: PluginServerState; effects: PluginServerEffect[] }>( + (result, event) => { + const next = transitionPluginServer(result.state, event) + + return { state: next.state, effects: [...result.effects, ...next.effects] } + }, + { state, effects: [] }, + ) +} + +// What the callbacks the driver ran did. +const log: string[] = [] + +const logs = (step: string) => () => void log.push(step) + +/** Plays the driver's `notify.connected`: runs every callback and returns what they logged. */ +function run(effects: readonly PluginServerEffect[]) { + const from = log.length + effects.forEach((effect) => effect.type === "notify.connected" && effect.callbacks.forEach((callback) => callback())) + + return log.slice(from) +} + +test("a second sign-in request joins the first instead of connecting again", () => { + const clicked = replay([ + { type: "authenticate", onConnected: logs("focus") }, + { type: "authenticate", onConnected: logs("open project") }, + ]) + + expect(clicked.effects).toEqual([{ type: "connect", attempt: 1, timeoutMs: PLUGIN_CONNECT_TIMEOUT_MS }]) + expect(selectPluginStatus(clicked.state)).toEqual({ type: "starting" }) + expect(canAuthenticatePluginServer(clicked.state)).toBe(true) + + const ready = transitionPluginServer(clicked.state, entry("ready", http)) + expect(selectPluginStatus(ready.state)).toEqual({ type: "ready", http }) + expect(run(ready.effects)).toEqual(["focus", "open project"]) +}) + +test("a password prompt during sign-in keeps the server connecting until the attempt ends", () => { + const prompted = replay([{ type: "authenticate", onConnected: logs("focus") }, entry("starting"), entry("auth")]) + expect(selectPluginStatus(prompted.state)).toEqual({ type: "starting" }) + + // The user cancels the prompt: the server asks for sign-in again and nothing continues. + const cancelled = transitionPluginServer(prompted.state, settled(1, false)) + expect(selectPluginStatus(cancelled.state)).toEqual({ type: "auth" }) + expect(cancelled.effects).toEqual([{ type: "timeout.cancel" }]) + expect(canAuthenticatePluginServer(cancelled.state)).toBe(true) +}) + +test.each([ + { name: "the plugin reports ready first", events: [entry("ready", http), settled(1, true)] }, + { name: "connect resolves first", events: [settled(1, true), entry("ready", http)] }, +])("sign-in continues exactly once when $name", (input) => { + const result = replay([{ type: "authenticate", onConnected: logs("focus") }, ...input.events]) + + expect(run(result.effects)).toEqual(["focus"]) + expect(selectPluginStatus(result.state)).toEqual({ type: "ready", http }) +}) + +test("a sign-in that runs out of time shows the plugin's stage again and never continues", () => { + const timedOut = replay([ + { type: "authenticate", onConnected: logs("focus") }, + entry("starting"), + { type: "connect.timed-out", attempt: 1 }, + ]) + + expect(selectPluginStatus(timedOut.state)).toEqual({ type: "starting" }) + + const late = replay([settled(1, true), entry("ready", http)], timedOut.state) + expect(run(late.effects)).toEqual([]) + expect(selectPluginStatus(late.state)).toEqual({ type: "ready", http }) +}) + +test.each<{ name: string; event: PluginServerEvent; status: PluginServerStatus["type"] }>([ + { name: "fails", event: entry("failed"), status: "failed" }, + { name: "stops", event: entry("stopped"), status: "stopped" }, + { name: "is unlisted", event: { type: "disposed" }, status: "stopped" }, +])("a sign-in ends and drops its callbacks when the server $name", (input) => { + const ended = replay([{ type: "authenticate", onConnected: logs("focus") }, input.event]) + + expect(ended.effects).toContainEqual({ type: "timeout.cancel" }) + expect(selectPluginStatus(ended.state).type).toBe(input.status) + expect(run(replay([settled(1, true), entry("ready", http)], ended.state).effects)).toEqual([]) +}) + +test.each(["stopped", "starting", "ready", "failed", "incompatible"] as const)( + "a server in %s does not start sign-in", + (stage) => { + const state = createPluginServerState({ state: stage, http }) + + expect(canAuthenticatePluginServer(state)).toBe(false) + expect(transitionPluginServer(state, { type: "authenticate" })).toEqual({ state, effects: [] }) + }, +) + +test("a ready report without an endpoint is a failure, never a ready server", () => { + const state = createPluginServerState({ state: "ready" }) + + expect(selectPluginStatus(state)).toEqual({ type: "failed" }) + expect(transitionPluginServer(state, entry("ready")).state).toBe(state) + + const ready = transitionPluginServer(state, entry("ready", { ...http })).state + expect(transitionPluginServer(ready, entry("ready", { ...http })).state).toBe(ready) +}) diff --git a/packages/app/src/runtime/server/plugin-machine.ts b/packages/app/src/runtime/server/plugin-machine.ts new file mode 100644 index 00000000000..3c9c3a48a9d --- /dev/null +++ b/packages/app/src/runtime/server/plugin-machine.ts @@ -0,0 +1,170 @@ +import type { ServerState } from "@opencode/plugin/gui" +import type { ServerConnection } from "./registry" + +/** How long a sign-in may run before the server shows what its plugin reports again. */ +export const PLUGIN_CONNECT_TIMEOUT_MS = 120_000 + +/** Where a contributed server stands. Only a ready server has an endpoint. */ +export type PluginServerStatus = + | { readonly type: "stopped" | "starting" | "auth" | "failed" | "incompatible" } + | { readonly type: "ready"; readonly http: ServerConnection.HttpBase } + +/** What the app reads from a plugin's `ServerEntry`, as plain values. */ +export type PluginEntrySnapshot = { readonly state: ServerState; readonly http?: ServerConnection.HttpBase } + +type Stage = Exclude + +/** + * One contributed server. `authenticating` starts when the user asks to sign in and lasts until the plugin reports the + * server ready, the plugin's `connect` gives up, or the timeout runs out; meanwhile the server shows as starting. + */ +export type PluginServerState = + | { readonly status: Stage; readonly attempt: number } + | { readonly status: "ready"; readonly attempt: number; readonly http: ServerConnection.HttpBase } + | { + readonly status: "authenticating" + readonly attempt: number + /** What the plugin reports meanwhile; the server returns to it when sign-in ends without it ready. */ + readonly reported: "auth" | "starting" + /** Run once the server is ready. */ + readonly callbacks: readonly (() => void)[] + } + +export type PluginServerEvent = + | { readonly type: "entry.updated"; readonly entry: PluginEntrySnapshot } + | { readonly type: "authenticate"; readonly onConnected?: () => void } + | { readonly type: "connect.settled"; readonly attempt: number; readonly ready: boolean } + | { readonly type: "connect.timed-out"; readonly attempt: number } + | { readonly type: "disposed" } + +export type PluginServerEffect = + | { readonly type: "connect"; readonly attempt: number; readonly timeoutMs: number } + | { readonly type: "timeout.cancel" } + | { readonly type: "notify.connected"; readonly callbacks: readonly (() => void)[] } + +export type PluginServerTransition = { + readonly state: PluginServerState + readonly effects: readonly PluginServerEffect[] +} + +export function createPluginServerState(entry: PluginEntrySnapshot): PluginServerState { + return { ...target(entry), attempt: 0 } +} + +export function transitionPluginServer(state: PluginServerState, event: PluginServerEvent): PluginServerTransition { + if (event.type === "entry.updated") return updated(state, event.entry) + + if (event.type === "authenticate") { + if (state.status === "auth") { + const attempt = state.attempt + 1 + + return { + state: { + status: "authenticating", + attempt, + reported: "auth", + callbacks: event.onConnected ? [event.onConnected] : [], + }, + effects: [{ type: "connect", attempt, timeoutMs: PLUGIN_CONNECT_TIMEOUT_MS }], + } + } + + // A second click joins the sign-in in progress instead of starting another. + if (state.status === "authenticating" && event.onConnected) + return { state: { ...state, callbacks: [...state.callbacks, event.onConnected] }, effects: [] } + + return unchanged(state) + } + + // An unlisted server drops the callbacks of its sign-in; a late answer finds nothing to resume. + if (event.type === "disposed") { + if (state.status === "stopped") return unchanged(state) + + return { + state: { status: "stopped", attempt: state.attempt }, + effects: state.status === "authenticating" ? [{ type: "timeout.cancel" }] : [], + } + } + + if (state.status !== "authenticating" || event.attempt !== state.attempt) return unchanged(state) + + if (event.type === "connect.timed-out") + return { state: { status: state.reported, attempt: state.attempt }, effects: [] } + + // The plugin reports the server ready on its own channel; until it does, the timeout bounds the wait. + if (event.ready) return unchanged(state) + + return { state: { status: state.reported, attempt: state.attempt }, effects: [{ type: "timeout.cancel" }] } +} + +/** What the app shows and connects to; a sign-in in progress shows as starting. */ +export function selectPluginStatus(state: PluginServerState): PluginServerStatus { + if (state.status === "ready") return { type: "ready", http: state.http } + + if (state.status === "authenticating") return stages.starting + + return stages[state.status] +} + +/** The server waits for sign-in, or one is in progress that a new request joins. */ +export function canAuthenticatePluginServer(state: PluginServerState) { + return state.status === "auth" || state.status === "authenticating" +} + +const stages = { + stopped: { type: "stopped" }, + starting: { type: "starting" }, + auth: { type: "auth" }, + failed: { type: "failed" }, + incompatible: { type: "incompatible" }, +} as const satisfies Record + +function updated(state: PluginServerState, entry: PluginEntrySnapshot): PluginServerTransition { + const next = target(entry) + + if (state.status === "authenticating") { + if (next.status === "ready") + return { + state: { ...next, attempt: state.attempt }, + effects: [ + { type: "timeout.cancel" }, + ...(state.callbacks.length ? [{ type: "notify.connected" as const, callbacks: state.callbacks }] : []), + ], + } + + // A sign-in can report a prompt (`auth`) or progress (`starting`) before it settles. + if (next.status === "auth" || next.status === "starting") + return next.status === state.reported + ? unchanged(state) + : { state: { ...state, reported: next.status }, effects: [] } + + return { state: { ...next, attempt: state.attempt }, effects: [{ type: "timeout.cancel" }] } + } + + if ( + next.status === state.status && + (next.status !== "ready" || (state.status === "ready" && same(next.http, state.http))) + ) + return unchanged(state) + + return { state: { ...next, attempt: state.attempt }, effects: [] } +} + +// A plugin that reports ready without an endpoint cannot be reached; it never reads as ready. +function target( + entry: PluginEntrySnapshot, +): { readonly status: Stage } | { readonly status: "ready"; readonly http: ServerConnection.HttpBase } { + if (entry.state !== "ready") return { status: entry.state } + + if (!entry.http) return { status: "failed" } + + return { status: "ready", http: { url: entry.http.url, password: entry.http.password } } +} + +function same(a: ServerConnection.HttpBase, b: ServerConnection.HttpBase) { + return a.url === b.url && a.password === b.password +} + +function unchanged(state: PluginServerState): PluginServerTransition { + return { state, effects: [] } +} diff --git a/packages/app/src/runtime/server/registry.test.ts b/packages/app/src/runtime/server/registry.test.ts index 00d11271dc8..69fb9400346 100644 --- a/packages/app/src/runtime/server/registry.test.ts +++ b/packages/app/src/runtime/server/registry.test.ts @@ -68,9 +68,8 @@ test("treats WSL and mismatched-port loopback servers as remote connections", () type: "plugin", key: "wsl:Debian", plugin: "wsl", - state: "ready", - connecting: false, - authenticationRequired: false, + status: { type: "ready", http: { url: "http://127.0.0.1:4097" } }, + authenticate: () => false, managed: false, http: { url: "http://127.0.0.1:4097" }, }, diff --git a/packages/app/src/runtime/server/registry.tsx b/packages/app/src/runtime/server/registry.tsx index 1255b36a9d0..99de6ba9313 100644 --- a/packages/app/src/runtime/server/registry.tsx +++ b/packages/app/src/runtime/server/registry.tsx @@ -4,7 +4,7 @@ import { batch, createMemo } from "solid-js" import { reconcile, type SetStoreFunction, type Store } from "solid-js/store" import { Persist, persisted } from "@/runtime/persistence/storage" import { ServerScope } from "@/runtime/server/scope" -import type { ServerEntry } from "@opencode/plugin/gui" +import type { PluginServerStatus } from "./plugin-machine" import { ServerHttp, ServerHttpBase, ServerKey, ServerState } from "./persistence" type StoredState = typeof ServerState.Type @@ -23,7 +23,7 @@ export function serverName(conn?: ServerConnection.Any, ignoreDisplayName = fals if (conn.displayName && !ignoreDisplayName) return conn.displayName - return conn.http.url.replace(/^https?:\/\//, "").replace(/\/+$/, "") + return (ServerConnection.http(conn)?.url ?? "").replace(/^https?:\/\//, "").replace(/\/+$/, "") } /** @@ -169,14 +169,21 @@ export namespace ServerConnection { type: "plugin" key: string plugin: string - state: ServerEntry["state"] - connecting: boolean - authenticationRequired: boolean + status: PluginServerStatus /** The plugin re-resolves the endpoint (e.g. a tunnel), so the connection can drop and come back. */ managed: boolean + /** + * Where the event stream starts and follows: the plugin's last endpoint, or an unroutable placeholder before it + * reports one. Everything else reads `ServerConnection.http`, which has an endpoint only while the server is ready. + */ http: HttpBase reconnect?: (signal: AbortSignal) => Promise - /** Called before opening a server that is not ready. Resolves true once it is. */ + /** + * Starts sign-in while the server waits for it, or joins the one in progress; false when the server does not + * wait for sign-in. `onConnected` runs once the server is ready. + */ + authenticate: (onConnected?: () => void) => boolean + /** Asks the plugin to connect, e.g. after the server rejected our credentials. Resolves true once it is ready. */ connect?: () => Promise } & Base @@ -200,14 +207,15 @@ export namespace ServerConnection { export type Key = typeof Key.Type export const builtin = (conn: Any) => conn.type === "sidecar" && conn.variant === "base" - /** Starts sign-in for a server that asks for it; false when it does not. */ - export const authenticate = (conn: Any, onConnected?: () => void) => { - if (conn.type !== "plugin" || !conn.authenticationRequired || !conn.connect) return false - void conn.connect().then((ready) => { - if (ready) onConnected?.() - }) + /** Starts sign-in for a server that asks for it, or joins the one in progress; false when it does not. */ + export const authenticate = (conn: Any, onConnected?: () => void) => + conn.type === "plugin" && conn.authenticate(onConnected) - return true + /** The endpoint to probe, show or call; undefined while a contributed server is not ready. */ + export const http = (conn: Any) => { + if (conn.type !== "plugin") return conn.http + + return conn.status.type === "ready" ? conn.status.http : undefined } // The desktop sidecar, or a loopback http connection on the same port as the app's own server. Other loopback diff --git a/packages/app/src/runtime/server/runtime.tsx b/packages/app/src/runtime/server/runtime.tsx index 4ad5d73bc16..e7f8cf75f00 100644 --- a/packages/app/src/runtime/server/runtime.tsx +++ b/packages/app/src/runtime/server/runtime.tsx @@ -23,6 +23,7 @@ import { } from "./machine" import { pathKey } from "@/workspaces/path-key" import { useServerHealth } from "@/runtime/server/health" +import { selectServerHostname } from "@/runtime/server/health-machine" import { createServerSdkContext } from "./client" import { createServerSyncContext } from "./sync" import { createData, createSessionRetention } from "@opencode/client/solid" @@ -89,15 +90,7 @@ export const { use: useGlobal, provider: GlobalProvider } = createSimpleContext( const serverHealth = useServerHealth( () => server.list, - () => true, - (probe) => - dispatch({ - type: "health.reported", - key: probe.key, - auth: probe.auth, - healthy: probe.health.healthy, - unauthorized: !!probe.health.unauthorized, - }), + (probe) => dispatch({ type: "health.reported", key: probe.key, auth: probe.auth, result: probe.result }), ) // Runs during updates, so listed servers have a controller before anything reads them, including on first render. @@ -117,7 +110,7 @@ export const { use: useGlobal, provider: GlobalProvider } = createSimpleContext( createEffect(() => { for (const conn of server.list) { const key = ServerConnection.key(conn) - const hostname = serverHealth.status[key]?.hostname + const hostname = selectServerHostname(serverHealth.status[key]) if (hostname) server.setHostname(key, hostname) } diff --git a/packages/app/src/servers/connect/dialog.tsx b/packages/app/src/servers/connect/dialog.tsx index b4573f410b7..b79441b44ee 100644 --- a/packages/app/src/servers/connect/dialog.tsx +++ b/packages/app/src/servers/connect/dialog.tsx @@ -27,6 +27,7 @@ import { useLanguage } from "@/runtime/i18n/language" import { normalizeServerUrl, ServerConnection, useServers } from "@/runtime/server/registry" import { useTabs } from "@/shell/tabs/tabs" import { useCheckServerHealth } from "@/runtime/server/health" +import { isServerFailed, isServerHealthy, isServerUnauthorized } from "@/runtime/server/health-machine" import { usePlatform } from "@/runtime/platform/platform" import { isMixedContent } from "./browser" import { cameraHint, createCameraAvailability } from "./camera" @@ -327,7 +328,7 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht if (!original || original.http.url === pairing.url) return pairing const http = { url: original.http.url, password: pairing.password } - return (await checkServerHealth(http)).healthy ? http : pairing + return (await checkServerHealth(http)).type === "healthy" ? http : pairing } // Where the form points: a scanned or pasted pairing link wins, else the address and password fields. @@ -380,7 +381,7 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht // asked for sign-in can continue. if ( original?.type === "http" && - !global.servers.health[ServerConnection.key(original)]?.unauthorized && + !isServerUnauthorized(global.servers.health[ServerConnection.key(original)]) && normalized === original.http.url && name === original.displayName && password === original.http.password @@ -401,7 +402,7 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht const result = await checkServerHealth(connection.http) - if (!result.healthy) { + if (result.type !== "healthy") { setStore( "error", language.t( @@ -456,11 +457,12 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht } const startEdit = (connection: ServerConnection.Http) => { + const health = global.servers.health[ServerConnection.key(connection)] reset() setStore({ mode: "edit", // A signed-out server needs a new pairing link; otherwise editing starts from its address. - method: global.servers.health[ServerConnection.key(connection)]?.unauthorized ? "link" : "password", + method: isServerUnauthorized(health) ? "link" : "password", originalUrl: connection.http.url, values: { url: connection.http.url, @@ -468,7 +470,7 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht password: connection.http.password ?? "", }, error: "", - status: global.servers.health[ServerConnection.key(connection)]?.healthy, + status: isServerFailed(health) ? false : isServerHealthy(health) || undefined, }) } @@ -508,7 +510,7 @@ function createFormController(options: { onSelect?: (server: ServerConnection.Ht signedOut: () => { const original = editing() - return !!original && !!global.servers.health[ServerConnection.key(original)]?.unauthorized + return !!original && isServerUnauthorized(global.servers.health[ServerConnection.key(original)]) }, }, change: { diff --git a/packages/app/src/servers/connect/pairing.ts b/packages/app/src/servers/connect/pairing.ts index 6db2ec00910..25bc974e60d 100644 --- a/packages/app/src/servers/connect/pairing.ts +++ b/packages/app/src/servers/connect/pairing.ts @@ -128,5 +128,5 @@ async function preferFirst( const http = { url: first, password: pairing.password } const health = await checkServerHealth(http, globalThis.fetch, { signal: AbortSignal.timeout(5_000), retryCount: 0 }) - return health.healthy ? http : pairing + return health.type === "healthy" ? http : pairing } diff --git a/packages/app/src/servers/connect/screen.tsx b/packages/app/src/servers/connect/screen.tsx index 21f54fb2ecd..247c3604f1d 100644 --- a/packages/app/src/servers/connect/screen.tsx +++ b/packages/app/src/servers/connect/screen.tsx @@ -47,13 +47,13 @@ export function ConnectServerScreen(props: { url?: string } = {}) { if (!props.url || props.url === pairing.url) return pairing const http = { url: props.url, password: pairing.password } - return (await check(http)).healthy ? http : pairing + return (await check(http)).type === "healthy" ? http : pairing } const connect = async (http: ServerConnection.HttpBase) => { const result = await check(http) - if (!result.healthy) { + if (result.type !== "healthy") { setState("error", connectionError(http.url)) return diff --git a/packages/app/src/servers/registry/controller.test.ts b/packages/app/src/servers/registry/controller.test.ts index a1ec7f039e5..9a65156cd0e 100644 --- a/packages/app/src/servers/registry/controller.test.ts +++ b/packages/app/src/servers/registry/controller.test.ts @@ -9,15 +9,18 @@ describe("sortServerConnections", () => { const first = server("http://first") const offline = server("http://offline") const unknown = server("http://unknown") + // A tunnel that just moved is still being checked, not failed. + const checking = server("http://checking") const result = sortServerConnections({ - servers: [first, offline, unknown], + servers: [first, offline, unknown, checking], health: { - [ServerConnection.key(first)]: { healthy: true }, - [ServerConnection.key(offline)]: { healthy: false }, + [ServerConnection.key(first)]: { type: "healthy" }, + [ServerConnection.key(offline)]: { type: "unreachable" }, + [ServerConnection.key(checking)]: { type: "checking", managed: true }, }, }) - expect(result).toEqual([first, unknown, offline]) + expect(result).toEqual([first, unknown, checking, offline]) }) }) diff --git a/packages/app/src/servers/registry/controller.ts b/packages/app/src/servers/registry/controller.ts index e29e0ab0f61..5cabcfa6b5c 100644 --- a/packages/app/src/servers/registry/controller.ts +++ b/packages/app/src/servers/registry/controller.ts @@ -3,7 +3,7 @@ import { useGlobal } from "@/runtime/server/runtime" import { useLanguage } from "@/runtime/i18n/language" import { ServerConnection, useServers } from "@/runtime/server/registry" import { useTabs } from "@/shell/tabs/tabs" -import { type ServerHealth } from "@/runtime/server/health" +import { selectServerHealthRank, type ServerHealth } from "@/runtime/server/health-machine" import { showToast } from "@/shell/notifications/toast" import { usePluginServers } from "@/runtime/plugin/servers" @@ -12,17 +12,10 @@ export function sortServerConnections(input: { health: Record }) { const order = new Map(input.servers.map((item, index) => [item, index] as const)) - - const rank = (value?: ServerHealth) => { - if (value?.healthy === true) return 0 - - if (value?.healthy === false) return 2 - - return 1 - } + const rank = (conn: ServerConnection.Any) => selectServerHealthRank(input.health[ServerConnection.key(conn)]) return input.servers.slice().sort((a, b) => { - const health = rank(input.health[ServerConnection.key(a)]) - rank(input.health[ServerConnection.key(b)]) + const health = rank(a) - rank(b) if (health !== 0) return health diff --git a/packages/app/src/servers/registry/management.test.ts b/packages/app/src/servers/registry/management.test.ts index a165fb8b08d..98070aec301 100644 --- a/packages/app/src/servers/registry/management.test.ts +++ b/packages/app/src/servers/registry/management.test.ts @@ -1,4 +1,5 @@ import { describe, expect, test } from "bun:test" +import type { ServerProbeResult } from "@/runtime/server/health-machine" import { ServerConnection } from "@/runtime/server/registry" import { createServerHealthPreview, replaceServerConnection, type ServerFormValues } from "./management" @@ -21,51 +22,55 @@ describe("createServerHealthPreview", () => { const preview = createServerHealthPreview(async (http) => { requests.push(http) - return { healthy: true } + return { type: "healthy" } }) await preview.preview({ ...values("server.example.com"), password }, () => {}) - expect(requests).toEqual([{ url: "http://server.example.com", ...(password ? { password } : {}) }]) + + const expected: ServerConnection.HttpBase = { url: "http://server.example.com" } + + if (password) expected.password = password + expect(requests).toEqual([expected]) }) test("ignores an older response that resolves after the latest response", async () => { - const first = deferred<{ healthy: boolean }>() - const second = deferred<{ healthy: boolean }>() + const first = deferred() + const second = deferred() const requests = [first, second] const status: Array = [] const preview = createServerHealthPreview(() => requests.shift()!.promise) const older = preview.preview(values("old.example.com"), (value) => status.push(value)) const latest = preview.preview(values("new.example.com"), (value) => status.push(value)) - second.resolve({ healthy: true }) + second.resolve({ type: "healthy" }) await latest - first.resolve({ healthy: false }) + first.resolve({ type: "unreachable" }) await older expect(status).toEqual([undefined, undefined, true]) }) test("an incomplete value invalidates an in-flight response", async () => { - const request = deferred<{ healthy: boolean }>() + const request = deferred() const status: Array = [] const preview = createServerHealthPreview(() => request.promise) const pending = preview.preview(values("server.example.com"), (value) => status.push(value)) await preview.preview(values("server"), (value) => status.push(value)) - request.resolve({ healthy: true }) + request.resolve({ type: "healthy" }) await pending expect(status).toEqual([undefined, undefined]) }) test("cancellation prevents an in-flight response from updating status", async () => { - const request = deferred<{ healthy: boolean }>() + const request = deferred() const status: Array = [] const preview = createServerHealthPreview(() => request.promise) const pending = preview.preview(values("server.example.com"), (value) => status.push(value)) preview.cancel() - request.resolve({ healthy: true }) + request.resolve({ type: "healthy" }) await pending expect(status).toEqual([undefined]) diff --git a/packages/app/src/servers/registry/management.ts b/packages/app/src/servers/registry/management.ts index 1f52f612494..33ab6365f4a 100644 --- a/packages/app/src/servers/registry/management.ts +++ b/packages/app/src/servers/registry/management.ts @@ -1,5 +1,5 @@ import { normalizeServerUrl, ServerConnection } from "@/runtime/server/registry" -import type { ServerHealth } from "@/runtime/server/health" +import type { ServerProbeResult } from "@/runtime/server/health-machine" export type ServerFormValues = { url: string @@ -7,9 +7,7 @@ export type ServerFormValues = { password: string } -export function createServerHealthPreview( - check: (server: ServerConnection.HttpBase) => Promise>, -) { +export function createServerHealthPreview(check: (server: ServerConnection.HttpBase) => Promise) { let generation = 0 const cancel = () => { @@ -35,7 +33,7 @@ export function createServerHealthPreview( const result = await check(http) if (current !== generation) return - setStatus(result.healthy) + setStatus(result.type === "healthy") } return { cancel, preview } diff --git a/packages/app/src/servers/registry/plugin-row.tsx b/packages/app/src/servers/registry/plugin-row.tsx index 4f19cce97a4..0d6f408bf05 100644 --- a/packages/app/src/servers/registry/plugin-row.tsx +++ b/packages/app/src/servers/registry/plugin-row.tsx @@ -2,11 +2,16 @@ import { untrack } from "solid-js" import type { ServerRow } from "@opencode/plugin/gui" import { Contribution } from "@/runtime/plugin/render" import { usePluginServers } from "@/runtime/plugin/servers" +import type { ServerHealth } from "@/runtime/server/health-machine" import type { ServerConnection } from "@/runtime/server/registry" import type { ServerCollectionController } from "./controller" import { ServerHealthIndicator } from "./row" import { ServerRowItems } from "./row-items" +type PluginHealth = ReturnType + +type PluginIndicator = Parameters[0] + /** A contributed server's connection row; the plugin renders it with the host's shared parts. */ export function PluginServerRow(props: { server: ServerConnection.Key; controller: ServerCollectionController }) { const servers = usePluginServers() @@ -15,14 +20,8 @@ export function PluginServerRow(props: { server: ServerConnection.Key; controlle const row: ServerRow = { key: props.server, - health: () => props.controller.collection.health()[props.server], - Indicator: (indicator) => ( - - ), + health: () => toPluginHealth(props.controller.collection.health()[props.server]), + Indicator: (indicator) => , remove: () => props.controller.connection.remove(props.server), Items: () => , } @@ -31,3 +30,28 @@ export function PluginServerRow(props: { server: ServerConnection.Key; controlle return {() => untrack(() => source.entry.row?.(row))} } + +// The SDK's health has no stages: a server that is not ready, or not checked yet, reports none. +function toPluginHealth(health: ServerHealth | undefined): PluginHealth { + if (health?.type === "healthy") return { healthy: true, version: health.version } + + if (health?.type === "checking") return health.managed ? { healthy: false, checking: true } : undefined + + if (health?.type === "unreachable" || health?.type === "unauthorized") return { healthy: false } + + if (health?.type === "incompatible") return { healthy: false, incompatible: true } + + return undefined +} + +function fromPluginIndicator(indicator: PluginIndicator): ServerHealth | undefined { + if (indicator.auth) return { type: "auth" } + + if (indicator.connecting || indicator.health?.checking) return { type: "starting" } + + if (indicator.health?.incompatible) return { type: "incompatible" } + + if (indicator.health === undefined) return undefined + + return indicator.health.healthy ? { type: "healthy", version: indicator.health.version } : { type: "unreachable" } +} diff --git a/packages/app/src/servers/registry/row.stories.tsx b/packages/app/src/servers/registry/row.stories.tsx index 71b5388957c..ae5e419ac1d 100644 --- a/packages/app/src/servers/registry/row.stories.tsx +++ b/packages/app/src/servers/registry/row.stories.tsx @@ -1,23 +1,17 @@ import { For } from "solid-js" import { ServerHealthIndicator } from "./row" -import type { ServerHealth } from "@/runtime/server/health" +import type { ServerHealth } from "@/runtime/server/health-machine" -const states: { label: string; connecting?: boolean; authenticationRequired?: boolean; health?: ServerHealth }[] = [ - { - label: "Authentication required (overrides failed health)", - authenticationRequired: true, - health: { healthy: false }, - }, - { - label: "Authentication required (overrides pending health)", - authenticationRequired: true, - health: { healthy: false, checking: true }, - }, - { label: "Connecting (previous health check failed)", connecting: true, health: { healthy: false } }, - { label: "Tunnel ready, checking its new endpoint", health: { healthy: false, checking: true } }, - { label: "Connected", health: { healthy: true } }, - { label: "Failed", health: { healthy: false } }, - { label: "Incompatible", health: { healthy: false, incompatible: true } }, +const states: { label: string; health?: ServerHealth }[] = [ + { label: "Waiting for sign-in", health: { type: "auth" } }, + { label: "Rejected our credentials", health: { type: "unauthorized" } }, + { label: "Connecting", health: { type: "starting" } }, + { label: "Tunnel ready, checking its new endpoint", health: { type: "checking", managed: true } }, + { label: "Checking", health: { type: "checking", managed: false } }, + { label: "Connected", health: { type: "healthy" } }, + { label: "Failed", health: { type: "unreachable" } }, + { label: "Incompatible", health: { type: "incompatible" } }, + { label: "Stopped", health: { type: "stopped" } }, { label: "Not checked" }, ] @@ -30,11 +24,7 @@ export const States = { {(state) => (
- +
{state.label}
diff --git a/packages/app/src/servers/registry/row.tsx b/packages/app/src/servers/registry/row.tsx index 9f490467fce..e4b0e391839 100644 --- a/packages/app/src/servers/registry/row.tsx +++ b/packages/app/src/servers/registry/row.tsx @@ -1,57 +1,47 @@ import { Icon } from "@opencode/ui/icon" import { Loader } from "@opencode/ui/loader" import { useLanguage } from "@/runtime/i18n/language" -import { Show } from "solid-js" -import type { ServerHealth } from "@/runtime/server/health" +import { Match, Switch } from "solid-js" +import { selectServerIndicator, type ServerHealth } from "@/runtime/server/health-machine" -export function ServerHealthIndicator(props: { - health?: ServerHealth - connecting?: boolean - authenticationRequired?: boolean -}) { +export function ServerHealthIndicator(props: { health?: ServerHealth }) { const language = useLanguage() + const indicator = () => selectServerIndicator(props.health) return ( - - } - > - - - } - > - - - -
+
} > - - - - + + + + + + + + + + + + + + ) } diff --git a/packages/app/src/session/resolution-machine.test.ts b/packages/app/src/session/resolution-machine.test.ts index fc6f9598bad..928eafe8b43 100644 --- a/packages/app/src/session/resolution-machine.test.ts +++ b/packages/app/src/session/resolution-machine.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test" -import type { ServerHealth } from "@/runtime/server/health" +import type { ServerHealth } from "@/runtime/server/health-machine" import { createSessionResolutionState, sessionRouteView, @@ -154,31 +154,31 @@ const missing: State = { status: "not-found", target: target("ses_a"), attempt: test.each<{ name: string; health: ServerHealth; state: State; view: SessionRouteView["type"] }>([ { name: "incompatible wins over a resolved session", - health: { healthy: true, incompatible: true }, + health: { type: "incompatible" }, state: resolved, view: "incompatible", }, { name: "an unreachable server explains a pending session", - health: { healthy: false }, + health: { type: "unreachable" }, state: resolving, view: "unreachable", }, { name: "a running health check keeps waiting", - health: { healthy: false, checking: true }, + health: { type: "checking", managed: true }, state: resolving, view: "resolving", }, { name: "a rejected credential keeps waiting", - health: { healthy: false, unauthorized: true }, + health: { type: "unauthorized" }, state: resolving, view: "resolving", }, { name: "an unreachable server keeps a known outcome", - health: { healthy: false }, + health: { type: "unreachable" }, state: missing, view: "not-found", }, diff --git a/packages/app/src/session/resolution-machine.ts b/packages/app/src/session/resolution-machine.ts index 01e12283a56..9d00306ae24 100644 --- a/packages/app/src/session/resolution-machine.ts +++ b/packages/app/src/session/resolution-machine.ts @@ -1,5 +1,5 @@ import { isSessionNotFoundError } from "@/runtime/server/errors" -import type { ServerHealth } from "@/runtime/server/health" +import type { ServerHealth } from "@/runtime/server/health-machine" // One routed session: its ID within the session store that resolves it. A replaced store is a new target. export type SessionTarget = { @@ -109,7 +109,7 @@ export function sessionRouteView( if (state.status === "idle") return { type: "idle" } const sessionID = state.target.sessionID - if (health?.incompatible) return { type: "incompatible", sessionID } + if (health?.type === "incompatible") return { type: "incompatible", sessionID } if (state.status === "ready") return { type: "ready", sessionID, session: state.session } @@ -119,7 +119,7 @@ export function sessionRouteView( // Nothing is cached and an unreachable server never resolves the session, so explain instead of waiting blank. // Only a failed health check counts: a version mismatch has its own panel, and a rejected credential is not an outage. - if (health && !health.healthy && !health.checking && !health.unauthorized) return { type: "unreachable", sessionID } + if (health?.type === "unreachable") return { type: "unreachable", sessionID } return { type: "resolving", sessionID } } diff --git a/packages/app/src/session/route-error.tsx b/packages/app/src/session/route-error.tsx index 9909b48b39b..89c05728f95 100644 --- a/packages/app/src/session/route-error.tsx +++ b/packages/app/src/session/route-error.tsx @@ -4,6 +4,7 @@ import { ErrorPage } from "@/shell/errors/error" import { useLanguage } from "@/runtime/i18n/language" import { useServer } from "@/runtime/server/current" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerIncompatible } from "@/runtime/server/health-machine" import { useTabs } from "@/shell/tabs/tabs" import { isSessionNotFoundError } from "@/runtime/server/errors" import { ServerUnavailablePanel } from "./server-unavailable-panel" @@ -19,7 +20,7 @@ export function SessionErrorFallback(props: { error: unknown; sessionID?: string return ( } > void }) { @@ -25,7 +26,7 @@ export function ServerUnavailablePanel(props: { reason: "incompatible" | "unreac {props.reason === "incompatible" ? language.t("session.error.incompatible.description", { server: servers.label(server.conn), - version: server.health?.version ?? "1", + version: selectServerVersion(server.health) ?? "1", }) : language.t("session.error.unreachable.description", { server: servers.label(server.conn) })}
diff --git a/packages/app/src/settings/servers/inventory.test.ts b/packages/app/src/settings/servers/inventory.test.ts index a03194c1843..0b7d01bf0ad 100644 --- a/packages/app/src/settings/servers/inventory.test.ts +++ b/packages/app/src/settings/servers/inventory.test.ts @@ -13,9 +13,8 @@ const connection: ServerConnection.Plugin = { type: "plugin", key: "ssh:build", plugin: "ssh", - state: "stopped", - connecting: false, - authenticationRequired: false, + status: { type: "stopped" }, + authenticate: () => false, managed: true, displayName: "Build server", http: { url: "http://127.0.0.1:4000", password: "secret" }, diff --git a/packages/app/src/settings/servers/inventory.ts b/packages/app/src/settings/servers/inventory.ts index 7b518a03bdc..bf7c10f63b5 100644 --- a/packages/app/src/settings/servers/inventory.ts +++ b/packages/app/src/settings/servers/inventory.ts @@ -1,7 +1,7 @@ import { createMemo } from "solid-js" import { ServerConnection, useServers } from "@/runtime/server/registry" import { usePluginServers, type PluginServer } from "@/runtime/plugin/servers" -import type { ServerHealth } from "@/runtime/server/health" +import { isServerFailed, type ServerHealth } from "@/runtime/server/health-machine" export type SettingsServer = { key: ServerConnection.Key @@ -56,5 +56,5 @@ export function useSettingsServers() { /** Whether a server's pages can load: it runs and its last health check did not fail. */ export function settingsServerReachable(server: SettingsServer, health: ServerHealth | undefined) { - return !!server.connection && health?.healthy !== false + return !!server.connection && !isServerFailed(health) } diff --git a/packages/app/src/settings/servers/servers.tsx b/packages/app/src/settings/servers/servers.tsx index 3ef7fa37039..c22c0c6f853 100644 --- a/packages/app/src/settings/servers/servers.tsx +++ b/packages/app/src/settings/servers/servers.tsx @@ -7,6 +7,7 @@ import { PluginServerRow } from "@/servers/registry/plugin-row" import { AddServerMenu } from "@/servers/registry/add-menu" import { useLanguage } from "@/runtime/i18n/language" import { ServerConnection } from "@/runtime/server/registry" +import { isServerFailed, isServerUnauthorized } from "@/runtime/server/health-machine" import { useServerCollectionController } from "@/servers/registry/controller" import { DialogServer } from "@/servers/connect/dialog" import { SettingsList } from "@/settings/list" @@ -33,7 +34,7 @@ export const SettingsServerGeneral: Component<{ entry: SettingsServer; onAddServ - + {(server) => (

{language.t("settings.tab.preferences")}

@@ -75,7 +76,7 @@ export const SettingsServerList: Component<{ - server.type === "http" && health()?.unauthorized ? server : undefined + server.type === "http" && isServerUnauthorized(health()) ? server : undefined return ( (
- +
{props.entry.name} - {server().http.url} + {ServerConnection.http(server())?.url}
diff --git a/packages/app/src/settings/servers/switcher.tsx b/packages/app/src/settings/servers/switcher.tsx index 336d691882c..34ce29b9b79 100644 --- a/packages/app/src/settings/servers/switcher.tsx +++ b/packages/app/src/settings/servers/switcher.tsx @@ -4,7 +4,7 @@ import { Menu } from "@opencode/ui/menu" import { For, Show } from "solid-js" import { useLanguage } from "@/runtime/i18n/language" import { useGlobal } from "@/runtime/server/runtime" -import type { ServerHealth } from "@/runtime/server/health" +import type { ServerHealth } from "@/runtime/server/health-machine" import { ServerHealthIndicator } from "@/servers/registry/row" import { ServerLabel } from "@/servers/registry/label" import { useSettingsSurface } from "@/settings/surface" @@ -72,11 +72,11 @@ export function SettingsServerSwitcher() { } function ServerDot(props: { health: ServerHealth | undefined; unreachable: boolean }) { - const health = () => props.health ?? (props.unreachable ? { healthy: false } : undefined) + const health = (): ServerHealth | undefined => props.health ?? (props.unreachable ? { type: "unreachable" } : undefined) return ( - + ) } diff --git a/packages/app/src/shell/commands/session-search.tsx b/packages/app/src/shell/commands/session-search.tsx index 2b503fc9eb3..11f4ee15281 100644 --- a/packages/app/src/shell/commands/session-search.tsx +++ b/packages/app/src/shell/commands/session-search.tsx @@ -3,6 +3,7 @@ import { startTransition } from "solid-js" import { useGlobal } from "@/runtime/server/runtime" import { useLanguage } from "@/runtime/i18n/language" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerFailed } from "@/runtime/server/health-machine" import { useTabs } from "@/shell/tabs/tabs" import { createKnownServerSessions, createServerSessionEntries, type CommandPaletteEntry } from "./palette" import { CommandPaletteView } from "./dialog" @@ -18,9 +19,7 @@ export function DialogSessionSearch(props: { archived?: boolean }) { const tabs = useTabs() const registry = useServers() - const servers = registry.visible.filter( - (conn) => global.servers.health[ServerConnection.key(conn)]?.healthy !== false, - ) + const servers = registry.visible.filter((conn) => !isServerFailed(global.servers.health[ServerConnection.key(conn)])) const loaders = servers.flatMap((conn) => { const ctx = global.serverCtx(ServerConnection.key(conn)) diff --git a/packages/app/src/shell/routes/routes.tsx b/packages/app/src/shell/routes/routes.tsx index ea097305e59..7303f5d26e5 100644 --- a/packages/app/src/shell/routes/routes.tsx +++ b/packages/app/src/shell/routes/routes.tsx @@ -4,6 +4,7 @@ import { Home } from "@/home/route" import { ServerProvider } from "@/runtime/server/current" import { useGlobal } from "@/runtime/server/runtime" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { isServerUnauthorized } from "@/runtime/server/health-machine" import { SessionPanelFrame, SessionRouteFrame } from "@/session/session-frame" import { ServerSignedOutPanel } from "@/session/server-unavailable-panel" import { LayoutProvider } from "@/shell/state/layout" @@ -115,7 +116,7 @@ function AppLayout(props: ParentProps) { if (only?.type !== "http") return - return global.servers.health[ServerConnection.key(only)]?.unauthorized ? only : undefined + return isServerUnauthorized(global.servers.health[ServerConnection.key(only)]) ? only : undefined } return ( diff --git a/packages/app/src/shell/titlebar/inbox-model.ts b/packages/app/src/shell/titlebar/inbox-model.ts index 257c360fdd0..151a836644d 100644 --- a/packages/app/src/shell/titlebar/inbox-model.ts +++ b/packages/app/src/shell/titlebar/inbox-model.ts @@ -4,6 +4,7 @@ import { createStore } from "solid-js/store" import { createEffect, createMemo, onCleanup, startTransition } from "solid-js" import { useGlobal, type ServerCtx } from "@/runtime/server/runtime" import { ServerConnection, useServers } from "@/runtime/server/registry" +import { canUseServer } from "@/runtime/server/health-machine" import { useLanguage } from "@/runtime/i18n/language" import { projectSettingsKey, useSettings } from "@/settings/model" import { useLayout } from "@/shell/state/layout" @@ -239,9 +240,7 @@ export function createSessionInbox(options: { archived?: boolean } = {}) { previous || (servers.hydrated() && connections().every((item, index) => { - const health = global.servers.health[ServerConnection.key(item.conn)] - - if (health && !health.checking && !health.healthy) return true + if (!canUseServer(global.servers.health[ServerConnection.key(item.conn)])) return true return !!queries[index]?.isSuccess || !!queries[index]?.isError }) && diff --git a/packages/app/src/workspaces/selection/picker.test.ts b/packages/app/src/workspaces/selection/picker.test.ts index 547d3d92924..e46b5c91cf0 100644 --- a/packages/app/src/workspaces/selection/picker.test.ts +++ b/packages/app/src/workspaces/selection/picker.test.ts @@ -11,9 +11,8 @@ const remote = { type: "plugin", key: "ssh:example", plugin: "ssh", - state: "ready", - connecting: false, - authenticationRequired: false, + status: { type: "ready", http: { url: "http://localhost:4096" } }, + authenticate: () => false, managed: true, http: { url: "http://localhost:4096" }, } as const diff --git a/packages/app/test-browser/plugin-server-stream.test.ts b/packages/app/test-browser/plugin-server-stream.test.ts index 5cfb5dae8af..1526646412b 100644 --- a/packages/app/test-browser/plugin-server-stream.test.ts +++ b/packages/app/test-browser/plugin-server-stream.test.ts @@ -47,9 +47,10 @@ test("a plugin server's event stream follows the endpoint the plugin reports", a type: "plugin", key: "remote:box", plugin: "remote", - state: "ready", - connecting: false, - authenticationRequired: false, + get status() { + return { type: "ready" as const, http: http() } + }, + authenticate: () => false, managed: false, get http() { return http() diff --git a/packages/app/test-browser/ssh-health.test.ts b/packages/app/test-browser/ssh-health.test.ts index 6684c4cf509..b4c66df745c 100644 --- a/packages/app/test-browser/ssh-health.test.ts +++ b/packages/app/test-browser/ssh-health.test.ts @@ -1,148 +1,143 @@ import { expect, test } from "bun:test" -import { createRoot } from "solid-js" -import { createStore } from "solid-js/store" -import { createServerHealth, type ServerHealth } from "../src/runtime/server/health" +import { createRoot, createSignal } from "solid-js" +import { createServerHealth } from "../src/runtime/server/health" +import type { ServerProbeResult } from "../src/runtime/server/health-machine" +import type { PluginServerStatus } from "../src/runtime/server/plugin-machine" import { ServerConnection } from "../src/runtime/server/registry" -test("SSH health is checked only with an active tunnel, and cancellation clears stale failures", async () => { - const requests: ReturnType>[] = [] +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)) + +function plugin(status: () => PluginServerStatus): ServerConnection.Plugin { + return { + type: "plugin", + key: "ssh:fixture", + plugin: "ssh", + managed: true, + http: { url: "http://127.0.0.1:0" }, + authenticate: () => false, + get status() { + return status() + }, + } +} + +function probes() { + const requests: ReturnType>[] = [] + + return { + requests, + check: () => { + const request = Promise.withResolvers() + requests.push(request) + + return request.promise + }, + } +} + +test("SSH health follows the plugin's stage and checks only an active tunnel", async () => { + const fake = probes() + const tunnel = { type: "ready", http: { url: "http://127.0.0.1:12345" } } as const const app = createRoot((dispose) => { - const [state, setState] = createStore<{ state: ServerConnection.Plugin["state"] }>({ state: "stopped" }) + const [status, setStatus] = createSignal({ type: "stopped" }) + const connection = plugin(status) + const health = createServerHealth(() => [connection], fake.check) - const connection: ServerConnection.Plugin = { - type: "plugin", - key: "ssh:fixture", - plugin: "ssh", - connecting: false, - authenticationRequired: false, - managed: true, - http: { url: "http://127.0.0.1:12345" }, - get state() { - return state.state - }, - } - - const health = createServerHealth( - () => [connection], - () => true, - () => { - const request = Promise.withResolvers() - requests.push(request) - - return request.promise - }, - ) - - return { dispose, setState, health: () => health.status[ServerConnection.key(connection)] } + return { dispose, setStatus, health: () => health.status[ServerConnection.key(connection)] } }) try { - expect(app.health()).toBeUndefined() - app.setState("state", "starting") - app.setState("state", "auth") - app.setState("state", "stopped") - expect(requests).toHaveLength(0) - expect(app.health()).toBeUndefined() - app.setState("state", "ready") - expect(requests).toHaveLength(1) - expect(app.health()?.checking).toBe(true) - app.setState("state", "auth") - requests[0]?.resolve({ healthy: false }) - await Promise.resolve() - expect(app.health()).toBeUndefined() - app.setState("state", "failed") - expect(app.health()?.healthy).toBe(false) - app.setState("state", "auth") - expect(app.health()).toBeUndefined() - app.setState("state", "ready") - requests[1]?.resolve({ healthy: false }) - await Promise.resolve() - expect(app.health()).toEqual({ healthy: false }) + expect(app.health()).toEqual({ type: "stopped" }) + app.setStatus({ type: "starting" }) + expect(app.health()).toEqual({ type: "starting" }) + app.setStatus({ type: "auth" }) + expect(app.health()).toEqual({ type: "auth" }) + expect(fake.requests).toHaveLength(0) + + app.setStatus(tunnel) + expect(fake.requests).toHaveLength(1) + expect(app.health()).toEqual({ type: "checking", managed: true }) + + // The tunnel asks for sign-in again before its probe answers: the late answer is stale. + app.setStatus({ type: "auth" }) + fake.requests[0]?.resolve({ type: "unreachable" }) + await flush() + expect(app.health()).toEqual({ type: "auth" }) + + app.setStatus({ type: "failed" }) + expect(app.health()).toEqual({ type: "unreachable" }) + + app.setStatus(tunnel) + fake.requests[1]?.resolve({ type: "healthy", version: "2.0.0" }) + await flush() + expect(app.health()).toEqual({ type: "healthy", version: "2.0.0" }) } finally { app.dispose() } }) test("a late failure from the old endpoint cannot overwrite the new endpoint check", async () => { - const requests: ReturnType>[] = [] + const fake = probes() const app = createRoot((dispose) => { - const [state, setState] = createStore({ url: "http://127.0.0.1:0" }) + const [url, setUrl] = createSignal("http://127.0.0.1:12344") + const connection = plugin(() => ({ type: "ready", http: { url: url() } })) + const health = createServerHealth(() => [connection], fake.check) - const connection: ServerConnection.Plugin = { - type: "plugin", - key: "ssh:fixture", - plugin: "ssh", - state: "ready", - connecting: false, - authenticationRequired: false, - managed: true, - get http() { - return { url: state.url } - }, - } - - const health = createServerHealth( - () => [connection], - () => true, - () => { - const request = Promise.withResolvers() - requests.push(request) - - return request.promise - }, - ) - - return { dispose, setState, health: () => health.status[ServerConnection.key(connection)] } + return { dispose, setUrl, health: () => health.status[ServerConnection.key(connection)] } }) try { - app.setState({ url: "http://127.0.0.1:12345" }) - requests[0]?.resolve({ healthy: false }) - await Promise.resolve() - expect(app.health()).toEqual({ healthy: false, checking: true }) - requests[1]?.resolve({ healthy: true }) - await Promise.resolve() - expect(app.health()).toEqual({ healthy: true }) + app.setUrl("http://127.0.0.1:12345") + fake.requests[0]?.resolve({ type: "unreachable" }) + await flush() + expect(app.health()).toEqual({ type: "checking", managed: true }) + fake.requests[1]?.resolve({ type: "healthy" }) + await flush() + expect(app.health()).toEqual({ type: "healthy" }) } finally { app.dispose() } }) -test("retry probes again before the next poll and resolves once that round answers", async () => { - const requests: ReturnType>[] = [] +test("retry probes again before the next poll and resolves once that probe answers", async () => { + const fake = probes() const connection: ServerConnection.Http = { type: "http", http: { url: "http://127.0.0.1:12346" } } const app = createRoot((dispose) => { - const health = createServerHealth( - () => [connection], - () => true, - () => { - const request = Promise.withResolvers() - requests.push(request) - - return request.promise - }, - ) + const health = createServerHealth(() => [connection], fake.check) return { dispose, retry: health.retry, health: () => health.status[ServerConnection.key(connection)] } }) try { - requests[0]?.resolve({ healthy: false }) - await Promise.resolve() - expect(app.health()?.healthy).toBe(false) + fake.requests[0]?.resolve({ type: "unreachable" }) + await flush() + expect(app.health()).toEqual({ type: "unreachable" }) const state = { done: false } const retried = app.retry().then(() => (state.done = true)) - expect(requests).toHaveLength(2) - await Promise.resolve() + expect(fake.requests).toHaveLength(2) + await flush() expect(state.done).toBe(false) - requests[1]?.resolve({ healthy: true }) + fake.requests[1]?.resolve({ type: "healthy" }) await retried - expect(app.health()?.healthy).toBe(true) + expect(app.health()).toEqual({ type: "healthy" }) } finally { app.dispose() } }) + +test("disposal releases a retry whose probe never answers", async () => { + const fake = probes() + const connection: ServerConnection.Http = { type: "http", http: { url: "http://127.0.0.1:12347" } } + + const app = createRoot((dispose) => ({ dispose, health: createServerHealth(() => [connection], fake.check) })) + + const retried = app.health.retry() + app.dispose() + + await retried + expect(fake.requests).toHaveLength(2) +}) diff --git a/packages/gui-plugins/src/ssh/dialog.stories.tsx b/packages/gui-plugins/src/ssh/dialog.stories.tsx index 27a48d83db4..abff7f3f3e9 100644 --- a/packages/gui-plugins/src/ssh/dialog.stories.tsx +++ b/packages/gui-plugins/src/ssh/dialog.stories.tsx @@ -245,7 +245,7 @@ function AuthenticationHome() { recentlyClosed={[]} selection={{ server: ServerConnection.key(server) }} homedir="/home/user" - serverHealth={() => ({ healthy: ssh.servers[0]?.stage === "ready" })} + serverHealth={() => (ssh.servers[0]?.stage === "ready" ? { type: "healthy" } : { type: "unreachable" })} serverLabel={(server) => server.displayName ?? server.http.url} projectsForServer={() => projects} collapsed={() => false}