The best way to control Claude Code, Codex and more from any computer or phone
fix(server): checkpoint restore reverts edits inside submodules (#30829)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Muhammad Auon committed
Nov 15, 2026 at 05:40 UTC
6e0cbb3ea9ffcc1c1c8480b32473934ba995d340
4 files changed
+168
-5
apps/server/src/storageCleanup.test.ts
+13
@@ -1014,6 +1014,19 @@ it.live("removes clean submodules recursively, including deinitialized repositor
1014
yield* command(legacy, ["submodule", "deinit", "stored"]);
1015
yield* recordSubmodules();
1016
yield* command(submodule, ["submodule", "deinit", "stored"]);
1017
+ const checkpoint = (yield* command(submodule, [
1018
+ "commit-tree",
1019
+ "HEAD^{tree}",
1020
+ "-p",
1021
+ "HEAD",
1022
+ "-m",
1023
+ "checkpoint",
1024
+ ])).stdout.trim();
1025
+ yield* command(submodule, [
1026
+ "update-ref",
1027
+ "refs/t3/orchestration-v2/checkpoints/test",
1028
+ checkpoint,
1029
+ ]);
1030
const admin = (yield* command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout.trim();
1031
const report = yield* service.runNow;
1032
expect(editedDuringClean).toBe(false);
apps/server/src/storageCleanup.ts
+2
@@ -934,6 +934,8 @@ const make = Effect.gen(function* () {
934
"rev-list",
935
"--max-count=1",
936
"HEAD",
937
+ // Checkpoint refs hold workspace snapshots, not commits a user could lose.
938
+ "--exclude=refs/t3/*",
939
"--all",
940
"--reflog",
941
"--not",
apps/server/src/vcs/GitVcsDriver.test.ts
+69
@@ -1562,6 +1562,8 @@ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1562
"write-tree",
1563
"commit-tree",
1564
"update-ref",
1565
+ // Without a parsed top level, capture cannot rule out submodules.
1566
+ "submodule",
1567
]);
1568
assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
1569
assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
@@ -1800,6 +1802,73 @@ it.effect("sparse restore rewrites manual skip-worktree files and keeps exclusio
1802
}).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1803
);
1804
1805
+it.effect("restores and deletes checkpoints inside populated submodules", () =>
1806
+ Effect.gen(function* () {
1807
+ const fileSystem = yield* FileSystem.FileSystem;
1808
+ const path = yield* Path.Path;
1809
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
1810
+ const git = (cwd: string, args: ReadonlyArray<string>) =>
1811
+ driver.execute({ operation: "test", cwd, args });
1812
+ const makeRepository = Effect.fn("makeRepository")(function* (prefix: string) {
1813
+ const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix });
1814
+ yield* git(cwd, ["init"]);
1815
+ yield* git(cwd, ["config", "user.email", "test@test.com"]);
1816
+ yield* git(cwd, ["config", "user.name", "Test"]);
1817
+ yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "original\n");
1818
+ yield* git(cwd, ["add", "."]);
1819
+ yield* git(cwd, ["commit", "-m", "initial"]);
1820
+ return cwd;
1821
+ });
1822
+ const nested = yield* makeRepository("t3-submodule-nested-");
1823
+ const library = yield* makeRepository("t3-submodule-library-");
1824
+ const addSubmodule = (cwd: string, url: string, name: string) =>
1825
+ git(cwd, ["-c", "protocol.file.allow=always", "submodule", "add", url, name]).pipe(
1826
+ Effect.andThen(git(cwd, ["commit", "-m", `add ${name}`])),
1827
+ );
1828
+ yield* addSubmodule(library, nested, "nested");
1829
+ const root = yield* makeRepository("t3-submodule-root-");
1830
+ yield* addSubmodule(root, library, "library");
1831
+ yield* git(root, [
1832
+ "-c",
1833
+ "protocol.file.allow=always",
1834
+ "submodule",
1835
+ "update",
1836
+ "--init",
1837
+ "--recursive",
1838
+ ]);
1839
+ const files = ["file.txt", "library/file.txt", "library/nested/file.txt"].map((file) =>
1840
+ path.join(root, file),
1841
+ );
1842
+ const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/submodules");
1843
+ yield* fileSystem.writeFileString(files[1]!, "captured\n");
1844
+ yield* driver.checkpoints.captureCheckpoint({ cwd: root, checkpointRef });
1845
+
1846
+ for (const file of files) {
1847
+ yield* fileSystem.writeFileString(file, "changed\n");
1848
+ yield* fileSystem.writeFileString(`${file}.added`, "new\n");
1849
+ }
1850
+ assert.isTrue(
1851
+ yield* driver.checkpoints.restoreCheckpoint({
1852
+ cwd: root,
1853
+ checkpointRef,
1854
+ fallbackToHead: false,
1855
+ }),
1856
+ );
1857
+
1858
+ assert.deepStrictEqual(
1859
+ yield* Effect.forEach(files, (file) => fileSystem.readFileString(file)),
1860
+ ["original\n", "captured\n", "original\n"],
1861
+ );
1862
+ for (const file of files) assert.isFalse(yield* fileSystem.exists(`${file}.added`));
1863
+
1864
+ yield* driver.checkpoints.deleteCheckpointRefs({ cwd: root, checkpointRefs: [checkpointRef] });
1865
+ for (const cwd of [root, path.join(root, "library"), path.join(root, "library", "nested")]) {
1866
+ const ref = yield* git(cwd, ["for-each-ref", "refs/t3"]);
1867
+ assert.strictEqual(ref.stdout, "");
1868
+ }
1869
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1870
+);
1871
+
1872
// The real VCS process, spawning `command` wherever the driver runs git.
1873
const vcsProcessSpawning = (command: string) =>
1874
Layer.effect(
apps/server/src/vcs/GitVcsDriver.ts
+84
-5
@@ -937,6 +937,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
937
"--git-common-dir",
938
"--git-path",
939
"index",
940
+ "--show-toplevel",
941
"--show-prefix",
942
"--verify",
943
"--quiet",
@@ -947,11 +948,12 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
948
const headExists = result.exitCode === 0;
949
const headChecked = headExists || result.exitCode === 1;
950
const lines = result.stdout.split("\n");
950
- const [gitCommonDir = "", indexPath = "", prefix = "", head = ""] = lines;
951
- if (headChecked && lines.length === (headExists ? 5 : 4) && lines.at(-1) === "") {
951
+ const [gitCommonDir = "", indexPath = "", topLevel = "", prefix = "", head = ""] = lines;
952
+ if (headChecked && lines.length === (headExists ? 6 : 5) && lines.at(-1) === "") {
953
return {
954
gitCommonDir: gitCommonDir.trim(),
955
indexPath: indexPath.trim(),
956
+ topLevel: topLevel.trim(),
957
prefix,
958
headExists,
959
head: headExists ? head : undefined,
@@ -968,12 +970,26 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
970
return {
971
gitCommonDir: fallbackCommonDir,
972
indexPath: undefined,
973
+ topLevel: undefined,
974
prefix: prefixResult.stdout.replace(/\n$/, ""),
975
headExists: headChecked ? headExists : yield* hasHeadCommit(cwd),
976
head: undefined,
977
};
978
});
979
980
+ // $displaypath is relative to cwd, so "../" marks submodules outside a nested workspace.
981
+ const listSubmodules = Effect.fn("GitVcsDriver.listSubmodules")(function* (cwd: string) {
982
+ const result = yield* execute({
983
+ operation: "GitVcsDriver.checkpoints.listSubmodules",
984
+ cwd,
985
+ args: ["submodule", "foreach", "--quiet", 'printf "%s\\0" "$displaypath"'],
986
+ });
987
+ return result.stdout
988
+ .split("\0")
989
+ .filter((entry) => entry !== "" && !entry.startsWith("../"))
990
+ .map((entry) => path.join(cwd, entry));
991
+ });
992
+
993
// Git defaults and macOS writeout-only can leave empty refs after crashes; flush before publication.
994
// Set fsync first: Git 2.36 ignores unknown batch without resetting the method.
995
// Batch fully flushes refs and equals fsync on macOS HFS+/APFS and Windows NTFS/ReFS.
@@ -1064,9 +1080,8 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
1080
"-c",
1081
"splitIndex.sharedIndexExpire=never",
1082
];
1067
- const { gitCommonDir, indexPath, prefix, headExists, head } = yield* resolveCaptureRepository(
1068
- input.cwd,
1069
- );
1083
+ const { gitCommonDir, indexPath, topLevel, prefix, headExists, head } =
1084
+ yield* resolveCaptureRepository(input.cwd);
1085
// Read before the snapshot and pinned to the same HEAD: a switch that lands while files are
1086
// being staged then belongs to the next checkpoint instead of being applied to this one.
1087
const headMoves = head === undefined ? [] : yield* readHeadMoves(input.cwd);
@@ -1709,6 +1724,40 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
1724
args: [...durableWrite, "update-ref", input.checkpointRef, commitOid],
1725
});
1726
}).pipe(Effect.ensuring(cleanupTempIndex));
1727
+
1728
+ // The tree records only each submodule's HEAD, so submodules keep their own ref.
1729
+ const hasSubmodules =
1730
+ topLevel === undefined ||
1731
+ (yield* fileSystem
1732
+ .exists(path.join(topLevel, ".gitmodules"))
1733
+ .pipe(Effect.orElseSucceed(() => true)));
1734
+ if (!hasSubmodules) return;
1735
+ // A submodule that cannot be captured drops its ref, so restore leaves it alone.
1736
+ yield* listSubmodules(input.cwd).pipe(
1737
+ Effect.flatMap((submodules) =>
1738
+ Effect.forEach(
1739
+ submodules,
1740
+ (cwd) =>
1741
+ checkpoints
1742
+ .captureCheckpoint({ cwd, checkpointRef: input.checkpointRef })
1743
+ .pipe(
1744
+ Effect.catch((cause) =>
1745
+ Effect.logWarning("submodule checkpoint capture failed", { cwd, cause }).pipe(
1746
+ Effect.andThen(
1747
+ checkpoints
1748
+ .deleteCheckpointRefs({ cwd, checkpointRefs: [input.checkpointRef] })
1749
+ .pipe(Effect.ignore),
1750
+ ),
1751
+ ),
1752
+ ),
1753
+ ),
1754
+ { discard: true },
1755
+ ),
1756
+ ),
1757
+ Effect.catch((cause) =>
1758
+ Effect.logWarning("submodule checkpoint capture failed", { cwd: input.cwd, cause }),
1759
+ ),
1760
+ );
1761
}, Effect.scoped),
1762
1763
hasCheckpointRef: (input) =>
@@ -1915,6 +1964,32 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
1964
}
1965
yield* resetIndex;
1966
1967
+ // A submodule without this ref was not captured, and restoring it to HEAD would lose edits.
1968
+ // The root is already restored, so a failing submodule must not hide that from the caller.
1969
+ yield* listSubmodules(input.cwd).pipe(
1970
+ Effect.flatMap((submodules) =>
1971
+ Effect.forEach(
1972
+ submodules,
1973
+ (cwd) =>
1974
+ checkpoints
1975
+ .restoreCheckpoint({
1976
+ cwd,
1977
+ checkpointRef: input.checkpointRef,
1978
+ fallbackToHead: false,
1979
+ })
1980
+ .pipe(
1981
+ Effect.catch((cause) =>
1982
+ Effect.logWarning("submodule checkpoint restore failed", { cwd, cause }),
1983
+ ),
1984
+ ),
1985
+ { discard: true },
1986
+ ),
1987
+ ),
1988
+ Effect.catch((cause) =>
1989
+ Effect.logWarning("submodule checkpoint restore failed", { cwd: input.cwd, cause }),
1990
+ ),
1991
+ );
1992
+
1993
return true;
1994
}, Effect.scoped),
1995
@@ -2122,6 +2197,10 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
2197
}),
2198
{ discard: true },
2199
);
2200
+ const submodules = yield* listSubmodules(input.cwd).pipe(Effect.orElseSucceed(() => []));
2201
+ for (const cwd of submodules) {
2202
+ yield* checkpoints.deleteCheckpointRefs({ cwd, checkpointRefs: input.checkpointRefs });
2203
+ }
2204
},
2205
),
2206
};