Disable checkout-selected filesystem monitors for CLI Git commands

This commit is contained in:
Michael D'Angelo
2026-11-04 17:06:50 +00:00
parent 72e3ccf711
commit 3dc0ef74d1
2 changed files with 70 additions and 63 deletions
+15 -17
View File
@@ -2459,23 +2459,21 @@ export async function main(
};
options.signal?.throwIfAborted();
if (command === "git") {
// Git index reads can launch a repository's fsmonitor outside the patch sandbox.
if (argv[0] === "patch") {
const count = Number(
options.environment?.["GIT_CONFIG_COUNT"] ??
environmentEntry(
parentDependencies.environment,
"GIT_CONFIG_COUNT",
) ??
0,
);
options.environment = {
...options.environment,
GIT_CONFIG_COUNT: String(count + 1),
[`GIT_CONFIG_KEY_${count}`]: "core.fsmonitor",
[`GIT_CONFIG_VALUE_${count}`]: "false",
};
}
// Any Git index read can run a checkout-selected fsmonitor as the CLI user.
const count = Number(
options.environment?.["GIT_CONFIG_COUNT"] ??
environmentEntry(
parentDependencies.environment,
"GIT_CONFIG_COUNT",
) ??
0,
);
options.environment = {
...options.environment,
GIT_CONFIG_COUNT: String(count + 1),
[`GIT_CONFIG_KEY_${count}`]: "core.fsmonitor",
[`GIT_CONFIG_VALUE_${count}`]: "false",
};
gitRepository ??= repository;
gitEnvironment ??= Object.fromEntries(
["GIT_DIR", "GIT_WORK_TREE"].flatMap((name) => {
@@ -3,7 +3,9 @@ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createTemporaryDirectories } from "./support/temporary-directories.js";
import {
completePatches,
publicationRepository,
resultWithFindings,
repositoryGit,
runGitRepositoryCommand,
runWorkflow,
@@ -93,50 +95,57 @@ test("patch discovery does not execute a workspace fsmonitor rewritten by the mo
expect(await readFile(effect, "utf8")).toBe("model-rewritten");
});
test("publication preflight and commit do not execute a checkout's fsmonitor", async () => {
const { directory, git, remote } = await publicationRepository(fixtures);
const support = await fixtures.create("patch-publication-fsmonitor-");
const effect = join(support, "callback");
const hook = join(directory, "monitor.cjs");
const script = (phase: string) =>
`require("node:fs").writeFileSync(${JSON.stringify(effect)}, ${JSON.stringify(phase)}); process.stdout.write("\\0");`;
await writeFile(hook, script("preflight"));
git("add", "monitor.cjs");
git("commit", "-m", "Synthetic filesystem monitor");
const quoted = (path: string) => JSON.stringify(path.replaceAll("\\", "/"));
git(
"config",
"core.fsmonitor",
`${quoted(process.execPath)} ${quoted(hook)}`,
);
git("status", "--porcelain");
expect(await readFile(effect, "utf8")).toBe("preflight");
await rm(effect);
let beforeTurn: string | undefined;
const result = await runWorkflow(
["patch", "Synthetic issue", "--create-pr", "--json"],
{
currentDirectory: directory,
onCodex: async (_args, output) => {
beforeTurn = await readFile(effect, "utf8").catch(() => undefined);
await rm(effect, { force: true });
await writeFile(hook, script("publication"));
await writeFile(join(directory, "src/finding-1.ts"), "fixed\n");
output?.stdout.write("Synthetic patch complete.\n");
return 0;
test.each(["scan", "patch"])(
"%s publication preflight and commit do not execute a checkout's fsmonitor",
async (command) => {
const { directory, git, remote } = await publicationRepository(fixtures);
const support = await fixtures.create("patch-publication-fsmonitor-");
const effect = join(support, "callback");
const hook = join(directory, "monitor.cjs");
const script = (phase: string) =>
`require("node:fs").writeFileSync(${JSON.stringify(effect)}, ${JSON.stringify(phase)}); process.stdout.write("\\0");`;
await writeFile(hook, script("preflight"));
git("add", "monitor.cjs");
git("commit", "-m", "Synthetic filesystem monitor");
const quoted = (path: string) => JSON.stringify(path.replaceAll("\\", "/"));
git(
"config",
"core.fsmonitor",
`${quoted(process.execPath)} ${quoted(hook)}`,
);
git("status", "--porcelain");
expect(await readFile(effect, "utf8")).toBe("preflight");
await rm(effect);
let beforeTurn: string | undefined;
const result = await runWorkflow(
command === "patch"
? ["patch", "Synthetic issue", "--create-pr", "--json"]
: ["scan", directory, "--patch", "--create-pr", "--json"],
{
currentDirectory: directory,
result: resultWithFindings(["high"]),
onCodex: async (args, output) => {
beforeTurn = await readFile(effect, "utf8").catch(() => undefined);
await rm(effect, { force: true });
await writeFile(hook, script("publication"));
await writeFile(join(directory, "src/finding-1.ts"), "fixed\n");
if (command === "scan") completePatches(args, output);
else output?.stdout.write("Synthetic patch complete.\n");
return 0;
},
onRepositoryCommand: (tool, args, cwd, options) =>
tool === "git"
? runGitRepositoryCommand(tool, args, cwd, options)
: args[1] === "list"
? "[]"
: "https://github.example.test/example/repository/pull/1",
},
onRepositoryCommand: (tool, args, cwd, options) =>
tool === "git"
? runGitRepositoryCommand(tool, args, cwd, options)
: args[1] === "list"
? "[]"
: "https://github.example.test/example/repository/pull/1",
},
);
expect(result.exitCode, result.stderr).toBe(0);
expect([
beforeTurn,
await readFile(effect, "utf8").catch(() => undefined),
]).toEqual([undefined, undefined]);
expect(git("ls-remote", remote)).toContain(git("rev-parse", "HEAD"));
});
);
expect(result.exitCode, result.stderr).toBe(0);
expect([
beforeTurn,
await readFile(effect, "utf8").catch(() => undefined),
]).toEqual([undefined, undefined]);
expect(git("ls-remote", remote)).toContain(git("rev-parse", "HEAD"));
},
);