@devries-cameron20 / t3code Mirror · read-only / commits / 449e3176c7
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, 23:15 UTC

fix(server): clean up worktrees with initialized submodules (#20368)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Scott Norteman committed Oct 20, 2026 at 00:54 UTC 449e3176c716865d17602fbc685f541fe06fd9d1
2 files changed +315 -3
apps/server/src/storageCleanup.test.ts
+182
@@ -503,6 +503,188 @@ const runCleanupTest = <A, E>(
503 >,
504 ) => effect.pipe(Effect.provide(cleanupTestLayer), Effect.scoped);
505
506 +const submoduleCleanupFixture = () =>
507 + Effect.gen(function* () {
508 + const fixture = yield* cleanupFixture();
509 + const { command, repo, worktree } = fixture;
510 + const submodule = `${worktree}/module`;
511 + const child = `${submodule}/child`;
512 + for (const [cwd, name] of [
513 + [worktree, "module"],
514 + [submodule, "child"],
515 + ] as const) {
516 + yield* command(cwd, ["-c", "protocol.file.allow=always", "submodule", "add", repo, name]);
517 + yield* command(`${cwd}/${name}`, ["config", "user.name", "Cleanup test"]);
518 + yield* command(`${cwd}/${name}`, ["config", "user.email", "cleanup@example.test"]);
519 + }
520 + const recordSubmodules = () =>
521 + Effect.gen(function* () {
522 + yield* command(submodule, ["commit", "-am", "record child"]);
523 + yield* command(submodule, ["push", "origin", "HEAD:submodule"]);
524 + yield* command(worktree, ["commit", "-am", "record module"]);
525 + });
526 + yield* recordSubmodules();
527 + return { ...fixture, submodule, child, recordSubmodules };
528 + });
529 +
530 +it.live("removes clean submodules recursively, including deinitialized repositories", () =>
531 + runCleanupTest(
532 + Effect.gen(function* () {
533 + const { service, fs, repo, worktree, submodule, command, recordSubmodules } =
534 + yield* submoduleCleanupFixture();
535 + yield* command(submodule, [
536 + "-c",
537 + "protocol.file.allow=always",
538 + "submodule",
539 + "add",
540 + repo,
541 + "stored",
542 + ]);
543 + yield* recordSubmodules();
544 + yield* command(submodule, ["submodule", "deinit", "stored"]);
545 + const admin = (yield* command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout.trim();
546 + const report = yield* service.runNow;
547 + expect(report.entries.find((entry) => entry.path === worktree)).toMatchObject({
548 + outcome: "removed",
549 + });
550 + expect(report.counts).toEqual({ removed: 1, kept: 0, failed: 0 });
551 + expect(yield* fs.exists(worktree)).toBe(false);
552 + expect(yield* fs.exists(admin)).toBe(false);
553 + }),
554 + ),
555 +);
556 +
557 +it.live("keeps nested submodule files protected by the current keep rule", () =>
558 + runCleanupTest(
559 + Effect.gen(function* () {
560 + const { service, fs, worktree, submodule, child, command, setPolicy } =
561 + yield* submoduleCleanupFixture();
562 + setPolicy("tracked-changes");
563 + yield* fs.writeFileString(`${child}/tracked.txt`, "local edit\n");
564 + expect(
565 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
566 + ).toMatchObject({ outcome: "kept" });
567 + expect(yield* fs.readFileString(`${child}/tracked.txt`)).toBe("local edit\n");
568 + yield* fs.writeFileString(`${child}/tracked.txt`, "original\n");
569 + setPolicy("any-local-files");
570 + yield* fs.writeFileString(`${child}/.env`, "local secret\n");
571 + expect(
572 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
573 + ).toMatchObject({ outcome: "kept" });
574 + expect(yield* fs.readFileString(`${child}/.env`)).toBe("local secret\n");
575 +
576 + yield* fs.remove(`${child}/.env`);
577 + yield* command(submodule, ["submodule", "deinit", "child"]);
578 + yield* fs.writeFileString(`${child}/.env`, "secret after deinit\n");
579 + const report = yield* service.runNow;
580 + expect({
581 + outcome: report.entries.find((entry) => entry.path === worktree)?.outcome,
582 + contents: (yield* fs.exists(`${child}/.env`))
583 + ? yield* fs.readFileString(`${child}/.env`)
584 + : null,
585 + }).toEqual({ outcome: "kept", contents: "secret after deinit\n" });
586 +
587 + yield* fs.remove(`${child}/.env`);
588 + setPolicy("uncommitted-changes");
589 + yield* fs.writeFileString(`${child}/notes.txt`, "notes after deinit\n");
590 + const untrackedReport = yield* service.runNow;
591 + expect({
592 + outcome: untrackedReport.entries.find((entry) => entry.path === worktree)?.outcome,
593 + contents: (yield* fs.exists(`${child}/notes.txt`))
594 + ? yield* fs.readFileString(`${child}/notes.txt`)
595 + : null,
596 + }).toEqual({ outcome: "kept", contents: "notes after deinit\n" });
597 + }),
598 + ),
599 +);
600 +
601 +it.live("keeps unpushed submodule history after checkout and deinitialization", () =>
602 + runCleanupTest(
603 + Effect.gen(function* () {
604 + const { service, fs, worktree, submodule, child, command, recordSubmodules } =
605 + yield* submoduleCleanupFixture();
606 + yield* command(child, ["checkout", "-b", "local-only"]);
607 + yield* fs.writeFileString(`${child}/tracked.txt`, "unpublished work\n");
608 + yield* command(child, ["commit", "-am", "local work"]);
609 + const local = (yield* command(child, ["rev-parse", "HEAD"])).stdout.trim();
610 + yield* recordSubmodules();
611 + expect(
612 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
613 + ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("remote-tracking") });
614 + expect(yield* fs.readFileString(`${child}/tracked.txt`)).toBe("unpublished work\n");
615 +
616 + yield* command(child, ["checkout", "main"]);
617 + yield* recordSubmodules();
618 + expect(
619 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
620 + ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("remote-tracking") });
621 + const childGit = (yield* command(child, ["rev-parse", "--absolute-git-dir"])).stdout.trim();
622 + yield* command(submodule, ["submodule", "deinit", "child"]);
623 + expect(
624 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
625 + ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("remote-tracking") });
626 + expect(
627 + (yield* command(worktree, [
628 + "--git-dir",
629 + childGit,
630 + "rev-parse",
631 + "refs/heads/local-only",
632 + ])).stdout.trim(),
633 + ).toBe(local);
634 + expect(
635 + (yield* command(worktree, ["--git-dir", childGit, "show", "local-only:tracked.txt"]))
636 + .stdout,
637 + ).toBe("unpublished work\n");
638 + yield* command(worktree, ["--git-dir", childGit, "branch", "-D", "local-only"]);
639 + expect(
640 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
641 + ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("remote-tracking") });
642 + expect(
643 + (yield* command(worktree, ["--git-dir", childGit, "show", `${local}:tracked.txt`])).stdout,
644 + ).toBe("unpublished work\n");
645 + }),
646 + ),
647 +);
648 +
649 +it.live("keeps submodules when stored repository metadata is malformed", () =>
650 + runCleanupTest(
651 + Effect.gen(function* () {
652 + const { service, fs, worktree, submodule, child, command } = yield* submoduleCleanupFixture();
653 + const childGit = (yield* command(child, ["rev-parse", "--absolute-git-dir"])).stdout.trim();
654 + yield* command(submodule, ["submodule", "deinit", "child"]);
655 + yield* fs.writeFileString(`${childGit}/HEAD`, "not a ref\n");
656 + expect(
657 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
658 + ).toMatchObject({ outcome: "kept", reason: expect.stringContaining("inspected") });
659 + expect(yield* fs.exists(worktree)).toBe(true);
660 + expect(yield* fs.readFileString(`${childGit}/HEAD`)).toBe("not a ref\n");
661 + }),
662 + ),
663 +);
664 +
665 +it.live("preserves late tracked edits when submodules were never initialized", () =>
666 + runCleanupTest(
667 + Effect.gen(function* () {
668 + const fixture = yield* cleanupFixture();
669 + const { service, fs, worktree, command } = fixture;
670 + const head = (yield* command(worktree, ["rev-parse", "HEAD"])).stdout.trim();
671 + yield* fs.writeFileString(
672 + `${worktree}/.gitmodules`,
673 + '[submodule "module"]\n\tpath = module\n\turl = /unused\n',
674 + );
675 + yield* command(worktree, ["add", ".gitmodules"]);
676 + yield* command(worktree, ["update-index", "--add", "--cacheinfo", `160000,${head},module`]);
677 + yield* command(worktree, ["commit", "-m", "record uninitialized module"]);
678 + yield* fs.makeDirectory(`${worktree}/module`);
679 + fixture.editBeforeRemoval();
680 + expect(
681 + (yield* service.runNow).entries.find((entry) => entry.path === worktree),
682 + ).toMatchObject({ outcome: "failed" });
683 + expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("late edit\n");
684 + }),
685 + ),
686 +);
687 +
688 describe("subagent worktree ownership", () => {
689 it.effect.each([
690 "idle",
apps/server/src/storageCleanup.ts
+133 -3
@@ -538,6 +538,122 @@ const make = Effect.gen(function* () {
538 return file === undefined ? null : `has ignored files (${file})`;
539 });
540
541 + const submoduleRemoval = Effect.fn("StorageCleanup.submoduleRemoval")(
542 + function* (cwd: string, rules: WorktreeCleanupRules) {
543 + const inspect = (args: string[], maxOutputBytes = 64 * 1024) =>
544 + git.execute({
545 + operation: "StorageCleanup.submodules",
546 + cwd,
547 + args,
548 + maxOutputBytes,
549 + });
550 + const admin = yield* inspect(["rev-parse", "--absolute-git-dir"]);
551 + if (admin.stdoutTruncated || admin.stderrTruncated)
552 + return { force: false, reason: "submodule inspection exceeded the limit" };
553 + const modules = path.join(admin.stdout.replace(/\n$/, ""), "modules");
554 + const stored = yield* fs.exists(modules);
555 + if (!stored && !(yield* fs.exists(path.join(cwd, ".gitmodules"))))
556 + return { force: false, reason: null };
557 + const populated = yield* inspect([
558 + "submodule",
559 + "foreach",
560 + "--recursive",
561 + "--quiet",
562 + 'printf "%s\\0" "$toplevel/$sm_path"',
563 + ]);
564 + if (populated.stdoutTruncated || populated.stderrTruncated)
565 + return { force: false, reason: "submodule inspection exceeded the limit" };
566 + const paths = populated.stdout.split("\0").filter(Boolean);
567 + const pending = [modules];
568 + for (const submodule of paths) {
569 + const directory = yield* inspect(["-C", submodule, "rev-parse", "--absolute-git-dir"]);
570 + if (directory.stdoutTruncated || directory.stderrTruncated)
571 + return { force: false, reason: "submodule inspection exceeded the limit" };
572 + pending.push(directory.stdout.replace(/\n$/, ""));
573 + }
574 + const visited = new Set<string>();
575 + while (pending.length > 0) {
576 + const directory = pending.pop()!;
577 + if (!(yield* fs.exists(directory))) continue;
578 + const canonical = yield* fs.realPath(directory);
579 + if (visited.has(canonical)) continue;
580 + visited.add(canonical);
581 + if (visited.size > 10_000)
582 + return { force: false, reason: "submodule inspection exceeded the limit" };
583 + if ((yield* fs.stat(directory)).type !== "Directory")
584 + return { force: false, reason: "submodule repository could not be inspected" };
585 + if (yield* fs.exists(path.join(directory, "HEAD"))) {
586 + const unpushed = yield* inspect([
587 + "--git-dir",
588 + directory,
589 + "--work-tree",
590 + cwd,
591 + "rev-list",
592 + "--max-count=1",
593 + "HEAD",
594 + "--all",
595 + "--reflog",
596 + "--not",
597 + "--remotes",
598 + ]);
599 + if (unpushed.stdoutTruncated || unpushed.stderrTruncated)
600 + return { force: false, reason: "submodule inspection exceeded the limit" };
601 + if (unpushed.stdout.trim() !== "")
602 + return { force: false, reason: "submodule has commits not on a remote-tracking ref" };
603 + pending.push(path.join(directory, "modules"));
604 + } else {
605 + for (const name of yield* fs.readDirectory(directory))
606 + pending.push(path.join(directory, name));
607 + }
608 + }
609 + // Check files after walking stored history to shorten the gap before forced removal.
610 + for (const submodule of paths) {
611 + const changes = yield* localChangesReason(submodule, rules);
612 + if (changes !== null) return { force: false, reason: `submodule ${changes}` };
613 + }
614 + const force = stored || paths.length > 0;
615 + if (force && rules.worktreeKeepWhen !== "tracked-changes") {
616 + const root = yield* fs.realPath(cwd);
617 + // Git status and foreach omit files inside deinitialized gitlink directories.
618 + const populatedPaths = new Set(paths.map(pathKey));
619 + const unchecked: string[] = [];
620 + for (const repository of [root, ...paths]) {
621 + const index = yield* inspect(
622 + ["-C", repository, "ls-files", "--stage", "-z"],
623 + 16 * 1024 * 1024,
624 + );
625 + if (index.stdoutTruncated || index.stderrTruncated)
626 + return { force: false, reason: "submodule inspection exceeded the limit" };
627 + for (const entry of index.stdout.split("\0")) {
628 + if (!entry.startsWith("160000 ")) continue;
629 + const submodule = path.resolve(repository, entry.slice(entry.indexOf("\t") + 1));
630 + if (!inside(root, submodule))
631 + return { force: false, reason: "submodules could not be inspected" };
632 + if (!populatedPaths.has(pathKey(submodule)) && (yield* fs.exists(submodule)))
633 + unchecked.push(submodule);
634 + }
635 + }
636 + let inspected = unchecked.length;
637 + while (unchecked.length > 0) {
638 + if (inspected > 10_000)
639 + return { force: false, reason: "submodule inspection exceeded the limit" };
640 + const target = unchecked.pop()!;
641 + if (
642 + (yield* fs.realPath(target)) !== path.resolve(target) ||
643 + (yield* fs.stat(target)).type !== "Directory"
644 + )
645 + return { force: false, reason: "submodule has local files" };
646 + if (path.basename(target) === "node_modules") continue;
647 + const names = yield* fs.readDirectory(target);
648 + inspected += names.length;
649 + for (const name of names) unchecked.push(path.join(target, name));
650 + }
651 + }
652 + return { force, reason: null };
653 + },
654 + Effect.orElseSucceed(() => ({ force: false, reason: "submodules could not be inspected" })),
655 + );
656 +
657 const cleanupGit = Effect.fn("StorageCleanup.cleanupGit")(function* (
658 cwd: string,
659 args: string[],
@@ -859,13 +975,27 @@ const make = Effect.gen(function* () {
975 )
976 )
977 return keep("settings changed since check");
862 - // Clean only untracked files; Git must still refuse removal if a tracked
863 - // edit arrives after our last status check.
978 + // Clean only untracked files. Ordinary worktrees retain Git's final tracked-edit check.
979 if (settings.worktreeKeepWhen === "tracked-changes")
980 yield* cleanupGit(worktreePath, ["clean", "-ffdx"], "git clean");
981 + const submodules = yield* submoduleRemoval(worktreePath, settings);
982 + if (submodules.reason !== null) return keep(submodules.reason);
983 + if (submodules.force) {
984 + const changes = yield* localChangesReason(worktreePath, settings);
985 + if (changes !== null) return keep(changes);
986 + }
987 yield* cleanupGit(
988 project.workspaceRoot,
868 - ["-c", "status.showUntrackedFiles=normal", "worktree", "remove", "--", worktreePath],
989 + [
990 + "-c",
991 + "status.showUntrackedFiles=normal",
992 + "worktree",
993 + "remove",
994 + // Git requires force for submodules; recursive checks above cannot make removal atomic.
995 + ...(submodules.force ? ["--force"] : []),
996 + "--",
997 + worktreePath,
998 + ],
999 "git worktree remove",
1000 );
1001 entries.push({ ...entry, outcome: "removed", reason: sentence(removalReason), bytes });