Skip to content

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

fix(plugins): audit a failed pi.net.fetch call as a failure

vastsaaa00bf206075
diff --git a/apps/desktop/electron/main/plugin-runtime.ts b/apps/desktop/electron/main/plugin-runtime.ts
index 131e11b40..50758dc5a 100644
--- a/apps/desktop/electron/main/plugin-runtime.ts
+++ b/apps/desktop/electron/main/plugin-runtime.ts
@@ -539,6 +539,49 @@ const PANEL_SKILL_CHANNELS = new Set([
const MAX_SKILLS_PER_PLUGIN = 32;
/** Redirect hops `pi.net.fetch` follows; each one is re-checked against egress. */
const NET_FETCH_MAX_REDIRECTS = 5;
+
+/**
+ * The delay a response advertises, verbatim — the value a plugin has to parse
+ * itself. `retry-after-ms` wins over `retry-after`, the same precedence the
+ * runtime's provider retry uses.
+ */
+function retryAfterHeader(headers: Record<string, string>): string | undefined {
+ let seconds: string | undefined;
+ for (const [name, value] of Object.entries(headers)) {
+ const lower = name.toLowerCase();
+ if (lower === "retry-after-ms") return value;
+ if (lower === "retry-after") seconds ??= value;
+ }
+ return seconds;
+}
+
+/**
+ * The audit entry for one completed `pi.net.fetch`. The response itself is
+ * passed to the plugin untouched, so the entry reports the upstream status
+ * verbatim: a 4xx/5xx is a failed call (`ok: false`), and that failed call
+ * records the delay it advertised instead of a bare `429`. The host never
+ * retries the plugin's request — retry and backoff are the plugin's own policy
+ * (`docs/spec/07-plugins/03-plugin-api.md` §7) — so this is observability, not
+ * a behaviour change: no field beyond these, and never a header set or a body.
+ */
+function netFetchAuditEntry(
+ pluginId: string,
+ url: string,
+ status: number,
+ headers: Record<string, string>,
+): Record<string, unknown> {
+ const retryAfter = status < 400 ? undefined : retryAfterHeader(headers);
+ return {
+ pluginId,
+ api: "net.fetch",
+ ok: status < 400,
+ ts: Date.now(),
+ url,
+ status,
+ ...(retryAfter === undefined ? {} : { retryAfter }),
+ };
+}
+
/** Skill documents above this size are refused (prompt budget, not disk). */
const MAX_SKILL_BYTES = 128 * 1024;
/** Catalog lines stay short — the body carries the detail. */
@@ -5091,14 +5134,9 @@ export class PluginRuntime {
this.assertEgress(loaded, input.url, "net.fetch");
if (this.services.fetch) {
const result = await this.services.fetch(input);
- this.services.audit?.({
- pluginId,
- api: "net.fetch",
- ok: true,
- ts: Date.now(),
- url: input.url,
- status: result.status,
- });
+ this.services.audit?.(
+ netFetchAuditEntry(pluginId, input.url, result.status, result.headers),
+ );
return result;
}
const controller = new AbortController();
@@ -5130,14 +5168,7 @@ export class PluginRuntime {
headers[key] = value;
});
const bodyText = await res.text();
- this.services.audit?.({
- pluginId,
- api: "net.fetch",
- ok: true,
- ts: Date.now(),
- url,
- status: res.status,
- });
+ this.services.audit?.(netFetchAuditEntry(pluginId, url, res.status, headers));
return { status: res.status, headers, bodyText };
} finally {
clearTimeout(timer);
diff --git a/apps/desktop/test/plugin-egress.test.mjs b/apps/desktop/test/plugin-egress.test.mjs
index 72c64acb5..93ed626be 100644
--- a/apps/desktop/test/plugin-egress.test.mjs
+++ b/apps/desktop/test/plugin-egress.test.mjs
@@ -141,6 +141,60 @@ test("net.fetch reaches a declared host", async (t) => {
assert.equal(result.bodyText, "ok");
});
+test("a failed upstream call is audited with its status and retry hint", async (t) => {
+ // The host passes the response through and never retries on the plugin's
+ // behalf, so a 429 has to be legible in the audit log: a failed call, naming
+ // the delay the plugin is expected to honor itself.
+ const hits = [];
+ const server = await listenOnce((req, res) => {
+ hits.push(req.url);
+ if (req.url === "/limited") {
+ res.writeHead(429, { "retry-after": "2" }).end("slow down");
+ return;
+ }
+ if (req.url === "/limited-ms") {
+ res.writeHead(429, { "retry-after-ms": "1500", "retry-after": "9" }).end("slow down");
+ return;
+ }
+ res.writeHead(200, { "retry-after": "7" }).end("ok");
+ });
+ t.after(() => server.close());
+
+ const { runtime, audits } = createRuntime(t);
+ const dir = writePlugin({ id: "egress.limited", net: { domains: ["127.0.0.1"] } });
+ await runtime.loadFromPath(dir);
+ const call = (path) =>
+ runtime.invokePanelBridge("egress.limited", "net.fetch", {
+ url: `http://127.0.0.1:${server.port}${path}`,
+ });
+
+ const limited = await call("/limited");
+ // Untouched passthrough: status, headers and body still reach the plugin.
+ assert.equal(limited.status, 429);
+ assert.equal(limited.headers["retry-after"], "2");
+ assert.equal(limited.bodyText, "slow down");
+ // One request: nothing here retried the plugin's call.
+ assert.equal(hits.length, 1);
+
+ assert.equal((await call("/limited-ms")).status, 429);
+ assert.equal((await call("/ok")).status, 200);
+ assert.equal(hits.length, 3);
+
+ const entries = audits.filter((e) => e.api === "net.fetch");
+ const limitedEntry = entries.find((e) => e.url.endsWith("/limited"));
+ assert.equal(limitedEntry.ok, false, "a 429 is a failed call");
+ assert.equal(limitedEntry.status, 429);
+ assert.equal(limitedEntry.retryAfter, "2");
+ // `retry-after-ms` wins, the precedence the provider retry already uses.
+ assert.equal(entries.find((e) => e.url.endsWith("/limited-ms")).retryAfter, "1500");
+ // A success keeps the shape it always had: the hint is a failure detail, and
+ // a body never enters the log.
+ const okEntry = entries.find((e) => e.url.endsWith("/ok"));
+ assert.equal(okEntry.ok, true);
+ assert.equal("retryAfter" in okEntry, false);
+ assert.equal(JSON.stringify(entries).includes("slow down"), false);
+});
+
test("a redirect off the allowlist cannot carry the request out", async (t) => {
// The classic bypass: declare a benign host, then let it 302 to the collector.
const collector = await listenOnce((_req, res) => {