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(plugins): give npm validation a budget a loaded machine survives
vastsaec97a735b27e
diff --git a/apps/desktop/electron/main/npm-executable.ts b/apps/desktop/electron/main/npm-executable.tsindex fac80a1dd..80b051e69 100644--- a/apps/desktop/electron/main/npm-executable.ts+++ b/apps/desktop/electron/main/npm-executable.ts@@ -13,7 +13,17 @@ export type DependencyCommandRunner = (envOverrides?: Record<string, string>,) => Promise<{ code: number; stderr: string; stdout?: string }>;-const NPM_VALIDATION_TIMEOUT_MS = 5_000;+/**+ * Budget for a standalone npm/Node.js validation call, covering both probes.+ * Process startup on a loaded machine costs seconds, and a probe that runs out+ * of time is indistinguishable from a missing tool.+ */+const NPM_VALIDATION_TIMEOUT_MS = 10_000;+/**+ * Ceiling for both probes when the caller has a longer budget (dependency+ * installs and the picker). Callers with a smaller budget stay bounded by it.+ */+export const NPM_VALIDATION_BUDGET_MS = 30_000;const DEPENDENCY_STDERR_KEEP_CHARS = 8192;const VERSION_STDOUT_MAX_CHARS = 1024;const SEMVER = "(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?";@@ -137,7 +147,8 @@ export async function prepareNpmExecutable(npmPath?: string,timeoutMs = NPM_VALIDATION_TIMEOUT_MS,): Promise<NpmExecutable> {- const deadline = Date.now() + Math.min(timeoutMs, NPM_VALIDATION_TIMEOUT_MS);+ const budget = Math.max(1, Math.min(timeoutMs, NPM_VALIDATION_BUDGET_MS));+ const deadline = Date.now() + budget;let cwd: string | undefined;try {const tool: NpmExecutable = { command: npmPath ?? "npm", args: [], env: {} };@@ -168,9 +179,10 @@ export async function prepareNpmExecutable([node, ["--version"], "Node.js"],[tool.command, [...tool.args, "--version"], "npm"],] as const) {- if (Date.now() >= deadline) throw new Error("npm/Node.js validation exceeded its time budget");+ const remaining = deadline - Date.now();+ if (remaining <= 0) throw new Error("npm/Node.js validation exceeded its time budget");const result = await defaultDependencyRunner(- command, [...args], cwd, Math.max(1, deadline - Date.now()), tool.env,+ command, [...args], cwd, Math.max(1, remaining), tool.env,);if (result.code !== 0) {throw new Error(`${label} --version failed (${result.code}): ${result.stderr.trim().slice(-200)}`);@@ -192,9 +204,10 @@ export async function prepareNpmExecutable(/** Only call with a main-process-owned path selected through the native dialog. */export async function validateNpmExecutable(npmPath: string,+ timeoutMs = NPM_VALIDATION_TIMEOUT_MS,): Promise<{ ok: true } | { ok: false; error: string }> {try {- await prepareNpmExecutable(npmPath);+ await prepareNpmExecutable(npmPath, timeoutMs);return { ok: true };} catch (error) {return { ok: false, error: error instanceof Error ? error.message : String(error) };diff --git a/apps/desktop/electron/main/npm-install-recovery.ts b/apps/desktop/electron/main/npm-install-recovery.tsindex 6c033d3c9..6a78068ea 100644--- a/apps/desktop/electron/main/npm-install-recovery.ts+++ b/apps/desktop/electron/main/npm-install-recovery.ts@@ -3,7 +3,7 @@ import { dirname } from "node:path";import type { BrowserWindow, Dialog, MessageBoxOptions, OpenDialogOptions } from "electron";import { catalogs, resolveLocale } from "@pi-desktop/i18n";import { installExtensionDependencies, type ExtensionDependencyInstallResult } from "./npm-installer";-import { validateNpmExecutable } from "./npm-executable";+import { NPM_VALIDATION_BUDGET_MS, validateNpmExecutable } from "./npm-executable";export type NpmRecoveryDependencies = {dialogs: Pick<Dialog, "showMessageBox" | "showOpenDialog">;@@ -64,7 +64,9 @@ export async function installDependenciesWithNpmRecovery(const selected = picked.canceled ? undefined : picked.filePaths[0];if (!selected) return result;- const validation = await validateNpmExecutable(selected);+ // A picked executable gets the full validation ceiling: rejecting a working+ // npm because process startup was slow would loop the user through the picker.+ const validation = await validateNpmExecutable(selected, NPM_VALIDATION_BUDGET_MS);if (!validation.ok) {result = { state: "failed", reason: "npm-unavailable", error: validation.error };invalidSelection = true;diff --git a/apps/desktop/test/npm-executable.test.mjs b/apps/desktop/test/npm-executable.test.mjsindex 0d21df9c9..f2c0b5fe8 100644--- a/apps/desktop/test/npm-executable.test.mjs+++ b/apps/desktop/test/npm-executable.test.mjs@@ -278,8 +278,11 @@ test("short install budgets cap validation and include its time in both installassert.ok(Date.now() - started < 2_000, "validation does not consume its default five seconds");const f = fixture(t, "if (args[0] === '--version') Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 300);");const budgets = [];+ // Validation spawns two real processes, so these short budgets only prove the+ // containment property on an idle machine. Five seconds still leaves the+ // budget far below the installer's default.const installed = await installExtensionDependencies(f.plugin, {- npmPath: f.npm, timeoutMs: 3_000,+ npmPath: f.npm, timeoutMs: 5_000,runner: async (_command, _args, _cwd, timeoutMs) => {budgets.push(timeoutMs);return { code: 0, stderr: "" };@@ -287,9 +290,9 @@ test("short install budgets cap validation and include its time in both install});assert.deepEqual(installed, { state: "installed" });assert.equal(budgets.length, 2);- assert.ok(budgets.every((budget) => budget <= 2_700 && budget > 0));+ assert.ok(budgets.every((budget) => budget <= 4_700 && budget > 0));const stalled = fixture(t, "if (args[0] === 'install') setInterval(() => {}, 60_000);");- const timedOut = await installExtensionDependencies(stalled.plugin, { npmPath: stalled.npm, timeoutMs: 1_500 });+ const timedOut = await installExtensionDependencies(stalled.plugin, { npmPath: stalled.npm, timeoutMs: 5_000 });assert.equal(timedOut.state, "failed");assert.equal(timedOut.reason, undefined, "install timeout is not an unavailable tool");assert.match(timedOut.error, /exceeded/);diff --git a/docs/spec/07-plugins/16-trusted-extensions.md b/docs/spec/07-plugins/16-trusted-extensions.mdindex dd423fa1a..afd437213 100644--- a/docs/spec/07-plugins/16-trusted-extensions.md+++ b/docs/spec/07-plugins/16-trusted-extensions.md@@ -124,9 +124,12 @@ target, so the sibling `node` remains discoverable. Selected paths never comefrom renderer text input.Main validates the selected executable and its Node.js runtime with bounded-version checks before saving or installing. Invalid selections (including npm-without usable Node.js) show a localized native warning and let the user choose-again or cancel; the guidance is to choose npm in the same directory as `node`.+version checks before saving or installing. The checks share the installation+budget and stop at a bounded ceiling, because a slow machine must not report a+working npm as unavailable and send the user back to the picker. Invalid+selections (including npm without usable Node.js) show a localized native+warning and let the user choose again or cancel; the guidance is to choose npm+in the same directory as `node`.Only a validated selection is persisted, atomically, in Main-owned`<dataDir>/npm-path.json`, not renderer settings or Host SQLite. Future importsreuse it after validation; a stale saved path returns to the recovery prompt.