Skip to content

Commit cdaeeca

Browse files
committed
fix(server): clean up worktrees with initialized submodules
1 parent 73d70a4 commit cdaeeca

2 files changed

Lines changed: 275 additions & 3 deletions

File tree

‎apps/server/src/storageCleanup.test.ts‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,187 @@ const runCleanupTest = <A, E>(
480480
>,
481481
) => effect.pipe(Effect.provide(cleanupTestLayer), Effect.scoped);
482482

483+
const submoduleCleanupFixture = () =>
484+
Effect.gen(function* () {
485+
const fixture = yield* cleanupFixture();
486+
const { command, fs, worktree } = fixture;
487+
const source = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-submodule-" });
488+
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-submodule-remote-" });
489+
yield* command(source, ["init", "-b", "main"]);
490+
yield* command(source, ["config", "user.email", "[email protected]"]);
491+
yield* command(source, ["config", "user.name", "Cleanup test"]);
492+
yield* fs.writeFileString(`${source}/tracked.txt`, "original\n");
493+
yield* fs.writeFileString(`${source}/.gitignore`, ".env\nnode_modules/\n");
494+
yield* command(source, ["add", "."]);
495+
yield* command(source, ["commit", "-m", "initial"]);
496+
yield* command(source, ["clone", "--bare", source, remote]);
497+
yield* command(worktree, [
498+
"-c",
499+
"protocol.file.allow=always",
500+
"submodule",
501+
"add",
502+
remote,
503+
"module",
504+
]);
505+
yield* command(`${worktree}/module`, ["config", "user.name", "Cleanup test"]);
506+
yield* command(`${worktree}/module`, ["config", "user.email", "[email protected]"]);
507+
yield* command(worktree, ["commit", "-am", "add submodule"]);
508+
const submodule = `${worktree}/module`;
509+
const addNested = () =>
510+
Effect.gen(function* () {
511+
yield* command(submodule, [
512+
"-c",
513+
"protocol.file.allow=always",
514+
"submodule",
515+
"add",
516+
remote,
517+
"child",
518+
]);
519+
yield* command(submodule, ["commit", "-am", "add child"]);
520+
yield* command(submodule, ["push", "origin", "HEAD:main"]);
521+
yield* command(worktree, ["commit", "-am", "update module"]);
522+
return `${submodule}/child`;
523+
});
524+
return { ...fixture, submodule, addNested };
525+
});
526+
527+
it.live.each(["populated", "deinitialized"])(
528+
"removes a clean worktree with %s nested submodules",
529+
(state) =>
530+
runCleanupTest(
531+
Effect.gen(function* () {
532+
const { service, fs, worktree, submodule, command, addNested } =
533+
yield* submoduleCleanupFixture();
534+
yield* addNested();
535+
const unpushed = yield* command(submodule, [
536+
"rev-list",
537+
"--count",
538+
"HEAD",
539+
"--not",
540+
"--remotes",
541+
]);
542+
expect(unpushed.stdout.trim()).toBe("0");
543+
if (state === "deinitialized") yield* command(worktree, ["submodule", "deinit", "--all"]);
544+
const report = yield* service.runNow;
545+
expect(report.entries.find((entry) => entry.path === worktree)).toMatchObject({
546+
outcome: "removed",
547+
});
548+
expect(yield* fs.exists(worktree)).toBe(false);
549+
}),
550+
),
551+
);
552+
553+
it.live.each(["HEAD", "local branch"])("keeps an unpushed submodule %s", (unpushed) =>
554+
runCleanupTest(
555+
Effect.gen(function* () {
556+
const { service, fs, worktree, submodule, command } = yield* submoduleCleanupFixture();
557+
yield* command(submodule, ["checkout", "-b", "local-only"]);
558+
yield* command(submodule, ["commit", "--allow-empty", "-m", "local only"]);
559+
if (unpushed === "local branch") yield* command(submodule, ["checkout", "main"]);
560+
yield* command(worktree, ["add", "module"]);
561+
yield* command(worktree, ["commit", "--allow-empty", "-m", "record module"]);
562+
const report = yield* service.runNow;
563+
expect(report.entries[0]).toMatchObject({ outcome: "kept" });
564+
expect(report.entries[0]?.reason).toContain("not on a remote-tracking ref");
565+
expect(yield* fs.exists(submodule)).toBe(true);
566+
}),
567+
),
568+
);
569+
570+
it.live.each([
571+
["tracked.txt", "uncommitted-changes"],
572+
[".env", "any-local-files"],
573+
] as const)("keeps submodule %s under %s", ([file, policy]) =>
574+
runCleanupTest(
575+
Effect.gen(function* () {
576+
const { service, fs, submodule, setPolicy } = yield* submoduleCleanupFixture();
577+
setPolicy(policy);
578+
yield* fs.writeFileString(`${submodule}/${file}`, "local edit\n");
579+
const report = yield* service.runNow;
580+
expect(report.entries[0]).toMatchObject({ outcome: "kept" });
581+
if (file === ".env") expect(report.entries[0]?.reason).toContain("ignored files (.env)");
582+
expect(yield* fs.readFileString(`${submodule}/${file}`)).toBe("local edit\n");
583+
}),
584+
),
585+
);
586+
587+
it.live("keeps a deinitialized nested submodule's local branch in an embedded parent", () =>
588+
runCleanupTest(
589+
Effect.gen(function* () {
590+
const { service, fs, worktree, submodule, command, addNested } =
591+
yield* submoduleCleanupFixture();
592+
const child = yield* addNested();
593+
for (const [key, value] of [
594+
["user.name", "Cleanup test"],
595+
["user.email", "[email protected]"],
596+
] as const)
597+
yield* command(child, ["config", key, value]);
598+
yield* command(child, ["checkout", "-b", "local-only"]);
599+
yield* command(child, ["commit", "--allow-empty", "-m", "local only"]);
600+
const local = (yield* command(child, ["rev-parse", "HEAD"])).stdout.trim();
601+
yield* command(child, ["checkout", "main"]);
602+
yield* command(submodule, ["submodule", "deinit", "--all"]);
603+
const parentGit = (yield* command(submodule, [
604+
"rev-parse",
605+
"--absolute-git-dir",
606+
])).stdout.trim();
607+
yield* command(submodule, ["config", "core.worktree", submodule]);
608+
yield* fs.remove(`${submodule}/.git`);
609+
yield* fs.rename(parentGit, `${submodule}/.git`);
610+
const childGit = `${submodule}/.git/modules/child`;
611+
const report = yield* service.runNow;
612+
expect(report.entries[0]).toMatchObject({ outcome: "kept" });
613+
expect(report.entries[0]?.reason).toContain("not on a remote-tracking ref");
614+
expect(yield* fs.exists(`${childGit}/HEAD`)).toBe(true);
615+
expect(
616+
(yield* command(worktree, [
617+
"--git-dir",
618+
childGit,
619+
"rev-parse",
620+
"refs/heads/local-only",
621+
])).stdout.trim(),
622+
).toBe(local);
623+
}),
624+
),
625+
);
626+
627+
it.live("keeps a worktree when a stored submodule cannot be inspected", () =>
628+
runCleanupTest(
629+
Effect.gen(function* () {
630+
const { service, fs, worktree, command } = yield* submoduleCleanupFixture();
631+
const admin = (yield* command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout.trim();
632+
yield* fs.makeDirectory(`${admin}/modules/broken`);
633+
yield* fs.writeFileString(`${admin}/modules/broken/HEAD`, "not a ref\n");
634+
const report = yield* service.runNow;
635+
expect(report.entries[0]).toMatchObject({ outcome: "kept" });
636+
expect(report.entries[0]?.reason).toContain("could not be inspected");
637+
expect(yield* fs.exists(worktree)).toBe(true);
638+
expect(yield* fs.readFileString(`${admin}/modules/broken/HEAD`)).toBe("not a ref\n");
639+
}),
640+
),
641+
);
642+
643+
it.live("preserves late tracked edits with never-initialized submodules", () =>
644+
runCleanupTest(
645+
Effect.gen(function* () {
646+
const fixture = yield* cleanupFixture();
647+
const { service, fs, worktree, command } = fixture;
648+
const head = (yield* command(worktree, ["rev-parse", "HEAD"])).stdout.trim();
649+
yield* fs.writeFileString(
650+
`${worktree}/.gitmodules`,
651+
'[submodule "module"]\n\tpath = module\n\turl = /unused\n',
652+
);
653+
yield* command(worktree, ["add", ".gitmodules"]);
654+
yield* command(worktree, ["update-index", "--add", "--cacheinfo", `160000,${head},module`]);
655+
yield* command(worktree, ["commit", "-m", "add uninitialized module"]);
656+
yield* fs.makeDirectory(`${worktree}/module`);
657+
fixture.editBeforeRemoval();
658+
expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "failed" });
659+
expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("late edit\n");
660+
}),
661+
),
662+
);
663+
483664
describe("subagent worktree ownership", () => {
484665
it.effect.each([
485666
"idle",

‎apps/server/src/storageCleanup.ts‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,83 @@ const make = Effect.gen(function* () {
533533
return file === undefined ? null : `has ignored files (${file})`;
534534
});
535535

536+
const submoduleRemoval = Effect.fn("StorageCleanup.submoduleRemoval")(
537+
function* (cwd: string, rules: WorktreeCleanupRules) {
538+
const inspect = (args: string[]) =>
539+
git.execute({
540+
operation: "StorageCleanup.submodules",
541+
cwd,
542+
args,
543+
maxOutputBytes: 64 * 1024,
544+
});
545+
const admin = yield* inspect(["rev-parse", "--absolute-git-dir"]);
546+
if (admin.stdoutTruncated)
547+
return { force: false, reason: "submodule inspection exceeded the limit" };
548+
const modules = path.join(admin.stdout.trim(), "modules");
549+
const stored = yield* fs.exists(modules);
550+
if (!stored && !(yield* fs.exists(path.join(cwd, ".gitmodules"))))
551+
return { force: false, reason: null };
552+
const populated = yield* inspect([
553+
"submodule",
554+
"foreach",
555+
"--recursive",
556+
"--quiet",
557+
'printf "%s\\0" "$toplevel/$sm_path"',
558+
]);
559+
if (populated.stdoutTruncated)
560+
return { force: false, reason: "submodule inspection exceeded the limit" };
561+
const paths = populated.stdout.split("\0").filter(Boolean);
562+
const pending = [modules];
563+
for (const submodule of paths) {
564+
const changes = yield* localChangesReason(submodule, rules);
565+
if (changes !== null) return { force: false, reason: `submodule ${changes}` };
566+
const directory = yield* git.execute({
567+
operation: "StorageCleanup.submoduleGitDirectory",
568+
cwd: submodule,
569+
args: ["rev-parse", "--absolute-git-dir"],
570+
maxOutputBytes: 64 * 1024,
571+
});
572+
if (directory.stdoutTruncated)
573+
return { force: false, reason: "submodule inspection exceeded the limit" };
574+
pending.push(directory.stdout.trim());
575+
}
576+
const visited = new Set<string>();
577+
while (pending.length > 0) {
578+
const directory = pending.pop()!;
579+
if (!(yield* fs.exists(directory))) continue;
580+
const canonical = yield* fs.realPath(directory);
581+
if (visited.has(canonical)) continue;
582+
visited.add(canonical);
583+
if (visited.size > 10_000)
584+
return { force: false, reason: "submodule inspection exceeded the limit" };
585+
if ((yield* fs.stat(directory)).type !== "Directory")
586+
return { force: false, reason: "submodule repository could not be inspected" };
587+
if (yield* fs.exists(path.join(directory, "HEAD"))) {
588+
const unpushed = yield* inspect([
589+
"--git-dir",
590+
directory,
591+
"--work-tree",
592+
cwd,
593+
"rev-list",
594+
"--max-count=1",
595+
"HEAD",
596+
"--all",
597+
"--not",
598+
"--remotes",
599+
]);
600+
if (unpushed.stdoutTruncated || unpushed.stdout.trim() !== "")
601+
return { force: false, reason: "submodule has commits not on a remote-tracking ref" };
602+
pending.push(path.join(directory, "modules"));
603+
} else {
604+
for (const name of yield* fs.readDirectory(directory))
605+
pending.push(path.join(directory, name));
606+
}
607+
}
608+
return { force: stored || paths.length > 0, reason: null };
609+
},
610+
Effect.orElseSucceed(() => ({ force: false, reason: "submodules could not be inspected" })),
611+
);
612+
536613
const cleanupGit = Effect.fn("StorageCleanup.cleanupGit")(function* (
537614
cwd: string,
538615
args: string[],
@@ -829,13 +906,27 @@ const make = Effect.gen(function* () {
829906
)
830907
)
831908
return keep("settings changed since check");
832-
// Clean only untracked files; Git must still refuse removal if a tracked
833-
// edit arrives after our last status check.
909+
// Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
834910
if (settings.worktreeKeepWhen === "tracked-changes")
835911
yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
912+
const submodules = yield* submoduleRemoval(worktreePath, settings);
913+
if (submodules.reason !== null) return keep(submodules.reason);
914+
if (submodules.force) {
915+
const changes = yield* localChangesReason(worktreePath, settings);
916+
if (changes !== null) return keep(changes);
917+
}
836918
yield* cleanupGit(
837919
project.workspaceRoot,
838-
["-c", "status.showUntrackedFiles=normal", "worktree", "remove", "--", worktreePath],
920+
[
921+
"-c",
922+
"status.showUntrackedFiles=normal",
923+
"worktree",
924+
"remove",
925+
// Git requires force for submodules; recursive checks above cannot make removal atomic.
926+
...(submodules.force ? ["--force"] : []),
927+
"--",
928+
worktreePath,
929+
],
839930
"git worktree remove",
840931
);
841932
entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });

0 commit comments

Comments
 (0)