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, 06:00 UTC
fix(server): storage cleanup names a broken Git ref and fetches it once per sweep (#39202)
Julius Marminge committed
Nov 19, 2026 at 09:05 UTC
412a1189bb1d696efd826f2245276bccee42ed3b
3 files changed
+95
-20
apps/server/src/storageCleanup.test.ts
+52
@@ -3489,6 +3489,58 @@ describe("storage cleanup reports and local file policies", () => {
3489
}),
3490
),
3491
);
3492
+ it.live("fetches a failing default branch once per sweep and names the broken local ref", () =>
3493
+ runCleanupTest(
3494
+ Effect.gen(function* () {
3495
+ const { service, fs, repo, worktree, command, setThreads, setStoredWorktreePaths } =
3496
+ yield* cleanupFixture({
3497
+ ...DEFAULT_SERVER_SETTINGS.storageCleanup,
3498
+ worktreeUnchanged: true,
3499
+ });
3500
+ const second = `${worktree}-2`;
3501
+ yield* command(repo, ["worktree", "add", "-b", "feature-2", second]);
3502
+ setStoredWorktreePaths([worktree, second]);
3503
+ setThreads([
3504
+ shell({ branch: "feature", worktreePath: worktree, archivedAt: at(-DAY_MS) }),
3505
+ shell({
3506
+ id: ThreadId.make("thread-2"),
3507
+ branch: "feature-2",
3508
+ worktreePath: second,
3509
+ archivedAt: at(-DAY_MS),
3510
+ }),
3511
+ ]);
3512
+ const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
3513
+ yield* command(remote, ["init", "--bare", "-b", "main"]);
3514
+ yield* command(repo, ["remote", "add", "origin", remote]);
3515
+ yield* command(repo, ["push", "origin", "main"]);
3516
+ yield* command(repo, ["remote", "set-head", "origin", "main"]);
3517
+ yield* command(repo, ["commit", "--allow-empty", "-m", "remote only"]);
3518
+ yield* command(repo, ["push", "origin", "main"]);
3519
+ yield* command(repo, ["reset", "--hard", "HEAD~1"]);
3520
+ yield* command(repo, ["update-ref", "refs/remotes/origin/main", "HEAD"]);
3521
+ const trackingRef = yield* fs.readFileString(`${repo}/.git/refs/remotes/origin/main`);
3522
+ yield* fs.writeFileString(`${repo}/.git/refs/remotes/origin/main 2`, trackingRef);
3523
+ const fetchLog = `${remote}/fetches.log`;
3524
+ const uploadPack = `${remote}/upload-pack.sh`;
3525
+ yield* fs.writeFileString(
3526
+ uploadPack,
3527
+ `#!/bin/sh\necho fetch >> '${fetchLog}'\nexec git-upload-pack "$@"\n`,
3528
+ );
3529
+ yield* fs.chmod(uploadPack, 0o755);
3530
+ yield* command(repo, ["config", "remote.origin.uploadpack", uploadPack]);
3531
+
3532
+ const report = yield* service.runNow;
3533
+ expect((yield* fs.readFileString(fetchLog)).trim().split("\n")).toHaveLength(1);
3534
+ expect(report.entries).toHaveLength(2);
3535
+ for (const entry of report.entries) {
3536
+ expect(entry.outcome).not.toBe("removed");
3537
+ expect(entry.reason).toContain("broken local ref");
3538
+ }
3539
+ expect(yield* fs.exists(worktree)).toBe(true);
3540
+ expect(yield* fs.exists(second)).toBe(true);
3541
+ }),
3542
+ ),
3543
+ );
3544
it.live("preserves tracked edits written after the final status check", () =>
3545
runCleanupTest(
3546
Effect.gen(function* () {
apps/server/src/storageCleanup.ts
+17
-9
@@ -30,6 +30,7 @@ import * as Clock from "effect/Clock";
30
import * as DateTime from "effect/DateTime";
31
import * as Effect from "effect/Effect";
32
import * as Equal from "effect/Equal";
33
+import * as Exit from "effect/Exit";
34
import * as FileSystem from "effect/FileSystem";
35
import * as Option from "effect/Option";
36
import * as Path from "effect/Path";
@@ -1123,7 +1124,8 @@ const make = Effect.gen(function* () {
1124
)).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
1125
const snapshot = yield* readThreads();
1126
const threadsById = new Map(snapshot.threads.map((thread) => [thread.id, thread]));
1126
- const refreshedDefaultRefs = new Map<string, Set<string>>();
1127
+ // Failures are cached too, so one broken repository costs one fetch per sweep.
1128
+ const refreshedDefaultRefs = new Map<string, Map<string, Exit.Exit<void, GitCommandError>>>();
1129
const defaultBranches = new Map<string, string | null>();
1130
// Threads may store one checkout through different symlinked spellings; compare real paths.
1131
// Keys are only compared, never opened, so they take pathKey's Windows case folding.
@@ -1511,16 +1513,22 @@ const make = Effect.gen(function* () {
1513
}
1514
if (branch === null) return keep("default branch is unavailable");
1515
defaultRef = `refs/remotes/${remote}/${branch}`;
1514
- const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();
1515
- if (!refreshed.has(defaultRef)) {
1516
- yield* git.fetchRemoteTrackingBranch({
1517
- cwd: repositoryCwd,
1518
- remoteName: remote,
1519
- remoteBranch: branch,
1520
- });
1521
- refreshed.add(defaultRef);
1516
+ const refreshed =
1517
+ refreshedDefaultRefs.get(repositoryCwd) ??
1518
+ new Map<string, Exit.Exit<void, GitCommandError>>();
1519
+ let refresh = refreshed.get(defaultRef);
1520
+ if (refresh === undefined) {
1521
+ refresh = yield* Effect.exit(
1522
+ git.fetchRemoteTrackingBranch({
1523
+ cwd: repositoryCwd,
1524
+ remoteName: remote,
1525
+ remoteBranch: branch,
1526
+ }),
1527
+ );
1528
+ refreshed.set(defaultRef, refresh);
1529
refreshedDefaultRefs.set(repositoryCwd, refreshed);
1530
}
1531
+ yield* refresh;
1532
}
1533
const base = yield* git.resolveCommit({
1534
cwd: worktreePath,
apps/server/src/vcs/GitVcsDriverCore.ts
+26
-11
@@ -5916,20 +5916,35 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5916
Effect.fn("fetchRemoteTrackingBranch")(function* (input) {
5917
// Storage cleanup runs this every sweep: like the status fetch, a killed fetch strands a
5918
// `tmp_pack_*` and a failing `gc --auto` repeats each time, so allow time and skip gc.
5919
- yield* runGit(
5919
+ const args = [
5920
+ "fetch",
5921
+ "--quiet",
5922
+ "--no-tags",
5923
+ "--no-auto-gc",
5924
+ "--",
5925
+ input.remoteName,
5926
+ `+refs/heads/${input.remoteBranch}:refs/remotes/${input.remoteName}/${input.remoteBranch}`,
5927
+ ];
5928
+ const result = yield* executeGitWithStableDiagnostics(
5929
"GitVcsDriver.fetchRemoteTrackingBranch",
5930
input.cwd,
5922
- [
5923
- "fetch",
5924
- "--quiet",
5925
- "--no-tags",
5926
- "--no-auto-gc",
5927
- "--",
5928
- input.remoteName,
5929
- `+refs/heads/${input.remoteBranch}:refs/remotes/${input.remoteName}/${input.remoteBranch}`,
5930
- ],
5931
- { timeoutMs: FETCH_REMOTE_TIMEOUT_MS },
5931
+ args,
5932
+ { timeoutMs: FETCH_REMOTE_TIMEOUT_MS, allowNonZeroExit: true },
5933
);
5934
+ if (result.exitCode === 0) return;
5935
+ const reason = classifyGitFailure(result.stderr);
5936
+ return yield* new GitCommandError({
5937
+ ...gitCommandContext({
5938
+ operation: "GitVcsDriver.fetchRemoteTrackingBranch",
5939
+ cwd: input.cwd,
5940
+ args,
5941
+ }),
5942
+ ...(reason === null ? {} : { reason }),
5943
+ detail: fetchFailureDetail(result.stderr) ?? gitFailureDetail(reason),
5944
+ ...(result.exitCode === null ? {} : { exitCode: result.exitCode }),
5945
+ stdoutLength: result.stdout.length,
5946
+ stderrLength: result.stderr.length,
5947
+ });
5948
});
5949
5950
const setBranchUpstream: GitVcsDriver.GitVcsDriver["Service"]["setBranchUpstream"] = Effect.fn(