authorgravatar for 73941998+UtkarshUsername@users.noreply.github.comUtkarsh Patil <73941998+UtkarshUsername@users.noreply.github.com> 2026-07-31 03:38:59+05:30
committergravatar for noreply@github.comGitHub <noreply@github.com> 2026-07-31 00:08:59+02:00
log34b15a9acec5519425c81bdd2a10f11e9ba3d23d
tree005f70838cdd5c3dc43e4c3017b196428cb8b46f
parentfbecb7365ede9892045dec4d03805d20ef9859f8
signature Signed by PGP key B5690EEEBB952194

perf(server): cache default branch name and origin existence across status refreshes (#5008)


2 files changed, 123 insertions(+), 13 deletions(-)

apps/server/src/vcs/GitVcsDriverCore.test.ts+45
......@@ -153,6 +153,51 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
153153 }).pipe(Effect.provide(layer));
154154});
155155
156it.effect("invalidates origin remote cache when a driver mutation adds origin", () =>
157 Effect.gen(function* () {
158 const driver = yield* GitVcsDriver.GitVcsDriver;
159 const cwd = yield* makeTmpDir();
160 const remote = yield* makeTmpDir("git-vcs-driver-remote-");
161 yield* initRepoWithCommit(cwd);
162 yield* git(remote, ["init", "--bare"]);
163
164 const before = yield* driver.statusDetailsLocal(cwd);
165 assert.equal(before.hasOriginRemote, false);
166
167 yield* driver.ensureRemote({ cwd, preferredName: "origin", url: remote });
168
169 const after = yield* driver.statusDetailsLocal(cwd);
170 assert.equal(after.hasOriginRemote, true);
171 }).pipe(Effect.provide(TestLayer)),
172);
173
174it.effect("re-reads origin remote status after cache TTL expiry and bypassed invalidation", () =>
175 Effect.gen(function* () {
176 const driver = yield* GitVcsDriver.GitVcsDriver;
177 const cwd = yield* makeTmpDir();
178 const remote = yield* makeTmpDir("git-vcs-driver-remote-");
179 yield* initRepoWithCommit(cwd);
180 yield* git(remote, ["init", "--bare"]);
181
182 // First call caches hasOriginRemote = false (5-min TTL)
183 assert.equal((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote, false);
184
185 // Add origin via raw git (bypasses invalidation hook)
186 yield* git(cwd, ["remote", "add", "origin", remote]);
187
188 // Cache still has the stale false (TTL not yet expired)
189 const stillCached = yield* driver.statusDetailsLocal(cwd);
190 assert.equal(stillCached.hasOriginRemote, false);
191
192 // Advance past the 5-minute TTL so the cache entry expires
193 yield* TestClock.adjust("6 minutes");
194
195 // After expiry, the next call re-executes and picks up the remote
196 const afterExpiry = yield* driver.statusDetailsLocal(cwd);
197 assert.equal(afterExpiry.hasOriginRemote, true);
198 }).pipe(Effect.provide(TestLayer)),
199);
200
156201it.effect("coalesces concurrent ref pages into one repository snapshot", () =>
157202 Effect.scoped(
158203 Effect.gen(function* () {
apps/server/src/vcs/GitVcsDriverCore.ts+78-13
......@@ -61,6 +61,8 @@ const LIST_REFS_SNAPSHOT_CACHE_CAPACITY = 64;
6161const LIST_REFS_SNAPSHOT_CACHE_TTL = Duration.minutes(2);
6262const LIST_REFS_REFRESH_COALESCE_TTL = Duration.seconds(5);
6363const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30);
64const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5);
65const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);
6466const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({
6567 GCM_INTERACTIVE: "never",
6668 GIT_ASKPASS: "",
......@@ -1119,6 +1121,63 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11191121 return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey);
11201122 };
11211123
1124 const defaultBranchCache = yield* Cache.makeWith(
1125 (gitCommonDir: string) =>
1126 Effect.gen(function* () {
1127 const path = yield* Path.Path;
1128 const fetchCwd =
1129 path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1130 return yield* executeGit(
1131 "GitVcsDriver.statusDetails.defaultBranch",
1132 fetchCwd,
1133 ["--git-dir", gitCommonDir, "symbolic-ref", "refs/remotes/origin/HEAD"],
1134 { allowNonZeroExit: true },
1135 ).pipe(
1136 Effect.map((result) => {
1137 if (result.exitCode !== 0) return null;
1138 return parseDefaultBranchFromRemoteHeadRef(result.stdout, "origin");
1139 }),
1140 );
1141 }),
1142 {
1143 capacity: 2_048,
1144 timeToLive: Exit.match({
1145 onSuccess: () => STATUS_DEFAULT_BRANCH_CACHE_TTL,
1146 onFailure: () => Duration.zero,
1147 }),
1148 },
1149 );
1150 const originExistsCache = yield* Cache.makeWith(
1151 (gitCommonDir: string) =>
1152 Effect.gen(function* () {
1153 const path = yield* Path.Path;
1154 const fetchCwd =
1155 path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1156 return yield* executeGit(
1157 "GitVcsDriver.statusDetails.originExists",
1158 fetchCwd,
1159 ["--git-dir", gitCommonDir, "remote", "get-url", "origin"],
1160 { allowNonZeroExit: true },
1161 ).pipe(Effect.map((result) => result.exitCode === 0));
1162 }),
1163 {
1164 capacity: 2_048,
1165 timeToLive: Exit.match({
1166 onSuccess: () => STATUS_ORIGIN_EXISTS_CACHE_TTL,
1167 onFailure: () => Duration.zero,
1168 }),
1169 },
1170 );
1171 const invalidateStatusStaticCaches = (cwd: string) =>
1172 Effect.gen(function* () {
1173 const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
1174 Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
1175 );
1176 const cacheKey = repositoryPaths?.gitCommonDir ?? normalizeRepositoryPathsCacheKey(cwd);
1177 yield* Cache.invalidate(defaultBranchCache, cacheKey);
1178 yield* Cache.invalidate(originExistsCache, cacheKey);
1179 });
1180
11221181 const resolveGitCommonDir = Effect.fn("resolveGitCommonDir")(function* (cwd: string) {
11231182 const repositoryPaths = yield* resolveRepositoryPaths(cwd);
11241183 if (repositoryPaths !== null) {
......@@ -1517,7 +1576,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15171576 });
15181577 }
15191578
1520 const [numstatStdout, defaultRefResult, hasPrimaryRemote] = yield* Effect.all(
1579 const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
1580 Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
1581 );
1582 const statusCacheKey = repositoryPaths?.gitCommonDir;
1583 const [numstatStdout, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
15211584 [
15221585 executeGitWithStableDiagnostics(
15231586 "GitVcsDriver.statusDetails.numstat",
......@@ -1574,21 +1637,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15741637 );
15751638 }),
15761639 ),
1577 executeGit(
1578 "GitVcsDriver.statusDetails.defaultRef",
1579 cwd,
1580 ["symbolic-ref", "refs/remotes/origin/HEAD"],
1581 { allowNonZeroExit: true },
1582 ),
1583 originRemoteExists(cwd).pipe(Effect.orElseSucceed(() => false)),
1640 statusCacheKey
1641 ? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
1642 : resolveDefaultBranchName(cwd, "origin").pipe(Effect.orElseSucceed(() => null)),
1643 statusCacheKey
1644 ? Cache.get(originExistsCache, statusCacheKey).pipe(Effect.orElseSucceed(() => false))
1645 : originRemoteExists(cwd).pipe(Effect.orElseSucceed(() => false)),
15841646 ],
15851647 { concurrency: "unbounded" },
15861648 );
15871649 const statusStdout = statusResult.stdout;
1588 const defaultBranch =
1589 defaultRefResult.exitCode === 0
1590 ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
1591 : null;
15921650
15931651 let refName: string | null = null;
15941652 let upstreamRef: string | null = null;
......@@ -2809,7 +2867,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
28092867 cwd: string,
28102868 effect: Effect.Effect<A, E>,
28112869 ): Effect.Effect<A, E> =>
2812 effect.pipe(Effect.ensuring(invalidateListRefsSnapshot(cwd).pipe(Effect.ignore)));
2870 effect.pipe(
2871 Effect.ensuring(
2872 Effect.all([
2873 invalidateListRefsSnapshot(cwd).pipe(Effect.ignore),
2874 invalidateStatusStaticCaches(cwd).pipe(Effect.ignore),
2875 ]),
2876 ),
2877 );
28132878 const initRepoWithListRefsInvalidation: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = (
28142879 input,
28152880 ) =>