@devries-cameron20 / t3code Mirror · read-only / commits / fe565c8958
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 21, 21:00 UTC

fix(server): storage cleanup keeps edits hidden from git status (#31069)

Julius Marminge committed Nov 11, 2026 at 16:00 UTC fe565c895822a76684b7c6b307b7518641131cca
2 files changed +181
apps/server/src/storageCleanup.test.ts
+84
@@ -2678,6 +2678,90 @@ describe("storage cleanup reports and local file policies", () => {
2678 }),
2679 ),
2680 );
2681 + it.live.each(["--assume-unchanged", "--skip-worktree"] as const)(
2682 + "keeps tracked edits that git status hides (%s)",
2683 + (flag) =>
2684 + runCleanupTest(
2685 + Effect.gen(function* () {
2686 + const { service, fs, worktree, command } = yield* cleanupFixture();
2687 + yield* command(worktree, ["update-index", flag, "tracked.txt"]);
2688 + yield* fs.writeFileString(`${worktree}/tracked.txt`, "hidden edit\n");
2689 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
2690 + expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("hidden edit\n");
2691 + }),
2692 + ),
2693 + );
2694 + // macOS and Windows file systems reject names that are not valid UTF-8.
2695 + it.live.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
2696 + "keeps hidden edits to files whose names are not valid UTF-8",
2697 + () =>
2698 + runCleanupTest(
2699 + Effect.gen(function* () {
2700 + const { service, fs, worktree, command } = yield* cleanupFixture();
2701 + const git = yield* GitVcsDriver.GitVcsDriver;
2702 + // Git unquotes C-quoted paths on stdin, which reaches a name a JS string cannot.
2703 + const quoted = '"bad-\\377.txt"';
2704 + const stdin = (args: string[], input: string) =>
2705 + git.execute({ operation: "cleanup-test", cwd: worktree, args, stdin: input });
2706 + const blob = (content: string) =>
2707 + stdin(["hash-object", "-w", "--stdin"], content).pipe(
2708 + Effect.map((result) => result.stdout.trim()),
2709 + );
2710 + const original = yield* blob("original\n");
2711 + yield* stdin(["update-index", "--index-info"], `100644 ${original}\t${quoted}\n`);
2712 + yield* stdin(["checkout-index", "--stdin"], `${quoted}\n`);
2713 + yield* command(worktree, ["commit", "-m", "bad name"]);
2714 + // The committed content moves on while the file on disk keeps the other version.
2715 + const edited = yield* blob("edited\n");
2716 + yield* stdin(["update-index", "--index-info"], `100644 ${edited}\t${quoted}\n`);
2717 + yield* command(worktree, ["commit", "-m", "edit"]);
2718 + yield* stdin(["update-index", "--assume-unchanged", "--stdin"], `${quoted}\n`);
2719 + expect((yield* command(worktree, ["status", "--porcelain"])).stdout).toBe("");
2720 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "kept" });
2721 + expect((yield* fs.readDirectory(worktree)).some((name) => name.startsWith("bad-"))).toBe(
2722 + true,
2723 + );
2724 + }),
2725 + ),
2726 + );
2727 + it.live("removes worktrees whose flagged files are unchanged or outside a sparse checkout", () =>
2728 + runCleanupTest(
2729 + Effect.gen(function* () {
2730 + const { service, fs, worktree, command } = yield* cleanupFixture();
2731 + yield* command(worktree, ["update-index", "--skip-worktree", "tracked.txt"]);
2732 + yield* fs.remove(`${worktree}/tracked.txt`);
2733 + yield* command(worktree, ["update-index", "--assume-unchanged", ".gitignore"]);
2734 + expect((yield* service.runNow).entries[0]).toMatchObject({ outcome: "removed" });
2735 + }),
2736 + ),
2737 + );
2738 + it.live.each([
2739 + ["missing", "symlink"],
2740 + ["tracked.txt", "symlink"],
2741 + ["missing", "file"],
2742 + ["tracked.txt", "file"],
2743 + ] as const)("compares flagged tracked symlinks by target (%s as a %s)", ([target, kind]) =>
2744 + runCleanupTest(
2745 + Effect.gen(function* () {
2746 + const { service, fs, worktree, command } = yield* cleanupFixture();
2747 + yield* fs.symlink("tracked.txt", `${worktree}/link`);
2748 + yield* command(worktree, ["add", "link"]);
2749 + yield* command(worktree, ["commit", "-m", "link"]);
2750 + yield* command(worktree, ["update-index", "--assume-unchanged", "link"]);
2751 + yield* fs.remove(`${worktree}/link`);
2752 + // Without core.symlinks, Git checks a link out as a regular file holding its target.
2753 + if (kind === "file") {
2754 + yield* command(worktree, ["config", "core.symlinks", "false"]);
2755 + yield* fs.writeFileString(`${worktree}/link`, target);
2756 + } else yield* fs.symlink(target, `${worktree}/link`);
2757 + expect((yield* service.runNow).entries[0]).toMatchObject(
2758 + target === "missing"
2759 + ? { outcome: "kept", reason: expect.stringContaining("(link)") }
2760 + : { outcome: "removed" },
2761 + );
2762 + }),
2763 + ),
2764 + );
2765 it.live.each([
2766 ["uncommitted-changes", "untracked", "Local changes exceed the inspection limit"],
2767 ["any-local-files", "ignored", "Ignored files exceed the inspection limit"],
apps/server/src/storageCleanup.ts
+97
@@ -86,6 +86,8 @@ const ORPHAN_WORKTREE_GRACE_MS = 60 * 60 * 1_000;
86 const sentence = (text: string) => text.charAt(0).toUpperCase() + text.slice(1);
87 const REPORT_ENTRY_LIMIT = 200;
88 const isGitCommandError = Schema.is(GitCommandError);
89 +const errorCode = (error: PlatformError) =>
90 + error.cause instanceof Error && "code" in error.cause ? error.cause.code : undefined;
91
92 function cleanupFailureReason(error: { readonly message: string }) {
93 return isGitCommandError(error)
@@ -636,6 +638,99 @@ const make = Effect.gen(function* () {
638 return (name?.startsWith("(") ? null : name) !== branch;
639 });
640
641 + // Git status and non-forced worktree removal both skip assume-unchanged and skip-worktree files.
642 + const hiddenTrackedChange = Effect.fn("StorageCleanup.hiddenTrackedChange")(function* (
643 + cwd: string,
644 + ) {
645 + const inspect = (args: string[], stdin?: string) =>
646 + git.execute({
647 + operation: "StorageCleanup.hiddenTrackedChanges",
648 + cwd,
649 + args,
650 + ...(stdin === undefined ? {} : { stdin }),
651 + maxOutputBytes: 1024 * 1024,
652 + });
653 + const sparse = yield* inspect([
654 + "config",
655 + "--type=bool",
656 + "--default=false",
657 + "core.sparseCheckout",
658 + ]);
659 + // Sparse checkouts flag every file outside the cone; Git 2.37+ unflags those it finds on disk.
660 + const hiddenTag = sparse.stdout.trim() === "true" ? /^[a-z]$/ : /^(?:S|[a-z])$/;
661 + const flagged: Array<{ readonly mode: string; readonly id: string; readonly file: string }> =
662 + [];
663 + let overflow = false;
664 + yield* git.execute({
665 + operation: "StorageCleanup.hiddenTrackedChanges",
666 + cwd,
667 + args: ["-c", "core.quotePath=false", "ls-files", "-s", "-v"],
668 + maxOutputBytes: 64 * 1024,
669 + appendTruncationMarker: true,
670 + keepLineCallbacksAfterTruncation: true,
671 + progress: {
672 + onStdoutLine: (line) =>
673 + Effect.sync(() => {
674 + const match = /^(\S) (\d+) ([0-9a-f]+) \d\t(.*)$/s.exec(line);
675 + if (match === null || !hiddenTag.test(match[1]!) || match[2] === "160000") return;
676 + if (flagged.length < 10_000)
677 + flagged.push({ mode: match[2]!, id: match[3]!, file: match[4]! });
678 + else overflow = true;
679 + }),
680 + },
681 + });
682 + if (overflow) return "hidden tracked files exceed the inspection limit";
683 + const reason = (file: string) => `has uncommitted changes hidden from git status (${file})`;
684 + // Quoted names (control characters, quotes, backslashes) and names that are not valid UTF-8
685 + // decode to text that is not the path on disk.
686 + const unreadable = flagged.find(({ file }) => file.startsWith('"') || file.includes("\uFFFD"));
687 + if (unreadable !== undefined) return reason(unreadable.file);
688 + const onDisk = yield* Effect.forEach(
689 + flagged,
690 + (entry) => {
691 + const file = path.join(cwd, entry.file);
692 + return fs.readLink(file).pipe(
693 + Effect.map((target) => ({ ...entry, type: "SymbolicLink", target })),
694 + Effect.catchIf(
695 + (error) => errorCode(error) === "EINVAL",
696 + () => fs.stat(file).pipe(Effect.map((info) => ({ ...entry, type: info.type }))),
697 + ),
698 + Effect.catchIf(
699 + (error) => errorCode(error) === "ENOENT" || errorCode(error) === "ENOTDIR",
700 + () => Effect.succeed(null),
701 + ),
702 + );
703 + },
704 + { concurrency: 16 },
705 + );
706 + const files: Array<{ readonly id: string; readonly file: string }> = [];
707 + // Without core.symlinks a tracked link is a regular file holding its target, compared unfiltered.
708 + const linkFiles: typeof files = [];
709 + for (const entry of onDisk) {
710 + if (entry === null) continue;
711 + if (entry.type === "File") (entry.mode === "120000" ? linkFiles : files).push(entry);
712 + else if (!("target" in entry) || entry.mode !== "120000") return reason(entry.file);
713 + // Hashing a link's path would hash the file it points to.
714 + else if (
715 + (yield* inspect(["hash-object", "--stdin"], entry.target)).stdout.trim() !== entry.id
716 + )
717 + return reason(entry.file);
718 + }
719 + const hashPaths = (entries: ReadonlyArray<{ readonly file: string }>, flags: string[]) =>
720 + entries.length === 0
721 + ? Effect.succeed([])
722 + : inspect(
723 + ["hash-object", ...flags, "--stdin-paths"],
724 + entries.map(({ file }) => `${file}\n`).join(""),
725 + ).pipe(Effect.map((result) => result.stdout.split("\n")));
726 + const worktreeIds = yield* hashPaths(files, []);
727 + const linkIds = yield* hashPaths(linkFiles, ["--no-filters"]);
728 + const changed =
729 + files.find(({ id }, i) => worktreeIds[i] !== id) ??
730 + linkFiles.find(({ id }, i) => linkIds[i] !== id);
731 + return changed === undefined ? null : reason(changed.file);
732 + });
733 +
734 const localChangesReason = Effect.fn("StorageCleanup.localChangesReason")(function* (
735 cwd: string,
736 rules: WorktreeCleanupRules,
@@ -663,6 +758,8 @@ const make = Effect.gen(function* () {
758 if (/^[RC]|^.[RC]/.test(records[i]!)) i++;
759 }
760 if (count > 0) return `has uncommitted changes (${count} ${count === 1 ? "file" : "files"})`;
761 + const hidden = yield* hiddenTrackedChange(cwd);
762 + if (hidden !== null) return hidden;
763 if (rules.worktreeKeepWhen !== "any-local-files") return null;
764 const ignored = yield* git.execute({
765 operation: "StorageCleanup.ignoredFiles",