Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(host-runtime): disable fsmonitor for workspace diffs

vastsa381588437dad
diff --git a/docs/spec/03-runtime/03-tools-and-permissions.md b/docs/spec/03-runtime/03-tools-and-permissions.md
index 0d6594981..4136803ce 100644
--- a/docs/spec/03-runtime/03-tools-and-permissions.md
+++ b/docs/spec/03-runtime/03-tools-and-permissions.md
@@ -396,6 +396,12 @@ project changes are not copied; git-ignored files listed in
and is never used as a tool root, merged, or synced, and deleting the
session removes only the link, never its target.
+The host's workspace diff collector applies the same Git hardening to every
+repository it inspects, including non-isolated workspaces. Each host-started
+Git command disables repository-configured hooks and `core.fsmonitor`, and
+removes environment variables that can redirect Git to another repository or
+inject command-scope configuration.
+
Merging into the project is an explicit user action (preview, then merge with
the preview token); it syncs the project branch into the worktree and only
fast-forwards the project, so conflicts stay in the worktree. Deleting an
diff --git a/docs/zh-CN/spec/03-runtime/03-tools-and-permissions.md b/docs/zh-CN/spec/03-runtime/03-tools-and-permissions.md
index 83a48b2f4..8a42d26c4 100644
--- a/docs/zh-CN/spec/03-runtime/03-tools-and-permissions.md
+++ b/docs/zh-CN/spec/03-runtime/03-tools-and-permissions.md
@@ -327,6 +327,9 @@ type ReviewChange = {
Windows junction,即视为缺失(`WORKTREE_MISSING`),不会被用作工具根目录、合并或同步;删除会话时
只删除链接本身,绝不删除其目标。
+宿主的工作区差异收集器会对其检查的所有仓库应用相同的 Git 加固措施,包括非隔离工作区。每次由宿主启动
+Git 命令都会禁用仓库配置的 hooks 和 `core.fsmonitor`,并移除可将 Git 重定向到其他仓库或注入命令级配置的环境变量。
+
合并到项目是显式的用户操作(先预览,再携带预览令牌合并):先把项目分支同步进
worktree,项目只做快进,因此冲突留在 worktree 中。删除隔离会话即丢弃其 worktree
和分支。
diff --git a/packages/host-runtime/src/workspace-diff.test.ts b/packages/host-runtime/src/workspace-diff.test.ts
new file mode 100644
index 000000000..e2ccf612b
--- /dev/null
+++ b/packages/host-runtime/src/workspace-diff.test.ts
@@ -0,0 +1,89 @@
+import { execFile as execFileCallback } from "node:child_process";
+import { existsSync } from "node:fs";
+import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { promisify } from "node:util";
+import { afterEach, describe, expect, it } from "vitest";
+import { collectWorkspaceDiff } from "./workspace-diff.js";
+
+const execFile = promisify(execFileCallback);
+const tempDirs: string[] = [];
+
+function shellQuote(value: string): string {
+ if (process.platform === "win32") {
+ return `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
+ }
+ return `'${value.replaceAll("'", "'\\''")}'`;
+}
+
+async function runGit(cwd: string, args: string[]): Promise<void> {
+ await execFile("git", args, { cwd, encoding: "utf8" });
+}
+
+afterEach(async () => {
+ await Promise.all(
+ tempDirs.splice(0).map((path) => rm(path, { recursive: true, force: true })),
+ );
+});
+
+describe("collectWorkspaceDiff", () => {
+ it("does not execute a repository-configured fsmonitor command", async () => {
+ const tempRoot = await mkdtemp(join(tmpdir(), "pi-workspace-diff-fsmonitor-"));
+ tempDirs.push(tempRoot);
+ const root = join(tempRoot, "repo");
+ await mkdir(root);
+
+ await runGit(root, ["init", "--quiet"]);
+ await runGit(root, ["config", "user.name", "Workspace diff test"]);
+ await runGit(root, ["config", "user.email", "[email protected]"]);
+ await writeFile(join(root, "tracked.txt"), "content\n", "utf8");
+ await runGit(root, ["add", "tracked.txt"]);
+ await runGit(root, ["commit", "--quiet", "-m", "initial"]);
+
+ const marker = join(tempRoot, "fsmonitor-ran");
+ const hook = join(tempRoot, "fsmonitor-hook.cjs");
+ await writeFile(
+ hook,
+ `require("node:fs").writeFileSync(${JSON.stringify(marker)}, "executed");\n`,
+ "utf8",
+ );
+ const command = `${shellQuote(process.execPath)} ${shellQuote(hook)}`;
+ await runGit(root, ["config", "core.fsmonitor", command]);
+
+ // Prove this Git build would execute the configured command without the
+ // host runner's overrides, then clear the marker before testing the host.
+ // Git may reject the helper's intentionally incomplete response afterward.
+ await execFile("git", ["status", "--porcelain=v1", "-z"], {
+ cwd: root,
+ encoding: "utf8",
+ }).catch(() => undefined);
+ expect(existsSync(marker)).toBe(true);
+ await rm(marker, { force: true });
+
+ const diff = await collectWorkspaceDiff(root);
+
+ expect(diff).toMatchObject({ repo: true, clean: true, files: [] });
+ expect(existsSync(marker)).toBe(false);
+
+ await runGit(root, ["config", "--unset", "core.fsmonitor"]);
+ const injectedConfig = [
+ ["GIT_CONFIG_COUNT", process.env.GIT_CONFIG_COUNT],
+ ["GIT_CONFIG_KEY_0", process.env.GIT_CONFIG_KEY_0],
+ ["GIT_CONFIG_VALUE_0", process.env.GIT_CONFIG_VALUE_0],
+ ] as const;
+ process.env.GIT_CONFIG_COUNT = "1";
+ process.env.GIT_CONFIG_KEY_0 = "core.fsmonitor";
+ process.env.GIT_CONFIG_VALUE_0 = command;
+ try {
+ const envDiff = await collectWorkspaceDiff(root);
+ expect(envDiff).toMatchObject({ repo: true, clean: true, files: [] });
+ expect(existsSync(marker)).toBe(false);
+ } finally {
+ for (const [key, value] of injectedConfig) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+ });
+});
diff --git a/packages/host-runtime/src/workspace-diff.ts b/packages/host-runtime/src/workspace-diff.ts
index b3b2d089c..cc8a1a3ed 100644
--- a/packages/host-runtime/src/workspace-diff.ts
+++ b/packages/host-runtime/src/workspace-diff.ts
@@ -20,9 +20,40 @@ export const MAX_PATCH_BYTES = 200 * 1024;
type RunResult = { code: number; stdout: string; stderr: string };
+const NULL_HOOKS_PATH = process.platform === "win32" ? "NUL" : "/dev/null";
+
+const REDIRECT_ENV = [
+ "GIT_DIR",
+ "GIT_WORK_TREE",
+ "GIT_INDEX_FILE",
+ "GIT_COMMON_DIR",
+ "GIT_OBJECT_DIRECTORY",
+ "GIT_ALTERNATE_OBJECT_DIRECTORIES",
+ "GIT_NAMESPACE",
+ "GIT_CEILING_DIRECTORIES",
+ "GIT_CONFIG_PARAMETERS",
+ "GIT_CONFIG_COUNT",
+] as const;
+
function runGit(cwd: string, args: string[]): Promise<RunResult> {
return new Promise((resolve) => {
- const child = spawn("git", args, { cwd, env: process.env });
+ const env: NodeJS.ProcessEnv = {
+ ...process.env,
+ GIT_OPTIONAL_LOCKS: "0",
+ };
+ for (const name of REDIRECT_ENV) delete env[name];
+
+ const child = spawn(
+ "git",
+ [
+ "-c",
+ "core.fsmonitor=false",
+ "-c",
+ `core.hooksPath=${NULL_HOOKS_PATH}`,
+ ...args,
+ ],
+ { cwd, env },
+ );
let stdout = "";
let stderr = "";
child.stdout.on("data", (d) => (stdout += String(d)));