Skip to content

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

fix(plugins): keep the classified error code across the plugin host API

vastsa418ca3c6c27f
diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.ts
index 534c19b03..131e11b40 100644
--- a/apps/desktop/electron/main/plugin-runtime.ts
+++ b/apps/desktop/electron/main/plugin-runtime.ts
@@ -639,6 +639,27 @@ function apiError(code: string, message: string): PluginApiError {
return err;
}
+/**
+ * Error code one host API call answers with. Host services classify their own
+ * failures as `errorCode` (agent-runtime, host-core) while this runtime's own
+ * refusals carry `code`; the broker forwards whichever is present so a plugin
+ * can branch on the same documented code the app uses instead of reading every
+ * service failure as a generic one. Every other host boundary reads the same
+ * precedence — `data.errorCode`, then `errorCode`, then `code`.
+ */
+function pluginCallErrorCode(error: unknown): string {
+ const candidate = error as
+ | { code?: string; errorCode?: string; data?: { errorCode?: string } }
+ | null
+ | undefined;
+ return (
+ candidate?.data?.errorCode ??
+ candidate?.errorCode ??
+ candidate?.code ??
+ "PLUGIN_API_FAILED"
+ );
+}
+
function pluginActionEnum(schema: unknown): readonly string[] | null {
if (!schema || typeof schema !== "object" || Array.isArray(schema)) return null;
const properties = (schema as { properties?: unknown }).properties;
@@ -1938,7 +1959,7 @@ export class PluginRuntime {
id: message.id,
ok: false,
error: {
- code: error?.code ?? "PLUGIN_API_FAILED",
+ code: pluginCallErrorCode(error),
message: error?.message ?? String(error),
},
}),
diff --git a/apps/desktop/electron/main/services/plugin-services.ts b/apps/desktop/electron/main/services/plugin-services.ts
index a44547cd3..fa752a081 100644
--- a/apps/desktop/electron/main/services/plugin-services.ts
+++ b/apps/desktop/electron/main/services/plugin-services.ts
@@ -384,7 +384,14 @@ export function createPluginServices({
runtimeProvider,
context,
launch.sidecarParams.thinkingLevel,
- { signal: input.signal, sessionId: launchSessionId },
+ {
+ signal: input.signal,
+ sessionId: launchSessionId,
+ // Spec 07-plugins/03-plugin-api.md: empty model output answers the
+ // plugin with INVALID_ARGUMENT, not the runtime's internal code.
+ emptyErrorCode: "INVALID_ARGUMENT",
+ emptyErrorMessage: "The model returned no text.",
+ },
);
return {
text: result.text,
diff --git a/apps/desktop/test/plugin-complete.test.mjs b/apps/desktop/test/plugin-complete.test.mjs
index 313af612e..e890013ad 100644
--- a/apps/desktop/test/plugin-complete.test.mjs
+++ b/apps/desktop/test/plugin-complete.test.mjs
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import { fork } from "node:child_process";
import { mkdtempSync, writeFileSync } from "node:fs";
+import { readFile } from "node:fs/promises";
import { register } from "node:module";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
@@ -292,3 +293,59 @@ test("agent.complete is rate-limited per plugin", async (t) => {
assert.equal(calls, 8);
assert.ok(output.errors.some((entry) => String(entry).includes("RATE_LIMITED") || String(entry).includes("rate")));
});
+
+test("agent.complete keeps the classified provider code for the plugin", async (t) => {
+ const runtime = new PluginRuntime({
+ hostEntry: hostProcessEntry,
+ spawnProcess: forkPluginProcess,
+ complete: async () => {
+ // Exactly the shape `completeOneShot` throws: `errorCode` (+ `data.errorCode`),
+ // never `code`. A plugin must be able to tell a rate limit from a network fault.
+ throw Object.assign(new Error("429: provider rate limited"), {
+ errorCode: "PROVIDER_RATE_LIMITED",
+ data: { retriable: true, errorCode: "PROVIDER_RATE_LIMITED" },
+ });
+ },
+ });
+ t.after(async () => {
+ for (const loaded of runtime.listLoaded()) await runtime.unload(loaded.manifest.id);
+ });
+ const dir = writePlugin({
+ permissions: ["agent.tool.register", "agent.complete"],
+ main: `
+ module.exports = {
+ async onLoad() {
+ await pi.agent.registerTool({
+ name: "ask",
+ description: "ask",
+ schema: { type: "object", properties: {} },
+ execute: async () => {
+ try {
+ await pi.agent.complete({
+ modelKey: "prov/model",
+ messages: [{ role: "user", content: "x" }],
+ });
+ return { code: null, message: null };
+ } catch (error) {
+ return { code: error.code || null, message: error.message || null };
+ }
+ },
+ });
+ },
+ };
+ `,
+ });
+ await runtime.loadFromPath(dir, ["agent.tool.register", "agent.complete"]);
+ const tool = runtime.getTools().find((entry) => entry.name === "ask");
+ const output = await tool.execute({}, { sessionId: "s" });
+ assert.equal(output.code, "PROVIDER_RATE_LIMITED");
+ assert.match(output.message, /429/);
+});
+
+test("the plugin completion declares INVALID_ARGUMENT for empty model output", async () => {
+ const source = await readFile(
+ new URL("../electron/main/services/plugin-services.ts", import.meta.url),
+ "utf8",
+ );
+ assert.match(source, /emptyErrorCode: "INVALID_ARGUMENT"/);
+});
diff --git a/docs/spec/07-plugins/03-plugin-api.md b/docs/spec/07-plugins/03-plugin-api.md
index bef5fdbc6..73445dc74 100644
--- a/docs/spec/07-plugins/03-plugin-api.md
+++ b/docs/spec/07-plugins/03-plugin-api.md
@@ -519,8 +519,18 @@ a secret. `includeSessionContext: true` also requires `session.read` and an
in-flight tool session; the host serializes that context and, if `messages` is
empty, appends `Please respond to the request.` System prompt
≤ 32 KiB; combined messages ≤ 200k characters; eight calls per plugin per
-rolling 60s (`RATE_LIMITED`); 90s budget (`TIMEOUT`). Empty model output is
-`INVALID_ARGUMENT`.
+rolling 60s (`RATE_LIMITED`); 90s budget (`TIMEOUT`). Provider 429 and other
+transient provider failures are retried inside the same call under the shared
+provider retry budget (ADR 0206) and a `Retry-After` header is honored. Empty
+model output is `INVALID_ARGUMENT`.
+
+When the call still fails, the plugin receives the host's classified code
+rather than a single generic failure — `PROVIDER_RATE_LIMITED` once the retry
+budget is exhausted, `PROVIDER_UNAUTHORIZED`, `CONTEXT_TOO_LARGE`,
+`NETWORK_ERROR` — so it can pace itself and report the cause. The broker
+answers with whichever code the failing service classified, in the same
+`data.errorCode` → `errorCode` → `code` precedence every other host boundary
+uses.
### clipboard / shell
```ts