@devries-cameron20 / t3code Mirror · read-only / commits / 2e860972ae
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

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

Co-authored-by: maria-rcks <maria@kuuro.net>

Joseph Vidal committed Oct 30, 2026 at 16:25 UTC 2e860972ae1740e0e82717a5fc2683513936188b
2 files changed +169 -9
apps/server/src/vcs/GitVcsDriver.test.ts
+120
@@ -918,6 +918,126 @@ it.effect.each(["missing", "invalid"] as const)(
918 }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
919 );
920
921 +const captureRecordingGitCommands = Effect.fn("test.captureRecordingGitCommands")(function* (
922 + cwd: string,
923 + checkpointRef: CheckpointRef,
924 +) {
925 + const liveProcess = yield* VcsProcess.VcsProcess;
926 + const commands: Array<string | undefined> = [];
927 + const driver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
928 + Effect.provideService(VcsProcess.VcsProcess, {
929 + run: (input) => {
930 + let index = 0;
931 + while (input.args[index] === "-C" || input.args[index] === "-c") index += 2;
932 + commands.push(input.args[index]);
933 + return liveProcess.run(input);
934 + },
935 + }),
936 + );
937 + yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
938 + return commands;
939 +});
940 +
941 +it.effect.each(["committed", "unborn"] as const)(
942 + "checkpoint capture reads the repository with one rev-parse (HEAD=%s)",
943 + (head) =>
944 + Effect.gen(function* () {
945 + const fileSystem = yield* FileSystem.FileSystem;
946 + const path = yield* Path.Path;
947 + const driver = yield* GitVcsDriver.makeVcsDriverShape();
948 + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-processes-" });
949 + const git = (args: ReadonlyArray<string>) =>
950 + driver.execute({ operation: "checkpoint-test", cwd, args });
951 + const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
952 + if (head === "committed") {
953 + yield* makeCheckpointFixture(driver, cwd);
954 + } else {
955 + yield* git(["init"]);
956 + yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
957 + }
958 +
959 + const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
960 +
961 + assert.deepStrictEqual(commands, [
962 + "rev-parse",
963 + "config",
964 + ...(head === "committed" ? ["read-tree", "ls-files"] : []),
965 + "check-ignore",
966 + "add",
967 + "write-tree",
968 + "commit-tree",
969 + "update-ref",
970 + ]);
971 + assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
972 + const [, ...parents] = (yield* git([
973 + "rev-list",
974 + "--parents",
975 + "-n",
976 + "1",
977 + checkpointRef,
978 + ])).stdout
979 + .trim()
980 + .split(" ");
981 + assert.deepStrictEqual(
982 + parents,
983 + head === "committed" ? [(yield* git(["rev-parse", "HEAD"])).stdout.trim()] : [],
984 + );
985 + }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
986 +);
987 +
988 +it.effect("checkpoint capture outside a repository fails with the common dir lookup error", () =>
989 + Effect.gen(function* () {
990 + const fileSystem = yield* FileSystem.FileSystem;
991 + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-no-repo-" });
992 +
993 + const error = yield* captureRecordingGitCommands(
994 + cwd,
995 + CheckpointRef.make("refs/t3/checkpoints/test"),
996 + ).pipe(Effect.flip);
997 +
998 + assert.instanceOf(error, VcsProcessExitError);
999 + assert.strictEqual(error.operation, "GitVcsDriver.checkpoints.resolveGitCommonDir");
1000 + assert.strictEqual(error.exitCode, 128);
1001 + }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1002 +);
1003 +
1004 +it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1005 + "checkpoint capture handles a repository path containing a newline",
1006 + () =>
1007 + Effect.gen(function* () {
1008 + const fileSystem = yield* FileSystem.FileSystem;
1009 + const path = yield* Path.Path;
1010 + const driver = yield* GitVcsDriver.makeVcsDriverShape();
1011 + const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-new\nline-" });
1012 + const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
1013 + const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
1014 +
1015 + const commands = yield* captureRecordingGitCommands(cwd, checkpointRef);
1016 +
1017 + assert.deepStrictEqual(commands, [
1018 + "rev-parse",
1019 + "rev-parse",
1020 + "config",
1021 + "rev-parse",
1022 + "read-tree",
1023 + "ls-files",
1024 + "check-ignore",
1025 + "add",
1026 + "write-tree",
1027 + "commit-tree",
1028 + "update-ref",
1029 + ]);
1030 + assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
1031 + assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
1032 + assert.deepStrictEqual(
1033 + (yield* fileSystem.readDirectory(path.join(cwd, ".git"))).filter((name) =>
1034 + name.startsWith("t3-checkpoint-index-"),
1035 + ),
1036 + [],
1037 + );
1038 + }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1039 +);
1040 +
1041 it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
1042 Effect.gen(function* () {
1043 const fileSystem = yield* FileSystem.FileSystem;
apps/server/src/vcs/GitVcsDriver.ts
+49 -9
@@ -830,6 +830,45 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
830 return path.resolve(physicalCwd, gitCommonDir);
831 });
832
833 + // --verify prints HEAD after the paths; --quiet leaves only paths for an unborn HEAD.
834 + const resolveCaptureRepository = Effect.fn("GitVcsDriver.resolveCaptureRepository")(function* (
835 + cwd: string,
836 + ) {
837 + const result = yield* execute({
838 + operation: VcsProcess.CHECKPOINT_CAPTURE_OPERATION,
839 + cwd,
840 + args: [
841 + "rev-parse",
842 + "--path-format=absolute",
843 + "--git-common-dir",
844 + "--git-path",
845 + "index",
846 + "--verify",
847 + "--quiet",
848 + "HEAD",
849 + ],
850 + allowNonZeroExit: true,
851 + });
852 + const headExists = result.exitCode === 0;
853 + const headChecked = headExists || result.exitCode === 1;
854 + const lines = result.stdout.split("\n");
855 + const [gitCommonDir = "", indexPath = ""] = lines;
856 + if (headChecked && lines.length === (headExists ? 4 : 3) && lines.at(-1) === "") {
857 + return {
858 + gitCommonDir: gitCommonDir.trim(),
859 + indexPath: indexPath.trim(),
860 + headExists,
861 + };
862 + }
863 + // Git before 2.31 echoes --path-format, and a path containing a newline spans lines.
864 + // Separate commands read the paths as before and report a failure the same way.
865 + return {
866 + gitCommonDir: yield* resolveGitCommonDir(cwd),
867 + indexPath: undefined,
868 + headExists: headChecked ? headExists : yield* hasHeadCommit(cwd),
869 + };
870 + });
871 +
872 // Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication.
873 // Set fsync first: Git 2.36 ignores unknown batch without resetting the method.
874 // 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* (
901 "-c",
902 "splitIndex.sharedIndexExpire=never",
903 ];
865 - const gitCommonDir = yield* resolveGitCommonDir(input.cwd);
904 + const { gitCommonDir, indexPath, headExists } = yield* resolveCaptureRepository(input.cwd);
905 const indexId = yield* crypto.randomUUIDv4().pipe(Effect.orDie);
906 const tempIndexPath = path.join(gitCommonDir, `t3-checkpoint-index-${indexId}`);
907 const commitEnv: NodeJS.ProcessEnv = {
@@ -881,7 +920,6 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
920 );
921
922 yield* Effect.gen(function* () {
884 - const headExists = yield* hasHeadCommit(input.cwd);
923 const sparseConfig = yield* execute({
924 operation,
925 cwd: input.cwd,
@@ -900,17 +938,19 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
938 }
939 if (headExists) {
940 const reusedIndex = yield* Effect.gen(function* () {
903 - const indexPath = yield* execute({
904 - operation,
905 - cwd: input.cwd,
906 - args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
907 - });
908 - const { mtime } = yield* fileSystem.stat(indexPath.stdout.trim());
941 + const userIndexPath =
942 + indexPath ??
943 + (yield* execute({
944 + operation,
945 + cwd: input.cwd,
946 + args: ["rev-parse", "--path-format=absolute", "--git-path", "index"],
947 + })).stdout.trim();
948 + const { mtime } = yield* fileSystem.stat(userIndexPath);
949 if (Option.isNone(mtime)) return false;
950 // Stay below the source timestamp even if Date rounded up, preserving Git's racy check.
951 const indexTime = Math.floor((mtime.value.getTime() - 1) / 1000);
952 if (indexTime <= 0) return false;
913 - yield* fileSystem.copyFile(indexPath.stdout.trim(), tempIndexPath);
953 + yield* fileSystem.copyFile(userIndexPath, tempIndexPath);
954 yield* execute({
955 operation,
956 cwd: input.cwd,