Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(desktop): keep main/index.ts under the 1500 LOC budget
vastsa03c19feb87bd
diff --git a/apps/desktop/electron/main/data-paths.ts b/apps/desktop/electron/main/data-paths.tsindex cbcbcda7d..c79c3c83a 100644--- a/apps/desktop/electron/main/data-paths.ts+++ b/apps/desktop/electron/main/data-paths.ts@@ -87,3 +87,28 @@ export function desktopDataDir(home: homedir(),});}++/** The Electron `app` surface this helper needs; kept structural so tests need no Electron. */+export type UserDataApp = {+ commandLine: { hasSwitch(name: string): boolean };+ getPath(name: "appData"): string;+ setPath(name: "userData", path: string): void;+};++/**+ * Give a development build its own `userData` so the single-instance lock+ * does not collide with a shipped app that is already running (D236, ADR 0094).+ * An explicit `--user-data-dir` wins, because that is how the E2E harnesses+ * point a build at a throwaway profile.+ */+export function applyDevelopmentUserData(+ app: UserDataApp,+ development: boolean,+): void {+ if (development && !app.commandLine.hasSwitch("user-data-dir")) {+ app.setPath(+ "userData",+ join(app.getPath("appData"), DEVELOPMENT_INSTALLATION_NAME),+ );+ }+}diff --git a/apps/desktop/electron/main/index.ts b/apps/desktop/electron/main/index.tsindex beba179fc..60b355588 100644--- a/apps/desktop/electron/main/index.ts+++ b/apps/desktop/electron/main/index.ts@@ -93,10 +93,7 @@ import {planExecutionFromUnknown,} from "@pi-desktop/host-runtime";import { readWindowState, writeWindowState } from "./window-preferences";-import {- desktopDataDir,- DEVELOPMENT_INSTALLATION_NAME,-} from "./data-paths";+import { applyDevelopmentUserData, desktopDataDir } from "./data-paths";import { createPlanUiProbe } from "./plan-ui-probe";import type { McpControlController, McpControlServer } from "./mcp-control";import type { AgentHostBridge } from "./agent-host-bridge";@@ -178,39 +175,19 @@ const ErrorCodes = {ignoreBrokenStdio();installMainProcessErrorHandlers();-// A development build is a second installation beside the shipped app, so it-// takes its own `userData`: the single-instance lock below lives there, and a-// shipped app that is already running must not refuse the developer the lock-// (D236, ADR 0094). An explicit `--user-data-dir` wins, because that is how the-// E2E harnesses point a build at a throwaway profile — overriding it would run-// their assertions against the developer's own state instead.const isDevelopmentBuild =process.env.PI_DESKTOP_DEV === "1" || !app.isPackaged;app.setName(APP_NAME);-if (isDevelopmentBuild && !app.commandLine.hasSwitch("user-data-dir")) {- app.setPath(- "userData",- join(app.getPath("appData"), DEVELOPMENT_INSTALLATION_NAME),- );-}+applyDevelopmentUserData(app, isDevelopmentBuild);if (process.platform === "win32") {app.setAppUserModelId(APP_ID);}-// One installation admits exactly one desktop process. host-core owns-// `pi.sqlite` exclusively (D002), Electron main owns the persistence outbox and-// the log tree beside it, and the tray, the global launcher shortcut, and the-// updater are singletons of the running app — a second process fights the first-// for every one of them and leaves the user with two shells over one database.-//-// Electron keeps the lock in `userData`, which the name and profile set just-// above decide, so it is taken after `setName` and before anything else in this-// module touches the data directory. That scope is the installation, not-// `PI_DESKTOP_DATA_DIR`: a development build is its own installation, and a run-// pointed at its own data directory (E2E harnesses, the capture rig, a-// side-by-side profile) shares no state with the shipped one either. Both stay-// launchable while the other is running.+// One installation, one process. The lock lives in `userData` (set just+// above), so it is taken after `setName` and before anything else here+// touches the data directory. A development build is its own installation;+// `PI_DESKTOP_DATA_DIR` still opts a run out of the lock (E2E, capture rig).const singleInstanceRequired = !process.env.PI_DESKTOP_DATA_DIR;const hasSingleInstanceLock = singleInstanceRequired? app.requestSingleInstanceLock()diff --git a/apps/desktop/test/development-profile.test.mjs b/apps/desktop/test/development-profile.test.mjsindex e168576de..4e2bdd337 100644--- a/apps/desktop/test/development-profile.test.mjs+++ b/apps/desktop/test/development-profile.test.mjs@@ -10,6 +10,7 @@ const here = dirname(fileURLToPath(import.meta.url));register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));const {+ applyDevelopmentUserData,DEVELOPMENT_DATA_DIR_NAME,DEVELOPMENT_INSTALLATION_NAME,INSTALLATION_DATA_DIR_NAME,@@ -71,27 +72,51 @@ test("an explicit data directory reaches the child processes as an absolute path);});-test("a development build takes its own userData before the single-instance lock", () => {+test("applyDevelopmentUserData sets userData unless --user-data-dir is set", () => {+ const calls = [];+ const app = {+ commandLine: { hasSwitch: () => false },+ getPath: () => "/tmp/appData",+ setPath: (name, path) => calls.push([name, path]),+ };+ applyDevelopmentUserData(app, true);+ assert.deepEqual(calls, [+ ["userData", join("/tmp/appData", DEVELOPMENT_INSTALLATION_NAME)],+ ]);+ calls.length = 0;+ applyDevelopmentUserData(app, false);+ assert.equal(calls.length, 0);+ applyDevelopmentUserData(+ { ...app, commandLine: { hasSwitch: (name) => name === "user-data-dir" } },+ true,+ );+ assert.equal(calls.length, 0);+});++test("a development build takes its own userData before the single-instance lock", async () => {// The lock lives under `userData`, so the profile has to be applied before// Electron asks for it; otherwise a running packaged app refuses the lock and// `pnpm dev` quits on arrival.- const setUserData = indexSource.search(- /app\.setPath\(\s*"userData",\s*join\(app\.getPath\("appData"\), DEVELOPMENT_INSTALLATION_NAME\)/,- );+ const pathsSource = await readMainModule("data-paths.ts");+ const apply = indexSource.indexOf("applyDevelopmentUserData(app, isDevelopmentBuild)");const setName = indexSource.indexOf("app.setName(APP_NAME)");const lock = indexSource.indexOf("app.requestSingleInstanceLock()");- assert.ok(setUserData > 0, "main must give the development build its own userData");+ assert.ok(apply > 0, "main must give the development build its own userData");assert.ok(lock > 0, "main must request the single-instance lock");- assert.ok(setName > 0 && setName < setUserData);- assert.ok(setUserData < lock);+ assert.ok(setName > 0 && setName < apply);+ assert.ok(apply < lock);// An explicit `--user-data-dir` wins. The E2E harnesses point a build at a// throwaway profile with that switch, so overriding it would run their// assertions against the developer's own state instead.assert.match(- indexSource,- /if \(isDevelopmentBuild && !app\.commandLine\.hasSwitch\("user-data-dir"\)\) \{/,+ pathsSource,+ /if \(development && !app\.commandLine\.hasSwitch\("user-data-dir"\)\) \{/,+ );+ assert.match(+ pathsSource,+ /app\.setPath\(\s*"userData",\s*join\(app\.getPath\("appData"\), DEVELOPMENT_INSTALLATION_NAME\)/,);// The two profiles are told apart by the same verdict everywhere, and it is@@ -99,7 +124,7 @@ test("a development build takes its own userData before the single-instance lockconst development = indexSource.search(/const isDevelopmentBuild =\s*\n?\s*process\.env\.PI_DESKTOP_DEV === "1" \|\| !app\.isPackaged;/,);- assert.ok(development > 0 && development < setUserData);+ assert.ok(development > 0 && development < apply);});test("main resolves one data directory and publishes it to everything below", () => {