Skip to content
TypeScript

perf(server): a checkpoint capture starts 8 git processes instead of 10 (#21315)

Joseph Vidal2e860972ae17
diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts
index 88ca1eed8f5..ba2b4abe50a 100644
--- a/apps/server/src/vcs/GitVcsDriver.test.ts
+++ b/apps/server/src/vcs/GitVcsDriver.test.ts
@@ -918,6 +918,126 @@ it.effect.each(["missing", "invalid"] as const)(
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
);
+const captureRecordingGitCommands = Effect.fn("test.captureRecordingGitCommands")(function* (
+ cwd: string,
+ checkpointRef: CheckpointRef,
+) {
+ const liveProcess = yield* VcsProcess.VcsProcess;
+ const commands: Array<string | undefined> = [];
+ const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
+ Effect.provideService(VcsProcess.VcsProcess, {
+ run: (input) => {
+ let index = 0;
+ while (input.args[index] === "-C" || input.args[index] === "-c") index += 2;
+ commands.push(input.args[index]);
+ return liveProcess.run(input);
+ },
+ }),
+ );
+ yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
+ return commands;
+});
+
+it.effect.each(["committed", "unborn"] as const)(
+ "checkpoint capture reads the repository with one rev-parse (HEAD=%s)",
+ (head) =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-processes-" });
+ const git = (args: ReadonlyArray<string>) =>
+ driver.execute({ operation: "checkpoint-test", cwd, args });
+ const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
+ if (head === "committed") {
+ yield* makeCheckpointFixture(driver, cwd);
+ } else {
+ yield* git(["init"]);
+ yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
+ }
+
+ const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
+
+ assert.deepStrictEqual(commands, [
+ "rev-parse",
+ "config",
+ ...(head === "committed" ? ["read-tree", "ls-files"] : []),
+ "check-ignore",
+ "add",
+ "write-tree",
+ "commit-tree",
+ "update-ref",
+ ]);
+ assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
+ const [, ...parents] = (yield* git([
+ "rev-list",
+ "--parents",
+ "-n",
+ "1",
+ checkpointRef,
+ ])).stdout
+ .trim()
+ .split(" ");
+ assert.deepStrictEqual(
+ parents,
+ head === "committed" ? [(yield* git(["rev-parse", "HEAD"])).stdout.trim()] : [],
+ );
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
+);
+
+it.effect("checkpoint capture outside a repository fails with the common dir lookup error", () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-no-repo-" });
+
+ const error = yield* captureRecordingGitCommands(
+ cwd,
+ CheckpointRef.make("refs/t3/checkpoints/test"),
+ ).pipe(Effect.flip);
+
+ assert.instanceOf(error, VcsProcessExitError);
+ assert.strictEqual(error.operation, "GitVcsDriver.checkpoints.resolveGitCommonDir");
+ assert.strictEqual(error.exitCode, 128);
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
+);
+
+it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "checkpoint capture handles a repository path containing a newline",
+ () =>
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-new\nline-" });
+ const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
+ const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
+
+ const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
+
+ assert.deepStrictEqual(commands, [
+ "rev-parse",
+ "rev-parse",
+ "config",
+ "rev-parse",
+ "read-tree",
+ "ls-files",
+ "check-ignore",
+ "add",
+ "write-tree",
+ "commit-tree",
+ "update-ref",
+ ]);
+ assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
+ assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
+ assert.deepStrictEqual(
+ (yield* fileSystem.readDirectory(path.join(cwd, ".git"))).filter((name) =>
+ name.startsWith("t3-checkpoint-index-"),
+ ),
+ [],
+ );
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
+);
+
it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts
index 74e409a8bc1..fe49a6b1391 100644
--- a/apps/server/src/vcs/GitVcsDriver.ts
+++ b/apps/server/src/vcs/GitVcsDriver.ts
@@ -830,6 +830,45 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
return path.resolve(physicalCwd, gitCommonDir);
});
+ // --verify prints HEAD after the paths; --quiet leaves only paths for an unborn HEAD.
+ const resolveCaptureRepository = Effect.fn("GitVcsDriver.resolveCaptureRepository")(function* (
+ cwd: string,
+ ) {
+ const result = yield* execute({
+ operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
+ cwd,
+ args: [
+ "rev-parse",
+ "--path-format=absolute",
+ "--git-common-dir",
+ "--git-path",
+ "index",
+ "--verify",
+ "--quiet",
+ "HEAD",
+ ],
+ allowNonZeroExit: true,
+ });
+ const headExists = result.exitCode === 0;
+ const headChecked = headExists || result.exitCode === 1;
+ const lines = result.stdout.split("\n");
+ const [gitCommonDir = "", indexPath = ""] = lines;
+ if (headChecked && lines.length === (headExists ? 4 : 3) && lines.at(-1) === "") {
+ return {
+ gitCommonDir: gitCommonDir.trim(),
+ indexPath: indexPath.trim(),
+ headExists,
+ };
+ }
+ // Git before 2.31 echoes --path-format, and a path containing a newline spans lines.
+ // Separate commands read the paths as before and report a failure the same way.
+ return {
+ gitCommonDir: yield* resolveGitCommonDir(cwd),
+ indexPath: undefined,
+ headExists: headChecked ? headExists : yield* hasHeadCommit(cwd),
+ };
+ });
+
// Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication.
// Set fsync first: Git 2.36 ignores unknown batch without resetting the method.
// Batch fully flushes refs and equals fsync on macOS HFS+/APFS and Windows NTFS/ReFS.
@@ -862,7 +901,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
"-c",
"splitIndex.sharedIndexExpire=never",
];
- const gitCommonDir = yield* resolveGitCommonDir(input.cwd);
+ const { gitCommonDir, indexPath, headExists } = yield* resolveCaptureRepository(input.cwd);
const indexId = yield* crypto.randomUUIDv4().pipe(Effect.orDie);
const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${indexId}`);
const commitEnv: NodeJS.ProcessEnv = {
@@ -881,7 +920,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
);
yield* Effect.gen(function* () {
- const headExists = yield* hasHeadCommit(input.cwd);
const sparseConfig = yield* execute({
operation,
cwd: input.cwd,
@@ -900,17 +938,19 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
}
if (headExists) {
const reusedIndex = yield* Effect.gen(function* () {
- const indexPath = yield* execute({
- operation,
- cwd: input.cwd,
- args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
- });
- const { mtime } = yield* fileSystem.stat(indexPath.stdout.trim());
+ const userIndexPath =
+ indexPath ??
+ (yield* execute({
+ operation,
+ cwd: input.cwd,
+ args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
+ })).stdout.trim();
+ const { mtime } = yield* fileSystem.stat(userIndexPath);
if (Option.isNone(mtime)) return false;
// Stay below the source timestamp even if Date rounded up, preserving Git's racy check.
const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000);
if (indexTime <= 0) return false;
- yield* fileSystem.copyFile(indexPath.stdout.trim(), tempIndexPath);
+ yield* fileSystem.copyFile(userIndexPath, tempIndexPath);
yield* execute({
operation,
cwd: input.cwd,