@devries-cameron20 / t3code-1 / commits / 34b15a9ace
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

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

Utkarsh Patil committed Jul 31, 2026 at 03:38 UTC 34b15a9acec5519425c81bdd2a10f11e9ba3d23d
2 files changed +123 -13
apps/server/src/vcs/GitVcsDriverCore.test.ts
+45
@@ -153,6 +153,51 @@ it.effect("uses stable diagnostics for every parsed non-repository command", ()
153 }).pipe(Effect.provide(layer));
154 });
155
156 +it.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 +
174 +it.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 +
201 it.effect("coalesces concurrent ref pages into one repository snapshot", () =>
202 Effect.scoped(
203 Effect.gen(function* () {
apps/server/src/vcs/GitVcsDriverCore.ts
+78 -13
@@ -61,6 +61,8 @@ const LIST_REFS_SNAPSHOT_CACHE_CAPACITY = 64;
61 const LIST_REFS_SNAPSHOT_CACHE_TTL = Duration.minutes(2);
62 const LIST_REFS_REFRESH_COALESCE_TTL = Duration.seconds(5);
63 const LIST_REFS_REFRESH_FAILURE_COOLDOWN = Duration.seconds(30);
64 +const STATUS_DEFAULT_BRANCH_CACHE_TTL = Duration.minutes(5);
65 +const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);
66 const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({
67 GCM_INTERACTIVE: "never",
68 GIT_ASKPASS: "",
@@ -1119,6 +1121,63 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1121 return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey);
1122 };
1123
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 +
1181 const resolveGitCommonDir = Effect.fn("resolveGitCommonDir")(function* (cwd: string) {
1182 const repositoryPaths = yield* resolveRepositoryPaths(cwd);
1183 if (repositoryPaths !== null) {
@@ -1517,7 +1576,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1576 });
1577 }
1578
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(
1584 [
1585 executeGitWithStableDiagnostics(
1586 "GitVcsDriver.statusDetails.numstat",
@@ -1574,21 +1637,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1637 );
1638 }),
1639 ),
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)),
1646 ],
1647 { concurrency: "unbounded" },
1648 );
1649 const statusStdout = statusResult.stdout;
1588 - const defaultBranch =
1589 - defaultRefResult.exitCode === 0
1590 - ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
1591 - : null;
1650
1651 let refName: string | null = null;
1652 let upstreamRef: string | null = null;
@@ -2809,7 +2867,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2867 cwd: string,
2868 effect: Effect.Effect<A, E>,
2869 ): 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 + );
2878 const initRepoWithListRefsInvalidation: GitVcsDriver.GitVcsDriver["Service"]["initRepo"] = (
2879 input,
2880 ) =>