Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import { check, HIGH_RISK_PERMISSIONS } from "./check.js";
import { scaffold } from "./templates.js";
const created: string[] = [];
async function tempDir(): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "pi-devkit-check-"));
created.push(dir);
return dir;
}
afterEach(async () => {
while (created.length) {
await rm(created.pop()!, { recursive: true, force: true });
}
});
async function editManifest(
dir: string,
mutate: (manifest: Record<string, any>) => void,
): Promise<void> {
const path = join(dir, "manifest.json");
const manifest = JSON.parse(await readFile(path, "utf8"));
mutate(manifest);
await writeFile(path, JSON.stringify(manifest, null, 2), "utf8");
}
describe("check", () => {
it("measures the package the way pack ships it: no dist/, no credential files", async () => {
const dir = join(await tempDir(), "sized");
await scaffold({ dir, template: "panel-basic" });
const clean = await check(dir);
expect(clean.ok).toBe(true);
await mkdir(join(dir, "dist"), { recursive: true });
await writeFile(join(dir, "dist/old.piplug"), "x".repeat(4096), "utf8");
await writeFile(join(dir, ".env"), "TOKEN=secret", "utf8");
await writeFile(join(dir, "server.pem"), "-----BEGIN-----", "utf8");
await writeFile(join(dir, ".npmrc"), "//registry/:_authToken=abc", "utf8");
const result = await check(dir);
expect(result.ok).toBe(true);
expect(result.fileCount).toBe(clean.fileCount);
expect(result.totalBytes).toBe(clean.totalBytes);
const skipped = result.warnings.find((w) => w.code === "package.secret-skipped");
expect(skipped?.message).toContain(".env");
expect(skipped?.message).toContain("server.pem");
expect(skipped?.message).toContain(".npmrc");
expect(skipped?.message).not.toContain("dist/");
});
it("warns when manifest.icon names a file that does not exist", async () => {
const dir = join(await tempDir(), "iconless");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.icon = "icon.png";
});
const missing = await check(dir);
expect(missing.ok).toBe(true);
expect(missing.warnings.map((w) => w.code)).toContain("icon.missing");
await writeFile(join(dir, "icon.png"), Buffer.from([0x89, 0x50, 0x4e, 0x47]));
const present = await check(dir);
expect(present.warnings.map((w) => w.code)).not.toContain("icon.missing");
});
it("warns, without failing, when net.domains admits loopback or metadata hosts", async () => {
const dir = join(await tempDir(), "localnet");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.permissions = [...(m.permissions ?? []), "net.fetch"];
m.net = { domains: ["api.example.com", "localhost", "169.254.169.254"] };
});
const result = await check(dir);
expect(result.ok).toBe(true);
const warning = result.warnings.find((w) => w.code === "net.local-domain");
expect(warning?.message).toContain("localhost");
expect(warning?.message).toContain("169.254.169.254");
expect(warning?.message).not.toContain("api.example.com");
await editManifest(dir, (m) => {
m.net = { domains: ["api.example.com"] };
});
expect((await check(dir)).warnings.map((w) => w.code)).not.toContain("net.local-domain");
});
it("rejects a manifest id outside the schema's dotted lowercase shape", async () => {
const dir = join(await tempDir(), "badid");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.id = "Bad Id/../x";
});
const result = await check(dir);
expect(result.ok).toBe(false);
expect(result.errors.map((e) => e.code)).toContain("manifest.invalid-id");
});
it("rejects a dotted id the installer refuses, with the installer's reason", async () => {
const dir = join(await tempDir(), "deviceid");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.id = "nul.plugin";
});
const result = await check(dir);
expect(result.ok).toBe(false);
expect(result.errors).toEqual([
{ code: "manifest.invalid-id", message: "manifest.id must not start with a Windows device name" },
]);
});
it("fails a manifest.renderer entry that does not exist, as the installer does (#1464)", async () => {
const dir = join(await tempDir(), "renderer-entry");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.renderer = "renderer/index.mjs";
m.permissions = [...(m.permissions ?? []), "renderer.extension"];
});
const missing = await check(dir);
expect(missing.ok).toBe(false);
const error = missing.errors.find((e) => e.code === "renderer.missing");
expect(error?.message).toContain("renderer/index.mjs");
// A directory with a module name is not an entry either.
await mkdir(join(dir, "renderer/index.mjs"), { recursive: true });
expect((await check(dir)).errors.map((e) => e.code)).toContain("renderer.missing");
await rm(join(dir, "renderer"), { recursive: true });
await mkdir(join(dir, "renderer"), { recursive: true });
await writeFile(join(dir, "renderer/index.mjs"), "export function activate() {}\n", "utf8");
const present = await check(dir);
expect(present.errors.map((e) => e.code)).not.toContain("renderer.missing");
// An entry outside the package is already refused by the manifest validator.
await editManifest(dir, (m) => {
m.renderer = "../renderer/index.mjs";
});
const escaping = await check(dir);
expect(escaping.ok).toBe(false);
expect(escaping.errors.map((e) => e.code)).toEqual(["manifest.invalid"]);
});
it("treats background audio and websocket access as high risk", () => {
for (const permission of [
"net.fetch",
"net.websocket",
"fs.write",
"fs.delete",
"agent.prompt.inject",
"agent.tool.register",
"browser.cdp",
"audio.capture.background",
"session.autoTitle",
]) {
expect(HIGH_RISK_PERMISSIONS).toContain(permission);
}
});
it("warns on exactly the permissions the permissions matrix marks high (#1463)", async () => {
const matrix = await readFile(
fileURLToPath(
new URL("../../../docs/spec/07-plugins/13-plugin-permissions-matrix.md", import.meta.url),
),
"utf8",
);
const high = [...matrix.matchAll(/^\| `([A-Za-z0-9.]+)` \| high \|/gm)].map((m) => m[1]);
expect(high).toContain("renderer.extension");
expect([...HIGH_RISK_PERMISSIONS].sort()).toEqual([...new Set(high)].sort());
});
it("names grants such as desktop.control and session.read in the high-risk warning", async () => {
const dir = join(await tempDir(), "high-risk-grants");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.permissions = [...(m.permissions ?? []), "desktop.control", "session.read", "session.autoTitle"];
});
const highRisk = (await check(dir)).warnings.find((w) => w.code === "permission.high-risk");
expect(highRisk?.message).toContain("desktop.control");
expect(highRisk?.message).toContain("session.read");
expect(highRisk?.message).toContain("session.autoTitle");
});
it("warns when background capability permissions are declared but never called", async () => {
const dir = join(await tempDir(), "background-apis");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.permissions = [...(m.permissions ?? []), "audio.capture.background", "net.websocket"];
});
const result = await check(dir);
expect(result.ok).toBe(true);
const unused = result.warnings
.filter((w) => w.code === "permission.unused")
.map((w) => w.message);
expect(unused.some((m) => m.includes('"audio.capture.background"'))).toBe(true);
expect(unused.some((m) => m.includes("audio.openInput"))).toBe(true);
expect(unused.some((m) => m.includes('"net.websocket"'))).toBe(true);
expect(unused.some((m) => m.includes("net.websocket.connect"))).toBe(true);
const highRisk = result.warnings.find((w) => w.code === "permission.high-risk");
expect(highRisk?.message).toContain("audio.capture.background");
expect(highRisk?.message).toContain("net.websocket");
// Calling one hinted API clears that permission's hint and leaves the other.
await writeFile(
join(dir, "main.js"),
"export async function onLoad() { await pi.audio.openInput({}); }\n",
"utf8",
);
const called = (await check(dir)).warnings
.filter((w) => w.code === "permission.unused")
.map((w) => w.message);
expect(called.some((m) => m.includes('"audio.capture.background"'))).toBe(false);
expect(called.some((m) => m.includes('"net.websocket"'))).toBe(true);
});
it("fails theme assets the installer rejects: package-relative paths (ADR 0255)", async () => {
const dir = join(await tempDir(), "theme-assets");
await scaffold({ dir, template: "panel-basic" });
await editManifest(dir, (m) => {
m.permissions = [...(m.permissions ?? []), "ui.theme"];
m.contributes = {
...(m.contributes ?? {}),
themes: [
{
id: "midnight",
label: "Midnight",
path: "themes/midnight.css",
assets: ["themes/assets/wallpaper-dark.webp"],
},
],
};
});
await mkdir(join(dir, "themes"), { recursive: true });
await writeFile(join(dir, "themes/midnight.css"), ":root {}\n", "utf8");
const result = await check(dir);
expect(result.ok).toBe(false);
const error = result.errors.find((e) => e.code === "theme.asset-package-relative");
expect(error?.message).toContain("themes/assets/wallpaper-dark.webp");
expect(error?.message).toContain("absolute image or font path");
expect(error?.message).toContain("pi.themes.upsert");
// The absolute spelling the installer accepts passes the check untouched.
await editManifest(dir, (m) => {
m.contributes.themes[0].assets = [
`${dir.replaceAll("\\\\", "/")}/themes/assets/wallpaper-dark.webp`,
];
});
const absolute = await check(dir);
expect(absolute.ok).toBe(true);
expect(absolute.errors.some((e) => e.code === "theme.asset-package-relative")).toBe(false);
});
});