The best way to control Claude Code, Codex and more from any computer or phone
perf(server): refreshing a branch list frees the snapshot it replaces (#39440)
Julius Marminge committed
Nov 19, 2026 at 16:49 UTC
38d27db7467099babcf6dfb39db04938603137a5
2 files changed
+74
-8
apps/server/src/vcs/GitVcsDriverCore.test.ts
+50
@@ -1712,6 +1712,56 @@ it.effect("retries an in-flight ref snapshot invalidated by a mutation", () =>
1712
),
1713
);
1714
1715
+it.effect("superseded ref snapshots of one repository do not evict another's", () =>
1716
+ Effect.scoped(
1717
+ Effect.gen(function* () {
1718
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1719
+ const quietRepo = yield* makeTmpDir();
1720
+ const quietRepoRealPath = NodeFS.realpathSync.native(quietRepo);
1721
+ const quietRepoScans = yield* Ref.make(0);
1722
+ const countingSpawner = ChildProcessSpawner.make((command) =>
1723
+ Effect.gen(function* () {
1724
+ if (
1725
+ ChildProcess.isStandardCommand(command) &&
1726
+ command.args.includes("for-each-ref") &&
1727
+ command.args.includes("refs/remotes") &&
1728
+ NodeFS.realpathSync.native(String(command.options.cwd)).startsWith(quietRepoRealPath)
1729
+ ) {
1730
+ yield* Ref.update(quietRepoScans, (count) => count + 1);
1731
+ }
1732
+ return yield* delegate.spawn(command);
1733
+ }),
1734
+ );
1735
+ const driver = yield* makeGitVcsDriverCore().pipe(
1736
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, countingSpawner),
1737
+ );
1738
+ const busyRepo = yield* makeTmpDir();
1739
+ yield* initRepoWithCommit(quietRepo).pipe(
1740
+ Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
1741
+ );
1742
+ yield* initRepoWithCommit(busyRepo).pipe(
1743
+ Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
1744
+ );
1745
+
1746
+ yield* driver.listRefs({ cwd: quietRepo });
1747
+ // More epochs than the snapshot cache holds.
1748
+ for (let index = 0; index < 70; index++) {
1749
+ yield* driver.invalidateRefs(busyRepo);
1750
+ yield* driver.listRefs({ cwd: busyRepo });
1751
+ }
1752
+ yield* driver.listRefs({ cwd: quietRepo });
1753
+
1754
+ assert.equal(yield* Ref.get(quietRepoScans), 1);
1755
+ }),
1756
+ ).pipe(
1757
+ Effect.provide(
1758
+ layerServerConfig.pipe(
1759
+ Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
1760
+ ),
1761
+ ),
1762
+ ),
1763
+);
1764
+
1765
it.effect("invalidates a ref snapshot when a mutation fails after changing Git", () =>
1766
Effect.scoped(
1767
Effect.gen(function* () {
apps/server/src/vcs/GitVcsDriverCore.ts
+24
-8
@@ -5037,15 +5037,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5037
timeToLive: (exit) => (Exit.isSuccess(exit) ? LIST_REFS_SNAPSHOT_CACHE_TTL : Duration.zero),
5038
},
5039
);
5040
+ // Readers only ask for the current epoch and Cache expires entries lazily, so a superseded
5041
+ // snapshot (megabytes in a repository with many remote branches) would stay until evicted.
5042
+ const advanceListRefsEpoch = Effect.fn("GitVcsDriver.advanceListRefsEpoch")(function* (
5043
+ gitCommonDir: string,
5044
+ ) {
5045
+ const previous = listRefsEpochByCommonDir.get(gitCommonDir);
5046
+ const epoch = bumpListRefsEpoch(gitCommonDir);
5047
+ if (previous !== undefined) {
5048
+ yield* Cache.invalidate(
5049
+ listRefsSnapshotCache,
5050
+ new GitRefsSnapshotCacheKey({ gitCommonDir, epoch: previous }),
5051
+ );
5052
+ }
5053
+ return epoch;
5054
+ });
5055
const listRefsRefreshSnapshotCache = yield* Cache.makeWith(
5056
(cacheKey: GitRefsRefreshCacheKey) =>
5042
- Effect.suspend(() => {
5043
- const epoch = bumpListRefsEpoch(cacheKey.gitCommonDir);
5044
- return Cache.get(
5045
- listRefsSnapshotCache,
5046
- new GitRefsSnapshotCacheKey({ gitCommonDir: cacheKey.gitCommonDir, epoch }),
5047
- );
5048
- }),
5057
+ advanceListRefsEpoch(cacheKey.gitCommonDir).pipe(
5058
+ Effect.flatMap((epoch) =>
5059
+ Cache.get(
5060
+ listRefsSnapshotCache,
5061
+ new GitRefsSnapshotCacheKey({ gitCommonDir: cacheKey.gitCommonDir, epoch }),
5062
+ ),
5063
+ ),
5064
+ ),
5065
{
5066
capacity: LIST_REFS_SNAPSHOT_CACHE_CAPACITY,
5067
timeToLive: (exit) =>
@@ -5087,7 +5103,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5103
if (commonDir == null) return;
5104
const previousGeneration = currentListRefsGeneration(commonDir);
5105
bumpListRefsGeneration(commonDir);
5090
- bumpListRefsEpoch(commonDir);
5106
+ yield* advanceListRefsEpoch(commonDir);
5107
yield* Cache.invalidate(
5108
listRefsRefreshSnapshotCache,
5109
new GitRefsRefreshCacheKey({