Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(plugins): answer renderer calls only in SDK error codes

vastsabdbdb102329c
diff --git a/apps/desktop/electron/main/plugin-renderer-extension.ts b/apps/desktop/electron/main/plugin-renderer-extension.ts
index a59e291dd..4bed570c0 100644
--- a/apps/desktop/electron/main/plugin-renderer-extension.ts
+++ b/apps/desktop/electron/main/plugin-renderer-extension.ts
@@ -155,7 +155,8 @@ function errorCodeOf(error: unknown): string | undefined {
* the caller's problem and never count against the plugin. Everything after
* the relay (timeout, a thrown error, a missing `onRendererCall`, an answer
* that is not JSON or too large) is the plugin's, and five in a row close the
- * relay for 30s; one success reopens the count.
+ * relay for 30s; one success reopens the count. Every refusal carries a
+ * `PluginRendererErrorCode` or the plugin's own code, never a host one.
*/
export class RendererCallRelay {
private readonly states = new WeakMap<object, RelayState>();
@@ -172,13 +173,13 @@ export class RendererCallRelay {
args: unknown,
send: RendererCallSend<T>,
): Promise<unknown> {
- if (!plugin || plugin.disposing) {
- throw rendererCallError("PLUGIN_NOT_FOUND", `renderer plugin not loaded: ${pluginId}`);
+ // The renderer host loads only what a descriptor describes, so a caller
+ // without one is a load that has already ended (unload, disable, crash or
+ // a revoked grant): the code the renderer's own channel uses for it.
+ if (!servesRenderer(plugin)) {
+ throw rendererCallError("PLUGIN_UNLOADED", `no renderer extension is loaded for ${pluginId}`);
}
- if (!plugin.permissions.has(RENDERER_EXTENSION_PERMISSION)) {
- throw rendererCallError("PLUGIN_PERMISSION_DENIED", `${RENDERER_EXTENSION_PERMISSION} is not granted`);
- }
- if (!plugin.manifest.renderer || !plugin.manifest.rendererCallMethods?.includes(method)) {
+ if (!plugin.manifest.rendererCallMethods?.includes(method)) {
throw rendererCallError("PLUGIN_CALL_NO_HANDLER", `method not declared: ${method}`);
}
const state = this.stateFor(plugin);
@@ -224,6 +225,9 @@ export class RendererCallRelay {
`plugin ${pluginId} did not answer ${method} within ${RENDERER_CALL_TIMEOUT_MS}ms`,
);
}
+ if (code === "PLUGIN_CRASHED") {
+ throw rendererCallError("PLUGIN_UNLOADED", `plugin ${pluginId} stopped before answering ${method}`);
+ }
// The plugin's own code passes through so its component can branch on
// it; an uncoded failure still reaches the renderer as a coded one.
throw rendererCallError(
diff --git a/apps/desktop/src/features/plugins/model.ts b/apps/desktop/src/features/plugins/model.ts
index 89f75d110..0234979ff 100644
--- a/apps/desktop/src/features/plugins/model.ts
+++ b/apps/desktop/src/features/plugins/model.ts
@@ -45,6 +45,8 @@ export const PERMISSION_RISK: Record<string, RiskTier> = {
"agent.tool.register": "high",
"agent.complete": "high",
"agent.extension": "high",
+ // Its code runs in the app's own document, so the grant is the boundary.
+ "renderer.extension": "high",
"desktop.control": "high",
"session.read": "high",
"browser.cdp": "high",
diff --git a/apps/desktop/test/plugin-renderer-call-runtime.test.mjs b/apps/desktop/test/plugin-renderer-call-runtime.test.mjs
index e8ef5c855..2f2dec782 100644
--- a/apps/desktop/test/plugin-renderer-call-runtime.test.mjs
+++ b/apps/desktop/test/plugin-renderer-call-runtime.test.mjs
@@ -161,7 +161,7 @@ test("plugin.call reaches onRendererCall in the plugin process and returns its a
assert.ok(Date.now() - started >= 1_900, "the timeout is the relay's 2s");
await runtime.unload("lab.calls");
- await assert.rejects(runtime.callRenderer("lab.calls", "echo"), refusedWith("PLUGIN_NOT_FOUND"));
+ await assert.rejects(runtime.callRenderer("lab.calls", "echo"), refusedWith("PLUGIN_UNLOADED"));
});
test("a plugin without onRendererCall answers every declared method with NO_HANDLER", async (t) => {
@@ -184,7 +184,7 @@ test("without the renderer.extension grant the plugin runs but has no renderer e
assert.equal(runtime.rendererDescriptor("lab.ungranted"), undefined);
await assert.rejects(
runtime.callRenderer("lab.ungranted", "echo", {}),
- refusedWith("PLUGIN_PERMISSION_DENIED"),
+ refusedWith("PLUGIN_UNLOADED"),
);
// No generation was ever handed out, so there is none to guess.
for (let generation = 1; generation < 64; generation += 1) {
diff --git a/apps/desktop/test/plugin-renderer-extension.test.mjs b/apps/desktop/test/plugin-renderer-extension.test.mjs
index ed1f83cce..63867098f 100644
--- a/apps/desktop/test/plugin-renderer-extension.test.mjs
+++ b/apps/desktop/test/plugin-renderer-extension.test.mjs
@@ -214,10 +214,11 @@ test("calls the relay must not make are refused before it, and never count again
// 65537 bytes, while its length in characters is only 32770.
const oversize = `a${"é".repeat(32_767)}`;
const cases = [
- ["not running", undefined, "ping", undefined, "PLUGIN_NOT_FOUND"],
- ["unloading", loadedPlugin({ disposing: true }), "ping", undefined, "PLUGIN_NOT_FOUND"],
- ["without the grant", loadedPlugin({ permissions: [] }), "ping", undefined, "PLUGIN_PERMISSION_DENIED"],
- ["without an entry", loadedPlugin({ manifest: { renderer: undefined } }), "ping", undefined, "PLUGIN_CALL_NO_HANDLER"],
+ // No descriptor, no load: the caller's load has already ended.
+ ["not running", undefined, "ping", undefined, "PLUGIN_UNLOADED"],
+ ["unloading", loadedPlugin({ disposing: true }), "ping", undefined, "PLUGIN_UNLOADED"],
+ ["without the grant", loadedPlugin({ permissions: [] }), "ping", undefined, "PLUGIN_UNLOADED"],
+ ["without an entry", loadedPlugin({ manifest: { renderer: undefined } }), "ping", undefined, "PLUGIN_UNLOADED"],
["without methods", loadedPlugin({ manifest: { rendererCallMethods: undefined } }), "ping", undefined, "PLUGIN_CALL_NO_HANDLER"],
["an undeclared method", plugin, "shutdown", undefined, "PLUGIN_CALL_NO_HANDLER"],
["cyclic args", plugin, "echo", cyclic, "PLUGIN_CALL_UNSERIALIZABLE"],
@@ -330,6 +331,7 @@ test("what goes wrong past the relay reaches the caller coded, and counts agains
["a timeout", coded("TIMEOUT", "no reply"), "PLUGIN_CALL_TIMEOUT", /did not answer ping within 2000ms/],
["the plugin's own code", coded("LAB_NOT_READY", "not ready"), "LAB_NOT_READY", /^not ready$/],
["a missing handler", coded("PLUGIN_CALL_NO_HANDLER", "no onRendererCall"), "PLUGIN_CALL_NO_HANDLER", /onRendererCall/],
+ ["a crash", coded("PLUGIN_CRASHED", "plugin host process exited"), "PLUGIN_UNLOADED", /stopped before answering ping/],
["an uncoded error", boom, "PLUGIN_CALL_FAILED", /^boom$/],
[
"a thrown string",
diff --git a/packages/plugin-sdk/src/renderer.ts b/packages/plugin-sdk/src/renderer.ts
index 4302478d2..3640c3bdb 100644
--- a/packages/plugin-sdk/src/renderer.ts
+++ b/packages/plugin-sdk/src/renderer.ts
@@ -363,7 +363,11 @@ export type PluginRendererErrorCode =
| "PLUGIN_ATTACHMENT_NOT_FOUND"
/** `attachments.add`: the host could not stage the file. */
| "PLUGIN_ATTACHMENT_FAILED"
- /** This load has ended: nothing registers, injects or dispatches through it. */
+ /**
+ * This load has ended, or is ending because the plugin stopped (unloaded,
+ * disabled, crashed or its grant revoked): nothing registers, injects or
+ * dispatches through it.
+ */
| "PLUGIN_UNLOADED"
/** `plugin.call`: the method is not declared, or `onRendererCall` is missing. */
| "PLUGIN_CALL_NO_HANDLER"