Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(desktop): show a specific message for failed SSH host installs
afsuyadi0029d0148f19
diff --git a/apps/desktop/src/lib/remote-host-errors.ts b/apps/desktop/src/lib/remote-host-errors.tsindex af593c212..7616e3aa8 100644--- a/apps/desktop/src/lib/remote-host-errors.ts+++ b/apps/desktop/src/lib/remote-host-errors.ts@@ -8,6 +8,7 @@ export function remoteHostErrorKey(error: unknown): string {case "REMOTE_PATH_NOT_FOUND": return "remote:pathUnavailable";case "REMOTE_PATH_FORBIDDEN": return "remote:pathForbidden";case "HOST_VERSION_MISMATCH": return "remote:upgradeRequired";+ case "HOST_BOOTSTRAP_FAILED": return "remote:hostBootstrapFailed";case "HOST_AUTH_FAILED":case "SSH_AUTH_FAILED":case "TOKEN_INVALID":diff --git a/apps/desktop/src/locales/remote.ts b/apps/desktop/src/locales/remote.tsindex 1b619d6b3..1ac341dbd 100644--- a/apps/desktop/src/locales/remote.ts+++ b/apps/desktop/src/locales/remote.ts@@ -17,6 +17,7 @@ export const remoteEn = {upgradeRequired: "Upgrade the remote Host to a matching Desktop release.",authenticationRequired: "Authentication failed. Check SSH access or pair the Host again.",connectionUnavailable: "The remote Host is unavailable. Check the connection and try again.",+ hostBootstrapFailed: "Installing or starting the remote Host over SSH failed. Check connectivity, credentials, and that the remote machine meets the Host's requirements, then try again.",operationFailed: "The remote operation failed. Check the Host connection and try again.",mutationUncertain: "The connection was interrupted; the remote change may have completed. Check the files before retrying.",empty: "No remote conversations yet.",@@ -72,6 +73,7 @@ export const remoteZh: typeof remoteEn = {upgradeRequired: "请将远程 Host 升级到与 Desktop 匹配的版本。",authenticationRequired: "认证失败,请检查 SSH 登录或重新配对主机。",connectionUnavailable: "远程主机不可用,请检查连接后重试。",+ hostBootstrapFailed: "通过 SSH 安装或启动远程 Host 失败,请检查网络连通性、登录凭据以及远程机器是否满足 Host 的运行要求后重试。",operationFailed: "远程操作失败,请检查主机连接后重试。",mutationUncertain: "连接已中断,远程修改可能已完成。请先检查文件,不要直接重试。",empty: "暂无远程会话。", project: "远程项目", selectProject: "选择远程项目",diff --git a/apps/desktop/test/remote-host-errors.test.mjs b/apps/desktop/test/remote-host-errors.test.mjsnew file mode 100644index 000000000..729cb18b9--- /dev/null+++ b/apps/desktop/test/remote-host-errors.test.mjs@@ -0,0 +1,29 @@+import assert from "node:assert/strict";+import test from "node:test";++import { remoteHostErrorKey } from "../src/lib/remote-host-errors.ts";+import { remoteEn, remoteZh } from "../src/locales/remote.ts";++test("remoteHostErrorKey maps a failed SSH bootstrap to a dedicated message", () => {+ const error = Object.assign(new Error("ssh command failed with exit code 255: Permission denied, please try again."), {+ errorCode: "HOST_BOOTSTRAP_FAILED",+ });+ assert.equal(remoteHostErrorKey(error), "remote:hostBootstrapFailed");+});++test("remoteHostErrorKey reads the IPC-wire `code` field as a fallback", () => {+ const error = Object.assign(new Error("bootstrap failed"), { code: "HOST_BOOTSTRAP_FAILED" });+ assert.equal(remoteHostErrorKey(error), "remote:hostBootstrapFailed");+});++test("remoteHostErrorKey falls back to the generic message for an unmapped code", () => {+ const error = Object.assign(new Error("boom"), { errorCode: "SOME_UNKNOWN_CODE" });+ assert.equal(remoteHostErrorKey(error), "remote:operationFailed");+});++test("hostBootstrapFailed has an English and Chinese translation distinct from the generic fallback", () => {+ assert.equal(typeof remoteEn.hostBootstrapFailed, "string");+ assert.equal(typeof remoteZh.hostBootstrapFailed, "string");+ assert.notEqual(remoteEn.hostBootstrapFailed, remoteEn.operationFailed);+ assert.notEqual(remoteZh.hostBootstrapFailed, remoteZh.operationFailed);+});diff --git a/apps/desktop/test/remote-host-ssh-install-user-path.test.mjs b/apps/desktop/test/remote-host-ssh-install-user-path.test.mjsnew file mode 100644index 000000000..002d7dad3--- /dev/null+++ b/apps/desktop/test/remote-host-ssh-install-user-path.test.mjs@@ -0,0 +1,245 @@+import assert from "node:assert/strict";+import { spawn } from "node:child_process";+import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { createRequire } from "node:module";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import { fileURLToPath } from "node:url";+import test from "node:test";+import { resolveElectronBinary } from "../../../scripts/e2e/boot.mjs";++const root = join(dirname(fileURLToPath(import.meta.url)), "../../..");+const require = createRequire(join(root, "packages/agent-runtime/package.json"));+const { build } = require("esbuild");++// RemoteHostsPage pulls `showToast`/`openProject` from the real app store, which+// composes many unrelated slices and their own IPC traffic on creation. This+// fixture does not need any of that: it stands the hook in for a plain object+// so the only thing actually exercised across an IPC-shaped boundary is the+// SSH install call the test is about.+const fakeAppStore = String.raw`+const state = {+ showToast: (message, options) => {+ globalThis.__toasts = globalThis.__toasts ?? [];+ globalThis.__toasts.push({ message, options });+ },+ openProject: async () => undefined,+};+export function useAppStore(selector) {+ return selector(state);+}+`;++const redirectAppStore = {+ name: "redirect-app-store",+ setup(pluginBuild) {+ pluginBuild.onLoad({ filter: /fake-app-store\.js$/ }, () => ({+ contents: fakeAppStore,+ loader: "js",+ }));+ pluginBuild.onResolve({ filter: /^\.\.\/\.\.\/stores\/app-store$/ }, () => ({+ path: join(root, "apps/desktop/__fixture_fake-app-store.js"),+ namespace: "file",+ }));+ },+};++const fixture = String.raw`+import React from "react";+import { flushSync } from "react-dom";+import { createRoot } from "react-dom/client";+import { createInstance } from "i18next";+import { I18nextProvider } from "react-i18next";+import { IPC } from "@pi-desktop/shared";+import { loadRendererResources } from "./src/lib/renderer-catalogs";+import { api } from "./src/lib/api";+import { RemoteHostsPage } from "./src/components/settings/RemoteHostsPage";++window.piDesktop = {+ channels: IPC,+ platform: "linux",+ invoke: async () => ({ ok: true, data: undefined }),+ on: () => () => undefined,+};++// The only mocked collaborator: the SSH/IPC edge the component calls out to.+// Everything else (form state, validation, error mapping, i18n, the toast+// store stand-in above) runs as the real production code would.+api.listRemoteHosts = async () => ({ hosts: [] });+api.onSessionsChanged = () => () => undefined;+api.bootstrapRemoteHost = async () => {+ const error = new Error("ssh command failed with exit code 255: Permission denied, please try again.");+ error.errorCode = "HOST_BOOTSTRAP_FAILED";+ throw error;+};++const host = document.createElement("div");+document.body.append(host);++const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));+const waitFor = async (predicate, frames = 60) => {+ for (let frame = 0; frame < frames; frame += 1) {+ const value = predicate();+ if (value) return value;+ await nextFrame();+ }+ throw new Error("condition did not become true in time");+};++function setNativeValue(element, value) {+ const descriptor = Object.getOwnPropertyDescriptor(+ Object.getPrototypeOf(element),+ "value",+ );+ descriptor.set.call(element, value);+ element.dispatchEvent(new Event("input", { bubbles: true }));+}++globalThis.sshInstallFailureProbe = async () => {+ const resources = await loadRendererResources("en");+ const i18n = createInstance();+ await i18n.init({ lng: "en", resources, interpolation: { escapeValue: false } });++ const root = createRoot(host);+ flushSync(() => {+ root.render(+ React.createElement(+ I18nextProvider,+ { i18n },+ React.createElement(RemoteHostsPage, null),+ ),+ );+ });++ // Initial inventory load (mocked to an empty list) before "Add via SSH" is enabled.+ const addButton = await waitFor(() =>+ document.getElementById("remote-host-add-ssh")?.hasAttribute("disabled") === false+ ? document.getElementById("remote-host-add-ssh")+ : undefined,+ );+ addButton.click();++ const form = await waitFor(() => document.getElementById("remote-host-add-panel-ssh"));+ setNativeValue(form.querySelector('[aria-label="Label"]'), "A6000");+ setNativeValue(form.querySelector('[aria-label="Host"]'), "10.246.1.5");++ const submitButton = form.querySelector('button[type="submit"]');+ await waitFor(() => !submitButton.hasAttribute("disabled"));+ submitButton.click();++ const toast = await waitFor(() => globalThis.__toasts?.at(-1));+ return { message: toast.message };+};+`;++test(+ "a failed SSH install shows the specific bootstrap-failure message, not the generic fallback",+ {+ skip:+ process.platform === "linux" && !process.env.DISPLAY+ ? "Electron requires a display"+ : false,+ },+ async () => {+ const temp = await mkdtemp(join(tmpdir(), "pi-remote-host-ssh-install-"));+ try {+ await build({+ stdin: {+ contents: fixture,+ resolveDir: join(root, "apps/desktop"),+ loader: "tsx",+ },+ outfile: join(temp, "renderer.js"),+ bundle: true,+ platform: "browser",+ format: "iife",+ jsx: "automatic",+ define: { "process.env.NODE_ENV": '"production"' },+ loader: { ".css": "empty" },+ plugins: [redirectAppStore],+ alias: {+ react: join(root, "apps/desktop/node_modules/react"),+ "react-dom": join(root, "apps/desktop/node_modules/react-dom"),+ },+ nodePaths: [join(root, "apps/desktop/node_modules")],+ });+ await writeFile(+ join(temp, "index.html"),+ '<!doctype html><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="default-src \'self\'; style-src \'self\' \'unsafe-inline\'"><body><script src="renderer.js"></script>',+ );+ await writeFile(+ join(temp, "main.cjs"),+ `+const { app, BrowserWindow } = require("electron");+const path = require("node:path");+app.setPath("userData", path.join(__dirname, "profile"));+app.whenReady().then(async () => {+ const window = new BrowserWindow({+ show: false,+ width: 900,+ height: 700,+ webPreferences: {+ sandbox: true,+ contextIsolation: true,+ nodeIntegration: false,+ backgroundThrottling: false,+ },+ });+ try {+ await window.loadFile(path.join(__dirname, "index.html"));+ const result = await window.webContents.executeJavaScript("globalThis.sshInstallFailureProbe()");+ console.log("SSH_INSTALL_FAILURE_PROBE " + JSON.stringify({ ok: true, result }));+ app.exit(0);+ } catch (error) {+ console.error("SSH_INSTALL_FAILURE_PROBE " + JSON.stringify({ ok: false, error: String(error) }));+ app.exit(1);+ }+});+`,+ );++ const env = { ...process.env };+ delete env.ELECTRON_RUN_AS_NODE;+ const child = spawn(+ resolveElectronBinary(root).electronBinary,+ [join(temp, "main.cjs")],+ { env, stdio: ["ignore", "pipe", "pipe"] },+ );+ let output = "";+ for (const stream of [child.stdout, child.stderr]) {+ stream.setEncoding("utf8");+ stream.on("data", (chunk) => {+ output += chunk;+ });+ }+ const timeout = setTimeout(() => child.kill("SIGKILL"), 45_000);+ let code;+ try {+ code = await new Promise((resolve, reject) => {+ child.once("error", reject);+ child.once("close", resolve);+ });+ } finally {+ clearTimeout(timeout);+ }++ const line = output+ .split(/\r?\n/)+ .find((item) => item.startsWith("SSH_INSTALL_FAILURE_PROBE "));+ assert.ok(line, output.slice(-6000));+ assert.equal(code, 0, output.slice(-6000));+ const report = JSON.parse(line.slice("SSH_INSTALL_FAILURE_PROBE ".length));+ assert.equal(report.ok, true, output.slice(-6000));+ assert.equal(+ report.result.message,+ "Install failed: Installing or starting the remote Host over SSH failed. Check connectivity, credentials, and that the remote machine meets the Host's requirements, then try again.",+ );+ assert.doesNotMatch(+ report.result.message,+ /The remote operation failed\. Check the Host connection and try again\./,+ );+ } finally {+ await rm(temp, { recursive: true, force: true });+ }+ },+);