A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(daemon): keep checking connector files when the child guard cannot be written
Alan Buscagliab6e235dad2a9
diff --git a/packages/daemon/src/connectors.ts b/packages/daemon/src/connectors.tsindex bb91ffc..d532628 100644--- a/packages/daemon/src/connectors.ts+++ b/packages/daemon/src/connectors.ts@@ -1449,25 +1449,33 @@ export class ConnectorStore {/*** Writes the child guard's file when it differs or is missing; true when it did. An extensions* folder that is a link loses the link, never what it points to. When the folder or the file's- * place holds something else, that is reported once and left alone, and the engine starts with- * subagents off ({@link childGuardReady}).+ * place holds something else, or it cannot be written, that is reported once and left alone, and+ * the engine starts with subagents off ({@link childGuardReady}); the other connector files are+ * still checked.*/private enforceChildGuard(): boolean {if (!this.childGuard || this.childGuardReady()) return false;const { file, text } = this.childGuard;const folder = dirname(file);- if (lstatOrUndefined(folder)?.isSymbolicLink()) unlinkSync(folder);- const inFolder = lstatOrUndefined(folder);- const inPlace = inFolder && lstatOrUndefined(file);- if ((inFolder && !inFolder.isDirectory()) || inPlace?.isDirectory()) {+ const cannot = (why?: unknown) => {if (!this.childGuardReported)- this.options.log?.(`the subagents' approval guard cannot be written to ${file}`);+ this.options.log?.(+ `the subagents' approval guard cannot be written to ${file}${why instanceof Error ? `: ${why.message}` : ""}`,+ );this.childGuardReported = true;return false;+ };+ try {+ if (lstatOrUndefined(folder)?.isSymbolicLink()) unlinkSync(folder);+ const inFolder = lstatOrUndefined(folder);+ const inPlace = inFolder && lstatOrUndefined(file);+ if ((inFolder && !inFolder.isDirectory()) || inPlace?.isDirectory()) return cannot();+ mkdirSync(folder, { recursive: true, mode: 0o700 });+ writePrivate(file, text);+ } catch (error) {+ return cannot(error);}this.childGuardReported = false;- mkdirSync(folder, { recursive: true, mode: 0o700 });- writePrivate(file, text);return true;}diff --git a/packages/daemon/test/child-guard.test.ts b/packages/daemon/test/child-guard.test.tsindex a3cfa00..ca86c82 100644--- a/packages/daemon/test/child-guard.test.ts+++ b/packages/daemon/test/child-guard.test.ts@@ -4,7 +4,16 @@// (it loads the guard with `-e`) or in any engine Gentle Dot did not start, and the daemon writes it// at every engine start and puts it back when it changes. The proxy and the native approvals stay// the boundary; the guard in children is defense in depth.-import { existsSync, lstatSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";+import {+ chmodSync,+ existsSync,+ lstatSync,+ mkdirSync,+ readFileSync,+ rmSync,+ symlinkSync,+ writeFileSync,+} from "node:fs";import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";import { APPROVAL_GUARD } from "../src/connectors.ts";@@ -157,4 +166,42 @@ describe("subagents in the assistant's engine (S25.4)", () => {expect(env().GENTLE_PI_AGENTS).toBe("0");expect(lines.some((line) => /subagents are off/i.test(line))).toBe(true);});++ it("starts with subagents off when the extensions folder cannot be written, on or off", async () => {+ for (const env of [{}, { GENTLE_DOT_SUBAGENTS: "off" }]) {+ let folder = "";+ try {+ const started = await start({+ env,+ before: (agentHome) => {+ folder = join(agentHome, "extensions");+ mkdirSync(folder, { recursive: true });+ chmodSync(folder, 0o555);+ },+ });+ expect(started.env().GENTLE_PI_AGENTS).toBe("0");+ expect(started.lines.filter((line) => line.includes("cannot be written")).length).toBe(1);+ } finally {+ if (folder) chmodSync(folder, 0o755);+ }+ }+ });++ it("still removes a planted .pi/mcp.json when the child guard cannot be put back, and says so once", async () => {+ const { dataDir, agentHome, shim, lines } = await start();+ const folder = join(agentHome, "extensions");+ const planted = join(dataDir, "workspace", ".pi", "mcp.json");+ try {+ rmSync(shim);+ chmodSync(folder, 0o555);+ mkdirSync(join(dataDir, "workspace", ".pi"), { recursive: true });+ writeFileSync(planted, '{"mcpServers":{}}\n');+ await waitFor(() => !existsSync(planted), 5000);+ await new Promise((resolve) => setTimeout(resolve, 2500));+ expect(lines.filter((line) => line.includes("cannot be written")).length).toBe(1);+ expect(lines.some((line) => line.includes("could not check"))).toBe(false);+ } finally {+ chmodSync(folder, 0o755);+ }+ });});