Skip to content

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

13.6 KB
Raw Blame
import {
  readMainModuleSync,
  readPluginsSourceSync,
  readStoreModuleSync,
  readMainSourceSync,
} from "./helpers/source-contracts.mjs";
import assert from "node:assert/strict";
import test from "node:test";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

import { en } from "../../../packages/i18n/dist/locales/en/index.js";
import { zhCN } from "../../../packages/i18n/dist/locales/zh-CN/index.js";
import { tr } from "../../../packages/i18n/dist/locales/tr/index.js";
import { ko } from "../../../packages/i18n/dist/locales/ko/index.js";

const catalogs = { en, "zh-CN": zhCN, tr, ko };

const here = dirname(fileURLToPath(import.meta.url));
const desktopRoot = join(here, "..");
const repoRoot = join(desktopRoot, "../..");

const runtimeSrc = readFileSync(join(desktopRoot, "electron/main/plugin-runtime.ts"), "utf8");
const panelSrc = readFileSync(join(desktopRoot, "electron/main/plugin-panel-host.ts"), "utf8");
const mainSrc = readMainSourceSync();
const pageSrc = readPluginsSourceSync();
const protocolSrc = readFileSync(join(repoRoot, "packages/shared/src/protocol.ts"), "utf8");
const marketIpcSrc = readMainModuleSync("ipc/market-ipc.ts");
const catalogSliceSrc = readStoreModuleSync("slices/catalog-slice.ts");
const registrySrc = readFileSync(join(repoRoot, "crates/host-core/src/plugins/registry.rs"), "utf8");
const marketplaceSrc = readFileSync(join(repoRoot, "crates/host-core/src/plugins/marketplace.rs"), "utf8");
const validationSrc = readFileSync(join(repoRoot, "crates/host-core/src/plugins/validation.rs"), "utf8");
const installSrc = readFileSync(join(repoRoot, "crates/host-core/src/plugins/install.rs"), "utf8");
const modelSrc = readFileSync(join(repoRoot, "crates/host-core/src/plugins/model.rs"), "utf8");

test("plugin runtime exposes gated high-risk host APIs", () => {
  for (const token of [
    "fs.write",
    "fs.delete",
    "fs.openDefault",
    "fs.openInBrowser",
    "fs.reveal",
    "net.fetch",
    "agent.complete",
    "desktop.control",
    "session.read",
    "session.autoTitle",
    "models.list",
    "shell.openExternal",
    "clipboard.read",
    "clipboard.write",
    "assertPermission",
  ]) {
    assert.match(runtimeSrc, new RegExp(token.replaceAll(".", "\\.")));
  }
});

test("large-file host reads stay behind the fs.read gateway", () => {
  for (const api of ["fs.stat", "fs.readRange", "fs.registerDropped"]) {
    assert.match(runtimeSrc, new RegExp(`\\"${api}\\"`));
  }
  assert.match(runtimeSrc, /MAX_FS_READ_RANGE_BYTES/);
  assert.match(runtimeSrc, /dropped-file grants are read-only/);
  assert.match(runtimeSrc, /new Map\(\)/);
});

test("native plugin notifications stay behind the existing notify permission", () => {
  for (const channel of [
    "ui.getNotificationPermission",
    "ui.requestNotificationPermission",
    "ui.showNativeNotification",
  ]) {
    assert.match(runtimeSrc, new RegExp(`\\"${channel}\\"`));
  }
  assert.match(runtimeSrc, /getNotificationPermission: async \(\) => \{/);
  assert.match(runtimeSrc, /requestNotificationPermission: async \(\) => \{/);
  assert.match(runtimeSrc, /showNativeNotification: async \(input/);
  assert.match(runtimeSrc, /this\.assertPermission\(loaded, "notify"\)/);
});

test("workspace deletion and panel operations stay bounded", () => {
  assert.match(runtimeSrc, /PANEL_SKILL_CHANNELS/);
  assert.match(runtimeSrc, /method: "panel.invoke"/);
  assert.match(runtimeSrc, /"fs.remove"/);
  assert.match(runtimeSrc, /recursive: false/);
  assert.match(runtimeSrc, /cannot remove the root itself/);
  // Deleting goes to the OS trash, so a delete this gate got wrong is still
  // recoverable; `rmSync` survives only as the fallback for a host that has no
  // trash to offer.
  assert.match(runtimeSrc, /this\.services\.trashItem\(full\)/);
  // A single-file remove in a loop empties a workspace as well as `rm -rf`;
  // the rolling window is what tells the two apart.
  assert.match(runtimeSrc, /MAX_DELETES_PER_WINDOW/);
});

test("the plugins page shows the file scope behind a file permission", () => {
  // A permission name says "may touch files"; only the scope says which ones,
  // so the row has to carry it or the user is approving a blank cheque.
  assert.match(pageSrc, /"fs\.write": "high"/);
  assert.match(pageSrc, /"fs\.read": "medium"/);
  assert.match(pageSrc, /"background\.service": "medium"/);
  assert.match(pageSrc, /function FsScopeChips\(/);
  assert.match(pageSrc, /t\("plugins\.fsAsksEachTime"\)/);
  assert.match(pageSrc, /t\("plugins\.legacyFsDowngraded"\)/);
  // The scope reaches the renderer from the registry, not from a second read
  // of the manifest, so an old record simply has no scope to show.
  assert.match(registrySrc, /fs: manifest\.fs\.clone\(\)/);
  for (const catalog of Object.values(catalogs)) {
    assert.equal(typeof catalog.plugins.legacyFsDowngraded, "string");
    assert.equal(typeof catalog.plugins.fsMode.delete, "string");
    assert.equal(typeof catalog.plugins.permissions["fs.delete"], "string");
    assert.equal(typeof catalog.plugins.permissionHelp["fs.delete"], "string");
    assert.equal(typeof catalog.plugins.permissions["agent.complete"], "string");
    assert.equal(typeof catalog.plugins.permissionHelp["session.read"], "string");
    assert.equal(typeof catalog.plugins.permissions["session.autoTitle"], "string");
    assert.equal(typeof catalog.plugins.permissionHelp["session.autoTitle"], "string");
    assert.equal(typeof catalog.plugins.permissions["models.list"], "string");
    assert.equal(typeof catalog.plugins.permissions["ui.microphone"], "string");
    assert.equal(typeof catalog.plugins.permissionHelp["ui.microphone"], "string");
    assert.equal(typeof catalog.plugins.permissions["desktop.control"], "string");
    assert.equal(typeof catalog.plugins.permissionHelp["desktop.control"], "string");
  }
});

test("plugin panels use sandboxed isolated host windows", () => {
  assert.match(panelSrc, /session\.fromPartition/);
  assert.match(panelSrc, /sandbox:\s*true/);
  assert.match(panelSrc, /nodeIntegration:\s*false/);
  assert.match(panelSrc, /plugin-panel\.js/);
  assert.match(panelSrc, /allowMicrophone/);
  assert.match(runtimeSrc, /this\.assertPermission\(loaded, "desktop\.control"\)/);
  assert.match(runtimeSrc, /desktop\.listOperations/);
  assert.match(runtimeSrc, /desktop\.invoke/);
});

test("plugins page includes marketplace install and auto-update controls", () => {
  assert.match(pageSrc, /tabMarket/);
  assert.match(pageSrc, /marketInstall/);
  assert.match(pageSrc, /applyAutoUpdates|marketApplyUpdates|checkUpdates/);
  assert.match(pageSrc, /permissionReview|grantedPermissions/);
});

test("plugins page refreshes installed update metadata when it opens", () => {
  assert.match(pageSrc, /useEffect\(\(\) => \{[\s\S]*api\.marketCheckUpdates\(false\)[\s\S]*refreshPlugins\(\)/);
  assert.match(
    marketIpcSrc,
    /IPC\.invoke\.marketCheckUpdates[\s\S]*refreshRemote: payload\?\.refreshRemote \?\? true/,
  );
  assert.match(
    catalogSliceSrc,
    /const existing = catalogRuntime\.getPluginRefresh\(\);\s*if \(existing\) return existing/,
  );
});

test("shared protocol declares marketplace and package install IPC", () => {
  for (const channel of [
    "pluginInstallFromPackage",
    "marketSearch",
    "marketInstall",
    "marketCheckUpdates",
    "marketApplyUpdates",
    "marketRefresh",
    "pluginOpenPanel",
    "pluginSetAutoUpdate",
  ]) {
    assert.match(protocolSrc, new RegExp(channel));
  }
});


test("plugins page can refresh the official marketplace repository", () => {
  assert.match(pageSrc, /marketRefresh|refreshMarket|refreshRemote/);
  assert.doesNotMatch(pageSrc, /MarketplaceSourceSettings|marketSource/);
});


test("marketplace detail pane renders readme changelog and versions", () => {
  assert.match(pageSrc, /marketGetDetail/);
  assert.match(pageSrc, /viewDetails|detailTitle|readmeMarkdown|versions/);
  assert.match(pageSrc, /installVersion|selectedVersion|changelog/);
});

// A publisher can list a version before uploading its package. The host then
// refuses the download, so the UI must not offer an install that can only end
// in PLUGIN_MARKET_INVALID.
test("marketplace blocks install for a version with no published package", () => {
  assert.match(pageSrc, /function versionInstallable\(/);
  assert.match(pageSrc, /const packagePending = item\.installable === false/);
  assert.match(pageSrc, /disabled=\{isPluginBusy\(item\.id\) \|\| packagePending\}/);
  assert.match(pageSrc, /disabled=\{isPluginBusy\(detail\.id\) \|\| detailPackagePending\}/);
  assert.match(pageSrc, /t\("plugins\.packagePending"\)/);
  assert.match(pageSrc, /t\("plugins\.packagePendingHint", \{/);

  const hostSrc = marketplaceSrc;
  assert.match(hostSrc, /fn has_package_metadata\(version: &MarketVersion\) -> bool/);
  // `installable` is the single answer every install affordance reads, so it
  // has to cover every reason the host would refuse the download: no package
  // yet, a host too old for the version, or a package URL off the allowlist.
  assert.match(hostSrc, /installable: latest_version\s*\n\s*\.map\(\|version\| \{/);
  assert.match(hostSrc, /has_package_metadata\(version\)\s*\n\s*&& host_supports_version\(version\)/);
  assert.match(hostSrc, /package_host_allowed\(&version\.url, &catalog_url\)\.is_ok\(\)/);
  for (const catalog of Object.values(catalogs)) {
    assert.equal(typeof catalog.plugins.packagePending, "string");
    assert.equal(typeof catalog.plugins.packagePendingHint, "string");
  }
});

// Plugin source now lives in publisher repositories, so a package URL is no
// longer guaranteed to sit under one repository the project controls. The
// download boundary is what keeps a catalog entry from aiming a request
// anywhere it likes.
test("marketplace package downloads stay inside the host allowlist", () => {
  const hostSrc = validationSrc + "\n" + installSrc;
  assert.match(
    hostSrc,
    /const PACKAGE_HOST_ALLOWLIST: &\[&str\] = &\["github\.com", "githubusercontent\.com", "cnb\.cool"\]/,
  );
  assert.match(hostSrc, /fn package_host_allowed\(package_url: &str, catalog_url: &str\) -> Result<\(\)>/);
  // Refuse before the request leaves the machine, then hold the redirect
  // chain to the same rule: a release asset always redirects. Every package
  // URL passes through this one path, whichever channel named it — the
  // official channel's mirror list included.
  assert.match(hostSrc, /package_host_allowed\(url, &catalog_url\)\?;/);
  // The observed downloader is the same boundary: it refuses an off-allowlist
  // host before the request and re-checks the effective URL after redirects.
  assert.match(
    hostSrc,
    /download_url_observed\(\s*url,\s*Some\(&catalog_url\),\s*expected_size,\s*report\)/,
  );
  assert.match(hostSrc, /"--proto-redir"\.into\(\)/);
  assert.match(hostSrc, /"%\{url_effective\}"\.into\(\)/);
  assert.match(hostSrc, /must not embed credentials/);
});

// A withdrawn version is a distribution signal, not permission to disable
// software somebody is relying on.
test("a withdrawn version is never offered and never silently disabled", () => {
  const hostSrc = marketplaceSrc + "\n" + modelSrc;
  assert.match(hostSrc, /\.filter\(\|version\| !version\.yanked\)/);
  assert.match(hostSrc, /PLUGIN_MARKET_YANKED/);
  assert.match(hostSrc, /PLUGIN_HOST_TOO_OLD/);
  assert.match(hostSrc, /pub struct PluginYankNotice/);

  assert.match(pageSrc, /function versionWithdrawn\(/);
  assert.match(pageSrc, /const detailWithdrawn = versionWithdrawn\(activeVersion\)/);
  assert.match(pageSrc, /t\("plugins\.withdrawnHint", \{ version: installTarget \}\)/);
  for (const catalog of Object.values(catalogs)) {
    assert.equal(typeof catalog.plugins.withdrawn, "string");
    assert.equal(typeof catalog.plugins.withdrawnHint, "string");
    assert.equal(typeof catalog.plugins.withdrawnReason, "string");
  }
});

// The verified shield is a claim about a publisher, so it must come from the
// center rather than from text a publisher can write.
test("verified trust is not something a catalog entry can grant itself", () => {
  const hostSrc = marketplaceSrc;
  assert.match(hostSrc, /fn resolve_trust\(&self, entry: &MarketCatalogEntry\) -> String/);
  assert.match(hostSrc, /"verified" if self\.is_trusted_channel\(\) => "verified"/);
  assert.match(hostSrc, /"verified" => "community"/);

  assert.match(pageSrc, /function showsVerifiedBadge\(/);
  assert.match(pageSrc, /\{showsVerifiedBadge\(item\) \?/);
  assert.match(pageSrc, /\{showsVerifiedBadge\(detail\) \?/);
});

test("PERMISSION_RISK mirrors every permission tier in the permissions matrix (#1480)", () => {
  const matrixPath = join(repoRoot, "docs/spec/07-plugins/13-plugin-permissions-matrix.md");
  const matrixContent = readFileSync(matrixPath, "utf8");
  const matrixEntries = [...matrixContent.matchAll(/^\| `([A-Za-z0-9.]+)` \| (low|medium|high) \|/gm)].map((m) => ({
    permission: m[1],
    risk: m[2],
  }));

  assert.ok(matrixEntries.length > 0, "permissions matrix must contain permission rows");

  const matrixMap = Object.fromEntries(matrixEntries.map((e) => [e.permission, e.risk]));
  const modelEntries = [...pageSrc.matchAll(/"([a-zA-Z0-9.]+)":\s*"(low|medium|high)"/g)].map((m) => ({
    permission: m[1],
    risk: m[2],
  }));
  const modelMap = Object.fromEntries(modelEntries.map((e) => [e.permission, e.risk]));

  for (const [permission, expectedRisk] of Object.entries(matrixMap)) {
    assert.equal(
      modelMap[permission],
      expectedRisk,
      `PERMISSION_RISK["${permission}"] should be "${expectedRisk}" as defined in permissions matrix`,
    );
  }

  assert.deepEqual(
    Object.keys(modelMap).sort(),
    Object.keys(matrixMap).sort(),
    "PERMISSION_RISK must define exactly the set of permissions from the permissions matrix",
  );
});