A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(daemon): put the bundled pi on the engine's PATH so first-run setup works on clean machines
Alan Buscaglia15afaa2b8e06
diff --git a/packages/daemon/src/isolation.ts b/packages/daemon/src/isolation.tsindex 8e41b24..eaa8bb8 100644--- a/packages/daemon/src/isolation.ts+++ b/packages/daemon/src/isolation.ts@@ -1,7 +1,21 @@import { execFile } from "node:child_process";import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";+import { createRequire } from "node:module";import { homedir } from "node:os";-import { join } from "node:path";+import { delimiter, join } from "node:path";++/**+ * The folder with the `pi` executable bundled with the daemon. The engine's+ * first-run setup needs `pi` on PATH, and a clean machine has none.+ */+function bundledBinDir(): string | undefined {+ const require = createRequire(import.meta.url);+ for (const dir of require.resolve.paths("@earendil-works/pi-coding-agent") ?? []) {+ const bin = join(dir, ".bin");+ if (existsSync(join(bin, "pi"))) return bin;+ }+ return undefined;+}/** Creates `path` with mode 0700, and tightens it to 0700 when it already exists. */export function ensurePrivateDir(path: string): void {@@ -33,7 +47,7 @@ export function ensureMemoryProject(workspace: string): void {/*** The engine's environment: its own HOME (`<dataDir>/home`, mode 0700) with* the XDG folders under it, because parts of the engine write to fixed places- * under the home folder. PATH is kept, and so is the user's git identity,+ * under the home folder. PATH is kept, with the bundled `pi` folder first, and so is the user's git identity,* through `GIT_CONFIG_GLOBAL` pointing at their real `~/.gitconfig`.** Memory is the user's global Engram: Engram ties a server to its data folder@@ -57,6 +71,11 @@ export function isolatedAgentEnv(base: NodeJS.ProcessEnv, dataDir: string): NodeXDG_STATE_HOME: join(home, ".local", "state"),};for (const key of INHERITED_HOMES) delete env[key];+ const bin = bundledBinDir();+ if (bin) {+ const rest = (base.PATH ?? "").split(delimiter).filter((part) => part && part !== bin);+ env.PATH = [bin, ...rest].join(delimiter);+ }// Subagent child engines do not load the approval guard, so subagents stay off until the// daemon enforces connectors itself (S25).env.GENTLE_PI_AGENTS = "0";diff --git a/packages/daemon/test/gateway.test.ts b/packages/daemon/test/gateway.test.tsindex 7cd507d..67535e4 100644--- a/packages/daemon/test/gateway.test.ts+++ b/packages/daemon/test/gateway.test.ts@@ -1,5 +1,5 @@import { chmodSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";-import { join } from "node:path";+import { delimiter, join } from "node:path";import type { ServerMessage } from "@gentle-dot/protocol";import { afterEach, describe, expect, it, vi } from "vitest";import WebSocket from "ws";@@ -459,6 +459,10 @@ describe("gateway", () => {});});+function escapeRegExp(text: string): string {+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+}+describe("own instance", () => {it("runs the agent with the assistant's own home and profile store", async () => {const scratch = tempDir();@@ -523,7 +527,10 @@ describe("own instance", () => {XDG_CACHE_HOME: join(home, ".cache"),XDG_STATE_HOME: join(home, ".local", "state"),GIT_CONFIG_GLOBAL: join(realHome, ".gitconfig"),- PATH: process.env.PATH,+ // The bundled `pi` folder first, then the user's PATH unchanged.+ PATH: expect.stringMatching(+ new RegExp(`node_modules/\\.bin${delimiter}${escapeRegExp(process.env.PATH ?? "")}$`),+ ),// The user's global Engram: its data dir, so the memory plugin accepts the user's server.ENGRAM_DATA_DIR: join(realHome, ".engram"),ENGRAM_URL: "http://127.0.0.1:7437",diff --git a/packages/daemon/test/isolation.test.ts b/packages/daemon/test/isolation.test.tsindex 081f99a..23ad12b 100644--- a/packages/daemon/test/isolation.test.ts+++ b/packages/daemon/test/isolation.test.ts@@ -15,6 +15,22 @@ afterEach(async () => {await Promise.all(daemons.splice(0).map((d) => d.close()));});+describe("isolatedAgentEnv PATH", () => {+ it("puts the bundled `pi` first so the engine's first-run setup works on a clean machine", () => {+ const env = isolatedAgentEnv({ PATH: "/usr/bin:/bin", HOME: tempDir() }, tempDir());+ const [first, ...rest] = (env.PATH ?? "").split(":");+ expect(first).toMatch(/node_modules\/\.bin$/);+ expect(existsSync(join(first ?? "", "pi"))).toBe(true);+ expect(rest).toEqual(["/usr/bin", "/bin"]);+ });++ it("does not add the bundled folder twice", () => {+ const once = isolatedAgentEnv({ PATH: "/bin", HOME: tempDir() }, tempDir());+ const twice = isolatedAgentEnv({ PATH: once.PATH, HOME: tempDir() }, tempDir());+ expect(twice.PATH).toBe(once.PATH);+ });+});+describe("isolatedAgentEnv memory", () => {it("points the engine's memory at the user's global Engram data and server", () => {const realHome = tempDir();