fix(server): clean status fetch temporary packs #4338
ClosednateEc(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/4338/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/vcs/GitVcsDriverCore.ts line 231OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -227,3 +228,4 @@227228 return splitNullSeparatedPaths(result.stdout, result.stdoutTruncated);228229 }229230231+export function findNewGitTemporaryPackFiles(
- apps/server/src/vcs/GitVcsDriverCore.ts line 1187OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +967,21 @@967+ ),968+ Effect.catch((cause) =>969+ Effect.logWarning("Failed to clean temporary Git pack files after status fetch.", {970+ cause,971+ }),972+ ),973+ );974+975+ const result = yield* executeGit(976+ "GitVcsDriver.fetchRemoteForStatus",977+ fetchCwd,978+ ["--git-dir", gitCommonDir, "fetch", "--quiet", "--no-tags", remoteName],979+ {980+ allowNonZeroExit: true,981+ env: STATUS_UPSTREAM_REFRESH_ENV,982+ timeoutMs: Duration.toMillis(STATUS_UPSTREAM_REFRESH_TIMEOUT),983+ },984+ ).pipe(Effect.tapError(() => cleanupNewTemporaryPacks));985+ if (result.exitCode !== 0) {986+ yield* cleanupNewTemporaryPacks;987+ }apps/server/src/vcs/GitVcsDriverCore.ts line 1187OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +967,21 @@967+ ),968+ Effect.catch((cause) =>969+ Effect.logWarning("Failed to clean temporary Git pack files after status fetch.", {970+ cause,971+ }),972+ ),973+ );974+975+ const result = yield* executeGit(976+ "GitVcsDriver.fetchRemoteForStatus",977+ fetchCwd,978+ ["--git-dir", gitCommonDir, "fetch", "--quiet", "--no-tags", remoteName],979+ {980+ allowNonZeroExit: true,981+ env: STATUS_UPSTREAM_REFRESH_ENV,982+ timeoutMs: Duration.toMillis(STATUS_UPSTREAM_REFRESH_TIMEOUT),983+ },984+ ).pipe(Effect.tapError(() => cleanupNewTemporaryPacks));985+ if (result.exitCode !== 0) {986+ yield* cleanupNewTemporaryPacks;987+ }
- apps/server/src/vcs/GitVcsDriverCore.ts line 996OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +976,21 @@976+ },977+ { concurrency: "unbounded", discard: true },978+ );979+ }).pipe(980+ Effect.catch((cause) =>981+ Effect.logWarning("Failed to clean stale temporary Git pack files.", { cause }),982+ ),983+ );984+985+ yield* cleanupStaleTemporaryPacks;986+ const result = yield* executeGit(987+ "GitVcsDriver.fetchRemoteForStatus",988+ fetchCwd,989+ ["--git-dir", gitCommonDir, "fetch", "--quiet", "--no-tags", remoteName],990+ {991+ allowNonZeroExit: true,992+ env: STATUS_UPSTREAM_REFRESH_ENV,993+ timeoutMs: Duration.toMillis(STATUS_UPSTREAM_REFRESH_TIMEOUT),994+ },995+ ).pipe(Effect.ensuring(cleanupStaleTemporaryPacks));996+ if (result.exitCode !== 0) return;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/vcs/GitVcsDriverCore.ts line 1370Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +1149,21 @@1149+ cwd: fetchCwd,1150+ detail: "Failed to prepare the Git pack directory.",1151+ cause,1152+ }),1153+ ),1154+ );1155+ yield* promoteObjectFiles;1156+ const fetchedCommit = yield* runGitStdout(1157+ "GitVcsDriver.fetchRemoteForStatus.resolve",1158+ fetchCwd,1159+ ["--git-dir", gitCommonDir, "rev-parse", "--verify", temporaryRef],1160+ ).pipe(Effect.map((stdout) => stdout.trim()));1161+ yield* runGit("GitVcsDriver.fetchRemoteForStatus.promoteRef", fetchCwd, [1162+ "--git-dir",1163+ gitCommonDir,1164+ "update-ref",1165+ targetRef,1166+ fetchedCommit,1167+ expectedTarget,1168+ ]);1169+ }).pipe(Effect.ensuring(cleanupIsolatedFetch));apps/server/src/vcs/GitVcsDriverCore.ts line 1129OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +1109,21 @@1109+ fetchCwd,1110+ ["--git-dir", gitCommonDir, "rev-parse", "--verify", targetRef],1111+ { allowNonZeroExit: true },1112+ );1113+ const expectedTarget =1114+ previousTarget.exitCode === 0 ? previousTarget.stdout.trim() : GIT_ZERO_OID;1115+ const result = yield* executeGit(1116+ "GitVcsDriver.fetchRemoteForStatus",1117+ fetchCwd,1118+ [1119+ "--git-dir",1120+ gitCommonDir,1121+ "-c",1122+ "fetch.unpackLimit=0",1123+ "fetch",1124+ "--quiet",1125+ "--no-tags",1126+ "--no-write-fetch-head",1127+ "--refmap=",1128+ remoteName,1129+ `+refs/heads/${branchName}:${temporaryRef}`,apps/server/src/vcs/GitVcsDriverCore.ts line 1129OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -945,0 +1109,21 @@1109+ fetchCwd,1110+ ["--git-dir", gitCommonDir, "rev-parse", "--verify", targetRef],1111+ { allowNonZeroExit: true },1112+ );1113+ const expectedTarget =1114+ previousTarget.exitCode === 0 ? previousTarget.stdout.trim() : GIT_ZERO_OID;1115+ const result = yield* executeGit(1116+ "GitVcsDriver.fetchRemoteForStatus",1117+ fetchCwd,1118+ [1119+ "--git-dir",1120+ gitCommonDir,1121+ "-c",1122+ "fetch.unpackLimit=0",1123+ "fetch",1124+ "--quiet",1125+ "--no-tags",1126+ "--no-write-fetch-head",1127+ "--refmap=",1128+ remoteName,1129+ `+refs/heads/${branchName}:${temporaryRef}`,
- apps/server/src/vcs/GitVcsDriverCore.ts line 1220OutdatedResolvedby nateEc(GitHub profile, opens in a new tab)@@ -945,0 +1200,21 @@1200+ };1201+ }),1202+ { concurrency: "unbounded" },1203+ );1204+ const result = yield* executeGit(1205+ "GitVcsDriver.fetchRemoteForStatus",1206+ fetchCwd,1207+ [1208+ "--git-dir",1209+ gitCommonDir,1210+ "-c",1211+ "fetch.unpackLimit=0",1212+ "fetch",1213+ "--quiet",1214+ "--no-tags",1215+ "--no-write-fetch-head",1216+ "--refmap=",1217+ remoteName,1218+ ...fetchTargets.map(1219+ ({ remoteRef: sourceRef, temporaryRef }) => `+${sourceRef}:${temporaryRef}`,1220+ ),apps/server/src/vcs/GitVcsDriverCore.ts line 974OutdatedResolvedby nateEc(GitHub profile, opens in a new tab)@@ -924,3 +955,20 @@955+ const parsedUpstream =924956 parseUpstreamRefWithRemoteNames(upstreamRef, remoteNames) ??925- parseUpstreamRefByFirstSeparator(upstreamRef)957+ parseUpstreamRefByFirstSeparator(upstreamRef);958+ if (!parsedUpstream) {959+ return null;960+ }961+962+ const upstreamRemoteRef = yield* runGitStdout(963+ "GitVcsDriver.resolveCurrentUpstreamRemoteRef",964+ cwd,965+ ["for-each-ref", "--format=%(HEAD)%09%(upstream:remoteref)", "refs/heads"],966+ true,967+ ).pipe(968+ Effect.map(parseCurrentUpstreamRemoteRef),969+ Effect.orElseSucceed(() => null),926970 );971+972+ return {973+ ...parsedUpstream,974+ remoteRef: upstreamRemoteRef ?? `refs/heads/${parsedUpstream.branchName}`,
- apps/server/src/vcs/GitVcsDriverCore.ts line 1249OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1015,0 +1229,21 @@1229+ )(function* (fetchTarget: (typeof fetchTargets)[number]) {1230+ const previousTarget = yield* executeGit(1231+ "GitVcsDriver.fetchRemoteForStatus.readTarget",1232+ fetchCwd,1233+ ["--git-dir", gitCommonDir, "rev-parse", "--verify", fetchTarget.targetRef],1234+ { allowNonZeroExit: true },1235+ );1236+ const expectedTarget =1237+ previousTarget.exitCode === 0 ? previousTarget.stdout.trim() : GIT_ZERO_OID;1238+ const fetchArgs = [1239+ "--git-dir",1240+ gitCommonDir,1241+ "-c",1242+ "fetch.unpackLimit=0",1243+ "fetch",1244+ "--quiet",1245+ "--no-tags",1246+ "--no-write-fetch-head",1247+ "--refmap=",1248+ remoteName,1249+ `+${fetchTarget.remoteRef}:${fetchTarget.temporaryRef}`,
- apps/server/src/vcs/GitVcsDriverCore.ts line 1169OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1015,0 +1149,21 @@1149+ .exists(destination)1150+ .pipe(1151+ Effect.flatMap((exists) =>1152+ exists ? fileSystem.remove(source, { force: true }) : Effect.fail(cause),1153+ ),1154+ ),1155+ ),1156+ );1157+ const promoteObjectFiles = Effect.gen(function* () {1158+ const objectEntries = yield* fileSystem.readDirectory(temporaryObjectDirectory, {1159+ recursive: false,1160+ });1161+ for (const entry of objectEntries) {1162+ if (entry === "pack") {1163+ const packEntries = yield* fileSystem.readDirectory(temporaryPackDirectory, {1164+ recursive: false,1165+ });1166+ yield* Effect.forEach(1167+ packEntries.filter((packEntry) => packEntry.startsWith("pack-")),1168+ (packEntry) =>1169+ promoteFile(
- apps/server/src/vcs/GitVcsDriverCore.ts line 1131OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1015,0 +1111,21 @@1111+ const temporaryRoot = path.join(isolatedFetchRoot, operationId);1112+ const temporaryObjectDirectory = path.join(temporaryRoot, "objects");1113+ const targetRef = `refs/remotes/${remoteName}/${branchName}`;1114+ const fetchTargets = [1115+ {1116+ remoteRef,1117+ temporaryRef: `${GIT_STATUS_FETCH_REF_PREFIX}/${operationId}/upstream`,1118+ targetRef,1119+ },1120+ ];1121+ const defaultTargetRef =1122+ defaultBranchName === null ? null : `refs/remotes/${remoteName}/${defaultBranchName}`;1123+ if (defaultTargetRef !== null && defaultTargetRef !== targetRef) {1124+ fetchTargets.push({1125+ remoteRef: `refs/heads/${defaultBranchName}`,1126+ temporaryRef: `${GIT_STATUS_FETCH_REF_PREFIX}/${operationId}/default`,1127+ targetRef: defaultTargetRef,1128+ });1129+ }1130+1131+ yield* runGit("GitVcsDriver.fetchRemoteForStatus.init", fetchCwd, [
- apps/server/src/vcs/GitVcsDriverCore.ts line 1585Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1226,4 +1488,10 @@12261488 const refreshStatusRemoteCacheEntry = Effect.fn("refreshStatusRemoteCacheEntry")(function* (12271489 cacheKey: StatusRemoteRefreshCacheKey,12281490 ) {1229- return yield* fetchRemoteForStatus(cacheKey.gitCommonDir, cacheKey.remoteName).pipe(1491+ return yield* fetchRemoteForStatus(1492+ cacheKey.gitCommonDir,1493+ cacheKey.remoteName,1494+ cacheKey.branchName,1495+ cacheKey.remoteRef,1496+ cacheKey.defaultBranchName,1497+ ).pipe(
- apps/server/src/vcs/GitVcsDriverCore.ts line 1138Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1019,0 +1080,21 @@1080+ }).pipe(1081+ Effect.catch((cause) =>1082+ Effect.logWarning("Failed to clean stale temporary Git pack files.", { cause }),1083+ ),1084+ );1085+1086+ const cleanupStaleIsolatedFetches = Effect.gen(function* () {1087+ if (!(yield* fileSystem.exists(isolatedFetchRoot))) return;1088+ const now = yield* DateTime.now;1089+ const nowMs = DateTime.toEpochMillis(now);1090+ const entries = yield* fileSystem.readDirectory(isolatedFetchRoot, { recursive: false });1091+ yield* Effect.forEach(1092+ entries,1093+ (entry) => {1094+ const entryPath = path.join(isolatedFetchRoot, entry);1095+ return fileSystem.stat(entryPath).pipe(1096+ Effect.flatMap((info) => {1097+ const modifiedAtMs = Option.getOrNull(info.mtime)?.getTime() ?? null;1098+ return modifiedAtMs !== null &&1099+ nowMs - modifiedAtMs >= Duration.toMillis(GIT_STATUS_FETCH_STALE_AGE)1100+ ? fileSystem.remove(entryPath, { recursive: true, force: true })
- apps/server/src/vcs/GitVcsDriverCore.ts line 1274OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1019,0 +1254,21 @@1254+ "fetch.unpackLimit=0",1255+ "fetch",1256+ "--quiet",1257+ "--no-tags",1258+ "--no-write-fetch-head",1259+ "--refmap=",1260+ temporaryRoot,1261+ fetchTarget.temporaryRef,1262+ ],1263+ {1264+ timeoutMs: null,1265+ },1266+ ).pipe(Effect.uninterruptible);1267+ yield* runGit("GitVcsDriver.fetchRemoteForStatus.promoteRef", fetchCwd, [1268+ "--git-dir",1269+ gitCommonDir,1270+ "update-ref",1271+ fetchTarget.targetRef,1272+ fetchedCommit,1273+ expectedTarget,1274+ ]);
github-actions[bot](GitHub profile, opens in a new tab) removed labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- @@ -74,3 +74,5 @@7474 const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30);7575 const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5);7676 const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);77+const GIT_TEMPORARY_PACK_PREFIX = "tmp_pack_";78+const GIT_TEMPORARY_PACK_STALE_AGE = Duration.hours(1);@@ -74,3 +74,9 @@7474 const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30);7575 const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5);7676 const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);77+const GIT_TEMPORARY_PACK_PREFIX = "tmp_pack_";78+const GIT_TEMPORARY_PACK_STALE_AGE = Duration.hours(1);79+const GIT_STATUS_FETCH_OBJECT_DIRECTORY = "t3-status-fetch";80+const GIT_STATUS_FETCH_STALE_AGE = Duration.hours(1);81+const GIT_STATUS_FETCH_REF_PREFIX = "refs/t3-status-fetch";82+const GIT_ZERO_OID = "0".repeat(40);
juliusmarminge closed this
Sign in to join this conversation.


📝 Walkthrough
Walkthrough#
Git status upstream resolution now returns structured remote data. Status refreshes fetch into isolated repositories, promote objects and refs atomically, remove stale temporary state, and refresh both feature and default branches.
Changes#
Git status refresh
apps/server/src/vcs/GitVcsDriverCore.ts,apps/server/src/vcs/GitVcsDriverCore.test.tsgit for-each-refdata. Gone upstreams return no upstream. Refresh cache keys include branch, remote ref, and default branch context.apps/server/src/vcs/GitVcsDriverCore.ts,apps/server/src/vcs/GitVcsDriverCore.test.tsEstimated code review effort: 4 (Complex) | ~45 minutes
Merge Risk: 🔵 Low · up to
c0a81The isolated-fetch design addresses the disk leak and is broadly mergeable, but edge cases may disrupt long-running Git operations, prevent ref promotion in SHA-256 repositories, omit upstream status in branch-heavy repositories, or add polling overhead.
Suggested reviewers:
juliusmarminge,t3dotgg,gigioxxSequence Diagram(s)#
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#4296by isolating status fetch objects, cleaning failed or timed-out temporary packs, removing stale fetch data, and preventing repeated shared-store accumulation. Regressio…#4296. No unrelated changes are i…✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.