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): fail a panel that never becomes ready (#998 item 5)
xjxc54a520312ed
diff --git a/apps/desktop/electron/main/plugin-panel-host.ts b/apps/desktop/electron/main/plugin-panel-host.tsindex 7a8f45bde..6717dee11 100644--- a/apps/desktop/electron/main/plugin-panel-host.ts+++ b/apps/desktop/electron/main/plugin-panel-host.ts@@ -9,7 +9,7 @@ import {import { builtinWindowBackground } from "@pi-desktop/shared";import { suppressLinuxFramelessSystemMenu } from "./frameless-system-menu";import { getModuleDirectory } from "./module-path";-import { PanelSenders, pageGoneWithin, resolvePanelInvocation } from "./plugin-panel-senders";+import { PanelSenders, panelReadyWithin, pageGoneWithin, resolvePanelInvocation } from "./plugin-panel-senders";import { PanelOperationSerializer } from "./plugin-panel-senders";import {isPluginPanelWindowControlAction,@@ -557,7 +557,14 @@ export class PluginPanelHost {// gone (see the `closed` handler above).this.senders.register(webContentsId, request.pluginId);try {- await win.loadURL(pathToFileURL(request.htmlPath).toString());+ // The window stays hidden until this settles, so a load that never+ // settles has to end the request as an error (#998 item 5): the page+ // that asked for the panel can then show why nothing appeared, and the+ // `catch` below destroys the hidden window.+ await panelReadyWithin(+ win.loadURL(pathToFileURL(request.htmlPath).toString()),+ request.pluginId,+ );if (!win.isDestroyed()) {win.show();}diff --git a/apps/desktop/electron/main/plugin-panel-senders.ts b/apps/desktop/electron/main/plugin-panel-senders.tsindex ed94f44f6..3f3e21032 100644--- a/apps/desktop/electron/main/plugin-panel-senders.ts+++ b/apps/desktop/electron/main/plugin-panel-senders.ts@@ -61,6 +61,48 @@ export class PanelSenders {*/export const PLUGIN_PAGE_CLOSE_SETTLE_MS = 1_000;+/**+ * How long a panel page is given to report its first load before the open+ * request fails visibly. The window is created hidden and shown only after the+ * page loads, so a load that never settles must not leave the user with a click+ * that does nothing to show for it (#998): it becomes an error the page that+ * asked for the panel can display.+ */+export const PLUGIN_PANEL_LOAD_SETTLE_MS = 15_000;++/**+ * A panel's page load, raced against that budget. A load that fails on its own+ * keeps the original error; a load that hangs becomes the timeout error instead+ * of an invisible, forever-hidden window. The window that lost the race is+ * destroyed by the open path's own `catch`, so nothing here reaches into+ * Electron — this stays a plain promise race a test can drive.+ */+export function panelReadyWithin(+ load: Promise<unknown>,+ pluginId: string,+ budgetMs: number = PLUGIN_PANEL_LOAD_SETTLE_MS,+): Promise<void> {+ return new Promise((resolve, reject) => {+ const timer = setTimeout(() => {+ reject(+ new Error(+ `PANEL_LOAD_TIMEOUT: the panel page of ${pluginId} did not report ready within ${Math.round(budgetMs / 1000)}s`,+ ),+ );+ }, budgetMs);+ Promise.resolve(load).then(+ () => {+ clearTimeout(timer);+ resolve();+ },+ (error: unknown) => {+ clearTimeout(timer);+ reject(error instanceof Error ? error : new Error(String(error)));+ },+ );+ });+}+type ClosablePage = {isDestroyed(): boolean;once(event: "destroyed", listener: () => void): unknown;diff --git a/apps/desktop/test/plugin-panel-invoker.test.mjs b/apps/desktop/test/plugin-panel-invoker.test.mjsindex dd04b453c..357d827f9 100644--- a/apps/desktop/test/plugin-panel-invoker.test.mjs+++ b/apps/desktop/test/plugin-panel-invoker.test.mjs@@ -8,8 +8,10 @@ register(new URL("./helpers/ts-import-hooks.mjs", import.meta.url));const {PanelOperationSerializer,PanelSenders,+ PLUGIN_PANEL_LOAD_SETTLE_MS,PLUGIN_PAGE_CLOSE_SETTLE_MS,pageGoneWithin,+ panelReadyWithin,resolvePanelInvocation,teardownPanelWindow,} = await import("../electron/main/plugin-panel-senders.ts");@@ -141,7 +143,7 @@ test(test("panel windows and docked views bind bridge identity to the page", () => {assert.match(panelHostSource,- /import \{ PanelSenders, pageGoneWithin, resolvePanelInvocation \} from "\.\/plugin-panel-senders"/,+ /import \{[^}]*\bPanelSenders\b[^}]*\bpageGoneWithin\b[^}]*\bresolvePanelInvocation\b[^}]*\} from "\.\/plugin-panel-senders"/,);assert.match(panelHostSource, /private senders = new PanelSenders\(\)/);// Registered before the document loads, released with the page — not with the@@ -328,3 +330,47 @@ test("teardownPanelWindow leaves window registered when not forced and page refuassert.equal(destroyed, false, "win.destroy() was not called without force");assert.equal(win.isDestroyed(), false);});++test("a panel page that never settles still fails the open request (#998)", async () => {+ // The window is hidden until the page loads: a hang must become an error the+ // asking page can show, not a click that does nothing.+ const hang = new Promise(() => undefined);+ await assert.rejects(+ panelReadyWithin(hang, "demo.plugin", 40),+ /PANEL_LOAD_TIMEOUT: the panel page of demo\.plugin did not report ready within 0s/,+ );+});++test("a panel page that loads before its budget shows normally", async () => {+ await panelReadyWithin(Promise.resolve("loaded"), "demo.plugin", 5_000);+});++test("a panel page that fails its own load keeps the original error", async () => {+ await assert.rejects(+ panelReadyWithin(+ Promise.reject(new Error("ERR_FILE_NOT_FOUND: views/panel.html")),+ "demo.plugin",+ 5_000,+ ),+ /ERR_FILE_NOT_FOUND/,+ );+});++test("the panel load budget is a generous, seconds-scale default", () => {+ assert.ok(PLUGIN_PANEL_LOAD_SETTLE_MS >= 10_000);+});++test("the open path races the page load against that budget (#998)", () => {+ const open = panelHostSource.slice(+ panelHostSource.indexOf("async open(request: PluginPanelOpenRequest)"),+ panelHostSource.indexOf("async close(pluginId"),+ );+ assert.ok(open.length > 0);+ assert.match(open, /await panelReadyWithin\(\s*win\.loadURL\(/);+ // The timeout error surfaces through the open path's own `catch`, which+ // destroys the hidden window before the rejection reaches the click.+ assert.match(+ open,+ /\} catch \(error\) \{\s*if \(!win\.isDestroyed\(\)\) \{\s*win\.destroy\(\);/,+ );+});