Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(daemon): give the engine its own AppData on Windows

Marcelitof034c01a6571
diff --git a/packages/daemon/src/isolation.ts b/packages/daemon/src/isolation.ts
index 7bac47d..332a588 100644
--- a/packages/daemon/src/isolation.ts
+++ b/packages/daemon/src/isolation.ts
@@ -124,8 +124,8 @@ export function ensureMemoryProject(workspace: string, access: EngineAccess = di
/**
* 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 (on Windows as one `PATH` key, whatever its spelling), with the bundled `pi` folder first, and so is the user's git identity,
+ * the XDG folders under it (on Windows `USERPROFILE`, `APPDATA` and `LOCALAPPDATA`), because
+ * parts of the engine write to fixed places under the home folder. PATH is kept (on Windows as one `PATH` key, whatever its spelling), 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
@@ -168,7 +168,14 @@ export function isolatedAgentEnv(
const tmp = join(home, ".cache", "tmp");
env.USERPROFILE = home;
env.TEMP = env.TMP = env.TMPDIR = tmp;
- access(() => ensurePrivateDir(tmp));
+ // Windows' counterparts of the XDG folders, so the engine's tools keep no state in the user's.
+ env.APPDATA = join(home, "AppData", "Roaming");
+ env.LOCALAPPDATA = join(home, "AppData", "Local");
+ access(() => {
+ ensurePrivateDir(tmp);
+ ensurePrivateDir(env.APPDATA as string);
+ ensurePrivateDir(env.LOCALAPPDATA as string);
+ });
} else if (!base.TMPDIR) {
const tmp = join(home, ".cache", "tmp");
env.TMPDIR = tmp;
diff --git a/packages/daemon/test/isolation.test.ts b/packages/daemon/test/isolation.test.ts
index f217809..570eb91 100644
--- a/packages/daemon/test/isolation.test.ts
+++ b/packages/daemon/test/isolation.test.ts
@@ -43,6 +43,23 @@ describe("Windows isolation", () => {
expect(env.ENGRAM_DATA_DIR).toBe(join(realHome, ".engram"));
});
+ it("gives the engine its own AppData on Windows, as XDG on Unix, and leaves Unix AppData alone", () => {
+ const data = tempDir();
+ const user = {
+ USERPROFILE: tempDir(),
+ APPDATA: "C:\\Users\\me\\AppData\\Roaming",
+ LOCALAPPDATA: "C:\\Users\\me\\AppData\\Local",
+ };
+ const env = isolatedAgentEnv(user, data, undefined, { platform: "win32" });
+ expect(env.APPDATA).toBe(join(data, "home", "AppData", "Roaming"));
+ expect(env.LOCALAPPDATA).toBe(join(data, "home", "AppData", "Local"));
+ expect(existsSync(env.APPDATA as string)).toBe(true);
+ expect(existsSync(env.LOCALAPPDATA as string)).toBe(true);
+ const unix = isolatedAgentEnv({ ...user, HOME: tempDir() }, tempDir(), undefined, { platform: "linux" });
+ expect(unix.APPDATA).toBe(user.APPDATA);
+ expect(unix.LOCALAPPDATA).toBe(user.LOCALAPPDATA);
+ });
+
it("recognizes a source pi.cmd without a POSIX shim", () => {
const bin = tempDir();
writeFileSync(join(bin, "pi.cmd"), "@echo off\r\n");