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 { readAppSource } from "./helpers/source-contracts.mjs";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import { loadStyles } from "./helpers/styles.mjs";
import { readMainModule, readMainSource } from "./helpers/main-source.mjs";
const mainSource = await readMainSource();
const windowIpcSource = await readMainModule("ipc/window-ipc.ts");
const activationSource = await readFile(
new URL("../electron/main/bootstrap/app-activation.ts", import.meta.url),
"utf8",
);
const menuSource = await readFile(
new URL("../electron/main/application-menu.ts", import.meta.url),
"utf8",
);
const shortcutSource = await readFile(
new URL("../../../packages/shared/src/keyboard-shortcuts.ts", import.meta.url),
"utf8",
);
const appSource = await readAppSource();
const stylesSource = await loadStyles();
const controlsSource = await readFile(
new URL("../src/components/WindowControls.tsx", import.meta.url),
"utf8",
);
const protocolSource = await readFile(
new URL("../../../packages/shared/src/protocol.ts", import.meta.url),
"utf8",
);
const packageJson = JSON.parse(
await readFile(new URL("../package.json", import.meta.url), "utf8"),
);
const iconScriptSource = await readFile(
new URL("../../../scripts/make-icon.py", import.meta.url),
"utf8",
);
test("macOS installs a standard application menu before window creation", () => {
assert.match(menuSource, /label:\s*APP_NAME/);
for (const role of [
"about",
"services",
"hide",
"hideOthers",
"unhide",
"quit",
]) {
assert.match(menuSource, new RegExp(`role: "${role}"`));
}
for (const topLevel of ["file", "edit", "view", "window"]) {
assert.match(menuSource, new RegExp(`label: labels\\.menu\\.${topLevel}`));
}
assert.match(menuSource, /role:\s*"help"/);
assert.match(menuSource, /resolveLocale\(locale\)/);
assert.match(mainSource, /app\.setName\(APP_NAME\)/);
assert.match(mainSource, /locale:\s*app\.getLocale\(\)/);
assert.match(menuSource, /Menu\.buildFromTemplate\(template\)/);
assert.match(menuSource, /Menu\.setApplicationMenu/);
assert.match(
menuSource,
/options\.platform \?\? process\.platform\) !== "darwin"/,
);
assert.match(menuSource, /Menu\.setApplicationMenu\(null\)/);
assert.match(
mainSource,
/installApplicationMenu\(\{\s+locale: app\.getLocale\(\),\s+dispatch: dispatchApplicationMenuCommand,\s+dispatchNative: dispatchNativeMenuAction,\s+\}\);/,
);
assert.ok(
mainSource.indexOf("prewarmPluginLauncher();") < mainSource.indexOf("registerIpc();"),
"launcher warm-up should start before IPC registration completes",
);
});
test("macOS application menu routes shell commands and preserves native roles", () => {
for (const [id, binding] of [
["openSettings", "Mod+Comma"],
["newTask", "Mod+N"],
["openProject", "Mod+O"],
["openSearch", "Mod+K"],
["toggleSidebar", "Mod+B"],
]) {
assert.match(menuSource, new RegExp(`accelerator\\("${id}"\\)`));
assert.match(shortcutSource, new RegExp(`defaultBinding: "${binding.replace("+", "\\+")}"`));
}
for (const role of [
"undo",
"redo",
"cut",
"copy",
"paste",
"selectAll",
"reload",
]) {
assert.match(menuSource, new RegExp(`role: "${role}"`));
}
assert.match(menuSource, /function nativeAction\(/);
// The File menu's window item is the merged visibility toggle (D438): its
// accelerator is the toggle's own binding (D438, rebound by D439) and its
// click is that native action.
assert.match(menuSource, /accelerator\("toggleWindow"\)/);
assert.match(menuSource, /labels\.menu\.toggleWindow/);
for (const action of [
"toggleMainWindow",
"resetZoom",
"zoomIn",
"zoomOut",
"toggleFullScreen",
]) {
assert.match(menuSource, new RegExp(`"${action}"`));
}
assert.match(menuSource, /A plain item keeps the command clickable/);
assert.match(mainSource, /IPC\.event\.menuCommand/);
assert.match(mainSource, /APP_MENU_COMMANDS\.includes\(command\)/);
assert.match(mainSource, /dispatchNative: dispatchNativeMenuAction/);
assert.doesNotMatch(menuSource, /CmdOrCtrl\+J|toggleWorkPanel/);
assert.doesNotMatch(protocolSource, /"toggleWorkPanel"/);
});
test("developer mode gates every devtools entry point in the main process", () => {
assert.match(menuSource, /developerMode = false/);
assert.match(
menuSource,
/\.\.\.\(developerMode[\s\S]*role: "toggleDevTools"/,
);
assert.match(mainSource, /(?:let\s+)?developerMode\s*=\s*false/);
assert.match(mainSource, /function applyDeveloperMode/);
assert.match(
mainSource,
/if \(!next[\s\S]*isDevToolsOpened\(\)[\s\S]*closeDevTools\(\)/,
);
assert.match(
mainSource,
/before-input-event[\s\S]*!windowState\.developerMode[\s\S]*input\.code === "F12"/,
);
assert.match(
mainSource,
/process\.platform !== "darwin"[\s\S]*input\.control[\s\S]*input\.shift/,
);
assert.match(protocolSource, /devtoolsToggle:\s*"pi-desktop\/devtools\/toggle"/);
const handler = mainSource.slice(mainSource.indexOf("IPC.invoke.devtoolsToggle"));
assert.ok(
handler.indexOf("!developerMode") < handler.indexOf("openDevTools"),
"the IPC gate must run before opening devtools",
);
});
test("the main app shell consumes unmodified Ctrl+R before Chromium reloads", () => {
const handlerStart = mainSource.indexOf(
'window.webContents.on("before-input-event"',
);
const handlerEnd = mainSource.indexOf("\n });", handlerStart);
const handler = mainSource.slice(handlerStart, handlerEnd);
const reloadGuard = handler.slice(
handler.indexOf("if (isReloadChord)"),
handler.indexOf("const isPluginLauncherChord"),
);
assert.match(
handler,
/const isReloadChord =\s*input\.type === "keyDown" &&\s*input\.code === "KeyR" &&\s*input\.control &&\s*!input\.meta &&\s*!input\.alt &&\s*!input\.shift;/,
);
assert.match(reloadGuard, /event\.preventDefault\(\);\s*return;/);
assert.ok(
handler.indexOf("if (isReloadChord)") <
handler.indexOf("const isPluginLauncherChord"),
"reload prevention must run before other focused-window shortcuts",
);
assert.ok(
handler.indexOf("if (isReloadChord)") <
handler.indexOf(
'if (input.type !== "keyDown" || !windowState.developerMode) return;',
),
"reload prevention must not depend on developer mode",
);
});
test("Windows and Linux use menu-free frameless chrome with window controls", () => {
assert.match(
mainSource,
/process\.platform === "darwin"[\s\S]*titleBarStyle:\s*"hiddenInset"[\s\S]*frame:\s*false/,
);
assert.doesNotMatch(appSource, /DesktopMenuBar/);
assert.doesNotMatch(stylesSource, /\.desktop-menu-/);
assert.match(appSource, /platform as ShortcutPlatform/);
assert.match(appSource, /resolveKeybinding/);
for (const id of ["newTask", "openProject", "openSettings"]) {
assert.match(appSource, new RegExp(`case "${id}"`));
}
assert.match(appSource, /runMenuCommand\(id\)/);
for (const id of ["resetZoom", "toggleFullScreen"]) {
assert.match(appSource, new RegExp(`case "${id}"`));
}
assert.match(appSource, /nativeMenuAction\(id\)/);
assert.match(controlsSource, /windowControl\("getState"\)/);
assert.match(controlsSource, /ariaLabel=\{t\("window\.minimize"/);
assert.match(controlsSource, /ariaLabel=\{t\("window\.close"/);
assert.equal((appSource.match(/<WindowControls\s*\/>/g) ?? []).length, 1);
// The controls are rendered under the recovery surface too, so a window that
// never reaches the shell is still closable (issue #831).
assert.match(
appSource,
/\{shell\}[\s\S]*?\{\(ready && !showSplash\) \|\| startupPhase !== "starting" \?/,
);
assert.match(
stylesSource,
/\.window-control-btn\s*\{[^}]*-webkit-app-region:\s*no-drag;[^}]*pointer-events:\s*auto;/s,
);
assert.match(
stylesSource,
/\.window-controls\s*\{[^}]*-webkit-app-region:\s*no-drag;[^}]*pointer-events:\s*auto;[^}]*width:\s*var\(--ds-window-controls-width\);/s,
);
assert.match(
stylesSource,
/\.window-controls\s*\{[^}]*height:\s*var\(--ds-toolbar-height\)[^}]*padding-left:\s*8px;[^}]*background:\s*var\(--ds-bg-primary\);/s,
);
// D297: no side seam between the control band and the titlebar.
assert.doesNotMatch(stylesSource, /\.window-controls\s*\{[^}]*border-left/s);
assert.doesNotMatch(
stylesSource,
/\.window-controls\s*\{[^}]*border-bottom:/s,
);
assert.match(stylesSource, /--ds-window-controls-width:\s*120px;/);
assert.match(
stylesSource,
/:root\[data-platform="win32"\] \.main-titlebar,\s*:root\[data-platform="linux"\] \.main-titlebar\s*\{[^}]*right:\s*var\(--ds-window-controls-width\);/,
);
assert.match(
stylesSource,
/:root\[data-platform="win32"\] \.main-titlebar\.work-panel-open,[\s\S]*:root\[data-platform="linux"\] \.main-titlebar\.work-panel-open\s*\{[^}]*right:\s*0;/,
);
// The base header rule stays platform-neutral; the win32/linux reservation
// ends the header's *box*, so its native drag rectangle stops before the
// control band instead of covering the window controls.
assert.doesNotMatch(
stylesSource,
/^\.work-panel-header\s*\{[^}]*margin-right:/ms,
"the base header rule stays platform-neutral",
);
assert.match(
stylesSource,
/:root\[data-platform="win32"\] \.work-panel-header,[\s\S]*:root\[data-platform="linux"\] \.work-panel-header\s*\{[^}]*margin-right:\s*var\(--ds-window-controls-width\);/,
);
assert.doesNotMatch(
stylesSource,
/padding-right:\s*calc\(var\(--ds-window-controls-width\)/,
"padding does not exclude an Electron draggable region",
);
assert.match(
stylesSource,
/:root\[data-platform="(win32|linux)"\] \.thread-content[\s\S]*?padding-top:\s*var\(--ds-toolbar-height\);/,
);
assert.match(
stylesSource,
/\.toast\s*\{[^}]*-webkit-app-region:\s*no-drag;[^}]*pointer-events:\s*auto;/s,
);
assert.match(mainSource, /installWindowStateSync\(window, sendMaximized, windowBoundsSettleMs\)/);
assert.match(
mainSource,
/window\.on\("unmaximize", \(\) => \{\s*sendMaximized\(\);[\s\S]*scheduleWorkPanelReservation\(\)/,
);
assert.match(
mainSource,
/window\.on\("leave-full-screen", \(\) => \{[\s\S]*scheduleWorkPanelReservation\(\)/,
);
assert.match(mainSource, /window\.webContents\.isDestroyed\(\)/);
assert.match(mainSource, /mainWindow = null/);
assert.match(mainSource, /windowCreationPromise/);
assert.match(mainSource, /pendingApplicationMenuCommands/);
assert.match(protocolSource, /menuRendererReady:\s*"pi-desktop\/menu\/rendererReady"/);
assert.match(mainSource, /waitForMenuRenderer\(window\)/);
assert.doesNotMatch(
mainSource.slice(
mainSource.indexOf("async function deliverApplicationMenuCommand"),
mainSource.indexOf("function dispatchApplicationMenuCommand"),
),
/setTimeout/,
);
assert.match(
mainSource,
/PI_DESKTOP_START_MAXIMIZED[\s\S]*window\.maximize\(\)/,
);
});
test("the work-panel tab menu is a validated native popup", () => {
assert.match(protocolSource, /workPanelTabMenu:\s*"pi-desktop\/workPanel\/tabMenu"/);
const start = windowIpcSource.indexOf("handleWithEvent(IPC.invoke.workPanelTabMenu");
const end = windowIpcSource.indexOf("\n });", start);
const handler = windowIpcSource.slice(start, end);
assert.ok(start >= 0 && end > start, "native tab-menu IPC handler is registered");
assert.match(handler, /registrar\.assertMainWindowSender\(event\)/);
assert.match(handler, /parseWorkPanelTabMenuRequest\(input\)/);
assert.match(handler, /Menu\.buildFromTemplate/);
assert.match(handler, /enabled:\s*request\.canCloseOthers/);
assert.match(handler, /event\.sender\.getZoomFactor\(\)/);
assert.match(handler, /waitForNativeMenuResult\(/);
assert.match(handler, /menu\.popup\(\{ window, x, y, callback:/);
});
test("menu and window IPC reject actions outside their shared allowlists", () => {
assert.match(protocolSource, /export const APP_MENU_COMMANDS/);
assert.match(protocolSource, /export const NATIVE_MENU_ACTIONS/);
assert.match(protocolSource, /export const WINDOW_CONTROL_ACTIONS/);
assert.match(protocolSource, /nativeMenuAction:\s*"pi-desktop\/menu\/nativeAction"/);
assert.match(protocolSource, /menuCommand:\s*"pi-desktop\/menu\/event\/command"/);
assert.match(mainSource, /NATIVE_MENU_ACTIONS\.includes/);
assert.match(mainSource, /WINDOW_CONTROL_ACTIONS\.includes/);
assert.match(mainSource, /throw new Error\("unsupported native menu action"\)/);
assert.match(mainSource, /throw new Error\("unsupported window control action"\)/);
const windowControlBlock = mainSource.slice(
mainSource.indexOf("IPC.invoke.windowControl"),
mainSource.indexOf("IPC.invoke.nativeMenuAction"),
);
assert.ok(
windowControlBlock.indexOf("WINDOW_CONTROL_ACTIONS.includes") <
windowControlBlock.indexOf("!mainWindow"),
"window actions must be validated before the no-window return",
);
});
test("Windows/Linux explicit minimize paths use the native taskbar", () => {
assert.match(mainSource, /import \{[\s\S]*Tray[\s\S]*\} from "electron"/);
assert.match(mainSource, /function createTray\(\)/);
assert.match(mainSource, /join\(resourceRoot, "tray-icon-mac\.png"\)/);
assert.match(mainSource, /icon\.setTemplateImage\(true\)/);
assert.match(mainSource, /tray\.on\("click", restoreMainWindow\)/);
assert.match(mainSource, /tray\.on\("double-click", restoreMainWindow\)/);
assert.match(
mainSource,
/window\.on\("minimize", \(\) => \{[\s\S]*process\.platform !== "darwin"\) return;[\s\S]*window\.hide\(\)/,
);
assert.match(mainSource, /tray\?\.destroy\(\)/);
assert.match(mainSource, /case "minimize":\s*target\.minimize\(\)/);
const nativeActionBlock = mainSource.slice(
mainSource.indexOf("function executeNativeMenuAction"),
mainSource.indexOf("function dispatchNativeMenuAction"),
);
assert.match(nativeActionBlock, /case "minimize":\s*target\.minimize\(\)/);
const trayResource = packageJson.build.extraResources.find(
(resource) => resource.to === "tray-icon.png",
);
assert.deepEqual(trayResource, {
from: "build/icon.png",
to: "tray-icon.png",
});
assert.deepEqual(packageJson.build.mac.extraResources, [
{
from: "../../target/release/pi-desktop-host-core",
to: "bin/pi-desktop-host-core",
},
{
from: "build/tray-icon-mac.png",
to: "tray-icon-mac.png",
},
]);
assert.match(iconScriptSource, /tray-icon-mac\.png/);
assert.match(iconScriptSource, /ImageChops\.multiply/);
});
test("Windows taskbar minimize keeps the taskbar entry", () => {
const minimizeHandler = mainSource.slice(
mainSource.indexOf('window.on("minimize"'),
mainSource.indexOf('window.on("minimize"') + 420,
);
assert.match(
minimizeHandler,
/if \(windowState\.quitting \|\| !windowState\.tray \|\| process\.platform !== "darwin"\) return;/,
);
assert.match(
mainSource,
/function restoreMainWindow\(\)[\s\S]*if \(window\.isMinimized\(\)\) window\.restore\(\);[\s\S]*window\.focus\(\);/,
);
assert.match(
mainSource,
/IPC\.invoke\.windowControl[\s\S]*case "minimize":\s*window\.minimize\(\)/,
);
});
test("macOS activation resurfaces a tray-hidden window", () => {
assert.match(
mainSource,
/registerApplicationActivation\(\{[\s\S]*restoreMainWindow,/,
);
assert.match(activationSource, /app\.on\("activate", restoreMainWindow\)/);
assert.match(
activationSource,
/app\.on\("did-become-active", \(\) => \{[\s\S]*restoreMainWindow\(\);/,
);
assert.match(
activationSource,
/if\s*\(\s*isQuitting\(\)\s*\|\|\s*!isApplicationBooted\(\)\s*\|\|\s*hasVisibleWindow\(\)\s*\)\s*return;/,
);
assert.match(
mainSource,
/function hasVisibleWindow\(\): boolean \{[\s\S]*BrowserWindow\.getAllWindows\(\)[\s\S]*window\.isVisible\(\)/,
);
});
test("desktop packaging builds the native host before every local target", () => {
assert.match(
packageJson.scripts["build:host-release"],
/cargo build --release .* -p host-core/,
);
for (const name of ["pack", "dist", "dist:mac", "dist:win", "dist:linux"]) {
const script = packageJson.scripts[name];
assert.match(script, /pnpm run build:host-release/);
const packagingCommand = script.includes("build-desktop-release.mjs")
? "build-desktop-release.mjs"
: "electron-builder";
assert.ok(
script.indexOf("pnpm run build:host-release") < script.indexOf(packagingCommand),
`${name} must build the native host before the packaging command`,
);
}
assert.ok(
packageJson.build.win.extraResources.some(
({ to }) => to === "bin/pi-desktop-host-core.exe",
),
);
assert.equal(packageJson.build.linux.extraResources[0].to, "bin/pi-desktop-host-core");
assert.equal(packageJson.build.mac.extraResources[0].to, "bin/pi-desktop-host-core");
assert.match(iconScriptSource, /package_icon = BUILD \/ "icon\.png"/);
assert.match(iconScriptSource, /shutil\.which\("iconutil"\)/);
});